[Resolved] Photon wrecking havoc on local WAN and drops connection to the cloud

I’m having issues with both of my photons.

I’ve set them up to my WiFi, my DMZ, and my alternate router.

What happens when it boots:

  • Goes cyan, as indicated by the logs (device came online)
  • That lasts for about 5 seconds, then it goes in to cloud not connected mode (slow green fade)
  • It stays in that mode forever

I’m loosing my mind, this is happening to both of the photons I have.

On top of that, it somehow resets my DNS to some invalid DNS server on my Mac, so I have to restart my network connection or I dont have connectivity.

Device ID is: 310026001847353236343033

Please help me out, i’ve been doing this for 2 days now, I was so impressed with the cloud part of using particle, especially the logs, to be let down by this strange behavior.

What code are you running?

The most common cause for this is blocking code.
If you add SYSTEM_THREAD(ENABLED) and that behaviour goes away, you got the proof that it’s a code issue.

1 Like

Here’s the code:

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <TinyGPS.h>

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

// This #include statement was automatically added by the Spark IDE.
#include "TinyGPS.h"

#define DHTPIN 6
#define DHTTYPE DHT11

// Variables
int temperature;
int humidity;

DHT dht(DHTPIN, DHTTYPE);

int DHpin = 6;
byte dat [5];
byte read_data () {
  byte data;
  for (int i = 0; i < 8; i ++) {
    if (digitalRead (DHpin) == LOW) {
      while (digitalRead (DHpin) == LOW); // wait for 50us
      delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'
      if (digitalRead (DHpin) == HIGH)
        data |= (1 << (7-i)); // high front and low in the post
      while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver
     }
  }
return data;
}

void start_test () {
  digitalWrite (DHpin, LOW); // bus down, send start signal
  delay (30); // delay greater than 18ms, so DHT11 start signal can be detected
 
  digitalWrite (DHpin, HIGH);
  delayMicroseconds (40); // Wait for DHT11 response
 
  pinMode (DHpin, INPUT);
  while (digitalRead (DHpin) == HIGH);
  delayMicroseconds (80); // DHT11 response, pulled the bus 80us
  if (digitalRead (DHpin) == LOW);
  delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data
 
  for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered
    dat[i] = read_data ();
 
  pinMode (DHpin, OUTPUT);
  digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal
}
 

TinyGPS gps;
char szInfo[64];
// Every 15 minutes 
int sleep = 10 * 1000;


void setup(){
    dht.begin();
    pinMode (DHpin, OUTPUT);
    Serial1.begin(9600);
}

void loop(){
    bool isValidGPS = false;
    

    for (unsigned long start = millis(); millis() - start < 1000;){
        // Check GPS data is available
        while (Serial1.available()){
            char c = Serial1.read();
            
            // parse GPS data
            if (gps.encode(c))
                isValidGPS = true;
        }
    }

    // If we have a valid GPS location then publish it
    if (isValidGPS){
        float lat, lon;
        unsigned long age;
    
        gps.f_get_position(&lat, &lon, &age);
        
        sprintf(szInfo, "%.6f,%.6f", (lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lat), (lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lon));
    }
    else{
        // Not a vlid GPS location, jsut pass 0.0,0.0
        // This is not correct because 0.0,0.0 is a valid GPS location, we have to pass a invalid GPS location
        // and check it at the client side
        sprintf(szInfo, "0.0,0.0");
    }
    
    start_test ();
    
    temperature = dht.getTempCelcius();

    Particle.publish("temp=", dat [0]);
    Spark.publish("temperature", dat [0] + " °C");
    

    //Spark.publish("gpsloc", szInfo);
    
    // Sleep for some time
    delay(sleep);
}

All these while(...); statements are potentially killing the cloud connection if the DHT doesn’t respond right.
Try it with while(...) Particle.process();.

Also your publishes don’t seem right
Particle.publish() expects a const char* or String as second parameter. dat[0] is neither.

1 Like

Hm, I don’t know much about 1-wire protocol, but it seems you are bitbanging it - judging from setting a pin high and waiting for it going low. Not sure if delayMicroseconds() calls Particle cloud code as delay() does, but it might not (as it is usually short delay only). In case wiring is not done correctly, it may happen sensor never pulls down the line, leading to infinite loop in read_data(). Try SYSTEM_THREAD(ENABLED) @ScruffR recommended, this should keep Particle alive at least.

And for Adafruit_DHT library, it actually works - I have a Photon happily using it on my table, running for half a year or so. Hopefully it runs after library update too :smile:
My code does bit more, below is stripped variant. I hope I did not remove anything important.

#include "Adafruit_DHT.h"

#define DHTPIN D4
#define DHTTYPE DHT11		// DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
}

void loop(void) {
    // 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.getHumidity();
    // Read temperature as Celsius
    float t = dht.getTempCelcius();
    // Read temperature as Farenheit
    float f = dht.getTempFarenheit();

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.print(Time.timeStr());
        Serial.println(" - Failed to read from DHT sensor!");
        return;
    }

    Serial.print("Humid: ");
    Serial.print(h);
    Serial.print("% - ");
    Serial.print("Temp: ");
    Serial.print(t);
    Serial.print("*C ");
    Serial.print(f);
    Serial.println("*F");

    delay(10000);
}

1 Like

So that code didnt work but I found one that did.

I’ve replaced the bad code with a new one. Seems something in the code was killing it. It was getting very frustrating, on top of things, seems like the sensor it self was broken and not responding. So i replaced it, and finally it started working.

2 Likes

Also, the internet issue seems to have been with my ISP. So we can close this case.

1 Like