BlogSoftware Development

IoT: Sending telemetry to MQTT servers

By September 24, 2018 No Comments

I first experience with IoT was during my undergraduate studies while working on my bachelor thesis. This is what motivated me to further explore IoT and the ABH Internship in IoT seemed like the best opportunity to gain practical experience in the field. Unlike the other ABH Internship projects, which have been offered before, the IoT internship was new so there was very little information and I had no idea what to expect. Needless to say, I was excited and nervous at the same time.

The goal of the project I worked on during the IoT internship was the implementation of firmware for the ESP32 microcontroller, which would support the following tasks: Wi-Fi connectivity, Configurability, read data from sensors and send data to MQTT server. I used 3 different sensors: temperature and humidity sensor, light sensor and sound sensor. I used the official Espressif IoT Development Framework for programming.

I took information from the sensors via analog inputs and I continued to distribute them via wi-fi using the MQTT communication protocol. The MQTT protocol works on the publish/subscribe principle. I used the ESP32 microcontroller as a publisher who published information collected from a sensor on a MQTT server on a defined topic. We could access this information from another client by connecting to the MQTT server and subscribing to the same topic.

For the purpose of this blog, I decided to only show the code for the temperature sensor.

In addition to the functions necessary for wi-fi connection and configuration of the MQTT server, it is necessary to implement functions for reading data from the sensor and sending it to the MQTT server. “sensor_task” serves to read data from the sensor, while “publish_task” serves to send the data to the MQTT server as shown in the code below:

 

The most difficult period was the first month of the Internship when I spent a lot of time doing research because I was introduced to new technologies. The second month was much more productive because I focused on specific tasks, and since I managed to finish the project, I had more time to realize some of the ideas that I thought were interesting. This Internship helped me learn many new things related to IoT, with which I have been able to expand my horizons in this field, and all thanks to my mentor Mirnes who gave me quality guidance and advice.