วัดค่า ความชื้นในดิน และค่าความเข้มของแสง แบบเชื่อมต่อ BLYNK แสดงค่าเป็น 0 ตลอดเวลา

ผมใช้บอร์ด ESP32
ตอนที่เขียนโค๊ดแบบไม่ได้เชื่อมต่อ BLYNK แสดงค่าปกติ
ตอนเชื่อมต่อ BLYNK ค่าแสดงเป็น0ตลอดเวลาไม่เปลี่ยนแปลง

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int RelayLight=12;
int RelayValue=13;
int sensorLight=2;
int sensorValue=4;

int sensorValue1 = 0;        // ตัวแปรค่า Analog
int outputValue1 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %
int sensorValue2 = 0;        // ตัวแปรค่า Analog
int outputValue2 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %

char auth[] = "L5xSTLXHIkZDiG_bWK8JqeWinn_G76xG";
char ssid[] = "CHALOR@1B";
char pass[] = "0862136574";
char server[] = "oasiskit.com";
int port = 8080;

BlynkTimer timer;
BLYNK_CONNECTED(){
  Blynk.syncAll();
}

void sensor(){
  sensorValue1 = analogRead(sensorLight);
  outputValue1 = map(sensorValue1,0,4095,100,0);
  Serial.print("Light: ");
  Serial.print(outputValue1);
  Serial.println(" %");
  lcd.setCursor(0,0);
  lcd.print("Light: ");
  lcd.setCursor(7,0);
  lcd.print(outputValue1);
  lcd.setCursor(11,0);
  lcd.print(" %");
  
  sensorValue2=analogRead(sensorValue);
  outputValue2 =map(sensorValue2,0,4095,0,100);
  Serial.print("Value: ");
  Serial.print(outputValue2);
  Serial.println(" %");
  lcd.setCursor(0,1);
  lcd.print("Value: ");
  lcd.setCursor(7,1);
  lcd.print(outputValue2 );
  lcd.setCursor(11,1);
  lcd.print(" %");
  delay(1000);

}

void setup() {
  Serial.begin(115200);
  pinMode(RelayLight,OUTPUT);
  pinMode(RelayValue,OUTPUT);
  Blynk.begin(auth, ssid, pass, server, port);
  timer.setInterval(500,sensor);
}

void loop() {
 Blynk.run();
 timer.run();
}

ค่าเป็น 0 ที่ไหนครับ ที่บอร์ด หรือว่าที่ app

Serial Monitor จอLCD แอพด้วยครับ

แคบตรง serial มาดูหน่อยครับ

ค่าตรงนี้ได้เท่าไหร่ก่อนแปลง

ค่าตรงนี้คือการอ่านค่า จากขา GPIOขา2 โดยตรงครับ

ใช่ครับ อยากรู้ว่ามันได้เท่าไหร่ ก่อนที่จะถูกแปลงออกมาเป็น 0

ตัวโปรแกรมแบบไม่เชื่อมต่อกับ Blynk ใช้งานได้ปกติ

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);

int sensorLight=2;
int sensorValue=4;

int sensorValue1 = 0;        // ตัวแปรค่า Analog
int outputValue1 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %
int sensorValue2 = 0;        // ตัวแปรค่า Analog
int outputValue2 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %
โค๊ดโปรแกรมแบบไม่เชื่อมต่อ Blynk

void setup() {
  Serial.begin(115200); // set up serial port for 9600 baud (speed)
  delay(500); // wait for display to boot up
  lcd.begin();
  lcd.backlight();

}

void loop() {
 sensorValue1 = analogRead(sensorLight);
  outputValue1 = map(sensorValue1,0,4095,100,0);
  Serial.print("Light: ");
  Serial.print(outputValue1);
  Serial.println(" %");
  lcd.setCursor(0,0);
  lcd.print("Light: ");
  lcd.setCursor(7,0);
  lcd.print(outputValue1);
  lcd.setCursor(11,0);
  lcd.print(" %");
  
  sensorValue2=analogRead(sensorValue);
  outputValue2 =map(sensorValue2,0,4095,100,0);
  Serial.print("Value: ");
  Serial.print(outputValue2);
  Serial.println(" %");
  lcd.setCursor(0,1);
  lcd.print("Value: ");
  lcd.setCursor(7,1);
  lcd.print(outputValue2 );
  lcd.setCursor(11,1);
  lcd.print(" %");
  delay(1000);

}
1 Likes

ค่าAnalogที่รับ esp32 อยู่ที่0-4095 ครับ

ลองเพิ่มการกำหนด port ดูครับว่าได้ไหม

  pinMode(sensorLight,INPUT); //กำหนด port ให้เป็น input
  pinMode(sensorValue,INPUT); //กำหนด port ให้เป็น input
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int RelayLight=12;
int RelayValue=13;
int sensorLight=2;
int sensorValue=4;

int sensorValue1 = 0;        // ตัวแปรค่า Analog
int outputValue1 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %
int sensorValue2 = 0;        // ตัวแปรค่า Analog
int outputValue2 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %

