สอบถามเรื่อง ESP8266 + Blynk + Dust sensor

ผมทำเครื่องวัดสภาพอากาศแล้วเกิดปัญหาครับ คือ Sensor บางตัวไม่ส่งข้อมูลไปที่ Blynk
อุปกรณ์ที่ใช้

  1. ESP8266 + shield
  2. DHT22
  3. Dust sensor ของ Sharp

รายละเอียดอุปกรณ์ตามรูป

โดย ปัญหาคือ ตัว Dust sensor ไม่ส่งข้อมูลไปออก Blynk เเต่ในหน้า moniter ของ arduinno IDE
ยังอ่านค่าปกติ
***จุดสังเกตุ คือ เหมือนพอ ตัววัดฝุ่นทำงาน(แบบว่าค่าฝุ่นมากกว่า 0)
เหมือน 8266 จะหยุด ส่งข้อมูลไปหา Blynk เหมือนค้างและหลุดไปเลย
เเต่ในหน้า moniter ของ arduinno IDE ยังอ่านค่าปกติ (ยังรันไปเรื่อยๆ ไม่ค้าง)
แต่พอค่าฝุ่น เหลือ 0 ก็หายค้างครับ

โดยที่ อย่างอื่นปกติ วัดค่าอุณหภูมิ ก็ปกติ เเต่พอ ตัววัดฝุ่นทำงาน ก็ค้างหมด
ผมไม่รู้ว่าผิดตรงไหนพลาดที่ใด รบกวนด้วยครับ ขอขอบคุณครับ

โค้ดครับ


#include "DHT.h"

#define DHTPIN D3 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxx";


int measurePin = A0;
int ledPower = D5;
 
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
 
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

float t;                                   // Declare the variables 
float h;
 
void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);
  Serial.begin(9600);
Serial.println("DHTxx test!");
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxxxxxx", "xxxxxxxxxxxx","blynk.honey.co.th",8080);
  timer.setInterval(2000, sendUptime);


dht.begin();
}

void sendUptime()
{
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float d = dustDensity ;
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V3, dustDensity);
}

void loop(){
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);
 
  voMeasured = analogRead(measurePin); // read the dust value
 
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
 
  // 0 - 3.3V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (3.3 / 1024);
 
  
  dustDensity = 0.17 * calcVoltage - 0.1;
 
if (dustDensity <= 0.00) {
    dustDensity = 0.00;

    Blynk.run();
    timer.run();
  }


  dustDensity = dustDensity * 1000;

  Serial.print(" - Dust Density: ");
  Serial.print(dustDensity);
  Serial.println(" µg/m³");
  delay(3000);

float d = dustDensity ;  
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);

// เช็คถ้าอ่านค่าไม่สำเร็จให้เริ่มอ่านใหม่
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t\n");
}

อธิบายโค้ตชุดนี้ให้ฟังหน่อยครับว่า ให้มันทำงานอะไร อย่างไร

หาก ค่าฝุ่นน้อยกว่าหรือเท่ากับ 0
ให้ เท่ากับ 0

เนื่องจากค่าปกติ ติดลบครับ แกว่งไปมา เช่น -0.01…-0.03
เหมือนทำให้ค่า ต่ำสุดคือ 0 อ่ะครับ

นี่คือ moniter ของ IDE ครับ ส่งค่าได้ปกติ 200กว่า (- Dust Density) พอไม่มีฝุ่น เท่ากับ 0

เเต่ใน Blynk ค่าไม่ขึ้นตามครับ 0 ตลอด

ขอดูการเซตอัพในนี้หน่อยครับ

ภาพ

แล้วค่านี้จะให้มันแสดงออกมาเป็นเลยอย่างนี้เลยหรอ?

นี่ครับ

ส่วน

ผมเอา Code มาจากร้านค้าครับ ผมไม่รู้ค่าแปลงจาก0-1023 เป็นค่าฝุ่นเทียบกันยังไง
ก็เลยยึดตามเค้า เพราะเห็นค่าที่ออกมาก็สมเหตุสมผลดีครับ 555

