The device is disconnecting from the Particle Cloud too frequently

I have a regular lost connection every 2-3 days. No idea why since the wifi is strong and the application works fine. Is a disconnection possible if I have a bug in my firmware? Let say some overflow… Or is it only a bad internet connection?
I think a bug usually end-up with a flashing green led. My flash is Cyan flashing fast.

It is possible for user firmware to affect the cloud connection, by using all available memory, corrupting memory, or failing to give the cloud connection enough processing time.

One way to rule that out is to run minimal firmware like tinker, or this project that is designed to troubleshoot Wi-Fi issues:

Another thing you can try is upgrading to 0.8.0-rc.10. There are some known issues in 0.7.0 that may affect the stability of the cloud connection that are fixed in 0.8.0.

Great, thank you for the answer.
I am using only half of the memory. The level is rc 0.8.0.
But it is getting worse…
I had someone to reset the device.
Now the device is up and running I can select the variable from the console, I can ping it, diagnostic says the device is healthy… but the device is “off line”… how can it be?
Also in the build I can see my device being off-line.
I think there are some problems with the system.
???

Which rc version of 0.8.0?

The amount of RAM used doesn’t tell the whole story. If you are using Strings a lot or other objects that require dynamic memory allocation, you could be getting heap fragmentation.

Fast flashing cyan indicates it is attempting to connect to the Particle Cloud. Have you considered running @rickkas7’s code he linked to?

Are you running with SYSTEM_THREAD(ENABLED)? What SYSTEM_MODE are you running? It is difficult to help without seeing your code.

1 Like

Also, the off-line vs. on-line status for Electrons is not really correct since the device can sleep for long periods by design.

Are you using Photon or Electron?

Right good point, sorry, I am using the Photon.

Here is my code, feel free and thank you for any comments:

/*
JMJ 2017

Hardware Connections:

   HTTP/IFTTT/MicroOLED ------------- Photon JMJ

     GND ------------------- GND
     VDD ------------------- 3.3V (VCC)
     D1/MOSI ----------------- A5 (don't change)
     D0/SCK ------------------ A3 (don't change)
     D2
     D/C ------------------- D6 (can be any digital pin)
     RST ------------------- D7 (can be any digital pin)
     CS  ------------------- A2 (can be any digital pin)

     LED ---------------- D0 Output
     PIR ---------------- D5 Input
     OnOff ---------------D4 Input
*/

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"

#include "application.h"
#include <sys/cdefs.h>

/**
* Declaring the variables.
*/

int passCount;

//variable for the PIR sensor
// define the LED on pin D0
int led = D0;
// define the PIR sensor input on pin D5
int pir = D5;
// define OnOff switch (for alarm send)
int OnOff = D4;//not used

bool published;
int IFTTTCounter;

int presence;//used for Thingspeak
long nextTime; 

HttpClient http;

char publishString[40];

String presenceStr = "0";

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Accept" , "*/*"},
    { "User-agent", "Particle HttpClient"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;

void setup() {
Serial.begin(9600);
//    pinMode(led7, OUTPUT); //led flash when xmit to ethernet
delay(1000);     // Delay 1000 ms

//setup the PIR sensor
// set the LED pin as output
pinMode(led, OUTPUT);//led
// set the PIR sensor input pin as input
pinMode(pir, INPUT_PULLUP);

pinMode(OnOff, INPUT_PULLUP);//was for the switch
// ensure the LED is off
digitalWrite(led, LOW);

//setup IFTTT variable

passCount = 0;
presence = 0;//init no presence

IFTTTCounter = 0;

Particle.variable("presence", presence);

nextTime = millis(); // initialize time between sending emails
}



void loop() {

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
   
// if nextTime is greater goes for the PIR, temperature, etc... otherwise send data to cloud
    if (nextTime > millis())
    {
      if(IFTTTCounter > 0) IFTTTCounter--;//if > 0 mean some message was sent to IFTTT


      // if motion is detected
      if (digitalRead(pir) == HIGH) //might be HIGH depending of PIR
        {
             // turn on the LED as an indicator to indicate the motion detection
             //digitalWrite(led, HIGH);//change position of light see below
        
             published = true;//to bypass the IFTTT
             //send a message now if the first time or time elapsed more than 10 minutes
             if(IFTTTCounter == 0)
             {
               if(digitalRead(OnOff) == HIGH) //switch ON/OFF (LOW) now disabled set to pullup
               {
                    sprintf(publishString,"%d",presence);
                    Particle.publish("motion-detected", publishString);
                    digitalWrite(led, HIGH);
                    
                    presence++;//use for Thingspeak, count the number of passage
               }
               
               IFTTTCounter = 3;//was 30 and 300 set to at least 10 minutes to avoid sending to much message to IFTTT (not used in this app)
             }

            if (!published)//if the publish was unsuccessful
            {
              //not connected correctly with IFTTT
            }
        }
      else
        {
             // if no motion is detected, turn off the LED
             digitalWrite(led, LOW);
             //presence = 0;//is reset in ThingSpeak
        }

      // wait anyway 2 seconds before taking another PIR reading, temperature etc...
    delay(2000);
    return;//do another loop without sending data to cloud
    }

    if(presence < 1) presence = 0;//filter for false detection
    //ThinkSpeak section
    String presenceStr = String(presence);
    request.hostname = "api.thingspeak.com";

    String strgThinkSpeak = String("/update?api_key=XXXXXXXXXXXXXXX&field1=" + presenceStr);
    request.path = strgThinkSpeak;
    http.get(request, response, headers);
    
    Serial.print("Presence= ");
    Serial.println(presenceStr);
    
    presence = 0;//reset presence
    //End ThinkSpeak section
    


    nextTime = millis() + 60000;//update every minute
    //nextTime = millis() + 3000;//
}