Enter Your Electronics & Design Project for a chance to win a $100 Shopping Cart! | Project14 Home | |
Monthly Themes | ||
Monthly Theme Poll |
PART 2-NOTIFICATIONS:
In part 1, Connor and I presented our three foundational blocks of Home Automation: Notification, Environmental Awareness, and Actuation. We recommend you read Part 1 before diving into this blog so you have your head around our approach to home automation. You can view it here: Home Automation Mojo You Must Know Part 1
In this blog, we will focus on our preferred IoT Notification tools. It only takes a three to meet every need: Push Bullet, IFTTT, and Alexa. Feel free to use any of this code for the Home Automation challenge! You could win a $100 shopping spree!
PUSH BULLET
Since folks from 5 to 95 years old have a phone in their hand or pocket these days, Push Bullet is the sure fire way to notify a home owner. Push Bullet (https://www.pushbullet.com/ ) is a cross platform internet based notification system. The beauty of it s that you can hit all your devices at once when you send it a notification either from their apps or using their API. So, no matter what device you have in your hand or are near, you will get the notification. Here is its capability:
- Send SMS Text Messages
- Smart Phone Notifications
- Share Links between devices
- Chat with friends
- File Sending
It works with Android, iOS, Chrome, Firefox, Safari, Opera, and Windows. We will use it when we need to urgently notify the home owner.
To use it, we use its API that is intended just for our purpose. We send a client web request with an access token, and it pushes the notification to all our devices. You get 500 notifications a month for free! That would work for home security even in Chicago!
Here is an example using the ESP8266 to send a Push Notification:
#include <ESP8266WiFi.h> #include <WiFiClientSecure.h> const char* ssid = "YOURSSID"; const char* password = "YOURPASSWORD"; const char* host = "api.pushbullet.com"; const int httpsPort = 443; const char* PushBulletAPIKEY = "YOURPUSHBULLETKEY"; //get it from your pushbullet account // Use web browser to view and copy SHA1 fingerprint of the certificate. Click the lock by the address in the browser and then click view certificate. // Alternately, go to https://www.grc.com/fingerprints.htm and enter api.pushbullet.com const char* fingerprint = "BB FC 9F 1B C1 3C D9 96 F2 68 A2 E3 41 29 D1 47 8F B9 33 BE"; void setup() { Serial.begin(115200); Serial.println(); 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()); pushBullet("Booted"); pinMode(2,INPUT_PULLUP); } void pushBullet(String the_msg){ // Use WiFiClientSecure class to create TLS connection WiFiClientSecure client; 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\": \"ESP8266\", \"body\": \""+the_msg+"\"}\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("request sent"); //print the response while (client.available() == 0); while (client.available()) { String line = client.readStringUntil('\n'); Serial.println(line); } } void loop() { delay(200); if (digitalRead(2)==LOW) {//Wait for pin 2 to go to ground to trigger. pushBullet("Hello World!"); delay(5000); }
On line 7, you see you need your own key. Visit pushbullet.com to get a free account, install the app, and get your key.
IFTTT
If This Then That. What a simple concept! IFTTT.com allows you to connect IoT devices both bought and made. We will primarily make use of the "This" end called Webhooks. Similar to Push Bullet, there is an API that allows you to send parameters as a trigger to the IFTTT service. The API coding is super easy because you are just sending a web client request - basically, just hitting a web page.
Using their web site, you set up custom Webhooks that then trigger other devices. You even can use it to send a Pushbullet notification if you wanted to just stick to the IFTTT API and use IFTTT as a middle man.
Using IFTTT gives you even further flexibility. For example, you could have it trigger a Webhook that turns on all your lights in your home should an intruder be detected. We used it to turn on lights in our home from our video game Son.Light.Sleepwalker in our recent blog 4D IoT Game Engine Blog.
The services have bloomed so much that it takes over a minute and a half to scroll through them all! Below is a video of me doing so:
To set up the trigger timer, we made a video a while back that walks you through creating the Webhooks:
Here is sample code for triggering IFTTT from the ESP8266:
#include "ESP8266WiFi.h" #include "WiFiClientSecure.h" const char* ssid = "........";//your home wifi router SSID const char* password = "........"; //your home wifi router password const char* host = "maker.ifttt.com";//we'll use IFTTT.com to trigger our Wemo Light Switch const int httpsPort = 443;//this is the SSL default port const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C"; void setup(void) { } void loop(void) { ToggleLight(); delay(5000); } void ToggleLight() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } // Use WiFiClientSecure class to create TLS connection WiFiClientSecure client; if (!client.connect(host, httpsPort)) { return; } //don't know if I actually needed this code from the example, so I kept the call anyway. if (client.verify(fingerprint, host)) { } else { } //this just blinks the light String url = "/trigger/YourBlinkerEvent/with/key/YourWebHook";//change this to the IFTTT URL that toggles the lightswitch. client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP8266\r\n" + "Connection: close\r\n\r\n"); // Send the hit to IFTTT.com at the above URL while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); }
From a Raspberry Pi or PocketBeagle, it's even simpler because you can use a simple "curl" system command from C++, Python, or whatever language you choose:
curl -X POST https://maker.ifttt.com/trigger/{event}/with/key/{YOURKEY}
ALEXA
Voice is definitely the wave of the future. You can now have Alexa as an app on your phone as well. With it, you can create Routines, which somewhat work like IFTTT, but solely configured by the app. In turn, Alexa can notify you based on triggers of time, change of location, and certified Zigbee automation devices (https://developer.amazon.com/docs/smarthome/echo-plus-zigbee-support.html ). These triggers will allow Alexa to notify you at any Echo in your home and say what ever you would like it to say.
You can notify Alexa of your custom built IoT device's changed state, too. One approach is to have an ESP8266 to a device that Alexa is familiar with such as a WeMo switch or Phillips Hue bulb. Once added to Alexa's devices, you can update Alexa with the device state (ie, Turned On). You can use this information to in Alexa Skills. However, you can't simply trigger an Alexa routine like a WebHook in IFTTT, but you can do a hack with a speaker to simulate a voice command. Alexa does allow certified Zigbee automation devices trigger a routine, though, but
You could hack an Echo Button to be a detection device. Imagine your back door opening while you are away. Your Echo Button triggers on and tells Alexa. Alexa then says, "Freeze. I'm calling the police and Cuju is waking up!"
Here is example code that will make your ESP8266 emulate a WeMo plug:
Requires this Library: https://github.com/witnessmenow/esp8266-alexa-wemo-emulator
#include <ESP8266WiFi.h> #include "WemoSwitch.h" #include "WemoManager.h" #include "CallbackFunction.h" // prototypes boolean connectWifi(); //on/off callbacks void lightOn(); void lightOff(); //------- Replace the following! ------ char ssid[] = "yourssid"; // your network SSID (name) char password[] = "yourpassword"; // your network key WemoManager wemoManager; WemoSwitch *light = NULL; const int ledPin = LED_BUILTIN; void setup() { Serial.begin(115200); // Set WiFi to station mode and disconnect from an AP if it was Previously // connected WiFi.mode(WIFI_STA); // Connect Serial.printf("[WIFI] Connecting to "); WiFi.begin(ssid,password); // Wait while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); IPAddress ip = WiFi.localIP(); Serial.println(ip); wemoManager.begin(); // Format: Alexa invocation name, local port no, on callback, off callback light = new WemoSwitch("test lights", 80, lightOn, lightOff); wemoManager.addDevice(*light); pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output. delay(10); digitalWrite(ledPin, HIGH); // Wemos BUILTIN_LED is active Low, so high is off } void loop() { wemoManager.serverLoop(); } void lightOn() { Serial.print("Switch 1 turn on ..."); digitalWrite(ledPin, LOW); } void lightOff() { Serial.print("Switch 1 turn off ..."); digitalWrite(ledPin, HIGH); }
NEXT STEPS
You now have the code you need to send notifications to your phone from your custom built Home Automation devices. If you made a simple ESP8266 motion sensor and blog on it, there is a strong chance you'd place in the contest! Have fun and we will see you next time when we cover the Home Automation building block Environmental Awareness.
-Sean and Connor
Next: Home Automation Mojo You Must Know Part 3: Environmental Awareness