Greetings! Welcome to DAY 20th of Classi Developers Advent Calendar 2022. I am @willsmile, a backend engineer serving the mobile application team in Classi Corp. and a father of a one-year-old boy.
The topic of this post is about sharing my experience and what I learned from building an IoT-based temperature monitoring system by MQTT. Let me explain why I am trying to learn IoT. Firstly, learning something new (I mean something I do not know) every year is my habit, and programming related to IoT is occasionally one of the things I learned this year. Secondly, I have been considering building some homemade toys for my kid recently, and the knowledge of IoT might be helpful.
Let’s go back to today’s topic. First, I am trying to transfer the data (for example, temperature) gained from the sensor to the server so that I can monitor the change in data. One of the essential things I need to decide is which communication protocol I should use.
Here are some comparisons of some of the primary communication protocols.
- HTTP: unidirectional communication; underlying transport protocol: TCP
- WebSocket: bidirectional communication; underlying transport protocol: TCP
- MQTT: bidirectional communication; underlying transport protocol: TCP or WebSocket
- gRPC: unidirectional or bidirectional communication (depends on which kind of service method uses); underlying transport protocol: HTTP/2
I will select MQTT because it is widely used for communication between IoT devices due to its features of being lightweight and efficient. To use MQTT, we need to set up an MQTT broker to transfer the messages from/to the devices. I had written the way how I set up the broker in the article (Try Mosquitto Broker). Please check it if you want to know about it.
For the IoT device and the sensor to collect the data on temperature, I use the M5StickC Plus and M5StickC ENV Hat III which made by M5Stack. M5Stack provides good products and also a not-bad programming platform for all kinds of IoT projects. I highly recommend it to people who want to try some IoT but need to gain more knowledge of electronic circuits, like me. By the way, there has been a sale named “TOP100 SALE” on switch-science.com recently, and please check it if you are interested in it (NOT a paid promotion).
With the devices described above, I built a demo application to collect the data on the temperature measured by the sensor and transfer them to the MQTT broker set up in my Raspberry Pi 4. I created the program (as shown in the screenshot) with the tool named M5Stack UI Flow. I want to try the Arduino programming language, but since I have little time, I will leave it to the next time.
The program connects to the server of the MQTT broker and publishes the messages, which contain the timestamp and the temperature value, to the topic temperature. By the following command, I may connect to the server of the MQTT broker, subscribe to the topic temperature, and receive the messages.
mosquitto_sub -h blackpi-4g.local -p 11883 -t temperature
The received data is shown in the following screenshot. Because I did not calibrate the sensor, the temperature data is inaccurate. So please don’t mind about it.
Let me conclude my post. First, I built a demo program to collect the temperature data and transfer them to other servers using the MQTT protocol. What I learned from this experience is that it is essential to select a proper communication protocol. As a web application engineer, I used to choose HTTP protocol defaultly when the requirement was unclear. It is alright, but I have lost the opportunity to learn other protocols and compare them with each other, which is a pity. In the future, I will try more and write them down, so if you feel interested, please follow my Twitter (@willsmile) or bookmark my blog. Thanks a lot.
Tomorrow is DAY 21st of Classi Developers Advent Calendar 2022, and @irisuinwl will bring a different but absorbing story for sure. Please try and read it.