ESP8266 Deep Sleep wakeup using a PIR

I have a sketch using ESP deepsleep function with a wake-up from a PIR sensor.
The sketch works fine, ESP goes in deep sleep and woke up when PIR is activated.
But my problem is, no information sends to Blynk, after PIR has been activated.
I want to be notified when PIR is active. My question is, how do I re-establish the connection, after DeepSleep?

Here is my Sketch:

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

extern "C" 
{
  #include "user_interface.h"
  #include "gpio.h"
}

char auth[] = "***";   // char ssid[] = "***";
char pass[] = "***";

int pirPin;                   // Pin 7 on NodeMCU
int PIRStatus;                // Read PIR Value HIGH if movement detected
int AlarmState;               // Status of buttons ON or OFF
bool isFirstConnect = true;

// ********************************************************************************
 BLYNK_CONNECTED() // runs every time Blynk connection is established
{
  if (isFirstConnect) 
  {
   Blynk.syncAll();       // Request server to re-send latest values for all pins
   isFirstConnect = false;
  }
}

WidgetLED led1(V2);           // LED turn ON when PIR active

WidgetLED led2(V7);           // LED turn ON when Battery is low

BLYNK_WRITE(V1)
{
  AlarmState = param.asInt(); 
  Serial.print("ALARM STATUS: ");
  Serial.println(AlarmState);
}

// ********************************************************************************

void setup() 
{
  Serial.begin(115200);
  Serial.write("\n\nSetup!\n");
  pinMode(13, INPUT);    //PIR sensor
}

void loop() 
{
  Blynk.run();
  led1.off();
  delay(2000);
  Serial.write("Go to Sleep!\n");
  sleep();                    // Go to sleep
  Serial.write("Am awake!\n");
  alarmON();
}

// ********************************************************************************
void sleep()
{   
    while(digitalRead(GPIO_ID_PIN(13)))
    {
    delay(2);//can't go to sleep when the pin is high, otherwise we won't wake!
    }
    wifi_station_disconnect();
    wifi_set_opmode(NULL_MODE);
    wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); 
    wifi_fpm_open();
    gpio_pin_wakeup_enable(GPIO_ID_PIN(13), GPIO_PIN_INTR_HILEVEL); // or LO/ANYLEVEL, no change
    Serial.write("Sleep!\n");
    Serial.flush();
    wifi_fpm_do_sleep(0xFFFFFFFF);
    delay(100);
    Serial.write("Woke up!\n");
    Blynk.run();
    Blynk.syncAll();       // Request server to re-send latest values for all pins
    delay(2000);
 }

 // ********************************************************************************
void alarmON()
{
  if (AlarmState == 1)
  {
    Serial.println("==> ALARM DETEKTED !");
    Blynk.notify("ALARM  - DETECTED");
    Blynk.email("my@email.xx", "ALARM DETECTED", "OFFICE");
    led1.on();
    delay (2000);
    led1.off();
  }
 led1.on();
 Serial.println("PIR DETECTED !");
 delay(2000);
 led1.off();
}
1 Like

4 posts were merged into an existing topic: ESP8266 JSNSR04T 2.0 Deep Sleep post Blynk Publish