ricki-z / SDS011

Arduino library for SDS011 dust sensor
GNU General Public License v3.0
81 stars 53 forks source link

ESP32 fails to read values? #38

Open lachlansleight opened 3 years ago

lachlansleight commented 3 years ago

I know that this is probably me just missing something, but I seriously can't seem to work out what it is. I literally have nothing but the SDS powered via an external 5V supply, with TX and RX connected to RX2 and TX2 of my ESP32 and the grounds connected up. The ESP32 is powered via the USB connection to my PC.

Running the HardwareSerial example sketch with the line HardwareSerial port(2) returns nothing - I'm getting an error value when calling my_sds.read(&p25, &p10)

Am I going crazy? I feel like this is the minimal possible setup for this sensor and it doesn't work, so I'm not really sure what else to try. No other SDS libraries seem to work either, but this seems to be the most popular so I thought I'd ask for a hand here!

#include <SDS011.h>

float p10, p25;
int err;

HardwareSerial port(2);

SDS011 my_sds;

void setup() {  
    Serial.begin(115200);
  my_sds.begin(&port);
}

void loop() {
    err = my_sds.read(&p25, &p10);
    if (!err) {
        Serial.println("P2.5: " + String(p25));
        Serial.println("P10:  " + String(p10));
    } else {
    Serial.println("Error: " + String(err));
    }
    delay(100);
}
pagliara commented 2 years ago

Did you ever figure this out? I'm running into the same issue with my test set up. The SDS011 sensor is blinking its red LED in a way that seems to confirm its receiving data from the serial port but I get an error when reading.

lachlansleight commented 2 years ago

Nope, I gave up on the project entirely and haven't touched my SDS011 since :'c

pagliara commented 2 years ago

I ended up hacking a version of this library using EspSoftwareSerial as a replacement for HardwareSerial and I got it to work!

Jogenist commented 2 years ago

@pagliara could you pls upload your modified library or at least explain what you have changed? I tried replacing every HardwareSerial in this library with SoftwareSerial, but it didnt work

pagliara commented 2 years ago

I'm using EspSoftwareSerial like so:

float p10, p25; //for the pm values.
int err; // for errors
SoftwareSerial *sensor;

int readSensor(float *p25, float *p10) {
    byte buffer;
    int value;
    int len = 0;
    int pm10_serial = 0;
    int pm25_serial = 0;
    int checksum_is;
    int checksum_ok = 0;
    int error = 1;
  while ((sensor->available() > 0) && (sensor->available() >= (10-len))) {
    buffer = sensor->read();
    value = int(buffer);
    switch (len) {
      case (0): if (value != 170) { len = -1; }; break;
      case (1): if (value != 192) { len = -1; }; break;
      case (2): pm25_serial = value; checksum_is = value; break;
      case (3): pm25_serial += (value << 8); checksum_is += value; break;
      case (4): pm10_serial = value; checksum_is += value; break;
      case (5): pm10_serial += (value << 8); checksum_is += value; break;
      case (6): checksum_is += value; break;
      case (7): checksum_is += value; break;
      case (8): if (value == (checksum_is % 256)) { checksum_ok = 1; } else { len = -1; }; break;
      case (9): if (value != 171) { len = -1; }; break;
    }
    len++;
    if (len == 10 && checksum_ok == 1) {
      *p10 = (float)pm10_serial/10.0;
      *p25 = (float)pm25_serial/10.0;
      len = 0; checksum_ok = 0; pm10_serial = 0.0; pm25_serial = 0.0; checksum_is = 0;
      error = 0;
    }
    yield();
  }
    return error;
}

SoftwareSerial port;

void setup() {
  // put your setup code here, to run once:
  port.begin(9600, SWSERIAL_8N1, 25, 26);
  if (!port) {
    Serial.println("Invalid software serial configuration");
  } else {
    sensor = &port;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  err = readSensor(&p25, &p10); // getting the pm25 and pm10 values. 
  if (!err) {
    Serial.printf("%f p25 %f p10", p25, p10);
    Serial.println();
  }
}