ต้องการจะส่งค่าจากเซนเซอร์วัดความชื้นนในดินไปที่guage Blynk
https://www.9arduino.com/product/171/soil-moisture-sensor-module-วัดความชื่นในดิน
ต้องเลือกอะไรบ้างคับและต้องเขียนโค้ดอย่างไร
ต้องการจะส่งค่าจากเซนเซอร์วัดความชื้นนในดินไปที่guage Blynk
ต้องเลือกอะไรบ้างคับและต้องเขียนโค้ดอย่างไร
อันดับแรกเอาโค้ตนี้ไปรันดูแล้วเช็คว่ามีค่าออกมาไหม
int SensorPin = A0; // พินที่ใช้รับค่า A0
int SensorValue = 0; // ตัวแปรของ ความชื้นในดิน เริ่มต้นด้วย 0
void setup() {
Serial.begin(9600);
}
void loop() {
// อ่านค่าจาก Sensor:
SensorValue = analogRead(SensorPin);
SensorValue = map(SensorValue, 350, 1023, 100, 0);
Serial.print("Moisture of Soil: "); ตั้งชื่อคำนำหน้า
Serial.println(SensorValue); //เรียนดูค่า SensorValue ใน Serial ว่ามีค่าเท่าไหร่
}
ถ้าได้ค่าแล้วไปต่อครับ
ตอนนี้ผ่านได้แล้วครับลองมาก่อนแล้ว
รู้สึกว่าโค้ดจะผิดนะครับต้องเป็น
map(sensorValue, 0, 1023, 100, 0);
โค้ตไหนถูกต้องใช้ได้ เอามาวางในโพสต์หน่อยนะครับ จะได้เข้าใจตรงกัน
อันนี้เป็นตัวอย่างการใช้งาน Gauge ให้เรากำหนด Vpin ก่อนพินไหนก็ได้ ส่งค่าออกด้วยคำสั่ง Blynk.virtualWrite(V83, hin);
int hin;
void sendSensor()
{
hin = (int)h;
Blynk.virtualWrite(V83, hin); //ส่งค่าออก Guage Blynk ที่ V83
Serial.println(String("Humidity: ") + hin); // 0 => index of the first sensor
}
const int analogInPin = A0;
int sensorValue = 0; // ตัวแปรค่า Analog
int outputValue = 0; // ตัวแปรสำหรับ Map เพื่อคิด %
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 100, 0);
Serial.print("Soil Moisture = ");
Serial.print(outputValue);
Serial.println(" %");
delay(500);
}
ทีนี้เอาโค้ตที่ส่งค่าขึ้น Guage ใส่เข้าไปครับ กำหนด Pin ไหนไว้ก็ใส่แทนที่ V83 ได้เลย
const int analogInPin = A0;
int sensorValue = 0; // ตัวแปรค่า Analog
int outputValue = 0; // ตัวแปรสำหรับ Map เพื่อคิด %
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 100, 0);
Serial.print(“Soil Moisture = “);
Serial.print(outputValue);
Blynk.virtualWrite(V83, outputValue);
Serial.println(” %”);
delay(500);
}
มันใช้ไม่ได้อ่าคับ
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int Relay = 5;
const int analogInPin = A0;
int sensorValue = 0; // ตัวแปรค่า Analog
int outputValue = 0; // ตัวแปรสำหรับ Map เพื่อคิด %
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "050d5c7fdd4b42dc89969f4edc3ec5cd";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "chaiphat";
char pass[] = "juris456";
BLYNK_WRITE(V2)
{
// You'll get HIGH/1 at startTime and LOW/0 at stopTime.
// this method will be triggered every day
// until you remove widget or stop project or
// clean stop/start fields of widget
Serial.print("Got a value: ");
Serial.println(param.asInt());
if(param.asInt() == 0)
{
digitalWrite(Relay, LOW);
}
if(param.asInt() == 1)
{
digitalWrite(Relay, HIGH);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(Relay,OUTPUT);
digitalWrite(Relay, LOW);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 100, 0);
Serial.print("Soil Moisture = ");
Serial.print(outputValue);
Serial.println(" %");
delay(500);
Blynk.run();
}
เอาใหม่ครับผมควรจะใส่ไว้ตรงไหนดี
แบบนี้ไม่ออกหรอครับ
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int Relay = 5;
const int analogInPin = A0;
int sensorValue = 0; // ตัวแปรค่า Analog
int outputValue = 0; // ตัวแปรสำหรับ Map เพื่อคิด %
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "050d5c7fdd4b42dc89969f4edc3ec5cd";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "chaiphat";
char pass[] = "juris456";
BLYNK_WRITE(V2)
{
// You'll get HIGH/1 at startTime and LOW/0 at stopTime.
// this method will be triggered every day
// until you remove widget or stop project or
// clean stop/start fields of widget
Serial.print("Got a value: ");
Serial.println(param.asInt());
if(param.asInt() == 0)
{
digitalWrite(Relay, LOW);
}
if(param.asInt() == 1)
{
digitalWrite(Relay, HIGH);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(Relay,OUTPUT);
digitalWrite(Relay, LOW);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 100, 0);
Blynk.virtualWrite(V83, outputValue);
Serial.print("Soil Moisture = ");
Serial.print(outputValue);
Serial.println(" %");
delay(500);
Blynk.run();
}
ดูที่ Serial มันได้ค่าอะไรมา
ตอนนี้เพิ่มโค้ดไปขึ้นแล้วครับ ขอบคุณครับ