IoT: Temperature logger

Introduction

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.

Hardware

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.

NodeMCU board
Figure 1 – NodeMCU board.

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.

DHT11 integrated in a ready to use board.
Figure 2 – DHT11 integrated in a ready to use board. Taken from [1].

It’s important to take into consideration that the DHT11 has a 1ºC resolution [2], 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.

Server platform

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 [3] and has a free plan [4], 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 [5], amongst others. In terms of databases, it supports MongoDB, MySQL, PostgreSQL and SQLite [5].

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.

Application

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 [6].

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).

Architecture of the system.
Figure 3 – Architecture of the system.

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.

IoT temperature chart
Figure 4 – Temperature chart.

Final Notes

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).

References

[1] https://www.mysensors.org/build/humidity
[2] http://www.micropik.com/PDF/dht11.pdf
[3] https://www.openshift.com/
[4] https://www.openshift.com/pricing/plan-comparison.html
[5] https://www.openshift.com/features/technologies.html
[6] http://www.chartjs.org/docs/#notes-license

14 thoughts on “IoT: Temperature logger”

    1. I don’t know how much you are familiarized with microcontrollers, Arduino, etc, but the ESP8266 is basically a device that you can program to do things like reading sensors, controlling actuators and it also has functions to connect to a WiFi network and then send/receive HTTP requests, amongst many other things.

      You can read a little bit more about the device here:
      https://techtutorialsx.wordpress.com/2016/02/14/esp8266/

      If you are interested in actual code for the ESP8266, check bellow,

      Connect the ESP8266 to a WiFi network:
      https://techtutorialsx.wordpress.com/2016/07/16/esp8266-connecting-to-a-wifi-network/

      Send HTTP requests with the ESP8266:
      https://techtutorialsx.wordpress.com/2016/07/17/esp8266-http-get-requests/
      https://techtutorialsx.wordpress.com/2016/07/21/esp8266-post-requests/

    1. I don’t know how much you are familiarized with microcontrollers, Arduino, etc, but the ESP8266 is basically a device that you can program to do things like reading sensors, controlling actuators and it also has functions to connect to a WiFi network and then send/receive HTTP requests, amongst many other things.
      You can read a little bit more about the device here:
      https://techtutorialsx.wordpress.com/2016/02/14/esp8266/
      If you are interested in actual code for the ESP8266, check bellow,
      Connect the ESP8266 to a WiFi network:
      https://techtutorialsx.wordpress.com/2016/07/16/esp8266-connecting-to-a-wifi-network/
      Send HTTP requests with the ESP8266:
      https://techtutorialsx.wordpress.com/2016/07/17/esp8266-http-get-requests/
      https://techtutorialsx.wordpress.com/2016/07/21/esp8266-post-requests/

  1. Pingback: Python: An introduction | techtutorialsx

  2. Pingback: Python: An introduction | techtutorialsx

  3. Pingback: Flask: Hello World | techtutorialsx

  4. Pingback: Flask: Hello World | techtutorialsx

  5. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx

  6. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading