传感器从覆盆子读取

时间:2017-02-03 22:12:14

标签: python pyqt raspberry-pi pyqt4 qthread

我有以下代码。此代码仅显示运行代码时的传感器读数。它不会自动更新。任何人都可以帮助让传感器实时读取吗?

<?php global $display_name , $user_email;
  get_currentuserinfo();

  echo $display_name . "'s email address is: " . $user_email;
?>

1 个答案:

答案 0 :(得分:1)

您的代码只读取传感器一次,因此它永远不会更改,实现一个线程,以便读取不会中断主线程。

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

import Adafruit_DHT


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

    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()