Loosing connection to cloud

Hi everyone,

Recently purchased a Particle Photon, its very similar to the arduino so I’ve picked up on it with ease however my Photon keeps disconnecting from the cloud after a random amount of time. Could be a few mins, could be an hour. I thought I had it fixed with my latest revision to the code but even with the reconnection command in the loop it still will not connect to the cloud. I believe its staying connected to wifi but i’m currently testing this now. I did some research and looks like i’m not the only one having cloud connection issues. I found 2 others within 10 mins of searching these forums. Is this a common issue?

This is using a example from the adafruit library for DHT sensors. I also have the library files attached to the code, just don’t see a point in posting them here.

I thought maybe the delay command was messing with the connection so I switched to a non-blocking time tracking but even with this, the connection is lost to the cloud and refuses to reconnect. Any ideas?

EDIT:

I did just verify after around an hour of running, it just disconnected from the cloud and it IS still connected to the wifi of my network.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Senso

#include "dht.h"

#define DHTPIN 3     // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousTimer01;
const int waitTime = 4000;

void setup() {

  dht.begin();
  
  Particle.connect();
}

void loop() {
  // Wait a few seconds between measurements.
  if (Particle.connected() == false) {
    Particle.connect();
  }
  
  if ((millis() - previousTimer01 >= waitTime) && (Particle.connected() == true)){
      previousTimer01 = millis();
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) {
        //Serial.println(F("Failed to read from DHT sensor!"));
        return;
      }
    
      // Compute heat index in Fahrenheit (the default)
      float hif = dht.computeHeatIndex(f, h);
      // Compute heat index in Celsius (isFahreheit = false)
      float hic = dht.computeHeatIndex(t, h, false);
      /*
      Serial.print(F("Humidity: "));
      Serial.print(h);
      Serial.print(F("%  Temperature: "));
      Serial.print(t);
      Serial.print(F("°C "));
      Serial.print(f);
      Serial.print(F("°F  Heat index: "));
      Serial.print(hic);
      Serial.print(F("°C "));
      Serial.print(hif);
      Serial.println(F("°F"));
      */
      String temper = String(f,DEC);
      if (Particle.connected()) {
        Particle.publish("Temperature", temper);
      }
  }
}

I don’t see a lot wrong. But your calls to Particle.connect() are not needed if you are running in AUTOMATIC mode (the default which you have not changed). I would remove all the Particle.connect calls. You can check for Particle.connected() which is good practice before calling Particle.publish().

And by the way, if you do call Particle.connect(), do it once and wait. The way you have it coded, you would call Particle.connect indiscriminately every loop iteration. Calling it that fast may cause the cloud to never complete a connection.

You could also consider using SYSTEM_THREAD(ENABLE).

1 Like

I have removed the connect lines from the code but it did just drop the connection to the cloud only within a few mins of flashing.

I’ll have to research what the SYSTEM_THREAD does before enabling it.

I did end up enabling the SYSTEM_THREAD however seems I still have the same issue. This time its not green though, its the cyan blue. I looked up the pattern and it appears its giving me the connected status however I’m not getting any data going to the console on the cloud platform.

I guess you are still running an outdated version of device OS.
With 1.0.0 this syntax wouldn't be allowed anymore

I'd recommend updating your Photon and then use this syntax

Particle.publish("Temperature", temper, PRIVATE);

And I'd also advise agains the use of String and rather go with character arrays.

  char temper[16];
  snprintf(temper, sizeof(temper), "%.1f", f);
1 Like

In addition to @ScruffR’s comments. The next step, IMHO, is to add some debugging Serial.print lines to your code to help you figure out where the problem lies. Is the problem that the publish never happens on the device?.. or is the problem that the publish is never received on the cloud? You already have a lot of commented out serial.print lines. Perhaps put a few of those in play so you can see what’s going on. Then add in some serial.print lines in and around your if (Particle.connected()) { } blocks. Maybe add some else {} blocks with a serial.print when Particle.connected() returns false.

