It looks like yesterday completed the server connection mqtt, as yesterday's array bounds answer to the question, though do not know the specific reasons
Look at today can subscribe and publish news.
To this page http://www.hivemq.com/demos/websocket-client/ connection mqtt, publishing, subscription operation.
However, connect did not respond how it is? Analysis to see what the url I wrong?
~ Liao Liao afraid afraid for js is white, a little more difficult. (Stupid method has been used directly to fill in the data to app.js, or will report a variety of other undefined)
Think about it: http://www.hivemq.com/demos/websocket-client/ the client, in fact, it is opened with a web client, if I write mqtt with python on the client computer, as also You can publish and subscriber. Try ~
Follow the steps on the third day of the first, python on the computer to add another library: paho-mqtt: pip3 install paho-mqtt
Then directly moving bricks, the code on the computer:
mqtt_servo.py
import paho.mqtt.client as mqtt
import time
HOST_IP = 'broker.hivemq.com' # Server的IP地址
HOST_PORT = 1883 # mosquitto 默认打开端口
CLIENT_ID = 'test621'
TOPIC_ID = 'mrjiale' # TOPIC的ID
# 创建一个客户端
client = mqtt.Client(client_id=CLIENT_ID)
# 连接到服务器
client.connect(HOST_IP, HOST_PORT, 60)
startmsg = 'start'
stopmsg = 'stop'
while True:
client.publish(TOPIC_ID, startmsg)
time.sleep(2)
client.publish(TOPIC_ID, stopmsg)
time.sleep(2)
The main code on ESP32:
subcriber.py
from umqtt import MQTTClient
import time
import led
SERVER = 'broker.hivemq.com'
CLIENT_ID = 'test620'
TOPIC = b'mrjiale'
def serve_start():
led.led2_on()
def serve_stop():
led.led2_off()
def mqtt_callback(topic, msg):
global TOPIC
print('topic: {}'.format(topic))
print('msg: {}'.format(msg))
if msg == b"start":
serve_start()
if msg == b"stop":
serve_stop()
def mqtt_connect():
client = MQTTClient(CLIENT_ID, SERVER, port=1883)
client.set_callback(mqtt_callback)
client.connect()
print("mqtt connect success")
client.subscribe(TOPIC)
while True:
client.check_msg()
time.sleep(1)
print("wait ...")
In fact, after the discovery last night, client_id need not be the same as before, only you need to subscribe to a topic or publish the same time to complete communications. When id id on the computer and ESP32 conflict, an error will be automatically dropped on ESP32. -_- ||
Although it will be used, but also sleepwalk, especially mqtt in those properties, or to find a few posts to learn about it.
mqtt entry and introduction: https://www.cnblogs.com/hayasi/p/7708962.html
mqtt details: https://www.jianshu.com/p/ecde412d2eeb
mqtt特殊属性:https://blog.csdn.net/deimon/article/details/88720028
这几篇也算是为我解惑了吧。