Sunday 15 January 2017

HOME AUTOMATION: Web Enabled Relay with Blynk

One of the most useful parts of my home automation system is internet enabled entry control that lets me use my phone to get in to my house when I don't have keys on me or it's just easier to use my phone. This setup requires an electric door strike and an wifi enabled relay.

I've tried a number of ways to get an easy to use solution to work from both the web and my phone including:

I won't go over the pros and cons of each here, I'll just give the details on how to get it working with Blynk.


Why Blynk?

There are a lot or reasons listed on the Blynk website, but the reasons I went for it over the competition were:
  • Android and iOS app
  • Arduino support 
  • Really easy to use
  • Free account option 
  • SSL support
  • Local server support
I'm yet to find anything that was as simple to use as Blynk. From the time I took out my tub of Wemos modules it took me 15 minutes to get a working example (including some troubleshooting!)


To get my wireless relay set up I used the following:
  • Wemos D1 mini (ESP8266)
  • Wemos D1 mini relay shield 
  • 12v power supply 
  • Electric door strike 
  • Soldering iron and heat shrink 
  • Blynk account (done via the app)
  • Computer with Arduino Studio installed and Sparkfun ESP8266 library installed via the library manager
  • Android or iOS device with the Blynk app installed


The default Blynk examples only show how to mirror the pins of the Arduino from the Android/iOS app. I wasn't able to find any way to hold a pin high once triggered for a set period of time within the Android app, so I developed an Arduino sketch (below) that uses virtual IO to trigger and latch physical IO.


Arduino Programming

The Wemos D1 Mini board has a built in USB to serial converter so all we need to do is plug in a micro USB cable to the device and install the required drivers.
Once we have Arduino Studio installed we need to install the toolchain for the ESP8266 board, I did this by following the instructions on the esp8266 github page. Wemos also has some libraries for their boards that give some examples of using their shields.

Once this is all setup the D1 mini can be programmed as you would any other Arduino board. Blynk also has a library that can be installed from the library manager with examples for the ESP8266 boards. One of those examples shows how to connect to Blynk servers using SSL.

Tip: Debugging over serial can be useful to ensure your device is connecting to your wireless network. Make sure your serial terminal is set to the correct baud rate, in the default examples the baud rate is set to 9600 but the default terminal rate is 155200 kbps

I'm using the below program to set the relay on-time via a virtual input and then toggle the relay via another virtual input. You will need the Blynk and Ticker libraries to compile the example.

 /**************************************************************  
  * Blynk is a platform with iOS and Android apps to control  
  * Arduino, Raspberry Pi and the likes over the Internet.  
  * You can easily build graphic interfaces for all your  
  * projects by simply dragging and dropping widgets.  
  *  
  *  Downloads, docs, tutorials: http://www.blynk.cc  
  *  Blynk community:      http://community.blynk.cc  
  *  Social networks:      http://www.fb.com/blynkapp  
  *                http://twitter.com/blynk_app  
  *  
  * Blynk library is licensed under MIT license  
  * This example code is in public domain.  
  *  
  **************************************************************  
  * This example runs directly on ESP8266 chip.  
  *  
  * WARNING! ESP8266 SSL support is still experimental.  
  *     More info here: https://github.com/esp8266/Arduino/issues/43  
  *  
  * Note: This requires ESP8266 support package:  
  *  https://github.com/esp8266/Arduino  
  *  
  * Please be sure to select the right ESP8266 module  
  * in the Tools -> Board menu!  
  *  
  * Change WiFi ssid, pass, and Blynk auth token to run :)  
  *  
  **************************************************************/  
 #define BLYNK_PRINT Serial  // Comment this out to disable prints and save space  
 #define RELAY_PIN D1  
 #include <ESP8266WiFi.h>  
 #include <BlynkSimpleEsp8266_SSL.h>  
 #include <Ticker.h>  
 // You should get Auth Token in the Blynk App.  
 // Go to the Project Settings (nut icon).  
 char auth[] = "XXXXXXXX";  
 // Your WiFi credentials.  
 // Set password to "" for open networks.  
 char ssid[] = "XXXXXXXX";  
 char pass[] = "XXXXXXXX";  
 bool vPinState = false;        // Set the default virtual pin state  
 int maxRelayOnTime = 10;       // Set the max on time of the relay   
 int minRelayOnTime = 1;        // Set the minimum on time of the relay  
 int vDelayTime = minRelayOnTime;   // Set the initial delay time value  
 Ticker doorLatch;           // Callback fuction instance  
 void setPinLow()  
 {  
  digitalWrite(RELAY_PIN, 0);  
  doorLatch.detach();  
 }  
 void setup()  
 {  
  Serial.begin(9600);  
  Blynk.begin(auth, ssid, pass);  
  pinMode(RELAY_PIN, OUTPUT);  
  digitalWrite(RELAY_PIN, 0);  
 }  
 void loop()  
 {  
  Blynk.run();  
 }  
 BLYNK_WRITE(V0)  
 {  
  // Get the virtual input state  
  vPinState = param.asInt();  
  // If the virtual input went high  
  if(vPinState)  
  {  
   // Open the relay  
   digitalWrite(RELAY_PIN, 1);  
   // Turn off the relay after the defined delay time  
   doorLatch.attach(vDelayTime, setPinLow);  
  }  
 }  
 BLYNK_WRITE(V1)  
 {  
  // Get the input from the virtual input slider  
  vDelayTime = param.asInt();  
  // Constrain the virtual slider input to within the preset bounds  
  vDelayTime = constrain(vDelayTime, minRelayOnTime, maxRelayOnTime);  
 }  

Android App:

My Blynk project (Android) looks like this:

New device setup screen
Physical Pin and Virtual Pin Mapping
Slider controlling the unlocked time using Virtual Pin 1
Unlock button linked to Virtual Pin 0

Example configuration of relay on physical pin D1

Example configuration of relay on Physical Pin D4. Note the inverse logic configuration of the LED as it is ON when pin D4 is pulled low.


Tip: if you are using the Wemos boards and use the generic ESP8266 board in the Blynk App the built-in led is pin 2 and the relay is pin 5 (D1 on the wemos web page)


Wiring:

My door strike is set up as per the wiring diagram below. I followed the instructions that came with my door strike.




Conclusion:

And that's it! It's as easy as that. I've had my setup running for a few days now with no issues. My past experience with ESP8266 solutions doesn't give me the greatest confidence in device up-time so I'll monitor the solution and see how stable it is.

It's worth noting that I'm not entirely sure how secure Blynk is, so at the moment I'm being cautious and running a local copy of the Blynk server hosted on my local network. I'll need to do some investigating in to this one.

No comments:

Post a Comment