One of the awesome things about the rise in Internet of Things (IoT) devices is availability of very inexpensive hardware to monitor anything that interests you. Today I want to share with you how to quickly prototype an Internet of Things (IoT) device that captures weather metrics (temperature and humidity) and sends them to a data backend for persistence and monitoring. We will use an esp8266 microcontroller (between $2 and $10) paired with the nodecmu firmware. This combination will let us write our logic in Lua, a lightweight, higher-level, multi-paradigm programming language.
The Internet of things (IoT) is the rapidly expanding network of physical objects with networking capabilities to communicate with the Internet. Sensors are a concrete example of this. It is increasingly easy to build small, autonomous devices that sleep most of their time and only wake up to perform measurements and send them to a remote server.
The arduino platform is one of the most common choices when working on IoT projects. It uses a very low power (both at the computational and consumption level) micro-controller. In this environment, C is the programming language of choice.
These sensors have limited computational resources and typically lack of any type of operating system. Your device's logic runs straight in the hardware. In addition, they run autonomously for months using only commodity batteries. These limitations result in a rather different programming environment when compared to desktop or client/server applications, which is why most of the devices are programmed in C. But thanks to the nodemcu project, it is possible to write your firmware in a higher-level programming language like Lua.
One of the essential characteristics of these devices is the ability to communicate with a server to persist the collected data. Using Wifi hardware and having a full TCP/IP stack is ideal but presents consumption challenges for devices running for long periods of time with regular batteries. Considering these sensors will send very small payloads, using this hardware can be overkill. With this in mind, most people wisely use radio frequency (RF) transceivers to enable communication in their projects.
In this article we want to explore how to build an IoT sensor that sends temperature data periodically to Librato.
I mentioned the Arduino as a common platform to develop IoT projects. In this post we want to explore another alternative: the esp8266. This is a SOC (System on a Chip) device that packs an impressive list of features, among them a full TCP/IP stack and WiFi capabilities. The chip ships on a variety of breakout boards, probably the most common one being the esp01. You can buy this one for around three dollars on ebay.
The manufacturer provides a SDK that you can use in combination with a toolchain to build your firmware in a similar way you do with the Arduino.
But we have decided to approach the programming of the device in a different way. The powerful hardware specs of the esp8266 make possible to embed a version of Lua (based on eLua) within the chip, enabling us to enjoy all the power of this tiny but powerful language. As I mentioned earlier, this is provided thanks to the nodemcu project. In addition, this project exposes some primitives that encourage event-driven programming which is ideal for developing IoT logic.
Before being able to use nodemcu, we have to flash the chip with the latest version of the firmware. Once the firmware is installed you should be presented with a Lua REPL when connecting to the chip via the serial interface. You can also send and store Lua scripts within the chip and run them as necessary.
We had some difficulties sending SSL requests from nodemcu. To work around this, we used statsd which forwards the data to Librato. The Raspberry Pi is an ideal candidate to host statsd in this scenario.
The Lua code we run in the chip is rather straightforward. We start by setting up the Wifi connection. Then, we periodically read the current temperature from a dht11 sensor and send it to statsd via UDP.
You'll run out of power in a few days if you choose to run the chip with batteries. In a future post, we will explore the deep sleep functionality exposed via `node.dsleep` in nodemcu. When used, we can set the chip in a low consumption state most of the time and wake up periodically to collect and send data to Librato. That will decrease power consumption enabling us to run the chip on batteries for months.
Prototyping and building IoT sensors is getting easier thanks to hardware like the esp8266 and projects like nodemcu. While this is a simple example, the concepts can easily be expanded to monitor anything of your choice.
Happy building and let us know what you create: @drio