I have a photon that has been doing some fairly simple temp/humidity reading (via 3 x DHT22 sensors), which I then pick up via Google scripts and save in a Spreadsheet. I have run it for a few months on and off and although there is the occasional issue with the photon becoming unavailable (going offline) its been working pretty well.
I wanted to add MQTT publishing as I can then integrate it with some other bits I have in the home but this has created so many issues for me to the point where the main behaviour currently is:
- Goes offline after ~30 seconds after boot (can see this via the Event log in the console) and I have to re-start it manually by pulling the plug
- Starts fine but no MQTT publishing seems to occur
- Starts fine, seems to start MQTT publishing but soon goes offline
Like I said, it had been working fine for quite a while before I attempted these latest changes so I can only think the issues are with the code side.
The photon is linked to 3 temp/humidity sensors, and also a switch for an alarm (this is monitored through IFTTT). All the code has essentially been taken from different online sources and cobbled together as I am not that good with coding.
Here is the current version of the code tho -
#include "MQTT/MQTT.h"
#include "PietteTech_DHT/PietteTech_DHT.h"
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINC D2
#define DHTPINR D3
#define DHTPINI D4
#define DHT_SAMPLE_INTERVAL 29000 // Sample every twenty nine seconds
double cantempc;
double canhumidity;
double cantempf;
double cantempk;
double candewp;
double candewpslow;
double canresult;
double roomtempc;
double roomhumidity;
double roomtempf;
double roomtempk;
double roomdewp;
double roomdewpslow;
double roomresult;
double intempc;
double inhumidity;
double intempf;
double intempk;
double indewp;
double indewpslow;
double inresult;
int blue = D7;
int alarmpin = D0;
int isalarm;
// For the MQTT client
byte server[] = { 192,168,1,150 };
MQTT client(server, 1883, callback);
// Declaration
void dht_wrapper();
// Lib Initialize
PietteTech_DHT DHTC(DHTPINC, DHTTYPE, dht_wrapper);
PietteTech_DHT DHTI(DHTPINI, DHTTYPE, dht_wrapper);
PietteTech_DHT DHTR(DHTPINR, DHTTYPE, dht_wrapper);
// globals
unsigned int DHTnextSampleTime;
bool bDHTstarted;
int NextCheckTime;
int n;
void setup() {
pinMode(blue, OUTPUT);
pinMode(alarmpin,INPUT);
DHTnextSampleTime = 0;
//RGB.control(true);
Particle.variable("canresult", &canresult, DOUBLE); // Set the variable values for Can sensor
Particle.variable("cantempc", cantempc);
Particle.variable("cantempf", cantempf);
Particle.variable("cantempk", cantempk);
Particle.variable("canhumidity", canhumidity);
Particle.variable("candewp", candewp);
Particle.variable("candewpslow", candewpslow);
Particle.variable("roomresult", &roomresult, DOUBLE); // Set the variable values for Room sensor
Particle.variable("roomtempc", roomtempc);
Particle.variable("roomtempf", roomtempf);
Particle.variable("roomtempk", roomtempk);
Particle.variable("roomhumidity", roomhumidity);
Particle.variable("roomdewp", roomdewp);
Particle.variable("roomdewpslow", roomdewpslow);
Particle.variable("inresult", &inresult, DOUBLE); // Set the variable values for In sensor
Particle.variable("intempc", intempc);
Particle.variable("intempf", intempf);
Particle.variable("intempk", intempk);
Particle.variable("inhumidity", inhumidity);
Particle.variable("indewp", indewp);
Particle.variable("indewpslow", indewpslow);
Particle.variable("isalarm", &isalarm, INT);
Particle.publish("Alarm", "off", 0, PRIVATE);
isalarm == 0;
client.connect("EnviroMonitor", "username", "password");
delay(10);
client.subscribe("can/temp");
client.subscribe("can/hum");
client.subscribe("room/temp");
client.subscribe("room/hum");
client.subscribe("intake/temp");
client.subscribe("intake/hum");
}
// receive MQTT message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
delay(1000);
}
void dht_wrapper() {
DHTC.isrCallback();
DHTI.isrCallback();
DHTR.isrCallback();
}
void loop()
{
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) {
DHTC.acquire();
DHTI.acquire();
DHTR.acquire();
bDHTstarted = true;
delay(100);
}
if (!DHTC.acquiring()) {
if (DHTC.getCelsius() <= 0) {
DHTC.acquire();
delay(200);
}
digitalWrite(blue, HIGH);
// get DHTC status
int cresult = DHTC.getStatus();
digitalWrite(blue, LOW);
if (cresult == DHTLIB_OK) {
digitalWrite(blue, HIGH);
canhumidity = DHTC.getHumidity();
cantempf = DHTC.getFahrenheit();
cantempc = DHTC.getCelsius();
cantempk = DHTC.getKelvin();
candewp = DHTC.getDewPoint();
candewpslow = DHTC.getDewPointSlow();
// publish via MQTT
if (cantempf > 0) {
client.publish("can/temp",String(cantempf,4));
delay(100);
}
if (canhumidity > 0) {
client.publish("can/hum",String(canhumidity,3));
}
digitalWrite(blue, LOW);
}
}
if (!DHTI.acquiring()) {
if (DHTI.getCelsius() <= 0) {
DHTI.acquire();
delay(200);
}
digitalWrite(blue, HIGH);
// get DHTI status
int iresult = DHTI.getStatus();
digitalWrite(blue, LOW);
if (iresult == DHTLIB_OK) {
digitalWrite(blue, HIGH);
inhumidity = DHTI.getHumidity();
intempf = DHTI.getFahrenheit();
intempc = DHTI.getCelsius();
intempk = DHTI.getKelvin();
indewp = DHTI.getDewPoint();
indewpslow = DHTI.getDewPointSlow();
// publish via MQTT
if (intempf > 0) {
client.publish("intake/temp",String(intempf,4));
delay(100);
}
if (inhumidity > 0) {
client.publish("intake/hum",String(inhumidity,3));
}
digitalWrite(blue, LOW);
}
}
if (!DHTR.acquiring()) {
if (DHTR.getCelsius() <= 0) {
DHTR.acquire();
delay(200);
}
digitalWrite(blue, HIGH);
// get DHTR status
int rresult = DHTR.getStatus();
digitalWrite(blue, LOW);
if (rresult == DHTLIB_OK) {
digitalWrite(blue, HIGH);
roomhumidity = DHTR.getHumidity();
roomtempf = DHTR.getFahrenheit();
roomtempc = DHTR.getCelsius();
roomtempk = DHTR.getKelvin();
roomdewp = DHTR.getDewPoint();
roomdewpslow = DHTR.getDewPointSlow();
// publish via MQTT
if (roomtempf > 0) {
client.publish("room/temp",String(roomtempf,4));
delay(100);
}
if (roomhumidity > 0) {
client.publish("room/hum",String(roomhumidity,3));
}
digitalWrite(blue, LOW);
}
}
n++;
bDHTstarted = false;
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;
}
isalarm = digitalRead(alarmpin);
if (isalarm == 1) {
digitalWrite(blue, HIGH);
Particle.publish("Alarm", "on", 0, PRIVATE);
delay(60000);
Particle.publish("Alarm", "off", 0, PRIVATE);
digitalWrite(blue, LOW);
}
}
Fairly sure its unrelated, but while attempting some of the latest changes one of the DHT22 sensors has stopped working and is only reading â0â for all variables - not sure how to troubleshoot this yet but I can only assume its coding related (as it started occurring when making these changes) or the sensor itself has gone (its actually in another room so it wasnât touched at all during these changes)
Any help is much appreciated!