We are now going to set up the part of the project that will actually control the door lock. We'll use the Adafruit ESP8266 breakout board here, as that's just what we need to connect to the WiFi network and activate/deactivate the lock. It will also illustrate how simple it is to make two different platforms (Arduino & the ESP8266) communicate with each other using Adafruit IO.
In this part, we are actually not going to use the lock right away, as the hardware is quite complicated to assembled. Instead, we are simply going to connect an LED to the pin where we'll put the lock later. This will allow us to test the code without having to care about the complexity of the hardware.
To assemble this part of the project, it's really simple: just put the ESP8266 on the breadboard first. Then, put the LED on the breadboard, with the longest side of the LED in series with the resistor. After that, connect the other side of the resistor the pin 5 of the ESP8266 board, and the other side of the LED to the GND pin of the ESP8266 board.
This is the completely assembled project:
Let's now see the sketch that we'll use for this part of the project. Again, as the sketch is quite complex, I'll only show you the most important parts here.
It starts by including the right libraries:
#include <ESP8266WiFi.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h"
Then, you need to set your WiFi settings:
#define WLAN_SSID "your_wifi_ssid" #define WLAN_PASS "your_wifi_password" #define WLAN_SECURITY WLAN_SEC_WPA2
And also your Adafruit IO settings, just as in the previous project:
#define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "adafruit_io_username" #define AIO_KEY "adafruit_io_key"
We also define on which pin we'll connect the LED (and later the lock, or a relay for example):
int relayPin = 5;
We also define a new feed to the lock:
const char LOCK_FEED[] PROGMEM = AIO_USERNAME "/feeds/lock"; Adafruit_MQTT_Subscribe lock = Adafruit_MQTT_Subscribe(&mqtt, LOCK_FEED);
In the setup() function of the sketch, we set the pin of the LED as an output:
pinMode(relayPin, OUTPUT);
We also subscribe to the lock feed that we just defined earlier:
mqtt.subscribe(&lock);
In the loop() function of the sketch, we first check if we are connected to Adafruit IO:
MQTT_connect();
Then, we check what's coming from the lock feed we subscribed to. If the message is '1', we activate the pin we defined earlier, that is connected to the LED. If we receive a '0', we just put this pin to a low state again:
Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(1000))) { if (subscription == &lock) { Serial.print(F("Got: ")); Serial.println((char *)lock.lastread); // Save command to String String command = String((char *)lock.lastread); command.trim(); if (command == "0") { digitalWrite(relayPin, LOW); } if (command == "1") { digitalWrite(relayPin, HIGH); } } }
Note that you can find the latest version of the code on the GitHub repository of the project:
https://github.com/openhomeautomation/lock-control-fingerprint
It's now time to test the project! Use the Arduino library manager to download all the required libraries of this project.
Also make sure that you modified the code with your settings, like your Adafruit IO settings & your WiFi network credentials.
To program the ESP8266 chip, I used a simple USB-FTDI board. To learn more about how to program the Adafruit ESP8266 breakout board, I recommend this excellent guide:
https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout
Finally, upload the code to the board. Also open the Serial monitor. You basically just want to check for now that the project can indeed connect to Adafruit IO: we'll test the other functionalities in the next page.
Page last edited September 08, 2015
Text editor powered by tinymce.