In this article, via the interface using Python MQTT ESP32-WROOM I’m going to tell you how to control the screen ST7789 with microcontrollers. In my project, ESP32 with MQTT messages and checking the images on the screen at the same timeI’m getting hardware information.
About The Project
My project, a Wi-Fi network that is connected to ESP32-WROOM device and send commands to the device over MQTT protocol is based on receiving information from the screen st7789. I developed the interface with Python, thanks to the image on the screen display, I can perform operations such as the removal of the screen. Additionally, the CPU and hardware information, such as MQTT messages XTAL frequencies APB I can get through.
ST7789 ESP32 and two-way communication
In this project, ESP32 microcontrollers, and can send and receive data over WiFi by connecting to a MQTT server. Images shown on this screen st7789 specific commands used with the model and screen and can be removed.
Used Libraries
- WiFi.h: ESP32s allows you to connect to a WiFi network.
- PubSubClient.h: ESP32 using MQTT protocolto communicate with a broker of Allow.
- Wire.h: needed for I2C communication library.
- TFT_eSPI.h: is used to control the display st7789.
Hardware and connection settings
WiFi SSID and password information are defined at the beginning of the code. Enter the IP address and Port information to connect to MQTT broker server. The MQTT server on the local network used in this project, 192.168.1.41
IP address.
Control with MQTT messages
ESP32 ‘control’ named MQTT topic receives a message. If the message ‘play’ if a picture is shown on the screen. If the message ‘stop’ if the screen is cleared. In this way, displayed by a remote device can be altered.
void callback(char* topic, byte* message, uint16_t length) {
mess = "";
for (uint16_t i = 0; i < length; i++) {
mess += (char)message[i];
}
if (String(topic) == "CONTROL") {
if(mess == "play"){
tft.fillScreen(TFT_BLACK);
tft.pushImage(0,0,240,240,mercy);
Serial.println("Displaying the image");
}
else if(mess == "stop"){
tft.fillScreen(TFT_BLACK);
Serial.println("Image removed");
}
}
}
This function receives the message from the MQTT server and display the content according to their controls.
ESP32send the information of
ESP32 every 5 seconds and Crystal processor frequencies, the measurement of the hall sensor is also received from MQTT sends it to the server. This information CPU XTAL, APB, and are published in issues of the Hall.
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
Freq = getCpuFrequencyMhz();
sprintf(dgr,"%d",Freq);
client.publish("CPU",dgr);
Freq = getXtalFrequencyMhz();
sprintf(dgr,"%d",Freq);
client.publish("XTAL",dgr);
Freq = getApbFrequency();
sprintf(dgr,"%d",Freq);
client.publish("APB",dgr);
measurement = hallRead();
sprintf(dgr,"%d",measurement);
client.publish("HALL",dgr);
}
This code ESP32MQTT specific system information it provides to monitor the sensor data and sends to the server.
Title: using the interface with Python MQTT ESP32-WROOM control and screen st7789
Hi, Mustafa ERGÜL. In this article, Python and MQTT using the ESP32-WROOM detaylandiraca I had with st7789 display control how I’ll do it. In my project, I wrote in Python to control the image on the screen of the device ESP32 MQTT interface that I used. In this article, the Python code works and how the workings ESP32 MQTT on the side and I’ll explain.
My goal in developing this project, two-way communicating both MQTT ESP32 basedmicrocontroller to control various operations on both yi was performing. I created a simple Python GUI (graphical user interface) to the display via I can control. Viewing an image on the screen as you clear the content on the screen, the functions in this interface, I am doing easily.
Description Of The Python Code Used In The Project
In the project, I developed a Python based control using MQTT interface language. Interface was created with PyQt5 and paho-mqtt
module with ESP32 using MQTT protocolmessages posted to eat.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
import paho.mqtt.client as mqtt
class MQTTApp(QWidget):
def init(self):
super().init()
self.initUI() # Arayüzün başlangıç ayarları
# MQTT client nesnesi oluşturuyoruz
self.client = mqtt.Client()
# MQTT broker'ına bağlanıyoruz
self.client.connect("192.168.1.41", 1883, 60)
self.client.loop_start() # MQTT döngüsü başlatılıyor
def initUI(self):
# Ana pencereyi ve düğmeleri oluşturuyoruz
layout = QVBoxLayout()
self.play_button = QPushButton('Play') # 'Play' düğmesi oluşturuldu
self.play_button.clicked.connect(self.send_play) # Düğmeye tıklayınca fonksiyon çalışacak
layout.addWidget(self.play_button)
self.stop_button = QPushButton('Stop') # 'Stop' düğmesi oluşturuldu
self.stop_button.clicked.connect(self.send_stop) # Düğmeye tıklayınca fonksiyon çalışacak
layout.addWidget(self.stop_button)
self.setLayout(layout)
self.setWindowTitle('MQTT App')
self.show()
# Play mesajı gönderildiğinde çalışacak fonksiyon
def send_play(self):
self.client.publish("CONTROL", "play") # 'CONTROL' konusuna 'play' mesajı gönderiyoruz
# Stop mesajı gönderildiğinde çalışacak fonksiyon
def send_stop(self):
self.client.publish("CONTROL", "stop") # 'CONTROL' konusuna 'stop' mesajı gönderiyoruz
if name == 'main':
app = QApplication(sys.argv)
ex = MQTTApp()
sys.exit(app.exec_())
The Logic Of The Code:
- MQTT Connection:
- In this code first as
mqtt.Client()
class with the MQTT client (client) I’m building. This client, ESP32eat will be used to send messages and receive messages. self.client.connect('192.168.1.41', 1883, 60)
with ESP32MQTT local broker that is connected to the Sto the A we go. Here, the IP address and port number are indicated by (1883, MQTTis the default port).self.client.loop_start()
function, the client is initiating the process of sending data continuously listening and MQTT.
- In this code first as
- Graphical Interface:
- A simple GUI with PyQt5 (interface) I’m creating.
QPushButton
using two buttons (play and stop) I’m adding. - The ‘play’ button is clicked,
send_play
MQTT function runs over and ‘play’ the message, ‘CONTROL’ of the topic is being sent. - The ‘stop’ button is clicked
send_stop
function is activated and ‘stop’ to the message that is being sent.
- A simple GUI with PyQt5 (interface) I’m creating.
- Send Message:
- The function of each button, ESP32MQTT send commands via eat. ESP32 these commandson the screen ST7789 finds and corresponding to the callback function in the appropriate operation is performed. For example, ‘play’ a message comes on the screen when displaying a picture in a ‘stop’ message when clearing the screen.
ESP32 Python code on the side of Functioning
ESP32 before I shared the code in the callback
function from within the messages being processed. The python interface from the ‘play’ and ‘stop’ control is provided to display the interpreted messages here.
void callback(char* topic, byte* message, uint16_t length) {
String mess = "";
for (uint16_t i = 0; i < length; i++) {
mess += (char)message[i]; // Mesajı stringe dönüştürüyoruz
}
if (String(topic) == "CONTROL") {
if(mess == "play") {
tft.fillScreen(TFT_BLACK); // Ekranı temizliyoruz
tft.pushImage(0, 0, 240, 240, mercy); // Resmi gösteriyoruz
Serial.println("Resim gösteriliyor");
}
else if(mess == "stop") {
tft.fillScreen(TFT_BLACK); // Ekranı temizliyoruz
Serial.println("Resim kaldırıldı");
}
}
}
With this function on the screen ST7789 messages that are sent from the python interface, and the necessary processing operations are performed.
Sonuç
With this project, ESP32 using MQTT with Python and I was able to establish a two-way communication. Python via a simple, intuitive interface to be able to control the screen by using, especially for IoT projects provides flexibility and practicality. While developing the project, both on the ESP32 MQTT protocol, such as a better understanding of how to control a display st7789 I both learned.
In such projects quick MQTT with Python-based interfaces can control them by developing your devices and can retrieve data from. My project that I created using this interface with MQTT Python both have made more useful and dynamic.
If you plan to make your ESP32 IoT devices and other similar projects using this approach you can develop control mechanisms on different devices.