Trouble connecting to cloud

My photon/P0 breaths green after most reboots. I need to press the reset button or power it down several times before it’ll connect to the cloud and start breathing cyan.
this used to work flawlessly.

I do see these events in my Oak Cloud Serial Terminal…

[09:06:22] Event: spark/status - online
[09:06:22] Event: spark/device/last_reset - pin_reset
[09:07:05] Event: spark/status - online
[09:07:05] Event: spark/device/last_reset - power_down
[09:08:06] Event: spark/status - offline

after several resets i get the following and things start working
[09:26:37] Event: spark/status - online
[09:26:37] Event: spark/device/last_reset - pin_reset
[09:26:39] Event: DHT22 - firmware version - 0.04
[09:26:39] Event: The temperature from the dht22 is: - 72.13
[09:26:39] Event: The humidity from the dht22 is: - 33.59

The console says it is in communication with the cloud, so that is good.

There could be something in your code affecting access to the Cloud (blocking code) or some variable overflow or other programmatic problem…

You would have to post your code for anyone to be really helpful.

1 Like

We’d need to know more about your code - the reason for the behaviour will most likely be found there.

Especially the power_down event does suggest there is a relation between connection state pre sleep and the following reconnect.

What’s the easiest way to post my code? I did it in the particle ide

Copy-paste and wrap in a set of these

 ```cpp
 // your code here

Here’s my code, (Actually, it’s someone else’s code that I stole and modified)

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

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

//#include "blynk/blynk.h"
//#include "PietteTech_DHT.h"

// system defines
#define DHTTYPE  DHT22              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   4         	    // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   15000  // Sample every 15 sec

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// globals
unsigned int DHTnextSampleTime;	    // Next time we want to start sample
bool bDHTstarted;		    // flag to indicate we started acquisition
int n;                              // counter

//this is coming from http://www.instructables.com/id/Datalogging-with-Spark-Core-Google-Drive/?ALLSTEPS
char resultstr[64]; //String to store the sensor data

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

char VERSION[64] = "0.04";

#define READ_INTERVAL 60000

void setup()
{

  Blynk.begin(auth);
 
 DHTnextSampleTime = 0;  // Start the first sample immediately
 Particle.variable("result", resultstr, STRING);

 Particle.publish("DHT22 - firmware version", VERSION, 60, PRIVATE);
 
}


// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}

void loop()
{

  Blynk.run(); // all the Blynk magic happens here
 

  // Check if we need to start the next sample
  if (millis() > DHTnextSampleTime) {
      
	if (!bDHTstarted) {		// start the sample
	    DHT.acquire();
	    bDHTstarted = true;
	}

 if (!DHT.acquiring()) {		// has sample completed?

  float temp = (float)DHT.getFahrenheit();
  int temp1 = (temp - (int)temp) * 100;

  char tempInChar[32];
  sprintf(tempInChar,"%0d.%d", (int)temp, temp1);
  Particle.publish("The temperature from the dht22 is:", tempInChar, 60, PRIVATE);

  //virtual pin 1 will be the temperature
  Blynk.virtualWrite(V1, tempInChar);
 
  //google docs can get this variable
  sprintf(resultstr, "{\"t\":%s}", tempInChar);

  float humid = (float)DHT.getHumidity();
  int humid1 = (humid - (int)humid) * 100;

  sprintf(tempInChar,"%0d.%d", (int)humid, humid1);
  Particle.publish("The humidity from the dht22 is:", tempInChar, 60, PRIVATE);

  //virtual pin 2 will be the humidity
  Blynk.virtualWrite(V2, tempInChar);

  n++;  // increment counter
  bDHTstarted = false;  // reset the sample flag so we can take another
  DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;  // set the time for next sample
 }
 
}

}

Hmm, that’s a surprise. I don’t see any calls that would cause the power_down reset that’s logged on your console.

But there are some other things I’d do differently.

e.g. instead of that

  float temp = (float)DHT.getFahrenheit();
  int temp1 = (temp - (int)temp) * 100;

  char tempInChar[32];
  sprintf(tempInChar,"%0d.%d", (int)temp, temp1);

I’d do

  float temp = (float)DHT.getFahrenheit();
  char tempInChar[32];
  sprintf(tempInChar,"%.2f", temp);

(same for humidity)
Additionally I’d also shorten and unify the event name and put all the info into the data field.

Also that’s not the cleanest of ways to do the millis() timing

#define DHT_SAMPLE_INTERVAL   15000  // Sample every 15 sec
...
unsigned int DHTnextSampleTime;	    // Next time we want to start sample
...
  if (millis() > DHTnextSampleTime) {
    ...
    DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL;  // set the time for next sample
  }

The safer version of that would be

const uint32_t DHT_SAMPLE_INTERVAL = 15000;
uint32_t lastSampleTime;
...
  if (millis() - lastSampleTime > DHT_SAMPLE_INTERVAL) {
    lastSampleTime = millis();
    ...
  }

Like I said, in the spirit of the internet, I stole the code…
The power down event was me pulling the plug on it, In order to get it to finally connect it takes several cycles of resetting and /or powering it off

I’ll try your changes later today…