MQTT with MicroPython on ESP32
MicroPython can directly send and receive MQTT on ESP32, which is very convenient.
Install MQTT library on ESP32¶
First, we need to install the mqtt library on the ESP32. (The implementation of MQTT client on ESP32)
First make sure that the ESP32-MicroPython is connected to the hotspot! ! ! , control ESP32 through REPL.
Introduce the upip
package manager:
>>> import upip >>> upip.install('micropython-umqtt.simple') Installing to: /lib/ Installing micropython-umqtt.simple 1.3.4 from https://files.pythonhosted.org/packages/bd/cf/697e3418b2f44222b3e848078b1e33ee76aedca9b6c2430ca1b1aec1ce1d/micropython-umqtt.simple-1.3.4.tar.gz
In this way, the umqtt.simple package is installed.
View the IP address of the Server¶
Check the current IP of the PC, and execute the command in the command line of Ubuntu (as Server):
ifconfig ➜ Download ifconfig enp3s0 Link encap: ethernet hardware address 5c:f9:dd:49:4b:ad UP BROADCAST MULTICAST MTU: 1500 Hops: 1 Received packets: 0 Errors: 0 Dropped: 0 Overload: 0 Frames: 0 Packets sent: 0 Errors: 0 Dropped: 0 Overload: 0 Carrier: 0 Collision: 0 Send Queue Length: 1000 Receive Byte: 0 ( 0.0 B ) Send Byte: 0 ( 0 .0 B ) Interrupts: 16 lo Link encap: local loopback inet address: 127.0.0.1 mask: 255.0.0.0 inet6 address: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU: 65536 Hops: 1 Received packets: 256668 Errors: 0 Dropped: 0 Overload: 0 Frames: 0 Packets sent: 256668 Errors: 0 Dropped: 0 Overload: 0 Carrier: 0 Collision: 0 Send Queue Length: 1000 Received Bytes: 138568580 ( 138.5 MB ) Sended Bytes: 138568580 ( 138.5 MB ) wlp2s0 Link encap: ethernet hardware address 68 :5d:43:ec:d3:58 inet address: 192.168.43.16 broadcast: 192.168.43.255 mask: 255.255.255.0 inet6 address: fe80::47ef:2ce1:f8e9:b0c2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU: 1500 跃 Score: 1 Received packets: 146459 Errors: 0 Dropped: 0 Overload: 0 Frames: 0 Packets sent: 137348 Errors: 0 Dropped: 0 Overload: 0 Carrier: 0 Collision: 0 Send Queue Length: 1000 Received Bytes: 147948142 ( 147.9 MB ) Sended Bytes: 20083083 ( 20.0 MB )
192.168.43.16 The IP address of the current PC in the LAN is
Implement the receiver using umqtt
esp32/subscriber.py from umqtt.simple import MQTTClient import time SERVER = '192.168.43.16' CLIENT_ID = 'PYESPCAR_A0' TOPIC = b'pyespcar_basic_control' def mqtt_callback(topic, msg): print('topic: {}'.format(topic)) print('msg: {}'.format(msg)) client = MQTTClient(CLIENT_ID, SERVER) client.set_callback(mqtt_callback) client.connect() client.subscribe(TOPIC) while True : # Check if there is data incoming # If yes, execute mqtt_callback client . check_msg () time . sleep ( 1 )
Implement sender using umqtt
esp32/publisher.py from umqtt.simple import MQTTClient import time SERVER = '192.168.43.16' CLIENT_ID = 'PYESPCAR_A0' # ID of the client TOPIC = b 'pyespcar_basic_control' # ID of TOPIC client = MQTTClient(CLIENT_ID, SERVER) client.connect() while True: client.publish(TOPIC, 'helloworld') time.sleep(1)
Note that in Esp32 it TOPIC
needs to be a bytes
type.
Comprehensive Experiment
You can test with the sender in paho-mqtt and the receiver in esp32.
It is also possible to test with the receiver in paho-mqtt and the sender in esp32.