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