Last active
October 1, 2018 12:36
-
-
Save knight-of-ni/8a1077f1651baa1ba3fd to your computer and use it in GitHub Desktop.
Minimal Approach for an ESP8266 trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ESP8266_zmtrigger | |
* | |
* Date of Last Revision: Jan 14, 2015 | |
* | |
* Upon activation of GPIO2, this sketch sends a user defined command to a ZoneMinder | |
* server running zmtrigger on port 6802. | |
* | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266WebServer.h> | |
#define GPIO0 0 | |
#define GPIO2 2 | |
ESP8266WiFiMulti WiFiMulti; | |
WiFiClient client; | |
ESP8266WebServer server(80); | |
// Define global variables | |
const char zmserver[] = "192.168.1.122"; | |
const int port = 6802; | |
bool sensorActive = false; | |
unsigned long previousMillis = 0; | |
// The zmtrigger command syntax is defined here: | |
// https://github.com/ZoneMinder/ZoneMinder/blob/master/scripts/zmtrigger.pl.in | |
String zmtriggerStartCmd = "2|on|200|Alarm Sensor Active|An alarm has occured. Investigate the cause.|Alarm"; | |
String zmtriggerStopCmd = "2|off+10|0|Alarm Sensor Normal|The Alarm has been restored|Normal"; | |
void handle_root() | |
{ | |
server.send(200, "text/plain", "Hello from the esp8266, read from /gpio0 or /gpio2"); | |
delay(100); | |
} | |
void WiFiStatus() { | |
Serial.println(""); | |
Serial.println("WiFi Status"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("MAC address: "); | |
Serial.println(WiFi.macAddress()); | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
Serial.print("Signal Strength (%): "); | |
Serial.println(2 * (WiFi.RSSI() + 100)); | |
} | |
void updateZMTrigger(String Cmd) { | |
if (client.connect(zmserver, port)) { | |
client.println(Cmd); | |
if (client.connected()) { | |
Serial.println(); | |
Serial.println("Event Trigger Sent to ZoneMinder"); | |
Serial.println(Cmd); | |
} | |
} else { | |
Serial.println(); | |
Serial.println("Error Connecting to ZoneMinder server!"); | |
Serial.println(client.readString()); | |
} | |
} | |
void setup() { | |
// Configure our GPIO's as inputs | |
pinMode(GPIO0, INPUT_PULLUP); | |
pinMode(GPIO2, INPUT_PULLUP); | |
Serial.begin(115200); | |
// Serial.setDebugOutput(true); | |
Serial.println(""); | |
for(uint8_t t = 4; t > 0; t--) { | |
Serial.printf("[SETUP] WAIT %d...\n", t); | |
Serial.flush(); | |
delay(1000); | |
} | |
// WiFIMulti class allows you to add as many wifi networks are you want | |
WiFiMulti.addAP("ssid1", "password1"); | |
WiFiMulti.addAP("ssid2", "password2"); | |
// Wait for connection | |
Serial.print("Waiting for WiFi to Connect..."); | |
while (WiFiMulti.run() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("Connected!"); | |
server.on("/", handle_root); | |
server.on("/gpio0", [](){ // if you add this subdirectory to your webserver call, you get text below :) | |
server.send(200, "text/plain", "Current value of GPIO0: "+String(digitalRead(GPIO0))); // send to someones browser when asked | |
}); | |
server.on("/gpio2", [](){ // if you add this subdirectory to your webserver call, you get text below :) | |
server.send(200, "text/plain", "Current value of GPIO2: "+String(digitalRead(GPIO2))); // send to someones browser when asked | |
}); | |
server.begin(); | |
Serial.println(""); | |
Serial.println("HTTP server started"); | |
} | |
void loop() { | |
server.handleClient(); | |
// Reset previousMillis when millis overflows back to zero | |
if (millis() - previousMillis < 0 ) { | |
previousMillis = 0; | |
} | |
if ( millis() - previousMillis > 10000 ) { | |
WiFiStatus(); | |
Serial.println("GPIO0 state: "+String(digitalRead(GPIO0))); | |
Serial.println("GPIO2 state: "+String(digitalRead(GPIO2))); | |
previousMillis = millis(); | |
} | |
if ( !digitalRead(GPIO2) && !sensorActive ) { //Sensor attached to this input detected something | |
updateZMTrigger(zmtriggerStartCmd); | |
sensorActive = true; | |
} | |
if ( digitalRead(GPIO2) && sensorActive ) { | |
updateZMTrigger(zmtriggerStopCmd); | |
sensorActive = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment