Created
October 10, 2016 19:21
-
-
Save NonaSuomy/0895a6778d7906ce79cfd64f93e4dae1 to your computer and use it in GitHub Desktop.
Arduino Garage Door System Push Notification
This file contains hidden or 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
//* Name: Garage Door System | |
//* Hacker: NonaSuomy | |
//* Date: 20160630 | |
//* Description: Garage door sends a push notification to instapush.im | |
//* | |
//* Sign-up & Setup instapush.im | |
//* | |
//* Goto the instapush dashboard after signing up: https://instapush.im/dashboard | |
//* Click "Apps" tab. | |
//* Click "+Add Application" button. | |
//* Application Title: GDS | |
//* Click "Add Application" button. | |
//* It should now show the "Events" tab. | |
//* Click "+Add Events". | |
//* Event Title *: GDS | |
//* Trackers*: status | |
//* (This confused me at first but you have to hit your keyboard TAB after typing the tracker name.) | |
//* Push Message*: The garage door is {status} | |
//* Click "Add Event" button. | |
//* Click "Basic Info" tab. | |
//* Replace the APPIDHERE with Application ID: 12345a678901b23c45d67ef8 (YOUR ID, NOT THIS ONE!) | |
//* and APPSECRETHERE with Application Secret: abc12de3fa4b56c78defa9b01cd2e3f4 (YOUR SECRET, NOT THIS ONE!) | |
//* in the spots identified below. | |
//* | |
//* Replace SSIDHERE & PASSWORDHERE with your WiFi SSID and Password. | |
//* Install the instapush app on your Android mobile device and login. | |
//* | |
//* Note: Mobile app wouldn't let me log-in like many of the app complaints were | |
//* saying in the Google Play Store. I reset my password through the web | |
//* interface and then it let me login with the mobile app. | |
//* | |
//* Cross your fingers and hope for the best, power up your ESP8266 and wait for | |
//* the push message in the mobile app. | |
//* | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
ESP8266WiFiMulti WiFiMulti; | |
const int CLOSEREED_PIN = 14; // Pin connected to reed switch | |
//const int OPENREED_PIN = 5; // Pin connected to reed switch | |
int SwitchStateOpen = 0; | |
int SwitchStateClosed = 0; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output. | |
// Since the other end of the reed switch is connected to ground, we need | |
// to pull-up the reed switch pin internally. | |
pinMode(CLOSEREED_PIN, INPUT_PULLUP); | |
// We start by connecting to a WiFi network | |
WiFiMulti.addAP("SSIDHERE", "PASSWORDHERE"); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Waiting for WiFi"); | |
while(WiFiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
delay(500); | |
} | |
void mobilepush(String PushStatus) { | |
const uint16_t port = 80; // Web port. | |
const char * host = "api.instapush.im"; // Instapush API website. | |
Serial.print("Connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
if (!client.connect(host, port)) { | |
Serial.println("Connection failed"); | |
Serial.println("wait 5 sec..."); | |
delay(5000); | |
return; | |
} | |
// Send a request to the server | |
// Make JSON request: | |
String json1 = String("{\"event\":\"GDS\",\"trackers\":{\"status\":\""); | |
String json2 = PushStatus; | |
String json3 = String("\"}}"); | |
// Make a HTTP request: | |
client.print("POST /v1/post HTTP/1.1\n"); | |
client.print("User-Agent: ESP8266\n"); | |
client.print("Host: api.instapush.im\n"); | |
client.print("Accept: *\\*\n"); | |
client.print("x-instapush-appid: APPIDHERE\n"); | |
client.print("x-instapush-appsecret: APPSECRETHERE\n"); | |
client.print("Content-Type: application/json\n"); | |
client.print("Content-Length: "); | |
client.print(json1.length() + json2.length() + json3.length()); | |
client.print("\n\n"); | |
client.print(json1); | |
client.print(json2); | |
client.print(json3); | |
client.print("\n\n"); | |
Serial.println("Sent push, closing connection."); | |
client.stop(); | |
} | |
void loop() { | |
int ClosedProximity = digitalRead(CLOSEREED_PIN); // Read the state of the switch. | |
if (SwitchStateClosed == 0) { | |
if (ClosedProximity == LOW) // If the pin reads low, the switch is closed. | |
{ | |
Serial.println("Garage door switch closed."); | |
mobilepush("closed"); | |
SwitchStateClosed = 1; | |
SwitchStateOpen = 0; | |
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH active low LED. | |
} | |
} | |
if (SwitchStateOpen == 0) { | |
if (ClosedProximity == HIGH) | |
{ | |
Serial.println("Garage door switch open."); | |
mobilepush("open"); | |
SwitchStateOpen = 1; | |
SwitchStateClosed = 0; | |
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level | |
// but actually the LED is on; this is because | |
// it is active low on the ESP-01) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment