Connecting dust sensor PMS5003 with other sensors

Hello.

I am trying to use 5 different emission and temperature sensors and display them on ThingSpeak.

I am using these sensors:

  1. PMS5003 - Dust sensor

  2. CO - MQ-7

  3. CO2 - CO2 MH-Z19

  4. Thermocouple - MAX6657

  5. DHT11 - Moisture

Four sensors work perfectly with ThingSpeak datasheet. Once I add the code for the fifth sensor (PMS5003), it doesnt compile and give me a strange error "call of overloaded...".

The code works perfectly on its own (for the dust), thats what confuses me. Is it maybe due to 1 UART port of Arduino Uno WiFi Rev3? Any experience?

Much much appreciated. Have a wonderful day.

For help, post the code, using code tags, and the complete error message (cut and paste, rather than a photo).

Please post the error code in the proper way. Mark and select the message for copying. Click on the code tag symbol in this window, </>, and paste.

And of course, post the entire code the same way.

Thank you very much.

Right away. Sorry, quite new to this

Error code:

Here is the code :slight_smile:

C:\Users\mater\OneDrive\Počítač\Programy na arduino\Final\WiFi_funguje_ThingSpeak_Prach\WiFi_funguje_ThingSpeak_Prach.ino: In function 'void loop()':
C:\Users\mater\OneDrive\Počítač\Programy na arduino\Final\WiFi_funguje_ThingSpeak_Prach\WiFi_funguje_ThingSpeak_Prach.ino:184:46: error: call of overloaded 'setField(int, double)' is ambiguous
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                       Finálny program NicoLite do ThingSpeak
                  Miriam Nicolanská - Katedra energetickej techniky
                        Diplomová práca - Bezdrôtové moniorovacie zariadenie
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/ 

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//             Základné nastavenie programu
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// Pridať knižnice na WiFi a ThingSpeak 


#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);

#include <WiFiNINA.h>
#include "ThingSpeak.h"

// Prach
#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);


      // Senzor č. 1 - Termočlánok - MAX6675

#include <max6675.h> // max6675.h file is part of the library that you should download from Robojax.com

int soPin = 4;// SO=Serial Out
int csPin = 5;// CS = chip select CS pin
int sckPin = 6;// SCK = Serial Clock pin
MAX6675 Module(sckPin, csPin, soPin);// create instance object of MAX6675

      // Senzor č. 2 - Vlhkosť
#include "DHT.h"

#define DHTPIN 13     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

      // Senzor č. 3 - CO - MQ7
#include "MQ7.h"

#define A_PIN 0
#define VOLTAGE 5

// init MQ7 device
MQ7 mq7(A_PIN, VOLTAGE);

  // Senzor č. 4 - CO2 - MH Z19

// Pripojenie na knižnicu
#include "MHZ19.h"
// Nastavenie pripojovacích pinov
#define rx_pin 11
#define tx_pin 10
#define pwmpin 9
// Vytvorenie objektov z knižnice
MHZ19 *mhz19_uart = new MHZ19(rx_pin, tx_pin);
MHZ19 *mhz19_pwm = new MHZ19(pwmpin);

// Senzor č. 5 - MQ135 (Kvalita ovzdušia)
#include <MQ135.h>
#define PIN_MQ135 A1
MQ135 mq135_sensor(PIN_MQ135);
float temperature = 21.0; // neskôr tu pridať meranie z prostredia
float humidity = 25.0; // neskôr pridať meranie vlhkosti


// Heslá na internet na ktorý sa pripojí Arduino Uno Rev3

#define SECRET_SSID "Ultra SuperSpeed Net Attc 8:)"		// replace MySSID with your WiFi network name
#define SECRET_PASS "TN2CaKxq"	// replace MyPassword with your WiFi password

#define SECRET_CH_ID 1721144			// replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "YZUCEGF56WETY8DE"   // replace XYZ with your channel write API Key

char ssid[] = SECRET_SSID;   // your network SSID (name) 
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//      "VOID" nastavenie - POčiatočné nastavenie
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void setup() {
  Serial.begin(115200);  // Initialize serial
  pmsSerial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }

   // Senzor č. 2 - Vlhkosť
  Serial.println(F("DHTxx test!"));
  dht.begin();
 
 // Senzor č. 3 - CO - MQ7
  Serial.println("");   // blank new line
  Serial.println("Calibrating MQ7");
  mq7.calibrate();    // calculates R0
  Serial.println("Calibration done!");

      // Senzor č. 4 - CO2 - MH Z19
  mhz19_uart->begin(rx_pin, tx_pin);
  // Vypnút autokalibráciu (takto to bolo originál)
  mhz19_uart->setAutoCalibration(false);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }
    
  ThingSpeak.begin(client);  //Initialize ThingSpeak
}

// Dust
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;

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// "LOOP" Nastenie - Opakovanie
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void loop() {

  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }

    // Senzor č. 2 - Vlhkosť
      float h = dht.readHumidity();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Senzor č. 4 - CO2 - MH Z19
  // Zahajenie meranie CO2 a odčítanie hodnoty
  measurement_t m = mhz19_uart->getMeasurement();
  int co2ppm = mhz19_pwm->getPpmPwm();

    // Senzor č. 5 - MQ135 (Kvalita ovzdušia)
    float rzero = mq135_sensor.getRZero();
    float correctedRZero = mq135_sensor.getCorrectedRZero(temperature, humidity);
    float resistance = mq135_sensor.getResistance();
    float ppm = mq135_sensor.getPPM();
    float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humidity);

  // set the fields with the values
  ThingSpeak.setField(1, Module.readCelsius());
  ThingSpeak.setField(2, mq7.readPpm());
  ThingSpeak.setField(3, m.co2_ppm);
  ThingSpeak.setField(4, h);
  ThingSpeak.setField(5, correctedPPM);

  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // Dust- This is what causes the problem when I add it
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  if (readPMSdata(&pmsSerial)) {
    ThingSpeak.setField(6,data.particles_10um)}

  // figure out the status message
  if(number1 > number2){
    myStatus = String("field1 is greater than field2"); 
  }
  else if(number1 < number2){
    myStatus = String("field1 is less than field2");
  }
  else{
    myStatus = String("field1 equals field2");
  }
  
  // set the status
  ThingSpeak.setStatus(myStatus);
  
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  
  // change the values
  number1++;
  if(number1 > 99){
    number1 = 0;
  }
  number2 = random(0,100);
  number3 = random(0,100);
  number4 = random(0,100);
  
  delay(15000); // Wait 15 seconds to update the channel again
}

// ####################################
// "extra" for the dust sensor
// ####################################

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;
  }
 
  // Now 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];
  }
  
  // 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;
}

The means that the compiler cannot interpret the variable type of one of the arguments to setField(), most likely the second, in order to make the appropriate function call.

You will need to look carefully at the library code for setField() to see what types are accepted, then look at the ThingSpeak.setField() calls in your code to see what types are being offered.

The code line number in the error message seems to be wrong, as the identified line does not call setField(). I will guess that the code you posted is not the code that resulted in the error message.

I found my mistake. However, the dust sensor is still not showing any data.

When I connect it alone, it works. When collected in the group of sensors, the ThinSpeak server fields (I chose 6 7 8).

Here is the code

sem /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                       Finálny program NicoLite do ThingSpeak
                  Miriam Nicolanská - Katedra energetickej techniky
                        Diplomová práca - Bezdrôtové moniorovacie zariadenie
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/ 

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//             Základné nastavenie programu
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// Pridať knižnice na WiFi a ThingSpeak 
#include <Arduino.h>
#include <WiFiNINA.h>
#include "ThingSpeak.h"

// Dust PMS5003 sensor
#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);


      // Senzor č. 1 - Termočlánok - MAX6675

#include <max6675.h> // max6675.h file is part of the library that you should download from Robojax.com

int soPin = 4;// SO=Serial Out
int csPin = 5;// CS = chip select CS pin
int sckPin = 6;// SCK = Serial Clock pin
MAX6675 Module(sckPin, csPin, soPin);// create instance object of MAX6675

      // Senzor č. 2 - Vlhkosť
#include "DHT.h"

#define DHTPIN 13     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

      // Senzor č. 3 - CO - MQ7
#include "MQ7.h"

#define A_PIN 0
#define VOLTAGE 5

// init MQ7 device
MQ7 mq7(A_PIN, VOLTAGE);

  // Senzor č. 4 - CO2 - MH Z19

// Pripojenie na knižnicu
#include "MHZ19.h"
// Nastavenie pripojovacích pinov
#define rx_pin 11
#define tx_pin 10
#define pwmpin 9
// Vytvorenie objektov z knižnice
MHZ19 *mhz19_uart = new MHZ19(rx_pin, tx_pin);
MHZ19 *mhz19_pwm = new MHZ19(pwmpin);

// Senzor č. 5 - MQ135 (Kvalita ovzdušia)
#include <MQ135.h>
#define PIN_MQ135 A1
MQ135 mq135_sensor(PIN_MQ135);
float temperature = 21.0; // neskôr tu pridať meranie z prostredia
float humidity = 25.0; // neskôr pridať meranie vlhkosti


// Heslá na internet na ktorý sa pripojí Arduino Uno Rev3