ผมอยากรู้ว่าทำไมมันไม่แสดง อุปกรณ์ก้อไม่น่าจะเสีย เพราะยังอ่านค่า ได้อยู่ เเต่ไปไม่ถึง Blynk
รบกวนด้วยครับ

สงสัยอยู่ว่าทำไมต้องให้รอ dustDensity <= 0.00 ก่อนค่อยให้มันส่งค่าทุกๆ 2 วิ มาอัพเอพที่ Blynk ละครับ

image

โค้ตชุดนี้มันแค่อัพเดทค่าขึ้น app เฉยๆ ไม่จำเป็นต้องมี sendUptime() ก็ได้นะผมว่า

ชุดนี้เอาไปรวมกันใน void loop() ก็ได้

void sendUptime()
{
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float d = dustDensity ;
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V3, dustDensity);
}

แบบนี้

#include "DHT.h"

#define DHTPIN D3     // what digital pin we're connected to

#define DHTTYPE DHT22 // DHT 11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#define DHTTYPE DHT21 // DHT 21 (AM2301)

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxx";

int measurePin = A0;

int ledPower = D5;

int samplingTime = 280;

int deltaTime = 40;

int sleepTime = 9680;

float voMeasured = 0;

float calcVoltage = 0;

float dustDensity = 0;

float d = dustDensity;

float t; // Declare the variables

float h;

void setup()

{

    Serial.begin(9600);

    pinMode(ledPower, OUTPUT);

    Serial.println("DHTxx test!");

    Blynk.begin(auth, "xxxxxxxxx", "xxxxxxxxxxxx", "blynk.honey.co.th", 8080);

    dht.begin();

}

void loop()

{

    Blynk.run();

    timer.run();

    digitalWrite(ledPower, LOW); // power on the LED

    delayMicroseconds(samplingTime);

    voMeasured = analogRead(measurePin); // read the dust value

    delayMicroseconds(deltaTime);

    digitalWrite(ledPower, HIGH); // turn the LED off

    delayMicroseconds(sleepTime);

    // 0 - 3.3V mapped to 0 - 1023 integer values

    // recover voltage

    calcVoltage = voMeasured * (3.3 / 1024);

    dustDensity = 0.17 * calcVoltage - 0.1;

    

    dustDensity = dustDensity * 1000;

    Serial.print(" - Dust Density: ");

    Serial.print(dustDensity);

    Serial.println(" µg/m³");

    delay(1000);

    Blynk.virtualWrite(V3, dustDensity);

    float h = dht.readHumidity();

    float t = dht.readTemperature();

    float f = dht.readTemperature(true);

    // เช็คถ้าอ่านค่าไม่สำเร็จให้เริ่มอ่านใหม่

    if (isnan(h) || isnan(t) || isnan(f))

    {

        Serial.println("Failed to read from DHT sensor!");

        return;

    }

    Serial.print("Humidity: ");

    Serial.print(h);

    Serial.print(" %\t");

    Serial.print("Temperature: ");

    Serial.print(t);

    Serial.print(" *C ");

    Serial.print(f);

    Serial.print(" *F\t\n");

    Blynk.virtualWrite(V1, t);

    Blynk.virtualWrite(V2, h);

}

ขอบคุณครับ ได้แล้วนะครับ :smile: :smile: :smile:

แก้ไขอะไรไปถึงได้ครับ สรุปหน่อย

ผมก็ได้ copy หน้าโค้ด ตามที่ให้ไปใช้ครับ ก็หายเลยครับ

เเต่หลังจากที่ เอาCode ส่วนนี้ออกไป
image

ค่าที่ได้ก็เลยมีลงไปถึงติดลบ เเต่ก็แก้โดนการ ไปตั้งค่า min ที่กราฟใน Blynk เอาครับ ให้ค่าต่ำสุดเป็น 0

อย่างอื่นก็ไม่มีปัญหานะครับ Code ผ่านหมด ไม่Error

1 Likes

เยี่ยมไปเลยครับ