Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

DHT11 Sensor with RPI



  • hello, i am currently working on my side hobby. im fairly new with pyqt, and im stuck in this step for quite sometime. ive created a UI showing the temperature and humidity. i wanted to show more. i want to display a graph on the UI which logs the temperature and humidity every 20 minutes, plotting the average of 20 mins. ive known about plotwidget and matplotlib, but i still cant figure out on how to get the data and draw out the graph. im kinda new to this whole thing. please do be patient with me im just a kid that needs help thanks in advanced !

    this is the code i currently have

    import Adafruit_DHT
    import time
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import *
    from pyqtgraph import PlotWidget
    
    sensor = Adafruit_DHT.DHT11
    pin = 4
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(634, 428)
            self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
            self.lcdNumber.setGeometry(QtCore.QRect(220, 40, 271, 131))
            self.lcdNumber.setObjectName("lcdNumber")
            self.lcdNumber_2 = QtWidgets.QLCDNumber(Dialog)
            self.lcdNumber_2.setGeometry(QtCore.QRect(220, 200, 271, 131))
            self.lcdNumber_2.setObjectName("lcdNumber_2")
            self.label = QtWidgets.QLabel(Dialog)
            self.label.setGeometry(QtCore.QRect(26, 42, 161, 131))
            font = QtGui.QFont()
            font.setPointSize(16)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.label_2 = QtWidgets.QLabel(Dialog)
            self.label_2.setGeometry(QtCore.QRect(30, 190, 161, 131))
            font = QtGui.QFont()
            font.setPointSize(16)
            self.label_2.setFont(font)
            self.label_2.setObjectName("label_2")
            self.widget = PlotWidget(Dialog)
            self.widget.setGeometry(QtCore.QRect(510, 40, 391, 131))
            self.widget.setObjectName("widget")
            self.widget_2 = PlotWidget(Dialog)
            self.widget_2.setGeometry(QtCore.QRect(510, 200, 391, 131))
            self.widget_2.setObjectName("widget_2")
            self.timer = QTimer()
            self.timer.setInterval(5000)
            self.timer.timeout.connect(self.DHTread)
            self.timer.start()
            
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.label.setText(_translate("Dialog", "Temperature :"))
            self.label_2.setText(_translate("Dialog", "Humidity :"))
            
        def DHTread(self):
            humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
            print('Temp =', temperature,'*C Humidity =', humidity)
            self.lcdNumber.display(temperature)
            self.lcdNumber_2.display(humidity)
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

    @helloworldddd What exactly does not work? Is DHTread(self) called? If so, what does it print?



  • @jsulm i cant figure out on how to plot a graph and display it with PlotWidget and pyqtgraph with the values taken from the sensor !


  • Lifetime Qt Champion

    @helloworldddd I never used PlotWidget or pyqtgraph and those are not part of Qt.
    You should consider reading some documentation/tutorial like https://www.learnpyqt.com/courses/graphics-plotting/plotting-pyqtgraph/
    Or check https://doc.qt.io/qt-5/qtcharts-index.html which is part of Qt.



  • @jsulm ive also read up on using .csv files to plot the graph by using the values recorded. the examples shown were only fixed values. how do i plot the graph with values that just being recorded ?


  • Lifetime Qt Champion

    @helloworldddd said in DHT11 Sensor with RPI:

    how do i plot the graph with values that just being recorded ?

    Well, use the recorded values instead of the values from csv. I really don't get what exactly the problem is! Can you please be more specific?



  • @jsulm how do i get the recorded values and put it into a list and limit the list to like in 2 hour intervals with 20 mins as every point. and the time, which is the x axis to self increment?


  • Lifetime Qt Champion

    @helloworldddd said in DHT11 Sensor with RPI:

    how do i get the recorded values and put it into a list and limit the list to like in 2 hour intervals with 20 mins as every point

    What is your level of experience with programming? Do you know how you can add values to a list in Python? Please don't forget that this is Qt forum not Python... "limit the list to like in 2 hour intervals with 20 mins" - this is simple math to calculate how many elements the list should contain (max).

    You never answered my question: "Is DHTread(self) called? If so, what does it print?" Can you please answer it?



  • @jsulm like i said im just a newbie with programming. was hoping to find some help here. which incase guess i came to the wrong place. DHTread(self) is called. it just shows the value onto the UI through lcdnumber display


  • Lifetime Qt Champion

    @helloworldddd said in DHT11 Sensor with RPI:

    DHTread(self) is called

    That is good. Now you can add the values to a list (myList.append(myValue)). But it is better to use https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s19.html in your case as you want to have a list with fixed max size and remove old values if you insert new values into full list.



  • @jsulm ahhh u could have started with that but thanks so much for your help !



  • @jsulm hello, sorry to bother u again. do u know how to use plot values to time, time as x axis on pyqtgraph? im using the command self.widget.plot() i would like to input the x axis value as time instead of a list of number like below. Thanks in advance !

    hour = [1,2,3,4,5,6,7,8,9,10]
    reading = []  #this is the reading from the sensor
    
    self.widget.plot(hour, reading)
    

  • Lifetime Qt Champion

    @helloworldddd said in DHT11 Sensor with RPI:

    do u know how to use plot values to time, time as x axis on pyqtgraph?

    Sorry, I never used pyqtgraph. Did you check its documentation?


Log in to reply