Photon loses connectivity every few minutes

Hi,

My photon loses it’s connection to the could every minute or so, and then immediately comes back online. This only started occurring when I added an I2C current sensor and new code, so it shouldn’t be the wifi signal or hardware. The RSSI is -65.

My main loop doesn’t do much but just sit there waiting for the device time to hit a multiple of 5 min, and then upload a reading from the sensor. Maybe this is the issue? I added in a 5sec delay, but it hasn’t solved the problem. Here’s a pic of the disconnects, and my code.

Any help would be greatly appreciated!

Thanks Particle Community.


#include "Adafruit_INA219.h"

#include <application.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

//Name pins, name functions, create variables////////////////////////////////////////////

int load = A0;
bool state = 0;
double w_inst = 0; // current power, in W per m^2
double wh_cum = 12; // cumulative energy collected today, in Wh / m^2
double burn = 0; //amount of time to run the light for, in milliseconds
int i=0;

  double shuntvoltage = 0;
  double busvoltage = 0;
  double current_mA = 0;
  double loadvoltage = 0;

char publishData[40] = ""; //empty variable 40 units long which will hold data being sent to cloud
char publishBurn[45] = ""; //empty variable 45 units long which will hold data being sent to cloud

void setup(void) 
{
  uint32_t currentFrequency;
    
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  ina219.begin();
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();
   
   
    Time.zone(-7); //set time zone to PT
     
    pinMode(load, OUTPUT);  // sets pin as output
    digitalWrite(load,LOW); // sets it Low
  
  Particle.variable("Current",current_mA);
  Particle.variable("Voltage",loadvoltage);
  Particle.variable("Power",w_inst);
  Particle.variable("Energy",wh_cum);
  
  
}

void loop(void) 
{
 
// delay to fix connectivity issues? 
  delay(5000);
  i++;
  
  //calculate power, energy, and publish every 5 min

if ( 8 <= Time.hour() && Time.hour() <= 19){
//if ( Time.minute() <= 15){
    if (Time.minute() % 5 == 0){
            
            shuntvoltage = ina219.getShuntVoltage_mV();
            busvoltage = ina219.getBusVoltage_V();
            current_mA = ina219.getCurrent_mA();
            loadvoltage = busvoltage + (shuntvoltage / 1000);
           
            w_inst = (current_mA / 1000) * loadvoltage ;
            wh_cum += w_inst / 12; 
            
            sprintf(publishData,"%.2f ||| %.2f ||| %.2f",current_mA,w_inst,wh_cum); //URL Example https://community.particle.io/t/example-using-ifttt-google-drive/8908
            Particle.publish("datawrite",publishData,30); //ttl number must be less than writeinterval
            
            //calculate burn time
            burn = ( wh_cum / (1.15 * 12.6) ) * 60 * 60 * 1000; // calculates the burn, in mseconds, based on lightbulb current and voltage
            
            // reset counters
            state = 1;
            
            delay(60000); //wait for the minute to no longer be a multiple of 5
     
    }
}

  
//burn off stored energy

if ( Time.hour() < 8 && state == 1){
//if ( Time.minute() > 15 && state == 1){
        sprintf(publishBurn,"Begin %.2f Wh burn lasting %.2f minutes",wh_cum,burn / 1000 / 60);
        Particle.publish("burnnotice",publishBurn,30); //ttl number must be less than writeinterval
        
        while (burn > 0 ){
        digitalWrite(load,HIGH); // turn on light
        delay(10000);
        burn -= 10000; //decrement burn by 10s
        }
            state = 0; //reset state
            wh_cum = 0; // reset cumulative energy
            digitalWrite(load,LOW); // turn off light
            Particle.publish("burnnotice","Burn Completed!",30); //ttl number must be less than writeinterval
    }
    
//end program
  
}

Your delays are far to huge, You have to exit the loop() function in order for it to handle the WiFi stuff.
Or, you may want to loo into this as well.
https://docs.particle.io/reference/firmware/photon/#system-thread
SYSTEM_THREAD(ENABLED);

@qmnjb007

If you are using SYSTEM_THREAD(ENABLED);, you can also try this function for a soft delay.

inline void softDelay(uint32_t msDelay)
{
  for (uint32_t ms = millis(); millis() - ms < msDelay; Particle.process());
}

Hmmm, adding SYSTEM_THREAD(ENABLED) didn’t seem to help…

When you say my delays are too big, are you referring to the 10 and 60 second delays I have? Those are inside the if statements that aren’t getting called, so could those really be the issue?

Really appreciate the help…

While I agree that non-blocking paradigm should be used wherever possible, I’d point out that delay() as such won’t be responsible for the connection drop out as it internally will call Particle.process() every 1000ms of accumulated delay time.

But I’d put my bets on the library which may not be considering the fact that the cloud housekeeping needs to be allowed to run every regularly (at least once in 10 sec).


Update:
After looking into the lib I can’t find anything in the lib either. Could it be that you have other devices fighting for an IP or your router having a very short lease time?

Also what system version are you running on your Photon? Old versions had hanging issues with I2C under some conditions. Currently 0.6.2 should be the version to use.

3 Likes

Where are you getting the library from, and how are you adding it to your project? On Build and Dev, I see the file name for the include as adafruit-ina219.h (lowercase with a dash instead of an underscore). Also, you have the library included twice. I don't know if that could cause your problem, but you should correct it anyway.

You say that you're losing connection every minute or so, but your console printout shows them to be 12, 19, 29, and 15 seconds apart. Does it continue this way indefinitely? Do you continue to get the data published every 5 minutes?

Well, this is embarrassing… it turns out that a POE cable plugged into my router was cut and must have been intermittently shorting out, which was causing all sorts of problems with some - but not all - of the devices on my network. Appreciate the help and code feedback!

2 Likes