Also, you may want to create some global variables to hold your temp and humidity data and then create Particle.variable() for each of those. This would allow you to request the variable from the device console. That would also allow you to verify connectivity and whether it is just the publishes that are failing.

Posting updated code would help as well.

I’d suspect the DHT reading to take longer than expected. To test that theory you can add a Serial.printlnf("Sensor read time %lu", millis() - previousTimer01); after your dht.computeHeatIndex() call.
If the time is more than 10sec you may have found your culprit.

1 Like

Hello,

So i’ve been tiediously chucking away at figuring out whats going on, I’m still not 100% certain. I did try to use serial, however I was unsuccessful in getting ANYTHING to pass through on serial. I tried many applications, downloaded the particle CLI (which I have no idea how to use), I tried using putty, arduino’s serial monitor, nothing worked. I will post the last revision I sent to the particle. I did assign it to light up the built-in LED everytime it has trouble which was actually helpful. It did struggle with reading it from time to time, very randomly. I’m also getting inaccurate values which makes me wonder if there’s an issue with my temperature sensor itself. This however doesn’t explain why the code doesn’t always send when it can’t read.

One thing I can say is when I unplug the DHT, it makes the particle stop everything, it doesn’t light the LED, it doesn’t post the error message to the cloud, it just freezes.

Good call on the character array. So, to sum things up, Yes still having issues with it posting to the cloud, also getting inaccurate values. I do have another AM2302 that just arrived a few days ago so i’ll pop that in and give it a try. On a opposite note, i’ll try the old one on a different controller to see if the results are the same and it may be malfunctioning.

#include "dht.h"

SYSTEM_THREAD(ENABLED);


#define DHTPIN 3     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

const int LED = D7;

DHT dht(DHTPIN, DHTTYPE);
unsigned long previousTimer01;
const int waitTime = 2000;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  dht.begin();
  //Particle.connect();
}

void loop() {
  // Wait a few seconds between measurements.
  if ((millis() - previousTimer01 >= waitTime) && (Particle.connected() == true)){
      previousTimer01 = millis();
      Serial.println("Running Function");
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(f)) {
        //Serial.println(F("Failed to read from DHT sensor!"));
        Particle.publish("Error", "Failed to read DHT", PRIVATE);
        digitalWrite(LED, HIGH);
        return;
      }
      char temper[16];
      snprintf(temper, sizeof(temper), "%.1f", f);
      //String temper = String(f,2);
      if (Particle.connected()) {
        Particle.publish("Temperature", temper, PRIVATE);
        digitalWrite(LED,LOW);
      } else {
        digitalWrite(LED, HIGH);
        Serial.println("Lost Connection to cloud (error 2)");
      }
  }
  if((millis() - previousTimer01 >= waitTime) && (Particle.connected() == false)){
      previousTimer01 = millis();
      digitalWrite(LED, HIGH);
      Serial.println("Lost connection to cloud");
  }
}

EDIT:
BTW, The LED didn’t start turning on until I added it to this part of the code. This led me to think the sensor is bad.

      if (isnan(h) || isnan(f)) {
        //Serial.println(F("Failed to read from DHT sensor!"));
        Particle.publish("Error", "Failed to read DHT", PRIVATE);
        digitalWrite(LED, HIGH);
        return;
      }

If you have CLI installed, you can use particle serial monitor --follow to catch the serial output.

For Photons the most reliable library for that sensor would be PietteTech_DHT (but don't use it with Mesh devices running 0.8.0-rc.27 due to some issue with the device OS).

I have just pushed an update to the PietteTech_DHT library (now v0.0.6) to work around this issue

Thank you for the notification ScruffR. I abandoned the use of the DHT22 for a short time and perused other sensors and methods. I will be trying this out today.

You should use 0.0.7 - in 0.0.6 I had a syntax error :blush: