ESP8266 Series
- Blink
- Arduino core for ESP8266 WiFi chip
- Sonoff Dissection
- MQ Telemetry Transport
- Firmata
- Johnny-Five
As soon as we have more than one IoT “thing” to manage; we need some kind of management; it quickly becomes cumbersome to use and manage the “smart devices”. We can use Shields like Ethernet, Wifi or GSM with Arduino or use ESP8266 with Arduino or on its own for connectivity, but we need some kind of server where all these devices connect to and from where we can control them. Secondly; even though these MCUs have descent processing power but for the user friendly solution often we need more and integration with the cloud or some proper PC / Single Board Computer is required. Say we deploy ESP8266 based Sonoff Smart switches for outdoor lights and want a “recipe” that when sun goes down the lights are turned on and when sun rises lights are turned off. For this we not only need the knowledge of sunsets and sunrises but we also need to coordinate the Sonoff “smart devices”. This is where protocols like MQTT comes in. As per wikipedia
MQTT (MQ Telemetry Transport) is an ISO standard publish-subscribe-based "lightweight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message.
There exists PubSubClient library that we can use in Arduino IDE; the library works perfectly fine with Arduino Core for ESP8266. For the context you might want to check out my previous blog posts
We need MQTT Server; called MQTT Broker some where with known and static address. This can be in the cloud; either commercial or free; or we can deploy our own broker within the network. Mosquitto is popular open source MQTT broker that we can install either on Linux or on Windows. Installing on Linux is straight forward; we can even install and configure it on Raspbian (Raspberry Pi), but setting it up on Windows is little tricky. You need to install few other softwares and copy over the DLLs from there to Mosquitto’s installlation folder. Its all documented in its Setup Wizard; but if you need the step by step instructions; visit https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/
Its always a good idea to use authentication with network servers; use mosquitto_passwd utility that comes with it to create a password file and add the required users.
- Given I am creating the password file in Program Files; I needed Administrative Command Prompt
Once the password file is in place; edit its conf file disabling anonymous users and setting path of the password file
- Given the conf file is in Program Files; you will need to open file in some editor that's running Administratively
We can use MQTTLens; a Chrome application; to test Mosquitto; our MQTT Broker
Once the broker is properly in place; we need to write a firmware for our appliance; that connects to MQTT broker; publish its status / logs there and gets the command from it by subscribing to related topics. The PubSubClient library comes with many examples including mqtt_auth that we can use to write the firmware for Arduino or ESP8266 MCUs
Here’s the Arduino IDE code for one such firmware that I wrote for my NodeMCU development board; it takes the command from the MQTT and turn the LED on or off accordingly.
Once we have the firmware developed and tested; we can easily use it in real world IoT appliances like Sonoff. Now that our smart appliance is connected to the MQTT; we can control it from elsewhere on demand. We can have a program / scheduled job / trigger; configured in a cloud or running on a PC or Raspberry Pi that at the time of Sunset and Sunrise send appropriate messages to the MQTT broker and the smart appliance with oblige accordingly. Eclipse’ Paho project provides open source client implementations of MQTT; https://eclipse.org/paho/clients/python/ is one such Python client library that’s just “pip install paho-mqtt” away. Here’s one such python script that flashes the LED connected to NodeMCU by sending required payloads to the appropriate topic where NodeMCU is subscribed to
On receiving the messages; the NodeMCU flashes the LED accordingly; here’s what my firmware is writing to Serial port
With MQTT understanding and how it can be used to orchestrate the IoT appliances; the architecture of our Sunset/Sunrise Automatic Outdoor Lights solution would be something like this
We can either user some Cloud API for sunset/sunrise times; or store the times in some local database; given Raspbian on Raspberry Pi gives us a very rich Linux experience; we can deploy mySQL and Mosquito on it along with our custom program in Python (or any other language / platform of our choice)