Get Temperature & Humidity Data On PC Using Python - NodeMCU (Tutorial-7) - Embedded Laboratory
Get Temperature & Humidity Data On PC Using Python - NodeMCU (Tutorial-7) - Embedded Laboratory
EMBEDDED
LABORATORY
H OME A RDUINO MICROCH IP IOT A RM P YTH ON RA SP BERRY P I MATLA B LA BVIEW OTH ERS
In this post, I am going to extend the Subscription part to PC also, by this we can subscribe for the
same topic using multiple clients on different platforms.
FACEBOOK
The language of choice in this case is Python, and we have already played with Python and MQTT
Embedded Laboratory
earlier, plus we also know how to create beautiful User Interfaces using PyQt5 in Python. 7,344 likes
Finally the most important part, in this post we will be publishing the temperature and humidity data
Twitter
using NodeMCU and DHT11 sensor, so to get the NodeMCU software, use the following link.
Get Temperature and Humidity Data on Android Phone | NodeMcu/ESP8266
YouTube
The following is the User Interface which is developed in Python using PyQt5.
RECENT CO MMENTS
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 1/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
Microcontroller
Embedded Laboratory May
2020
BLOG ARCHIVE
Have a look at the following video, to view the Python User Interface, Publishing and Subscribing
Topics. TOTAL PAGEVIEWS
7 7 7 4 0 5
Measure Temperature and Humidity using Nod…
Nod…
Python Software
In this script, I am storing the server information such as Server Address, Server Port Number,
Username and Password, as a Python Dictionary and storing it in 'settings.txt' file.
Now coming to the important code snippets.
The following function is called, when connect button on user interface is pressed.
Code
def connect_with_server():
global server_info
client.username_pw_set(server_info["Username"], server_info["Password"])
client.connect(server_info["Server_Address"], int(server_info["Server_Port"]),
client.loop_start()
The following Callback function is called when this application successfully connected with server, this
function also enables the controls which will be used after connection and it will also disables the
controls which will not be used after connection is established.
Code
# Callback Function on Connection with MQTT Server
def on_connect( client, userdata, flags, rc):
print ("Connected with Code :" +str(rc))
if rc == 0:
# Subscribe Topic from here
client.subscribe("home/#")
# Enable Disconnect Button and Enable Others
ui.connect_btn.setDisabled(True)
# ui.server_add.setEnabled(False) Don't use this
ui.server_add.setDisabled(True)
ui.server_port.setDisabled(True)
ui.username.setDisabled(True)
ui.password.setDisabled(True)
ui.disconnect_btn.setEnabled(True)
ui.led_btn.setEnabled(True)
ui.statusBar.setStatusTip("Connected")
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 2/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
The following Callback function is called when Subscribed topics are received on PC, in our case it will
consist of temperature and humidity values, which are extracted from the payload and displayed on
the user interface.
Code
# Callback Function on Receiving the Subscribed Topic/Message
def on_message( client, userdata, msg):
# print the message received from the subscribed topic
#print ( str(msg.payload) )
#print ( str(len(msg.payload) ))
if len(msg.payload) == 5:
message = msg.payload
message = message.decode() # default decoding utf-8
temp, humidity = message.split(',')
ui.temp.setText(temp)
ui.humidity.setText(humidity)
The following function is called whenever led button is pressed, this function publishes the led state,
and as you know NodeMCU has subscribed to this topic and it will change the state of the led based
on the payload.
Code
def led_state_loggle():
global led_state
if led_state == True:
client.publish("led", bytes([0x00]))
led_state = False
ui.led_btn.setText("OFF")
else:
client.publish("led", bytes([0x01]))
led_state = True
ui.led_btn.setText("ON")
The above are some of the important snippets from the code, the complete Python code is given
below.
Code
import os
import codecs
import pickle
import paho.mqtt.client as mqtt
from PyQt5 import QtCore, QtGui, QtWidgets
global server_info
global led_state
led_state = False
filename = 'settings.txt'
def save_file():
with open(filename, "wb") as myFile:
pickle.dump(server_info, myFile)
if os.path.exists(filename):
# Read Dictionary from this file
with open(filename, "rb") as myFile:
server_info = pickle.load(myFile)
else:
# Create Dictionary Using Default parameters
server_info = { "Server_Address":"m14.cloudmqtt.com", \
"Server_Port":"18410", \
"Username": "setsmjwc", \
"Password":"apDnKqHRgAjA"}
save_file()
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 3/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
ui.server_port.setDisabled(True)
ui.username.setDisabled(True)
ui.password.setDisabled(True)
ui.disconnect_btn.setEnabled(True)
ui.led_btn.setEnabled(True)
ui.statusBar.setStatusTip("Connected")
def save_server_add():
global server_info
server_info["Server_Address"] = ui.server_add.text()
save_file()
def save_server_port():
server_info["Server_Port"] = ui.server_port.text()
save_file()
def save_username():
server_info["Username"] = ui.username.text()
save_file()
def save_password():
server_info["Password"] = ui.password.text()
save_file()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
def connect_with_server():
global server_info
client.username_pw_set(server_info["Username"], server_info["Password"])
client.connect(server_info["Server_Address"], int(server_info["Server_Port"]),
client.loop_start()
def disconnect_with_server():
client.loop_stop()
client.disconnect()
# Enable Connect Button and Disable Others
ui.connect_btn.setEnabled(True)
ui.server_add.setEnabled(True)
ui.server_port.setEnabled(True)
ui.username.setEnabled(True)
ui.password.setEnabled(True)
ui.disconnect_btn.setDisabled(True)
ui.led_btn.setDisabled(True)
ui.statusBar.setStatusTip("Not Connected")
def led_state_loggle():
global led_state
if led_state == True:
client.publish("led", bytes([0x00]))
led_state = False
ui.led_btn.setText("OFF")
else:
client.publish("led", bytes([0x01]))
led_state = True
ui.led_btn.setText("ON")
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(306, 199)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.connect_btn = QtWidgets.QPushButton(self.centralwidget)
self.connect_btn.setGeometry(QtCore.QRect(220, 10, 75, 31))
self.connect_btn.setObjectName("connect_btn")
self.disconnect_btn = QtWidgets.QPushButton(self.centralwidget)
self.disconnect_btn.setEnabled(False)
self.disconnect_btn.setGeometry(QtCore.QRect(220, 70, 75, 31))
self.disconnect_btn.setObjectName("disconnect_btn")
self.temp_lbl = QtWidgets.QLabel(self.centralwidget)
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 4/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
self.temp_lbl.setGeometry(QtCore.QRect(10, 120, 69, 18))
self.temp_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.temp_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.temp_lbl.setObjectName("temp_lbl")
self.humid_lbl = QtWidgets.QLabel(self.centralwidget)
self.humid_lbl.setGeometry(QtCore.QRect(110, 120, 69, 18))
self.humid_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.humid_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.humid_lbl.setObjectName("humid_lbl")
self.temp = QtWidgets.QLabel(self.centralwidget)
self.temp.setGeometry(QtCore.QRect(10, 140, 31, 18))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.temp.setFont(font)
self.temp.setLayoutDirection(QtCore.Qt.LeftToRight)
self.temp.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.
self.temp.setObjectName("temp")
self.c_lbl = QtWidgets.QLabel(self.centralwidget)
self.c_lbl.setGeometry(QtCore.QRect(50, 140, 21, 18))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.c_lbl.setFont(font)
self.c_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.c_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.c_lbl.setObjectName("c_lbl")
self.humidity = QtWidgets.QLabel(self.centralwidget)
self.humidity.setGeometry(QtCore.QRect(110, 140, 31, 18))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.humidity.setFont(font)
self.humidity.setLayoutDirection(QtCore.Qt.LeftToRight)
self.humidity.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtC
self.humidity.setObjectName("humidity")
self.percent_lbl = QtWidgets.QLabel(self.centralwidget)
self.percent_lbl.setGeometry(QtCore.QRect(150, 140, 21, 18))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.percent_lbl.setFont(font)
self.percent_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.percent_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.percent_lbl.setObjectName("percent_lbl")
self.led_btn = QtWidgets.QPushButton(self.centralwidget)
self.led_btn.setGeometry(QtCore.QRect(220, 110, 71, 51))
self.led_btn.setObjectName("led_btn")
self.led_btn.setEnabled(False)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 71, 91))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.server_add_lbl = QtWidgets.QLabel(self.widget)
self.server_add_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.server_add_lbl.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTraili
self.server_add_lbl.setObjectName("server_add_lbl")
self.verticalLayout.addWidget(self.server_add_lbl)
self.server_port_lbl = QtWidgets.QLabel(self.widget)
self.server_port_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.server_port_lbl.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrail
self.server_port_lbl.setObjectName("server_port_lbl")
self.verticalLayout.addWidget(self.server_port_lbl)
self.username_lbl = QtWidgets.QLabel(self.widget)
self.username_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.username_lbl.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing
self.username_lbl.setObjectName("username_lbl")
self.verticalLayout.addWidget(self.username_lbl)
self.password_lbl = QtWidgets.QLabel(self.widget)
self.password_lbl.setLayoutDirection(QtCore.Qt.LeftToRight)
self.password_lbl.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing
self.password_lbl.setObjectName("password_lbl")
self.verticalLayout.addWidget(self.password_lbl)
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(90, 10, 111, 91))
self.widget1.setObjectName("widget1")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget1)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.server_add = QtWidgets.QLineEdit(self.widget1)
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 5/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
self.server_add.setMaxLength(30)
self.server_add.setObjectName("server_add")
self.verticalLayout_2.addWidget(self.server_add)
self.server_port = QtWidgets.QLineEdit(self.widget1)
self.server_port.setMaxLength(30)
self.server_port.setObjectName("server_port")
self.verticalLayout_2.addWidget(self.server_port)
self.username = QtWidgets.QLineEdit(self.widget1)
self.username.setMaxLength(30)
self.username.setObjectName("username")
self.verticalLayout_2.addWidget(self.username)
self.password = QtWidgets.QLineEdit(self.widget1)
self.password.setMaxLength(30)
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.password.setObjectName("password")
self.verticalLayout_2.addWidget(self.password)
MainWindow.setCentralWidget(self.centralwidget)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Most of the code is generated automatically using pyuic5.exe by providing Qt designed file as input,
you can learn the complete steps of doing so by reading the post, Design User Interface in Python
Using PyQt5.
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 6/7
23/05/2021 Get Temperature & Humidity Data on PC Using Python | NodeMCU (Tutorial-7) - Embedded Laboratory
RELATED POSTS:
Design GUI using PyQt5 on Control Devices Using Python and Get Temperature & Humidity Data Getting Started with MQTT Using
Raspberry Pi NodeMCU Over Internet (Tutorial- on PC Using Python | NodeMCU Python
9) (Tutorial-7)
No comments:
Post a Comment
Update the AT Firmware in Your ESP8266 WiFi Displaying Images on 3.2 Inch TFT using Arduino Serial Communication Using Python
Module Embedded Laboratory Apr 29, 2018
Embedded Laboratory Feb 06, 2021
Analog Clock using OLED and PIC Microcontroller Control Devices Using Android Phone and NodeMCU Posting DHT11 Values to ThingSpeak Using
Embedded Laboratory May 27, 2020 over Internet (Tutorial-8) Nodemcu (Tutorial-5)
Embedded Laboratory Mar 04, 2018
Displaying Basic 2D Shapes Using emWin Get Temperature & Humidity Data on PC Using OLED I2C Display Using Microchip PIC
Embedded Laboratory May 23, 2020 Python | NodeMCU (Tutorial-7) Microcontroller
Embedded Laboratory Feb 04, 2018
embeddedlaboratory.blogspot.com/2018/02/get-temperature-humidity-data-on-pc.html 7/7