Thanks in advance for any help you can offer.
I'm trying to bring together a DHT11 sensor and a PMS7003 Plantower particle monitor on a Wemos D1 R1 board which will ultimately ping the readings to Thingspeak.
A sketch containing just the DHT11 or the PMS7003 code works fine (albeit I know the PMS7003 code I'm using is lengthy and should probably be a library, although I haven't successfully found a simple,reliable 7003 library yet)
Together though, I either find that the DHT11 function seems to 'dominate' and I stop getting reliable feedback on the PMS7003 data, or as in the current version of the sketch, I also get sporadic WDT resets after the code has only iterated through a few loops.
-
I can't find any simple implementations of DHT11.h alongside a softwareserial input - is the code too much of a bully to allow this, or is there a way of isolating the two?
-
How can I get the pms7003ReadData function to loop until it has a successful reading when it's running alongside the DHT11 function? I often seem to get back just the temp/humidity, and nothing on the particles.
-
Can anyone shed light on what is causing the Watchdog timer errors in my code and, well basically, help me get rid of this?
Code attached and below. Should just say that this is a mashup of a couple of people's sketches, and I haven't yet put full attributions back in yet.
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <DHT.h>
SoftwareSerial mySerial(D5, D6);
String apiKey = "THINGSPEAKAPI";
const char* ssid = "MYWIFI";
const char* password = "MYPASS";
const char* server = "api.thingspeak.com";
// STATIC VARIABLES
int inputHigh = 0;
int inputLow = 0;
uint16_t inputChecksum = 0; // variable to caclulate checksum input variables
uint16_t concPM1_0_CF1; // Lots of sensor variables to capture individual data points within the string
uint16_t concPM2_5_CF1;
uint16_t concPM10_0_CF1;
uint16_t concPM1_0_amb;
uint16_t concPM2_5_amb;
uint16_t concPM10_0_amb;
uint16_t rawGt0_3um;
uint16_t rawGt0_5um;
uint16_t rawGt1_0um;
uint16_t rawGt2_5um;
uint16_t rawGt5_0um;
uint16_t rawGt10_0um;
uint8_t version;
uint8_t errorCode;
uint16_t checksum;
int dustSlowDownCounter = 0;
#define CHILD_ID_DUST_PM10 0
#define CHILD_ID_DUST_PM25 1
#define CHILD_ID_DUST_PM100 2
#define DHTPIN D2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
delay(100);
dht.begin();
delay(100);
mySerial.begin(9600);
delay(100);
Serial.println("Setup Complete");
}
void loop() {
// Attempt to read dust sensor
int dustSensorOutput = pms7003ReadData();
// Attempt to read temperature sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// visual outputs
Serial.print("PMS7003; PM2.5 =");
Serial.print(concPM2_5_amb);
Serial.print("ug/m3. (");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(concPM2_5_amb);
postStr += "\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celsius Humidity: ");
Serial.print(h);
Serial.print("PMS: ");
Serial.print(concPM2_5_amb);
Serial.println("Sending data to Thingspeak");
}
client.stop();
delay(30000);
}
// MAIN FUNCTION FOR THE DUST SENSOR, Thanks to Scapeler.nl
int pms7003ReadData() {
unsigned long now = millis (); //
while (millis () - now < 2000) //
mySerial.read ();
delay(700); // with a clear buffer, let it fill with some data
if (mySerial.available() < 32) {
if (mySerial.available() == 0) {
delay(150);
return -1;
};
if (mySerial.available() > 16) {
delay(10);
return -1;
};
if (mySerial.available() > 0) {
delay(30);
return -1;
};
delay(100);
return -1;
}
if (mySerial.read() != 0x42) return -1;
if (mySerial.read() != 0x4D) return -1;
inputChecksum = 0x42 + 0x4D;
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
if (inputHigh != 0x00) return -1;
if (inputLow != 0x1c) return -1;
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM1_0_CF1 = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM2_5_CF1 = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM10_0_CF1 = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM1_0_amb = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM2_5_amb = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
concPM10_0_amb = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt0_3um = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt0_5um = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt1_0um = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt2_5um = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt5_0um = inputLow+(inputHigh<<8);
inputHigh = mySerial.read();
inputLow = mySerial.read();
inputChecksum += inputHigh + inputLow;
rawGt10_0um = inputLow+(inputHigh<<8);
inputLow = mySerial.read();
inputChecksum += inputLow;
version = inputLow;
inputLow = mySerial.read();
inputChecksum += inputLow;
errorCode = inputLow;
//removed a section on serial printing this out - see file for full code on this
}
delay(700); // If you set this be be higher than 700 you will get checksum errors.
return concPM2_5_CF1; //
}
DHT_PMS7003_help.ino (7.76 KB)