Monday, February 26, 2024

Get Sensor Data and Controlling Devices Using ESP32 and Android Application over MQTT

Hello everyone, In this post I will explain the working of my project based on the ESP32 microcontroller. In this project, I will show you, how to control the devices and get sensor data using ESP32 microcontroller and MQTT.
The following is a demo animation of this project.

Project Demo

As you can see in the above animated image the ESP32 is connected with the DHT11 sensor to sense the temperature and humidity data and it is also connected with the TFT display, which is used to display the temperature and humidity values.
You can see on the display there are 2 LEDs, one is a normal Red LED and one is a RGB LED. I didn’t use the physical LEDs instead I am simulating the LEDs using the LVGL graphics library. There is a switch, which is used to control the single-color LED state.

This post is sponsored by PCBWay, with more than a decade in the field of PCB prototype and fabrication, PCBWay is committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness, and any other demanding requests.
As one of the most experienced PCB manufacturers in the World, PCBWay prides itself on being your best business partner as well as a good friend in every aspect of your PCB needs.
I will use this block diagram to explain what is happening inside the system, as the first step the ESP32 will connect to the Wi-Fi router, and after the connection is successful, it will connect with the MQTT broker, once the connection is successful, it will start publishing the Sensor data to the broker.
Block Diagram

On the other hand, the application developed using the QT framework, will subscribe to this data, and display this on its application, similarly, we can control the LEDs from the application, using a normal switch which is used to control the red LED and also by using the sliders, with sliders we can generate a color, and the same color will be displayed on the ESP32 TFT screen.
QT Android Application

The above image illustrates the user interface of the application is developed, this application is intentionally developed using the QT framework, so that it can run on all major operating systems like Windows, Android, Linux, and others, without any modifications, as QT is cross-platform.

COMPONENTS NEEDED:

Connection Diagram
From the above picture, the pinout is not visible properly, so you use the below-mentioned table for the connections.
ILI9341 Connection with ESP32

SOFTWARE REQUIREMENTS:

  • ESP-IDF (Espressif IoT Development Framework)
  • Time synchronization component from ESP-IDF
  • ILI9341 Display Driver (Click Here)

What is MQTT

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for small-footprint devices with limited resources, such as sensors, microcontrollers, and mobile devices, to communicate over networks with varying levels of reliability.
Here are some key features of MQTT:
  • Lightweight: MQTT is designed to be lightweight and efficient, making it ideal for use in resource-constrained environments. The protocol uses a publish/subscribe messaging pattern that minimizes network bandwidth and device processing requirements.
  • Publish/Subscribe Model: MQTT follows a publish/subscribe messaging model, where devices (publishers) publish messages to topics, and other devices (subscribers) subscribe to topics to receive messages. This decoupling of publishers and subscribers enables flexible and scalable communication patterns.
  • Quality of Service (QoS): MQTT supports three levels of Quality of Service (QoS) to ensure message delivery reliability: 
    • QoS 0 (At most once): The message is delivered at most once, without any acknowledgment or guarantee of delivery.
    • QoS 1 (At least once): The message is guaranteed to be delivered at least once, but duplicates may occur.
    • QoS 2 (Exactly once): The message is guaranteed to be delivered exactly once by using a two-step handshake mechanism.
  • Retained Messages: MQTT supports retained messages, which are special messages that are stored on the broker and delivered to new subscribers when they subscribe to a topic. Retained messages are useful for transmitting the most recent state or configuration information to new subscribers.
  • Last Will and Testament (LWT): MQTT allows clients to specify a Last Will and Testament message, which is published by the broker if a client unexpectedly disconnects. LWT messages are useful for detecting and handling client failures or disconnections.
  • Session Persistence: MQTT brokers can optionally support session persistence, which allows clients to resume their previous sessions and receive missed messages when they reconnect. Session persistence ensures reliable message delivery even in the presence of network interruptions or client disconnects.
  • Security: MQTT supports various security mechanisms, including username/password authentication, SSL/TLS encryption, and client certificates, to ensure secure communication between clients and brokers.
MQTT is widely used in Internet of Things (IoT) applications, home automation systems, industrial monitoring and control systems, and other distributed systems where lightweight and reliable messaging is required. Its simplicity, efficiency, and flexibility make it a popular choice for IoT communication protocols.

Project Explanation

The following video demonstrates the complete working of the project.

  • At power-up ESP32 will connect to the WiFi router and then it will connect with the MQTT broker, here I am using the free MQTT broker, for hobby based project the free MQTT broker such as "test.mosquitto.org" is sufficient, but keep in mind to not send any sensitive data.
  • Clicking on the "connect" button will connect the application to the MQTT broker and clicking on the "disconnect" button will disconnect it from the MQTT broker.
  • Moving the switch button to the "On" position in the application or on the TFT screen will turn on the LED on the ESP32, and moving the switch button to the "Off" position in the application or on the TFT position will turn it off.
  • The switch button in the application is synchronized with the switch button on the TFT display, which means if we change the state of the switch button on the TFT display, the state is adjusted in the application also.
  • Now coming to the sliders, there are three sliders to control the Red, Green, and Blue colors of the LED, as we change the slider, the colors are changed, and the same colors are displayed on the TFT screen, this feature can be used to control the RGB LED bulbs, a Home Automation project.
  • Moving the sliders to 0, 0, 0 position means LED off, which is represented as black color, similarly setting the value 255, 255, 255 will represent the full white color.
  • Sometimes you might feel that the color generated on the application does not exactly match the color on display, but this is due to the camera recording, with naked eyes this looks exactly similar.
  • Since we are using the QT framework the same application works the same on Windows, Android, and Linux platforms, I have tested this on Windows and Android, but I am very sure that it will work on other platforms also without any modification.

Conclusion

This is a small project made by using ESP32, DHT11, and ILI9341 display controllers, the project can be extended to make a full home automation project, where sensor data from several devices can be published and displayed on the applications, in future my plan is to visualize this whole data on Grafana as a dashboard, similar to this project.
The code for this project can be downloaded from the following links.

No comments:

Post a Comment