[Solved] Photon disconnects after values update

Hello Blynkers,

I am in the process of creating an automated garden watering system with Blynk and a Particle Photon. I seem to be having an issue where the device goes offline between updating values on my Blynk app. The values update every 5 seconds or so, but after they update I get the notification “Photon is offline” for the next 5 seconds. Then it goes away, my values update, and it goes “offline” again. I was wondering if there was a fix for this, as I wanted to use Push notifications for when my hardware goes offline, but I don’t want to do that as of now because of the issue I’m having. Any suggestions would be greatly appreciated!

Post your formatted code.

Here you go!

#include <PietteTech_DHT.h>
#include <blynk.h>

#define DHTTYPE  DHT22
#define DHTPIN   2
#define DHT_SAMPLE_INTERVAL   10000

void dht_wrapper();
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

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



WidgetLED ledPump(V8);

int pumpOverride;
float temperature;
float humidity;
int brightness;
int zoneA;
int zoneB;
int zoneC;

void dht_wrapper() {
    DHT.isrCallback();
}

void setup() {
    Blynk.begin("900139ca7f784f0f928be763dbfbddb8");
    pinMode(D7, OUTPUT);
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);
    pinMode(A3, INPUT);
    digitalWrite(D7, HIGH);
    DHTnextSampleTime = 0;
}

void loop() {
    Blynk.run();
    brightness = map(analogRead(A0), 0, 4095, 0, 100);
    zoneA = map(analogRead(A1), 0, 4095, 0, 100);
    zoneB = map(analogRead(A2), 0, 4095, 0, 100);
    zoneC = map(analogRead(A3), 0, 4095, 0, 100);
    
    if (millis() > DHTnextSampleTime) {
	    if (!bDHTstarted) {		// start the sample
	        DHT.acquire();
	        bDHTstarted = true;
    	}
    	
    	if (!DHT.acquiring()) {		// has sample completed?

	        // get DHT status
	        int result = DHT.getStatus();

	        if(result == 0) {
	            humidity = DHT.getHumidity();
	            temperature = DHT.getFahrenheit();
	        }
	    

	        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
	    }
    }
    
    Blynk.virtualWrite(V2, zoneA);
    Blynk.virtualWrite(V3, zoneB);
    Blynk.virtualWrite(V4, zoneC);
    Blynk.virtualWrite(V5, temperature);
	Blynk.virtualWrite(V6, humidity);
	Blynk.virtualWrite(V7, brightness);
}


BLYNK_WRITE(V10) {
    pumpOverride = param.asInt();
}

You can’t do all that kind of stuff in a Blynk loop().

You must carefully time your events as covered in the docs.

For ESP’s and Arduino’s this is done with SimpleTimer.

The Photon equivalent is SparkCorePolledTimer. Search this site and the wider internet to learn how it is implemented.

So you’re saying that the only things I should have in my main loop are the timer method calls and the Blynk.run() where all of the functions I want to perform are in timer methods?

Precisely.

Great, thanks Costas! Very helpful.

Costas, I took your suggestion but I still seem to be having problems. I thought it could have to do with the the DHT library, but I’ve eliminated all instances using those functions in my code to try and isolate the problem without luck. My sensor values aren’t even updating in the Blynk app, and I still get the “Photon is offline” warning on the app. Anything else I could be doing wrong?

#include <SparkIntervalTimer.h>
//#include <PietteTech_DHT.h>
#include <blynk.h>

/*
#define DHTTYPE  DHT22
#define DHTPIN   2
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
*/

//void dhtUpdate(void);
void sensorUpdate(void);

IntervalTimer myTimer1;
IntervalTimer myTimer2;


WidgetLED ledPump(V8);

int pumpOverride;
float temperature;
float humidity;
int brightness;
int zoneA;
int zoneB;
int zoneC;


void setup() {
    Blynk.begin("900139ca7f784f0f928be763dbfbddb8");
    pinMode(D7, OUTPUT);
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);
    pinMode(A3, INPUT);
    digitalWrite(D7, HIGH);
    //myTimer1.begin(dhtUpdate, 10000, hmSec);
    myTimer2.begin(sensorUpdate, 10000, hmSec);
}

void loop() {
    Blynk.run();
}

/*
void dhtUpdate() {
    int result = DHT.acquireAndWait();
    
    while(result != 0) {
        
    }
    
    humidity = DHT.getHumidity();
    temperature = DHT.getFahrenheit();
    
    Blynk.virtualWrite(V5, temperature);
	Blynk.virtualWrite(V6, humidity);
}
*/

void sensorUpdate(void) {
    brightness = map(analogRead(A0), 0, 4095, 0, 100);
    zoneA = map(analogRead(A1), 0, 4095, 0, 100);
    zoneB = map(analogRead(A2), 0, 4095, 0, 100);
    zoneC = map(analogRead(A3), 0, 4095, 0, 100);
    
    
    Blynk.virtualWrite(V2, zoneA);
    Blynk.virtualWrite(V3, zoneB);
    Blynk.virtualWrite(V4, zoneC);
	Blynk.virtualWrite(V7, brightness);
}



BLYNK_WRITE(V10) {
    pumpOverride = param.asInt();
}

Try Blynk.begin() after the pinMode() 's and digitalWrite().

Depending what they relate to, you might not need some of them, but leave them in for now.

Still no luck. The first 3 pinMode’s are soil moisture sensors and the 4th one is a photoresistor. The D7 is just for the onboard LED so I know the sketch is running.

What feedback do you get from the Photon’s Serial Monitor?

Actually you need the SparkCorePolledTimer.

Look through this thread Virtual pins need a timer for Particle and ensure you call the timer in loop().

Works like a charm! Thanks again!!