//#define SECRET_SSID "Ultra SuperSpeed Net Attc 8:)"		// replace MySSID with your WiFi network name
//#define SECRET_PASS "TN2CaKxq"	// replace MyPassword with your WiFi password

#define SECRET_SSID "Adapter"		// replace MySSID with your WiFi network name
#define SECRET_PASS "vn5enjnvgr5f"	// replace MyPassword with your WiFi password

#define SECRET_CH_ID 1721144			// replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "YZUCEGF56WETY8DE"   // replace XYZ with your channel write API Key

char ssid[] = SECRET_SSID;   // your network SSID (name) 
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//      "VOID" nastavenie - POčiatočné nastavenie
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void setup() {
  Serial.begin(115200);  // Initialize serial
  pmsSerial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }

   // Senzor č. 2 - Vlhkosť
  Serial.println(F("DHTxx test!"));
  dht.begin();
 
 // Senzor č. 3 - CO - MQ7
  Serial.println("");   // blank new line
  Serial.println("Calibrating MQ7");
  mq7.calibrate();    // calculates R0
  Serial.println("Calibration done!");

      // Senzor č. 4 - CO2 - MH Z19
  mhz19_uart->begin(rx_pin, tx_pin);
  // Vypnút autokalibráciu (takto to bolo originál)
  mhz19_uart->setAutoCalibration(false);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }
    
  ThingSpeak.begin(client);  //Initialize ThingSpeak
}

// Dust
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;

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// "LOOP" Nastenie - Opakovanie
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void loop() {

  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }

    // Senzor č. 2 - Vlhkosť
      float h = dht.readHumidity();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Senzor č. 4 - CO2 - MH Z19
  // Zahajenie meranie CO2 a odčítanie hodnoty
  measurement_t m = mhz19_uart->getMeasurement();
  int co2ppm = mhz19_pwm->getPpmPwm();

    // Senzor č. 5 - MQ135 (Kvalita ovzdušia)
    float rzero = mq135_sensor.getRZero();
    float correctedRZero = mq135_sensor.getCorrectedRZero(temperature, humidity);
    float resistance = mq135_sensor.getResistance();
    float ppm = mq135_sensor.getPPM();
    float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humidity);

  // set the fields with the values
  ThingSpeak.setField(1, float(Module.readCelsius()));
  ThingSpeak.setField(2, float(mq7.readPpm()));
  ThingSpeak.setField(3, float(m.co2_ppm));
  ThingSpeak.setField(4, float(h));
  ThingSpeak.setField(5, float(correctedPPM));
  
  if (readPMSdata(&pmsSerial)) {
    ThingSpeak.setField(6, float(data.particles_03um));
    ThingSpeak.setField(7, float(data.particles_05um));
    ThingSpeak.setField(8, float(data.particles_10um));}

  // figure out the status message
  if(number1 > number2){
    myStatus = String("field1 is greater than field2"); 
  }
  else if(number1 < number2){
    myStatus = String("field1 is less than field2");
  }
  else{
    myStatus = String("field1 equals field2");
  }
  
  // set the status
  ThingSpeak.setStatus(myStatus);
  
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  
  // change the values
  number1++;
  if(number1 > 99){
    number1 = 0;
  }
  number2 = random(0,100);
  number3 = random(0,100);
  number4 = random(0,100);
  
  delay(15000); // Wait 15 seconds to update the channel again
}

// ####################################
// "extra" for the dust sensor
// ####################################

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;
  }
 
  // Now 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];
  }
  
  // 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;
}
zadajte alebo vložte kód

But when everything else is tossed into the pot, it doesn't work, and you don't know why.

I don't know either, but I can recommend a new approach.

Start from that working code, and add features and sensors one at time, until something breaks.

Fix the problem, then add more features and sensors, one at a time.

Repeat until done!

Put some Serial.print statements in that block to confirm that it works.
If that appears to work try swapping channels 1,2 and 3 for the dust sensor with those of the temperature/ gas sensors.

I did try that :D. Somehow I fixed the errors, but it simply doesnt print the data...Thank you for your feedback :slight_smile:

Will do :slight_smile:

So...I did try that. And basically, it isnt printing anything. It seems that the if statement is always FALSE. I need that "if" statement, because the sensor does not work otherwise (I did try). So....I am thinking of using a small ESP8266 device just for the dust.....

This looks untidy. You've repeated the definition of pmsSerial which you are using for the dust sensor:

#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);

#include <WiFiNINA.h>
#include "ThingSpeak.h"

// Prach
#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);

If this statement is correct:

That would appear to rule out the classic error of not crossing over the TX and RX wires between the sensor and the MCU.
Maybe also show the code which does work with the dust sensor.

Ensure compiler warnings are enabled in the Arduino IDE.

edit
You appear to have corrected the duplicate definition in post #7

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