利用Python在Rapsberry上监测传感器

2024-04-20 13:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一些帮助来找到最好的方法来编写一个树莓python程序。你知道吗

在这个程序中,我需要监视来自连接到raspberry的各种传感器的几个值,并与pyqt GUI中的这些值进行交互。GUI和传感器驱动程序必须完全分离。你知道吗

为此,我考虑在GUI和传感器之间创建并使用一个“变量管理器”对象,作为“缓冲区”或类似的东西。通过使用线程和锁,传感器将不断更新变量管理器(在相应的驱动程序中使用while循环),而GUI将在我需要时查询特定值。你知道吗

能够从gui向传感器发送一些命令也不错,但我不知道如何在这种配置中做到这一点。你知道吗

你认为这能正常工作吗?有没有其他更有效/更适合这样做的方法?你知道吗

提前多谢了!你知道吗


Tags: 对象方法命令程序管理器驱动程序gui传感器
1条回答
网友
1楼 · 发布于 2024-04-20 13:15:47

我会这样做:

[物理传感器]>;[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!你知道吗

相关问题 更多 >