sam sam - 4 years ago 127
Python Question

sensor reading from raspberry

I have the following code. This code only shows the sensor reading at the time of running of the code.It is not updating automatically. Can anyone help to make the sensor reading real time?

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import Adafruit_DHT
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }

humidity, temperature = Adafruit_DHT.read_retry(11, 4)

def main():
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()

tabs.setWindowTitle('Weather')
tabs.resize(250, 150)

vBoxlayout1 = QtGui.QVBoxLayout()
vBoxlayout2 = QtGui.QVBoxLayout()
vBoxlayout1.addWidget(QtGui.QLabel('Temp={:0.1f}'.format(temperature) ))
vBoxlayout2.addWidget(QtGui.QLabel('Humidity={:0.1f}%'.format(humidity) ))
vBoxlayout1.setAlignment(QtCore.Qt.AlignCenter)
vBoxlayout2.setAlignment(QtCore.Qt.AlignCenter)

tab1.setLayout(vBoxlayout1)
tab2.setLayout(vBoxlayout2)

tabs.addTab(tab1,"Temp")
tabs.addTab(tab2,"Humidity")

print 'Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)

tabs.show()

sys.exit(app.exec_())

if ___name___ == '___main___':
main()

Answer Source

Your code only reads the sensor once so it never changes, implement a thread so that the reading does not interrupt the main thread.

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

import Adafruit_DHT


class Thread(QtCore.QThread):
    data_sensors = QtCore.pyqtSignal(tuple)

    def __init__(self, parent=None):
        super(Thread, self).__init__(parent=parent)

    def run(self):
        while True:
            humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
            self.data_sensors.emit((humidity, temperature))


class TabWidget(QtGui.QTabWidget):
    def __init__(self, parent=None):
        super(TabWidget, self).__init__(parent=parent)
        tab1 = QtGui.QWidget()
        tab2 = QtGui.QWidget()

        self.setWindowTitle('Weather')
        self.resize(250, 150)

        vBoxlayout1 = QtGui.QVBoxLayout()
        vBoxlayout2 = QtGui.QVBoxLayout()
        self.lTemp = QtGui.QLabel('Temp={:0.1f}'.format(0))
        vBoxlayout1.addWidget(self.lTemp)
        self.lHumidity = QtGui.QLabel('Humidity={:0.1f}%'.format(0))
        vBoxlayout2.addWidget(self.lHumidity)

        vBoxlayout1.setAlignment(QtCore.Qt.AlignCenter)
        vBoxlayout2.setAlignment(QtCore.Qt.AlignCenter)

        tab1.setLayout(vBoxlayout1)
        tab2.setLayout(vBoxlayout2)

        self.addTab(tab1, "Temp")
        self.addTab(tab2, "Humidity")

        thread = Thread(self)
        thread.data_sensors.connect(self.update_Sensors)
        thread.start()

    def update_Sensors(self, data):
        humidity, temperature = data
        self.lTemp.setText('Temp={:0.1f}'.format(temperature))
        self.lHumidity.setText('Humidity={:0.1f}%'.format(humidity))


def main():
    app = QtGui.QApplication(sys.argv)
    tabs = TabWidget()
    tabs.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download