PM2.5 AIr Quality Sensor + DHT22 reading/publishing issues

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);
}

Welcome to the community :slight_smile:

Not sure why PM25AQI is doing this but I found some pull request on GitHub regarding Serial communication here:

and here is full .cpp code. You will need to add everything manually on the Web IDE but maybe is worth to try.
Also instead of Adafruit_DHT_Particle for DHT try to use PIETTETECH_DHT which appears to be more reliable.
the last couple things:

  1. use SYSTEM_THREAD(ENABLED) to allow your app running in separately thread
  2. avoid using String bether option is to use snprintf or JSONWriter to build your JSON
  3. try to avoid long delay(30000) use millis() instead

Hope this will help :+1:
Best Regards,
Arek

1 Like

I will look into everything! Thank you so much! Will update you if this stuff works or I have any questions.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.