Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. DHT11 Sensor with RPI

DHT11 Sensor with RPI

Scheduled Pinned Locked Moved Unsolved General and Desktop
graphrpidht11
13 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    helloworldddd
    wrote on last edited by helloworldddd
    #1

    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_())
    
    jsulmJ 1 Reply Last reply
    0
    • H helloworldddd

      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_())
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        H Offline
        H Offline
        helloworldddd
        wrote on last edited by
        #3

        @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 !

        jsulmJ 1 Reply Last reply
        0
        • H helloworldddd

          @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 !

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          H 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            H Offline
            H Offline
            helloworldddd
            wrote on last edited by
            #5

            @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 ?

            jsulmJ 1 Reply Last reply
            0
            • H helloworldddd

              @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 ?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              H 1 Reply Last reply
              0
              • jsulmJ jsulm

                @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?

                H Offline
                H Offline
                helloworldddd
                wrote on last edited by
                #7

                @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?

                jsulmJ 1 Reply Last reply
                0
                • H helloworldddd

                  @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?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  H 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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?

                    H Offline
                    H Offline
                    helloworldddd
                    wrote on last edited by
                    #9

                    @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

                    jsulmJ 1 Reply Last reply
                    0
                    • H helloworldddd

                      @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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @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.

                        H Offline
                        H Offline
                        helloworldddd
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @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.

                          H Offline
                          H Offline
                          helloworldddd
                          wrote on last edited by
                          #12

                          @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)
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • H helloworldddd

                            @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)
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved