[SOLVED] Electron stops communicating even if connected

Hi, newbye here :slight_smile:
It’s all the day I’m fighting against my Electron.
At the moment I have a Free(Telecom) SIM inside and I have the firmare 0.7.0.

I added my Electron in “Devices” and I flashed the firmware with APN (‘free’) with
particle flash --serial firmwareFree.bin
in DFU mode without problems.

After flashing, it blinks green for half a second, then cyan blinking and cyan breathing.
I can see the Electron online, I can signal it or ping it.
Perfect…but after some minutes it stops responding ping or signals…
The led is cyan breathing all the time (Supposes to be connected…).
If I reset it with the button, it reconnects for a while but it eventually stops working…

What’s the problem?
I already reset it with tinker+update+tinker+firmwareFree.bin without success :frowning:

After 30min it rebooted and it got a new connection. Now is responding again but I expect it’s not working in some minutes again…

That sounds like a keep-alive issue with your 3rd-party SIM card. Not only do you need to set it, as explained here, you need to set it in a specific way:

In system firmware 0.6.2 through 0.8.0-rc.3, there is an issue where the Particle.keepAlive value does not stay properly set. The workaround is as follows:

Create a global variable, such as:

bool hasSetKeepAlive = false;

And add this to your loop:

	if (Particle.connected()) {
		if (!hasSetKeepAlive) {
			hasSetKeepAlive = true;
			Particle.keepAlive(120);
		}
	}
	else {
		hasSetKeepAlive = false;		
	}

Basically, you need to set the keep alive only after successfully connected to the Particle cloud, and every time you’ve disconnected.

1 Like

Hi @rickkas7, thank you for helping me.
I tried flashing Tinker online with

STARTUP(cellular_credentials_set("free", "", "", NULL));

From the online console I put D7=HIGH or LOW on digitalWrite on the right, checking for a sensor’s response.
First time I had responses for 17min, then I restarted and I had responses even for 25+ min, reset again and responses for 7min…I can’t really say what’s my interval…5 min maybe?
I put 300 in my setup and loop.

Now I flashed my script from online IDE; flashing ok but it hasn’t even started :confused: The led is breathing cyan.

Here’s my code (important parts):

// This #include statement was automatically added by the Particle IDE.
#include "Ublox.h"
#include "cellular_hal.h"
#include "PMS.h"
#include "Wire.h"
#include "PMS.h"
#include "Serial5/Serial5.h"
#include "Ublox.h"

#define window 10000		// the flash firmware window time
#define GPSwait 15000

void setup()
{
    Particle.keepAlive(300);
    uint8_t osrs_t = 1;             //Temperature oversampling x 1
    uint8_t osrs_p = 1;             //Pressure oversampling x 1
    uint8_t osrs_h = 1;             //Humidity oversampling x 1
    uint8_t mode = 3;               //Normal mode
    uint8_t t_sb = 5;               //Tstandby 1000ms
    uint8_t filter = 0;             //Filter off
    uint8_t spi3w_en = 0;           //3-wire SPI Disable

    uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode;
    uint8_t config_reg    = (t_sb << 5) | (filter << 2) | spi3w_en;
    uint8_t ctrl_hum_reg  = osrs_h;

    Serial.begin(9600);
    Serial1.begin(GPS_BAUD);
    Serial5.begin(9600);
    Wire.begin();

    writeReg(0xF2,ctrl_hum_reg);
    writeReg(0xF4,ctrl_meas_reg);
    writeReg(0xF5,config_reg);
    readTrim();
}

Now my loop is like this:

void loop()
{
   
	
    Particle.connect();
    bool cloudReady = Particle.connected();
    for (unsigned long starttime = millis(); millis() - starttime < 30000; ) {
      // if the cloud is ready
	  cloudReady = Particle.connected();
      delay(1000);
      if (cloudReady) {
      	if (!hasSetKeepAlive) {
		    hasSetKeepAlive = true;
		    Particle.keepAlive(300);
            String sent = "{ " + getTime() + ", " + getGPS() + ", " + getPM() + ", " + getBME() + ", " + getBattery() +  " }";
            Particle.publish("BME-PMT-GPS", sent, PUBLIC, WITH_ACK);
            // 1s for Particle sent data to cloud
            delay(1000);
            break;
      	}
        //Serial.println(sent);
      }
    }
    
    if (!cloudReady)
        hasSetKeepAlive = false;

    delay(window);
    System.sleep(SLEEP_MODE_DEEP, 60, SLEEP_NETWORK_STANDBY);
}

Just started, led is breathing cyan but I cannot ping, even after few seconds after flashing.

Question - probably - unrealted: when I “hard reset” the Electron with tinker+update+tinker, I noticed that Electron becomes cyan (breathing) even after flashing Tinker. How? I didn’t put setCredentials in my local Tinker.

The APN setting is stored in the u-blox cellular modem. If you remove power to the modem it will be reset.

However, if you reset the Electron, even flash user or system firmware, the modem is not reset (to save on time and data usage), so the setting will stick. This also allows safe mode to work, but only if you've previously run your code to set the APN.

1 Like

Just a general note, you are using String and all these concatenation operations ("..." + "...") will create additional String instances which - over time - does lead to heap fragmentation.
It might not be related to your issue, but it’s generally better to avoid String and rather go with C-strings (char[]) and snprintf() to create “complex” strings.

1 Like