Hamster Überwachung

Wir haben einen Hamster adoptiert. Nachdem ich ihn fast zwei Wochen nicht gesehen habe, habe ich einen alten RedBear WiFi micro ausgegraben (NodeMCU tut’s auch) und mit einem Bewegungsmelder Modul im Hamsterkäfig platziert. Das Modul hat mir dann mittels WiFi Verbindung via IFTTT eine Benachrichtigung zukommen lassen, sobald eine Bewegung im Käfig wahrgenommen wurde.

Es hat nicht lange gedauert, da sind mir die ständigen Benachrichtigungen in der Nacht auf die Nerven gegangen und ich habe noch ein Zeitmodul hinzugefügt, sowie einen Schalter, der dafür sorgt das ich keine Benachrichtigung bekomme wenn jemand den Käfig öffnet. Außerdem habe ich ein Timeout von 5 Minuten hinzugefügt, um die Benachrichtigungen auf ein Minimum zu reduzieren.

Code

#ifndef __CC3200R1M1RGC__
#include <SPI.h>
#endif
#include <WiFi.h>
#include <Wire.h>
#include "RTClib.h"

char ssid[] = ""; // wifi ssid
char password[] = ""; // wifi password

int button = 8;
int caseClosed = 0;
int pir = 0;
int timeout = 0;
int movement = 0;

char server[] = "maker.ifttt.com";

WiFiClient client;

String IFTTT_KEY = ""; // private ifttt key
String IFTTT_EVENT = "hamster_moved";

RTC_DS1307 rtc;

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

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  pinMode(LED, OUTPUT);
  pinMode(pir, INPUT);
  pinMode(button, INPUT);

  movement = digitalRead(pir);
  caseClosed = digitalRead(button);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
  }
  
  while (WiFi.localIP() == INADDR_NONE) {
    delay(300);
  }
}

// the loop routine runs over and over again forever:
void loop() {
  caseClosed = digitalRead(button);
  DateTime now = rtc.now();

  if (caseClosed == HIGH) {
    if (timeout >= 600 * 5) { // every 5min
      timeout = 0;
    }
  
    if (timeout == 0) {
      movement = digitalRead(pir);
    } else {
      movement = 0;
      timeout++;
    }
  
    if (movement == HIGH) { // motion detected
      delay(1000); // wait a little bit and then check again if case is still closed
      caseClosed = digitalRead(button); // check once again if case is closed

      if (caseClosed == HIGH) {
        timeout++; // disable to trigger all time
        digitalWrite(LED, LOW);
  
        if (now.hour() >= 9 && now.hour() < 20) {
          ifttt_trigger(IFTTT_KEY, IFTTT_EVENT, now);
        }
  
        digitalWrite(LED, HIGH);
      }
    } else {
      digitalWrite(LED, HIGH);
    }
  } else {
    timeout = 1; // wait 5min after case open to check movement again
  }

  delay(100);
}

//******************************************************************************************
// This method makes a HTTP connection to the server & does a GET request for user name
// Simply pass in the badge ID to query as a String.
// Returns string:
//   - Returns "<NAME>"if badge ID is registered
//   - Returns "NULL" if badge ID is not registered
//   - Returns "FAIL" if REST API failed
//******************************************************************************************
String ifttt_trigger(String KEY, String EVENT, DateTime now) {
  String name = "";
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    
    // This is optional. You can send additional data to IFTTT along with your HTTP POST that triggers the action
    String PostData = "{\"value1\" : \"" + String(now.unixtime()) + "\", \"value2\" : \""+ String(now.hour()) +"\", \"value3\" : \"" + String(now.minute()) + "\" }";
    
    Serial.println(PostData);
    //Serial.println("connected to server... Getting name...");
    // send the HTTP PUT request:
    String request = "POST /trigger/";
    request += EVENT;
    request += "/with/key/";
    request += KEY;
    request += " HTTP/1.1";
    
    Serial.println(request);    
    client.println(request);
    client.println("Host: maker.ifttt.com");
    client.println("User-Agent: Energia/1.1");
    client.println("Connection: close");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    client.println();

  }
  else {
    // if you couldn't make a connection:
    Serial.println("Connection failed");
    return "FAIL"; // rest API failed...
  }
  
  // Capture response from the server. (10 second timeout)
  long timeOut = 4000;
  long lastTime = millis();
  
  while((millis()-lastTime) < timeOut){  // Wait for incoming response from server
    while (client.available()) {         // Characters incoming from the server
      char c = client.read();            // Read characters
      Serial.write(c);
    }
    
  }
  Serial.println();
  Serial.println("Request Complete!");
  //return name; // Return the complete name received from server
  return "SUCCESS";
}