Photon disconecting from wifi

Hi! After a while working 24/7 (about 4 months) the board is starting to behave wierdly. It’s first connect to the cloud (blue LED fading in and out) and after about 30 to 40 seconds, the led turns green and it doesn’t connect again until a reset is made.

I had made a little software update, but it was just changing some pins definition.

What may I do to fix this issue?

In case it’s need, the code that I’m using is this:

    //#include "Adafruit_DHT.h"
    #include <Adafruit_DHT.h>
    #include <blynk.h>

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

    WidgetTerminal terminal(V8);

    #define DHTPIN 2     // what pin we're connected to
    #define mosfetPin 4

    bool mosfetState = false;
    // Uncomment whatever type you're using!
    //#define DHTTYPE DHT11		// DHT 11 
    #define DHTTYPE DHT22		// DHT 22 (AM2302)
    //#define DHTTYPE DHT21		// DHT 21 (AM2301)

    // Connect pin 1 (on the left) of the sensor to +5V
    // Connect pin 2 of the sensor to whatever your DHTPIN is
    // Connect pin 4 (on the right) of the sensor to GROUND
    // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

    #define threesholdTemp_SUP 27
    #define threesholdTemp_INF 20

    DHT dht(DHTPIN, DHTTYPE);

    //DANGER - DO NOT SHARE!!!!
    char auth[] = "bf5174a178ae417d9f7993a5bc409eaa"; // Put your blynk token here
    //DANGER - DO NOT SHARE!!!!

    char VERSION[64] = "0.04";

    int sampleInterval = 1000;
    unsigned long DHTnextSampleTime = 0;

    #define threshold 2 //Threshold in temperature/humidity changes to avoid misreadings
    int lastT, lastH;


    /* TEST BME280*/

    // This #include statement was automatically added by the Particle IDE.
    #include <Wire.h>
    #include <SPI.h>
    #include <CE_BME280.h> // //Modified library to match the i2c address on the sensor
    //#include <Adafruit_Sensor.h>
    //#include <Adafruit_BME280.h>

    #define SEALEVELPRESSURE_HPA (1013.25)

    CE_BME280 bme; // I2C



    void setup() {
    	Serial.begin(9600); 

    	dht.begin();
    	pinMode(mosfetPin,OUTPUT);
    	
    Blynk.begin(auth);
    DHTnextSampleTime = 0;

    lastT = dht.getTempCelcius();
    lastH = dht.getHumidity();

    Blynk.virtualWrite(V3,sampleInterval);


      if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
      }
    }

    void loop(){

      Blynk.run(); // all the Blynk magic happens here
     
      // Check if we need to start the next sample
    if (millis() > DHTnextSampleTime) {
        getDHT(); 
        //getBME();
        terminal.println("sample");
        terminal.flush();
    }
    }

    // SLIDER
    BLYNK_WRITE(V0) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin

    //Blynk.syncVirtual(V1);
    int value = param.asInt();
    analogWrite(mosfetPin, value);
    if (value == 0) { // If slider is OFF, make sure button is as well
        Blynk.virtualWrite(V1,0);
    }else { // If slider is anything equal or higher than 1, set button ON
        Blynk.virtualWrite(V1,1);
    }
    }

    // BUTTON
    BLYNK_WRITE(V1) {
    int value = param.asInt();
    digitalWrite(mosfetPin,value);
    if (value==0) {
        //LOW
        // change button to off
        Blynk.virtualWrite(V0,0);
    }else{        // change button to on
        
        Blynk.virtualWrite(V0,255);
    }
    }

    //cHANGE SAMPLE INTERVAL
    BLYNK_WRITE(V4){
    Blynk.syncVirtual(V3); //Get Labeled value in V3
    int value = param.asInt();
    value = value * 1000; // Transform into millisecond
    Blynk.virtualWrite(V3,value);
    sampleInterval = value;

    }


    void changeColor(float _t){
    if (_t > threesholdTemp_SUP){ //change to red
        Blynk.setProperty(V2,"color","#FF0000");
        Blynk.setProperty(V6,"color","#FF0000");
    }else if (_t < threesholdTemp_INF){ // change to blue
        Blynk.setProperty(V2,"color","#0000FF");
        Blynk.setProperty(V6,"color","#0080FF");   
    }else{ //green
        Blynk.setProperty(V2,"color","#00FF00");
        Blynk.setProperty(V6,"color","#00FF00");
    }
    }
    void getDHT(){

    	float h = dht.getHumidity();

    	float t = dht.getTempCelcius();
    	
    	changeColor(t);
    // ADD A CHECK OF THE VALUES TO AVOID DATA CORRUPTION
    if (t > lastT + threshold || t < lastT - threshold){
        // the reading is outside the admited limits, so, asume the value hasn't changed since last time.
        t = lastT;   
    }else{
        lastT = t;
    }
    if(h > lastH + threshold || h < lastH - threshold){
        // the reading is outside the admited limits, so, asume the value hasn't changed since last time.
        h = lastH;
    }else{
        lastH = h;
    }
    // Check if any reads failed and exit early (to try again).
    	if (isnan(h) || isnan(t)) {
    	    //Blynk.notify("Failed to read sensor");
    	    //Serial.println("Failed to read sensor");
    		return;
    	}else{
    	    Blynk.virtualWrite(V5, h);
    	    Blynk.virtualWrite(V7, h);
        Blynk.virtualWrite(V6, t);
        Blynk.virtualWrite(V2, t);
    	}
    	DHTnextSampleTime = millis() + sampleInterval;
    	
    	
    }

    void getBME(){

    float t = bme.readTemperature();
    float h = bme.readHumidity();
    float p = bme.readPressure() / 100.0F; // hPa
    float alt = bme.readAltitude(SEALEVELPRESSURE_HPA); // m

    terminal.print("p = ");
    terminal.println(p);
    terminal.print("Alt = ");
    terminal.print(alt);
    terminal.flush();
    changeColor(t);
    // ADD A CHECK OF THE VALUES TO AVOID DATA CORRUPTION
    if (t > lastT + threshold || t < lastT - threshold){
        // the reading is outside the admited limits, so, asume the value hasn't changed since last time.
        t = lastT;   
    }else{
        lastT = t;
    }
    if(h > lastH + threshold || h < lastH - threshold){
        // the reading is outside the admited limits, so, asume the value hasn't changed since last time.
        h = lastH;
    }else{
        lastH = h;
    }
    // Check if any reads failed and exit early (to try again).
    	if (isnan(h) || isnan(t)) {
    	    //Blynk.notify("Failed to read sensor");
    	    //Serial.println("Failed to read sensor");
    		return;
    	}else{
    	    Blynk.virtualWrite(V5, h);
    	    Blynk.virtualWrite(V7, h);
        Blynk.virtualWrite(V6, t);
        Blynk.virtualWrite(V2, t);
    	}
    	DHTnextSampleTime = millis() + sampleInterval;
    }

Greetings.

First I’d add SYSTEM_THREAD(ENABLED) and what exactly have you changed?
There are things that may work with one pin but not another.

Also, in the Particle world, we call pins by their full name “D2” & “D4” and not just non-descript numbers - they are no prisoners :wink:

This line is most likely the one that knocks off your cloud connection.
Cange that to

  while(1) Particle.process();
1 Like

I’ve uploaded a simple blynk test and that happend again. Maybe it’s not upgrading the code in the device and keeps this code in the photon?

It’s any way to upload a code locally?

You made my day with this hahaha

Greetings.

You can build semi-locally with CLI or download the binary via the download icon in Web IDE
The little cloud :cloud_lightning: symbol next to the project name

After that you can flash the binary via CLI again like this

particle flash --usb firmware.bin
// or
particle flash --serial firmware.bin

But I have noticed some times that a device gets knocked offline when the Blynk app or server are not available/responsive enough for some reson too.

1 Like

Great, thanks. Will try that when I arrive at home.

I’ve no issues with Blynk server at all (I’ve been usign it for about 1 year), so, I don’t think that it’s the issue.

Maybe it’s the while(1); what it’s blocking the code and not allowing the photon to stay connected. Maybe upgrading the code locally will fix this.

Thanks.

@ScruffR That was the problem. It’s working like a charm now

2 Likes

Thanks ScruffR for the help!

1 Like