The objective of this post is to explain the architecture of a simple temperature logger using the ESP8266 and a web server deployed on the cloud.
In order to implement the smart node that measures the temperature and sends it to the server, we simply used a microcontroller with the capability of connecting to the Internet and a temperature sensor.
The microcontroller is the ESP8266, integrated in a NodeMCU board, shown in figure 1. The advantage of the NodeMCU board in comparison to other simpler ESP8266 based boards is that this one already has a usb header and the usb-serial converter device in the PCB. So, it’s a plug and play solution.
The programming of the Node MCU board was done using the Arduino based IDE.
The sensor used to measure temperature was the DHT11, shown in figure 2. This is a cheap sensor that allows to measure not only temperature but also humidity. In this example, we only send temperature to the server, but the architecture would be the same to also measure humidity.
It’s important to take into consideration that the DHT11 has a 1ºC resolution , meaning that it may not be suitable for more precise applications. Nevertheless, the objective was to create a proof of concept of the whole system and thus, changing the sensor to a more precise one, is a simple and easy solution.
The main objective of this project was to make the temperature measurements available at a server, where a user could access them in a dashboard style (in this case, a temperature chart).
In this project, we used the OpenShift Platform to deploy the application. Openshift is a Platform-as-a-Service from RedHat that allows for quick development, hosting and scaling of applications in the cloud  and has a free plan , which is perfect for this kind of tests.
Openshift allows the development of applications in typical languages such as Python, Java, Ruby and PHP, and even in more recent ones, such as Scala and Go , amongst others. In terms of databases, it supports MongoDB, MySQL, PostgreSQL and SQLite .
One down side of this platform is that the configuration process is not trivial and is mainly performed using the command line, which may not be easy for everyone. Although there are many installation tutorials, some configurations are not so easy as they seem. Personally, I had a really hard time configuring the remote access using SSH. Nevertheless, once everything is properly configured, it becomes a very easy to use and powerful platform.
The backend of the application was developed using the Flask web framework for Python. This is a very easy to use framework which allows the creation of a simple application with very few lines of code (see the “hello world” example here).
In a very succinct way, in Flask we define functions that are triggered by a specified URL. In those Python functions, we define the backend behavior of the application for each URL, such as accessing databases, validating user credentials or serving the frontend files. Of course, this is a very reductive description of the wide range of possibilities that can be implemented using this framework, which are outside the scope of this post.
In this concrete case, we implemented two distinct URLs on our application. The first one receives HTTP requests with a temperature value as parameter. For each request, the application puts a timestamp on it and inserts it in a database.
Naturally, this is the entry point where the ESP8266 connects and posts the measurements from the DTH11.
The other URL is the one the user accesses to check the data in the format of a chart. So, the handling function accesses the database to retrieve the last measurements stored (we limited the maximum number of measurements retrieved) and using Flask’s template engine it passes those values to the HTML template (more about template engines here), which is returned to the user’s browser.
In the HTML file, the chart is created using Charts.js, which is an Open Source chart library .
Architecture of the system
The architecture of the system is summarized in figure 3 and contains all the elements described before. Although the system was created to log temperature, it is easly scalable to work with other types of information from sensors (ex: humidity, luminosity).
Figure 4 shows the chart that is presented to the user and contains the temperature measurements performed. It’s important to refer that the frontend was note one of the main preoccupations and thus we used a very simple chart type, just to illustrate the concept.
As stated before, this is just a proof of concept project, with the objective of demonstrating the interaction between an ESP8266 board and a web server, in order to materialize the concept of IoT. So, the application just simply stores and retrieves data from a database, and there is not user authentication or any other implementations needed for a full web service for users.
The requests performed to the server are in HTTP, meaning that the information is sent in plain text, for the sake of simplicity. Of course that, for a commercial implementation, the data would need to be protected using HTTPS or any other type of mechanism.
It’s also important to take into account that timestamping the measurements in the server may not be the best option, since the delay between the measurement and the arrival of the information to the server may not be negligenciable. The best option would be implementing the timestamping in the ESP8266 (see an example here of an implementation of a clock for microcontrollers).