Hi
I have written code to read a dht22 sensor, a pms5003 sensor and send that data to a mosquitto broker in home assistant. To do my tests and see the data, i was writing the output of the pms5003 sensor in the console via particle.publish. The code works fine.
I dont need that publish code anymore, so i tried to remove the particle publish step. But, when i do this, after flashing the code, the photon briefly blinks cyan and then starts to blink red 10 times, until it gets restarted.
This is the code
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
#include "DustSensor.h"
#include <PietteTech_DHT.h>
#define DHTTYPE DHT22
#define DHTPIN D4
DustSensor dustSensor;
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
double serverTemp;
double serverHumidity;
static char humidity[4];
static char temperature[4];
char resultstr[128];
pms5003data data;
void callback(char* topic, byte* payload, unsigned int length);
MQTT client("192.168.1.12", 1883, callback);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
}
void setup()
{
dustSensor.begin();
Particle.variable("result", resultstr, STRING);
}
void send_dht22_data() {
int result = DHT.acquireAndWait(2000);
serverTemp = DHT.getCelsius();
serverHumidity = DHT.getHumidity();
sprintf(temperature, "%2.1f", serverTemp);
sprintf(humidity, "%2.1f", serverHumidity);
client.connect("photon", "XXX", "XXX");
if (client.isConnected()) {
client.publish("temperature",temperature);
client.publish("humidity",humidity);
}
}
void send_pms5003_data() {
if(dustSensor.listen()) {
data = dustSensor.readData();
sprintf(resultstr, "{\"pm10\":%i,\"pm25\":%i,\"pm100\":%i,\"checksum\":%i}", data.pm10_standard, data.pm25_standard, data.pm100_standard, data.checksum);
Particle.publish("reading", resultstr);
client.connect("photon", "XXX", "XXX");
if (client.isConnected()) {
client.publish("pm10",String(data.pm10_standard));
client.publish("pm25",String(data.pm25_standard));
client.publish("pm100",String(data.pm100_standard));
}
}
}
void loop()
{
send_dht22_data();
send_pms5003_data();
delay(100);
}
What i try to remove is
Particle.publish(“reading”, resultstr);
Any idea why i cant remove it? What can i do to start publishing in the console?
Thanks