使用 MQTT 和 Python 控制程序 [英] Controlling Program with MQTT and Python

查看:91
本文介绍了使用 MQTT 和 Python 控制程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哟,伙计们.所以我对 Python 比较陌生,并且是 MQTT 的完全新手.所以,我试图通过 MQTT 建立两个程序的简单连接.其中一个程序是发布者:

Yo, guys. So I'm relatively new to Python and a complete newbie with MQTT. So, I'm trying to make a simple connection of two programs via MQTT. One of the programs is the publisher:

   import paho.mqtt.client as mqtt
   import sys, tty, termios 
   ## Publisher reads a keyboard input 
   def getch():
       fd = sys.stdin.fileno()
       old_settings = termios.tcgetattr(fd)
       try:
           tty.setraw(sys.stdin.fileno())
           ch = sys.stdin.read(1)
       finally:
           termios.tcsetattr(fd,termios.TCSADRAIN, old_settings)
           return ch

   while True:
   ##Publisher connects to MQTT broker
       mqttc= mqtt.Client("python_pub")
       mqttc.connect("iot.eclipse.org", 1883)
       char= getch()
       mqttc.publish("Labbo/control", str(char))
       mqtt.Client()

因此,基本上发布者读取一个键输入并将其发送给代理.客户端程序应该读取击键并做出相应的反应:

So, basically the publisher reads a key input and sends it to the broker. And the client program is supposed to read the key stroke and react accordingly:

   import paho.mqtt.client as mqtt

   def on_connect(client, userdata, flags, rc):
       print("Connected with result code "+str(rc))
       client.subscribe("Labbo/control")

   def on_message(client, userdata, msg):
       print(msg.topic+" "+str(msg.payload))
   ## v v PROBLEM LINE v v ## 
   char=str(msg.payload)
   ## ^ ^ PROBLEM LINE ^ ^ ##
   client = mqtt.Client()
   client.on_connect = on_connect
   client.on_message = on_message  
   client.connect("iot.eclipse.org", 1883, 60)
   ##The program just needs to close itself upon entering "x" on the Publisher
   while True:
       if char=="x":
          break

这是一个简单的测试程序,但我在尝试读取"MQTT 负载时遇到了很多麻烦.

This is a simple test program but I've been having a lot of trouble trying to "read" the MQTT payload.

推荐答案

您的订阅者代码正在循环,但没有做任何有效的工作.必须改成如下

You Subscriber code is looping without doing anything productive. It must be changed as follows

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
   print("Connected with result code "+str(rc))
   client.subscribe("Labbo/control")

def on_message(client, userdata, msg):
   print(msg.topic+" "+str(msg.payload))
   char = str(msg.payload)
   if char == 'x':
       client.disconnect()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.eclipse.org", 1883, 60)
client.loop_forever()

您的发布者代码也是如此,它创建了一个新客户端来发送一封信,这有点矫枉过正

So is your Publisher code where it create a new client to send a single letter which is kind of a overkill

import paho.mqtt.client as mqtt
import sys, tty, termios
## Publisher reads a keyboard input 
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd,termios.TCSADRAIN, old_settings)
    return ch


##Publisher connects to MQTT broker
mqttc= mqtt.Client("python_pub")
mqttc.connect("iot.eclipse.org", 1883)
mqttc.loop_start()

while True:
    char= getch()
    mqttc.publish("Labbo/control", str(char))

这篇关于使用 MQTT 和 Python 控制程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