Issues with using PMS5003 sensor with WiFi

Hi

I am trying to connect PMS5003 and AM2320 sensors with my ESP 8266 board. I am facing an issue with the readings of the PMS Sensor

The PMS sensor gives the readings the first time the device is switched on but after that the same values for the dust are repeated instead of giving new readings with every parse of the loop.

I have tried several steps including striping the code to just include the PMS5003 bit but still no help. the code is pasted below. Any help is appreciated

/*

Send weather data to cloud via ESP wifi

Input: wifi username and pass
Host and key : default we using thingspeak

Connections acc to ESP8266 NodeMCU dev board:
PMS5003
+5V
Tx to GPIO13 D7
Rx to GPIO15 D8

SDS011
+5V
Tx (Green) to GPIO10 SD3
Rx (Blue)to GPIO9 SD2

BMP180
+3.3V
SDA to GPIO4 D2
SCL to GPIO5 D1

AM2320
+5V
SDA to GPIO4 D2
SCL to GPIO5 D1

Developed by ICF Technologies (P) Ltd, India
Dated: 10-SEPT-2016

Updated on 4-12-2019 by Mohit for wi-fi enable
*/

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include "AM2320.h"

#define pms_pin 16
#define PMSupdate 5 //specify update time in minutes for PMS
#define SDSupdate 5

const char* ssid = "abcdef"; //ssid
const char* password = "abcdef"; //pass

//PMS5003
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int PM01_PMS=0; //define PM1.0 value of the air detector module
int PM25_PMS=0; //define PM2.5 value of the air detector module
int PM10_PMS=0; //define PM10 value of the air detector module
String PM25_PMSug, PM10_PMSug, PM01_PMSug;
SoftwareSerial PMS(13,15); //RX, TX
AM2320 th;

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "*********";
const int updateThingSpeakInterval = 60 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

// Variable Setup
long lastConnectionTime = 0;
int failedCounter = 0;
char t_buffer[10];
char h_buffer[10];
float t1, h1;
String temp, humid;
unsigned long PMS_prevtime,SDS_prevtime;
boolean lastConnected = false;
WiFiClient client; // Initialize Arduino Ethernet Client

void setup()
{

// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
PMS.begin(9600);
WiFi.begin(ssid, password);
delay(5000);
delay(5000);
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(pms_pin, OUTPUT);
digitalWrite(pms_pin, LOW);
delay(5000);
}

void loop()
{
Serial.println("WakeUp! and Start...");

//--------------------AM2320-----------------------
for(int i=0; i<3; i++){
switch(th.Read()) {
case 2:
Serial.print(" "); //CRC failed
break;
case 1:
Serial.print(" "); //Sensor offline
break;
case 0:
t1=th.t;
temp=dtostrf(t1,0,5,t_buffer);
h1=th.h;
humid=dtostrf(h1,0,5,h_buffer);
break;
}
}
//----------------------xxxxxx------------------

//-----------------------PMS5003-----------------------------------------

//get readings from PMS every 5 minutes then put it in low power state
if(millis() - PMS_prevtime > PMSupdate * 60 * 1000){
//Serial.println("Get PMS5003:");
Serial.println("\t FAN-ON");
PM01_PMS=0; //define PM1.0 value of the air detector module
PM25_PMS=0; //define PM2.5 value of the air detector module
PM10_PMS=0;
digitalWrite(pms_pin, HIGH);
delay(16000);
if(PMS.find(0x42)){ //start to read when detect 0x42
PMS.readBytes(buf,LENG);
//Serial.println("\t FANread");
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01_PMS=transmitPM01(buf); //count PM1.0 value of the air detector module
PM25_PMS=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10_PMS=transmitPM10(buf); //count PM10 value of the air detector module
}
}
PM01_PMSug = val_to_str(PM01_PMS).c_str();
PM25_PMSug = val_to_str(PM25_PMS).c_str();
PM10_PMSug = val_to_str(PM10_PMS).c_str();
}

digitalWrite(pms_pin, LOW);
Serial.println("\t FAN-OFF");
PMS_prevtime = millis();
//Serial.print("PM 1 PMS ");Serial.println(PM01_PMS);
//Serial.print("PM 2.5 ");Serial.println(PM25_PMS);
//Serial.print("PM 10 "); Serial.println(PM10_PMS);

Serial.print("PM 1 PM SUG");Serial.println(PM01_PMSug);
Serial.print("PM 2.5 ");Serial.println(PM25_PMSug);
Serial.print("PM 10 "); Serial.println(PM10_PMSug);
Serial.print("temp "); Serial.println(temp);
Serial.print("Humid "); Serial.println(humid);

}

//-------------------------xxxxx-------------------------------------------

//-----------------------------------send data to thingspeak------------------------
if((millis() - lastConnectionTime) > updateThingSpeakInterval)
{
// Serial.print(t1); Serial.print(" "); Serial.print(h1); Serial.print(" ");Serial.print(PM01_PMSug);Serial.print(" ");Serial.print(PM25_PMSug); Serial.print(" "); Serial.println(PM10_PMSug);
// updateThingSpeak("field1="+temp+"&field2="+humid+"&field3="+PM01_PMSug+"&field4="+PM25_PMSug+"&field5="+PM10_PMSug);

}
//-----------------------------------XXXXX----------------------------------------

Serial.println("Sleep");
Serial.print("PM 1 PM SUG");Serial.println(PM01_PMSug);
Serial.print("PM 2.5 ");Serial.println(PM25_PMSug);
Serial.print("PM 10 "); Serial.println(PM10_PMSug);
Serial.print("temp "); Serial.println(temp);
Serial.print("Humid "); Serial.println(humid);
delay(10000);
}

void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);

lastConnectionTime = millis();

if (client.connected())
{
Serial.print("Connecting to ThingSpeak...");
//Serial.println(WiFi.localIP());
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.print("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
}

/************************************************************
Function: Val to String Conversion Time: <1s
***********************************************************/
String val_to_str(uint16_t v)
{
String r;

r = String(v);
if (v < 1000 && v%10) {
r += String(".") + String(v%10);
}

for (int i = 4 - r.length(); i > 0; i--) {
r = String(" ") + r;
}

return r;
}
// Function over
//Check Value
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;

for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf*;*

  • }*

  • receiveSum=receiveSum + 0x42;*

  • if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data*

  • {*

  • receiveSum = 0;*

  • receiveflag = 1;*

  • }*

  • return receiveflag;*
    }
    ///function over
    int transmitPM01(unsigned char *thebuf)
    {

  • int PM01Val;*

  • PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module*

  • return PM01Val;*
    }

//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{

  • int PM2_5Val;*
  • PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module*
  • return PM2_5Val;*
  • }*

//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{

  • int PM10Val;*
  • PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module *
  • return PM10Val;*
    }