Interrupts and Timeouts

Hi, I’m new to Blynk. I’m using Blynk and a ESP8266 for a battery-powered furnace monitoring system.

Is it true that it is not possible to interrupt the ESP8266 from deep sleep? It has to be reset? I realize this is an ESP question but my second question is tied to Blynk.

Since I’m trying to monitor the overall “on” time, I have to reset the ESP to start the clock and then monitor the status of a GPIO pin to determine the end time. After doing a virtualwrite, I put the ESP back to sleep. (Code below).

During the course of testing I’ve discovered that my Blynk app and the server can handle a 20 - 23 second delay before they lose contact with the server. Elapsed times greater than ~ 20 seconds are not logged.

I’ve gone through and removed all the delay() calls. I can’t figure out what else might be wrong.

One clue is the app shows a message “ESP disconnected” at about the 20 second limit (although this may also be the ESP going into deep sleep).

`#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Ticker.h>

#define PINOUT 0
#define INTERRUPTPIN 2
float previousMillis = millis();
float currentMillis = 0;
float ElapsedTime = 0;
float ElapsedTimeM = 0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “authcode”;

void setup()
{
pinMode(INTERRUPTPIN, INPUT);
digitalWrite(INTERRUPTPIN, HIGH);
pinMode(PINOUT, OUTPUT);
digitalWrite(PINOUT, LOW);
Serial.begin(9600);
Blynk.begin(auth, “network”, “pass”, IPAddress(192,168,1,xx));
}

void loop()
{
Blynk.run();
for (byte i = 0; i < 10; i++)
{
digitalWrite (PINOUT, HIGH);
delay (50);
digitalWrite (PINOUT, LOW);
delay (50);
}
bool x = digitalRead(INTERRUPTPIN);
while (x == LOW)
{
x = digitalRead(INTERRUPTPIN);
Serial.print(" x "); Serial.println(x);
}

float currentMillis = millis();
float ElapsedTime = ((currentMillis - previousMillis)/1000);
Serial.print("Elapsed Time (in seconds) = "); Serial.println(ElapsedTime);
Blynk.run();
Blynk.virtualWrite(V4, ElapsedTime);
ESP.deepSleep(0);
}

`

The only way to wake from deepsleep is to set the wake time within your sketch (or physically ground the RST pin).

int sleepSeconds = 600;  // 10 minutes

void gotosleep(){

    ESP.deepSleep((sleepSeconds * 1000000));
    delay(1); // recommended

}

O.K. So that is confirmed. Since the furnace starting isn’t a periodic event, I’ll have to do a hard reset.

Any ideas what could be going on in the second part of my question?

I’m monitoring a GPIO pin to determine when the furnace blower has stopped. During that monitoring process, which can last from 2 to 10 minutes, the connection with my local Blynk server is lost and the elapsed time is not logged.

Is there are better way to do this?

@swede have you looked through any of the example sketches provided by Blynk?

It is not recommended to have anything in your loop other than Blynk and a Timer.
You have quite modest delays in your loop but I wouldn’t have them in my loop.
You are reading a pin continuously in the while loop until it goes low having set it high in setup and I don’t know how long it takes to go low but if it is longer than 5 or 10 seconds you will be disconnected from the Blynk server because you exceeded the timeout.

Check pins at intervals with timers but not in the loop.

thanks, @Costas.

I’ll try rearranging my code this evening and put the call to check the pin in a function called by the SimpleTimer timer.
The issue, I think, is going to be that I want the loop to run once and then put everything back into deep sleep.
I’m not sure if this is going to work with the SimpleTimer library but I will try . . .

SimpleTimer can be set to run just once after x seconds.

I’ve worked some more on this. The code runs fine but I’m losing the connection to the Blynk server after 20 seconds or so. How do I establish the connection to the server AFTER my elapsed time has been calculcated?

It would make sense to do this in my onetime loop but I’m guessing that won’t work.

Most of the Blynk examples seem to be based on repetitive tasks done at set intervals. Again, my need is for a woken up ESP8266 to calculate an elapsed time, send it to the Blynk server and then go back to sleep. This is possible, no?!?

Here is the code :stuck_out_tongue_winking_eye:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Ticker.h>
#include <SimpleTimer.h>

#define PINOUT 0
#define INTERRUPTPIN 2
float previousMillis = millis();
float currentMillis = 0;
float ElapsedTime = 0;
int j = 0;
bool x = HIGH;

SimpleTimer timer; 

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";

void onetime() {
   x = digitalRead(INTERRUPTPIN); 
   Serial.print(" pin reads = ");Serial.println(x);
   while (x == LOW)
   {
     x = digitalRead(INTERRUPTPIN); 
     Serial.print(" pin reads = ");Serial.println(x);
     delay(1000);
  }
   float currentMillis = millis();  
   ElapsedTime = ((currentMillis - previousMillis)/1000);
   Serial.print("Elapsed Time (in seconds) = "); Serial.println(ElapsedTime); 
   Blynk.virtualWrite(V8, ElapsedTime);
   ESP.deepSleep(0);  
}

void setup()
{
  pinMode(INTERRUPTPIN, INPUT);
  digitalWrite(INTERRUPTPIN, HIGH);
  pinMode(PINOUT, OUTPUT);
  digitalWrite(PINOUT, LOW);
  Serial.begin(9600);
  Blynk.begin(auth);
  timer.setTimeout(5000, onetime);  
  for (byte i = 0; i < 10; i++)
    {
    digitalWrite (PINOUT, HIGH);
    delay (50);
    digitalWrite (PINOUT, LOW);
    delay (50);
    }
}

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

P.S. I know some might suggest removing the delay() function in the onetime loop but if I do so, the chip becomes unstable and loses connection in any case

@swede yes when an ESP wakes up it can connect to Blynk, pass same data to the server and then go back to sleep.

@costas it still doesn’t work for me. I put Blynk.run() and also tried with Blynk.begin() in my one-time loop. Doesn’t work.

I’m running on a local Raspberry Pi server and have one application going along fine. I was not able to find and execute the following setup instruction :

Do you know where the Raspberry Pi javascript is? Is this possibly why only one app seems to be working?

@swede have you tried Blynk.connect(); ?

Maybe explain in more detail what you are looking to do and include your latest sketch together with what is working and what is not working.

@costas Yes, using Blynk.connect() did it.
I moved my Blynk.begin() statement and added a Blynk.connect() statement to my onetime function. I removed Blynk.run() from the main loop().

The sketch I provided above will run for as long as the furnace GPIO pin is “ON”, then connect to the server, write the value of the elapsed time, and then go back to sleep.