So I started my first Particle project, and have been trying to run a script that reads temperature data, as well as air quality data from a DHT22 and PM2.5 PMS5003 sensor. When I first start up the Particle Argon after flashing the code, the particle publishes data instantly. However, soon after it starts publishing many errors and wont read data for up to 15 minutes. Is there something wrong with the code that is causing it? What am I doing wrong?
#include "Adafruit_DHT_Particle.h"
#include "Adafruit_PM25AQI.h"
#define DHTPIN D2 // Data pin being used to send data from Particle Argon
#define DHTTYPE DHT22 // Defines the type of Sensor to use from the Library
Adafruit_PM25AQI aqi = Adafruit_PM25AQI();
DHT dht(DHTPIN, DHTTYPE);
double TempC = 0; //Initialize Value for Temperature (Celsius)
double H = 0; //Initialize Value for Humidity (Percent)
double PM1_0=0;
double PM2_5=0;
double PM10=0;
int loopCount;
int temperatureAlert = 0;
// setup() runs once, when the device is first turned on.
void setup()
{
// Put initialization like pinMode and begin functions here.
Particle.variable("PM1_0",PM1_0);
Particle.variable("PM2_5",PM2_5);
Particle.variable("PM5_0",PM10);
Particle.variable("temperature",TempC);
Particle.variable("humidity",H);
Particle.variable("temperatureAlert",temperatureAlert);
Particle.publish("state","DHT22 Begin");
Serial.begin(119600);
while (!Serial) delay(10);
Particle.publish("state", "Adafruit PMSA003I Air Quality Sensor");
// Wait one second for sensor to boot up!
delay(1000);
Serial1.begin(9600);
if (! aqi.begin_UART(&Serial1))
{ // connect to the sensor over hardware serial
//if (! aqi.begin_UART(&pmSerial)) { // connect to the sensor over software serial
Particle.publish("state", "Could not find PM 2.5 sensor!");
while (1) delay(10);
}
dht.begin();
loopCount=0;
delay (2000);
}
// loop() runs over and over again, as quickly as it can execute.
void loop()
{
PM25_AQI_Data data;
if (! aqi.read(&data)) {
Particle.publish("state", "Could not read from AQI");
delay(10000); // try again in a bit!
return;
}
// *********************************
PM1_0=data.particles_10um;
PM2_5=data.particles_25um;
PM10=data.particles_100um;
// *********************************
TempC=dht.getTempCelcius();
if(TempC>30 || TempC<20)
{
temperatureAlert=1;
Particle.publish("warning",String::format("\"temperatureAlert\": %4.2f", temperatureAlert));
}
H=dht.getHumidity();
if(isnan(H) || isnan(TempC))
{
Particle.publish("state","Failed to read from sensor");
}
/* else if (TempC>40 || TempC<4)
{
Particle.publish("state", "Deviation");
}*/
else
{
/*Particle.publish("temperature", String (TempC), PRIVATE);
Particle.publish("humidity", String (H), PRIVATE);*/
Particle.publish("PM",String::format("\"PM1_0\": %4.2f, \"PM2_5\": %4.2f, \"PM10\": %4.2f, \"temperature\": %4.2f, \"humidity\": %4.2f", PM1_0, PM2_5, PM10, TempC, H));
}
delay(30000);
}