开发者社区> 问答> 正文

使用app inventor通过url向Paho MQTT python发送消息

我通过app inventor发送http消息 通过发送url像图片和下面的代码 我想知道MQTT是否可以接收相同类型的url或JSON 连接客户端并发送消息

from http.server import BaseHTTPRequestHandler, HTTPServer
class RequestHandler_httpd(BaseHTTPRequestHandler):
    def do_GET(self):
        global Request, test, data1, data2, state, case
        messagetosend = bytes('test', "utf")
        self.send_response(200)
        self.send_header('Content-Type', 'text/plain')
        self.send_header('Content-Length', len(messagetosend))
        self.end_headers()
        self.wfile.write(messagetosend)
        Request = self.requestline
        Request = Request[5: int(len(Request)-9)]
        return
    server_address_httpd = ('192.168.66.19', 8080)
    httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
    httpd.serve_forever()

下面是我尝试使用的带有MQTT客户机的python代码

mqtt.Client(transport="websockets")

但我不确定这是否有效

import paho.mqtt.client as mqtt
from test1 import abc
import datetime

def on_connect(clien, userdata, flags, rc):
    print("connected with" + str(rc))
    client.subscribe("epaper/headphoto")
def on_message(client, userdata, msg):
    print(msg.topic + "" + str(msg.payload))
if __name__ == '__main__':
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect("loclhost", 1883, 60)
    client.loop_forever()

问题来源StackOverflow 地址:/questions/59384586/using-app-inventor-sending-message-to-paho-mqtt-python-by-url

展开
收起
kun坤 2019-12-26 14:35:12 2509 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
RocketMQ Client-GO 介绍 立即下载
RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载
基于 RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载