char auth[] = "L5xSTLXHIkZDiG_bWK8JqeWinn_G76xG";
char ssid[] = "CHALOR@1B";
char pass[] = "0862136574";
char server[] = "oasiskit.com";
int port = 8080;

BlynkTimer timer;
BLYNK_CONNECTED(){
  Blynk.syncAll();
}

void sensor(){
  sensorValue1 = analogRead(sensorLight);
  outputValue1 = map(sensorValue1,0,4095,100,0);
  Serial.print("Light: ");
  Serial.print(outputValue1);
  Serial.println(" %");
  lcd.setCursor(0,0);
  lcd.print("Light: ");
  lcd.setCursor(7,0);
  lcd.print(outputValue1);
  lcd.setCursor(11,0);
  lcd.print(" %");
  
  sensorValue2=analogRead(sensorValue);
  outputValue2 =map(sensorValue2,0,4095,0,100);
  Serial.print("Value: ");
  Serial.print(outputValue2);
  Serial.println(" %");
  lcd.setCursor(0,1);
  lcd.print("Value: ");
  lcd.setCursor(7,1);
  lcd.print(outputValue2 );
  lcd.setCursor(11,1);
  lcd.print(" %");
  delay(1000);

}

void setup() {
  Serial.begin(115200);
  pinMode(RelayLight,OUTPUT);
  pinMode(RelayValue,OUTPUT);
  pinMode(sensorLight,INPUT); //กำหนด port ให้เป็น input
  pinMode(sensorValue,INPUT); //กำหนด port ให้เป็น input

  Blynk.begin(auth, ssid, pass, server, port);
  timer.setInterval(500,sensor);
}

void loop() {
 Blynk.run();
 timer.run();
}
1 Likes

ทำยังไงหรอครับ

ดูโค้ตที่ผมแก้ให้ไปอะครับ

1 Likes

ลองแล้วครับ ขึ้นเหมือนเดิมครับ

อันนี้คือแค่เปลี่ยนโค้ตเป็นอันเก่าก็อ่านได้เลยใช่ป่ะครับ

แค่ตัดส่วนของ blynk ออกทั้งหมดก็ใช้งานได้ปกติครับ

ใช้เซ็นเซอร์ตัวไหนถ่านมาดูหน่อยครับ

1 Likes

1 Likes

ลองย้ายชุดนี้กลับไปที่ void loop() ดูว่าได้ผลอย่างไร

 sensorValue1 = analogRead(sensorLight);
  outputValue1 = map(sensorValue1,0,4095,100,0);
  Serial.print("Light: ");
  Serial.print(outputValue1);
  Serial.println(" %");
  lcd.setCursor(0,0);
  lcd.print("Light: ");
  lcd.setCursor(7,0);
  lcd.print(outputValue1);
  lcd.setCursor(11,0);
  lcd.print(" %");
  
  sensorValue2=analogRead(sensorValue);
  outputValue2 =map(sensorValue2,0,4095,0,100);
  Serial.print("Value: ");
  Serial.print(outputValue2);
  Serial.println(" %");
  lcd.setCursor(0,1);
  lcd.print("Value: ");
  lcd.setCursor(7,1);
  lcd.print(outputValue2 );
  lcd.setCursor(11,1);
  lcd.print(" %");
  delay(1000);

ลองแล้วครับ ขึ้น0เหมือนเดิม

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int RelayLight=12;
int RelayValue=13;
int sensorLight=2;
int sensorValue=4;

int sensorValue1 = 0;        // ตัวแปรค่า Analog
int outputValue1 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %
int sensorValue2 = 0;        // ตัวแปรค่า Analog
int outputValue2 = 0;        // ตัวแปรสำหรับ Map เพื่อคิด %

char auth[] = "L5xSTLXHIkZDiG_bWK8JqeWinn_G76xG";
char ssid[] = "CHALOR@1B";
char pass[] = "0862136574";
char server[] = "oasiskit.com";
int port = 8080;





void setup() {
  Serial.begin(115200);
  pinMode(RelayLight,OUTPUT);
  pinMode(RelayValue,OUTPUT);
  pinMode(sensorLight,INPUT);
  pinMode(sensorValue,INPUT);
  
  Blynk.begin(auth, ssid, pass, server, port);
 
}

void loop() {
   sensorValue1 = analogRead(sensorLight);
  outputValue1 = map(sensorValue1,0,4095,100,0);
  Serial.print("Light: ");
  Serial.print(outputValue1);
  Serial.println(" %");
  lcd.setCursor(0,0);
  lcd.print("Light: ");
  lcd.setCursor(7,0);
  lcd.print(outputValue1);
  lcd.setCursor(11,0);
  lcd.print(" %");
  
  sensorValue2=analogRead(sensorValue);
  outputValue2 =map(sensorValue2,0,4095,100,0);
  Serial.print("Value: ");
  Serial.print(outputValue2);
  Serial.println(" %");
  lcd.setCursor(0,1);
  lcd.print("Value: ");
  lcd.setCursor(7,1);
  lcd.print(outputValue2 );
  lcd.setCursor(11,1);
  lcd.print(" %");
  delay(1000);
  
 Blynk.run();
 
}