Toezicht met respect voor privacy (ESP8266 + PIR sensor)

Hi,
Om ouderen die zelfstandig wonen extra in de gaten te houden in geval er iets mis mocht zijn, kan naast bezoek en telefonisch contact een elektronisch toezicht handig zijn.
Een camera is volgens mij een te grote inbreuk op de privacy en dat geldt ook voor het steeds af kunnen lezen van bewegingsdetectoren.
Deze script slaat alleen alarm als er op het ingestelde tijdstip geen beweging is gemeld.
In dit voorbeeld 1x per dag om 12:12 is er een controle moment.
Het script is ruimschoots van commentaar voorzien, zodat je weet wat er gebeurt.

#include <ESP8266WiFi.h>
#include <time.h>

// Replace with your network credentials
const char *ssid     = "SSID";
const char *password = "password";

const char* host = "api.pushbullet.com";
const int httpsPort = 443;
const char* PushBulletAPIKEY = "o.1234567890ABcdEFghIJklMNopQRstUVwxYZ"; //get it from your pushbullet account

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "11 CC F6 EC D9 30 AB 3A 2D D8 DD 17 03 B9 8B 35 3A 6C 4B 71"; //get it from https://www.grc.com/fingerprints.htm

// define PIR sensor and LED
int Status = 12;
int sensor = 13;
// define motion detected
bool motion_detected = false;

// timeframe setup
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

//***************************************************************
// Time to check
int hour = 12;
int minute = 12;
//****************************************************************

// string to store time data
char buffer[60];

void setup() {
  // let Serial display start
  Serial.begin(115200);
    delay(1000);   // delay to wait for serial connection 
  // if omitted the first lines on the Serial Monitor won't be visible
  Serial.println("");
  Serial.println("Startup sequence visualised");
  Serial.println("");

  // setup PIR sensor and LED output
  pinMode(sensor, INPUT); // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
  
  // Init and get the time. Works like magic when setup before WiFi connected
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  setenv("TZ","CET-1CEST-2,M3.5.0,M10.5.0/3,1",1);
  
  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP Address : ");
  Serial.print(WiFi.localIP());
  Serial.println("");

  // check if current time is fetched from NTP server
  // year in 'C' language is calculated since 1900
  // Epoch time is calculated since 01/01/1970 00:00:00
  // information at https://www.cplusplus.com/reference/ctime/tm/
  // if no time is obtained from NTP server then time will be set at 01/01/1970 00:00:00
  time_t now = time(nullptr);
  struct tm * timeinfo;
  //time (&now);
  timeinfo = localtime(&now);
  sprintf(buffer, "Device startup time : %02d/%02d/%04d %02d:%02d", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min);
  Serial.println(buffer);
  //printf ("Current local time and date: %s\n", asctime(timeinfo));
  
  if(timeinfo->tm_year == 70){     // == number of years since 1900, 70 means no time obtained from NTP server
    Serial.println("Restart..");
    delay(6000);
    ESP.restart();
  }
  
  // delay not needed at all, just for the view on the monitor
  delay(6000);
  
  //disconnect WiFi as it's no longer needed
  Serial.println("Disconnecting WiFi");
  Serial.println("");
  delay(1000);
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
  Serial.println("WiFi disconnected");
  Serial.println("");
  
  delay(1000);
}

void loop(){

  // declare struct tm explained at: https://cpp.hotexamples.com/examples/-/-/localtime_s/cpp-localtime_s-function-examples.html
  time_t now = time(nullptr);
  struct tm * timeinfo;
  //time (&now);
  timeinfo = localtime(&now);
  
  // test if sensor detects movement
  long state = digitalRead(sensor);
  if(state == HIGH){
    digitalWrite (Status, HIGH);
    motion_detected = true;
    sprintf(buffer, "Last motion detection : %02d/%02d/%04d %02d:%02d", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min);
    Serial.println(buffer);
  }
  else {
    digitalWrite (Status, LOW);
    Serial.println("No motion detected");
  }

  if((timeinfo->tm_hour == hour) && (timeinfo->tm_min == minute) && (timeinfo->tm_sec >= 55 )){
  // (timeinfo->tm_sec >= 55 ) to prevent restarting several times within one minute
    if(!motion_detected){               // send PUSH message
      // setup WiFi connection
      Serial.print("connecting to ");
      Serial.println(ssid);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
  
      // Use WiFiClientSecure class to create TLS connection
      WiFiClientSecure client;
      client.setInsecure();
      Serial.print("connecting to ");
      Serial.println(host);
    
      if (!client.connect(host, httpsPort)) {
        Serial.println("connection failed");
        //return;
      }

      if (client.verify(fingerprint, host)) {
        Serial.println("certificate matches");
      } else {
        Serial.println("certificate doesn't match");
      }

      String url = "/v2/pushes";
      String messagebody = "{\"type\": \"note\", \"title\": \"Pushmessage from my ESP8266\", \"body\": \"" + (String)buffer + "\"}\r\n";
      Serial.print("requesting URL: ");
      Serial.println(url);

      client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Authorization: Bearer " + PushBulletAPIKEY + "\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: " +               
               String(messagebody.length()) + "\r\n\r\n");
      client.print(messagebody);
      Serial.println(messagebody);
      Serial.println("request sent");
      //disconnect WiFi as it's no longer needed 
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
      Serial.println("");
      Serial.println("WiFi disconnected");
      delay(6000);
      // just to be shure ;-)
      motion_detected = false;
    }else if(motion_detected){
      delay(6000);
      // reset motinon detected
      motion_detected = false;
    }
  }
  Serial.println((String)"Motion detected : "+motion_detected);  
  delay(1000);    

}

Meld je aan bij PushBullet voor een token, installeer PushBullet op je telefoon en het werkt.
Test door te uploaden met een controletijdstip binnen 2 minuten na compilen
De delays zijn grotendeels voor het gemak bij het aflezen van de serial monitor.