I have a circuit with 3 sensors (1 is PMS5003) and an ESP8266 WiFi module (the small esp-01 variant). The code works when it's just the 3 sensors but when I added the ESP module, the PMS5003 sensor gives me checksum errors (it still works tho/all the sensors work except that the PMS5003 gives me either high or low values).
My code works so that the detected values from the sensors are sent by the ESP module to a web server through WiFi. I'm using AT commands for that. I want it to update in real-time, or at least every 15 seconds. I think the checksum error is because of the timing (?) or perhaps because it's the Arduino board is reading responses/outputs from both the PMS5003 and ESP8266 (I seriously don't know). I tried to adjust the timeout value and that didn't work. I also tried removing the espData function (?) and integrating it separately into each AT command but that didn't work either. I'm a beginner so I don't entirely know how the code for the PMS5003 sensor works. Does anyone know how to remove the checksum errors? My entire code is shown below.
#include <SoftwareSerial.h>
SoftwareSerial espSerial(2, 3);
#define DEBUG true
#include <dht.h>
#define dht_apin A1
dht DHT;
int temp;
int humi;
int co2;
SoftwareSerial pmsSerial (4,5);
#include <Wire.h>
int pm25;
int pm251;
String mySSID = "ssid";
String myPWD = "password";
String myAPI = "api key";
String myHOST = "api.thingspeak.com";
String myPORT = "80";
void setup(){
Serial.begin(115200);
espSerial.begin(115200);
delay(500);
espData("AT+RST", 1000, DEBUG);
espData("AT+CWMODE=1", 1000, DEBUG);
espData("AT+CWJAP=\""+ mySSID +"\",\""+ myPWD +"\"", 1000, DEBUG);
/*while(!espSerial.find("OK"))
{
//Wait for connection
}*/
delay(1500);
pmsSerial.begin(9600);
}
struct pms5003data {
uint16_t framelen;
uint16_t pm10_standard, pm25_standard, pm100_standard;
uint16_t pm10_env, pm25_env, pm100_env;
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
uint16_t unused;
uint16_t checksum;
};
struct pms5003data data;
boolean readPMSdata(Stream *s) {
if (! s->available()) {
return false;
}
// Read a byte at a time until we get to the special '0x42' start-byte
if (s->peek() != 0x42) {
s->read();
return false;
}
// Read all 32 bytes
if (s->available() < 32) {
return false;
}
uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);
// get checksum ready
for (uint8_t i=0; i<30; i++) {
sum += buffer[i];
}
/* for debugging
for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
*/
// The data comes in endian'd, this solves it so it works on all platforms
uint16_t buffer_u16[15];
for (uint8_t i=0; i<15; i++) {
buffer_u16[i] = buffer[2 + i*2 + 1];
buffer_u16[i] += (buffer[2 + i*2] << 8);
}
// put it into a nice struct :)
memcpy((void *)&data, (void *)buffer_u16, 30);
if (sum != data.checksum) {
Serial.println("Checksum failure");
return false;
}
// success!
return true;
}
void loop(){
DHT.read11(dht_apin);
temp = DHT.temperature;
humi = DHT.humidity;
co2 = analogRead(A0);
if (readPMSdata(&pmsSerial)){
pm25 = data.particles_25um;
String sendData = "GET /update?api_key="+ myAPI +"&field1=" + co2 + "&field2=" + String(pm25) + "&field3="+ String(temp) + "&field4=" + String(humi);
Serial.println(sendData);
espData("AT+CIPMUX=1", 150, DEBUG);
espData("AT+CIPSTART=0,\"TCP\",\""+ myHOST +"\","+ myPORT,150, DEBUG);
espData("AT+CIPSEND=0," +String(sendData.length()+4),150,DEBUG);
espSerial.find(">");
espSerial.println(sendData);
/*Serial.println("Values to be sent: ");
Serial.println("Carbon Dioxide current value: " + String(co2) + "ppm");
Serial.println("Particulate Matter 2.5 Particles: " + String(pm25) + "μg/m³");
Serial.println("Particulate Matter 2.5 Environmental Value: " + String(pm251) + "μg/m³");
Serial.println("Temperature current value: " + String(temp) + "°C");
Serial.println("Humidity current value: " + String(humi) + "%");
Serial.println(sendData);*/
espData("AT+CIPCLOSE=0",150,DEBUG);
delay(150);
}
}
String espData(String command, const int timeout, boolean debug)
{
Serial.print("AT Command ==> ");
Serial.print(command);
Serial.println(" ");
String response = "";
espSerial.println(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (espSerial.available())
{
char c = espSerial.read();
response += c;
}
}
/*if (debug)
{
Serial.print(response);
}*/
return response;
Serial.println("Response" + response);
}
Feel free to ask questions cuz I seriously need help thank you!