在Rapsberry上使用Python进行传感器监控

时间:2018-07-06 12:06:16

标签: python multithreading pyqt raspberry-pi iot

我希望获得一些帮助,以找到编码Raspberry python程序的最佳方法。

在此程序中,我需要监视来自连接到树莓派的各种传感器的多个值,并通过pyqt GUI与这些值进行交互。 GUI和传感器驱动程序必须完全分开。

为此,我正在考虑在GUI和传感器之间制作和使用“变量管理器”对象,作为“缓冲区”或类似的对象。通过使用线程和锁,传感器将连续更新变量管理器(相应驱动程序中的while循环),而GUI会在需要时查询某些值。

能够从gui发送一些命令到传感器也很好,但是我不知道如何在此配置中做到这一点。

您认为这可以正常工作吗?还有其他方法可以更高效/更适应此要求吗?

非常感谢您!

1 个答案:

答案 0 :(得分:1)

我会这样:

[物理传感器]-> [Python传感器监视器]-> [PubNub /类似]

# Not tested, probably not good
from threading import Thread
from pubnub import PubNub

class SensorMonitor(threading.Thread):
  def __init__(self):
    self.pubnub = PubNub("demo", "demo")
    self.switch = True

  def _on_receive(self, sensor_data):
    # can also do async() with a callback - https://www.pubnub.com/docs/python/data-streams-publish-and-subscribe
    self.pubnub.publish().channel('raw_data').message(sensor_data).sync()

  @staticmethod
  def _poll_sensor(address):
    data = get_sensor_data(address)
    return data

  def run(self):
    while self.switch:
      _data = poll_sensor('my_address')
      self._on_receive(_data)
      time.sleep(1)

  def terminate(self):
    self.switch = False

monitor = SensorMonitor()
monitor.start()

然后,您的GUI可以使用PubNub使用者获取流式传输的数据并检查/执行操作。您可以在这里使用JS或Python!

相关问题