Temperamental photon, connection issues - possible code issues?


#1

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!


#2

@starby, one thing I noticed is that you have 21 Particle variables defined. The docs indicate:

Up to 20 cloud variables may be registered and each variable name is limited to a maximum of 12 characters.


#3

didn’t notice that one - I have dropped three of the variables so it gives 19 total now (I think there was 22 before) but still having issues.

Also, a little more information on the “going offline after 30 seconds” issue - trying it now it seems it still happens but it actually comes back online around 30 seconds later

spark/status online EnviroMonitor 11/8/18 at 9:12:09 pm
spark/device/last_reset power_down EnviroMonitor 11/8/18 at 9:12:10 pm
spark/status offline EnviroMonitor 11/8/18 at 9:12:35 pm
spark/status online EnviroMonitor 11/8/18 at 9:13:04 pm

And it looks like the MQTT broker is receiving the first round of variables but only the one…no more updates after the first. The published variables seem to still update as usual though?


#4

Did also do some more reading around the MQTT library and have increased the message size to 512 and set the keep alive timeout to 60 seconds (as it should run every 29) so it shouldn’t conflict there.

Unfortunately no change in behaviour as of yet (although all three sensors seem to be retrieving results again which is a plus!)

I did find that the photon manages to get through three loops (the MQTT broker receives three publishes, around 30 seconds apart) before it goes offline momentarily. If I reduce the wait time to 59 seconds it only gets through 2 loops before the photon goes offline momentarily. Once its gone offline, once its back online the MQTT updates no longer work.

I guess the issue lies with the disconnect/reconnect around the ~1 minute mark

Edit / Update - I did some more testing and after restarting the photon did manage to stay alive for just over 2 minutes (with 4 MQTT updates in that time) before disconnecting


#5

Okay final update - I don’t think its really a fix but more of a bandage, but for the time being it seems to be working

I ended up moving the MQTT connection code to the start of the loop, and added a ‘client.disconnect()’ at the end of the loop so each time it runs an update it makes a new connection. Seems to be working fine now