I am in the process of putting the final touches on my home security project which contains only a commercial PIR, pet friendly, motion detector. The output is only connected to GPIO2 of the ESP8266 / ESP-1 Wi-Fi device.
The Arduino sketch code decodes a zero or one as 25 or 100 to Ubidot’s cloud. When triggered by the PIR motion detector, Ubidot’s cloud will see this change (100) and with the change can easily send an email, sms, telegraph etc. Using the Arduino adjustable millis() command and any further PIR trigger/alert will be locked out for one hour - this is to prevent excessive emails and restrictions from my ISP. The value of 50 will show on Ubidots chart when the device alarm is locked out.
Here is my sample Arduino test sketch/code:
Arduino sketch / code:
/*
GPIO used: GPIO 2 digital trigger input, GPIO 0 input - for device programming.
Note: GPIO 0 is grounded when programming the ESP8266
Program description:
On each loop scan of GPIO_2 input Boolean trigger is read and the ESP8266 sends to
Ubidots cloud server a float variable. Variable 25.0 = 0 Boolean input, 100.0 = 1 Boolean input,
50.0 = input trigger lockout which prevents multiple UN-necessary emails to be sent to the
user. This millis lockout occurs after the input trigger = 1 = high and stays locked out until the interval
millis timer of 60 minutes is reached and then resets or is unlocked.
#include "UbidotsMicroESP8266.h"
#define TOKEN "?????????" //Your_token_here" <---- Put here your Ubidots TOKEN
#define WIFISSID "???????" // <----Your_WiFi_SSID
#define PASSWORD "????????" // <----Your_WiFi_Password
#define DEVICE "HS1" // <--- Your device variable string-number 1-5 max.
int tInput = 2; // trigger input on GPIO_2
int val = 0; // read value for trigger input
int lockout = 0; // prevents multiple email triggers
//unsigned long interval = 20000 ; // 5 secs
unsigned long interval = 3600000 ; // 60 minutes or 24 triggers/day
unsigned long previousMillis = 0;
Ubidots client(TOKEN);
void setup() {
pinMode(0, INPUT); //
pinMode(tInput, INPUT); // trigger input 5v=1=true 0v=0=false
Serial.begin(115200);
delay(10);
client.wifiConnection(WIFISSID, PASSWORD);
}
void loop() {
unsigned long currentMillis = millis(); // grab current time
val = digitalRead(tInput); // read trigger input
// check if "interval" time has passed (5000 milliseconds or 3600000 milliseconds)
if (((unsigned long)(currentMillis - previousMillis) >= interval) && (lockout == 1)) {
lockout = 0;
// save the "current" time
previousMillis = millis();
}
if (lockout == 1) {
float value3 = 50.0;
client.add(DEVICE, value3);
client.sendAll(true);
}
if ((val == 1) && (lockout == 0)) {
float value1 = 100.0;
client.add(DEVICE, value1);
client.sendAll(true);
lockout = 1;
previousMillis = millis();
}
if ((val == 0) && (lockout == 0)) {
float value2 = 25.0;
client.add(DEVICE, value2);
client.sendAll(true);
}
} // end of loop
// end of code
Any comments appreciated