MicroPython uses MQTT protocol to access OneNET cloud platform
Previously, Arduino+ESP8266 was successfully connected to OneNET using the EDP protocol to achieve IoT control. This time, the ESP-MP-01 MicroPython development board with ESP8266 onboard was also successfully connected using the MQTT protocol, and smart device control was successfully achieved by publishing TOPIC.
【1】Introduction to MQTT protocol:
MQTT (Message Queuing Telemetry Transport) is a "lightweight" communication protocol based on the publish/subscribe model. The protocol is built on the TCP/IP protocol and was released by IBM in 1999. The biggest advantage of MQTT is that it can provide real-time and reliable message services for connecting remote devices with very little code and limited bandwidth. As an instant messaging protocol with low overhead and low bandwidth usage, it is widely used in the Internet of Things, small devices, mobile applications, etc.
[2] Hardware and software:
The hardware used in this article is the ESP-MP-01 development board:
The software you need to prepare are:
1. ESP_DOWNLOAD_TOOL is used to download the firmware
of the development board; 2. uPyLoader is used to transfer files with the development board. At the same time, this software also has the function of terminal software debugging. The use and introduction of this software can be viewed at:
https://github.com/BetaRavener/uPyLoader/
;
3. Third-party library file: umqtt, this module encapsulates the MQTT protocol, this module can be found at:
https://github.com/micropython/micropython-lib
.
[3] Create products and devices:
Create products with MQTT public protocol on the OneNET platform and add devices:
【4】Program code:
1. Use ESP_DOWNLOAD_TOOL tool to download Micropython firmware to the development board. Firmware:
2. Open the command line window and enter python main.py in the uPyLoader directory to open the uPyLoader software. To run the software, you need to use pip to install PyQt5 and pyserial;
3. Select the serial port connected to the development board. After the connection is successful, click File->Init transfer files to initialize the transfer file. After the initialization is successful, two files _upload.py and _download.py will be added on the MCU side:
4. Edit boot.py, mqtt.py and main.py, and transfer the three edited files and the simple.py file in the umqtt directory to the MCU;
boot.py is used to set up and connect to the network:
-
def do_connect():
-
import network
-
sta_if = network.WLAN(network.STA_IF)
-
ap_if = network.WLAN(network.AP_IF)
-
if ap_if.active():
-
ap_if.active(False)
-
if not sta_if.isconnected():
-
print('connecting to network...')
-
sta_if.active(True)
-
sta_if.connect('MicroPython', '12345678') #wifi SSID and password
-
while not sta_if.isconnected():
-
pass
-
print('network config:', sta_if.ifconfig())
-
do_connect()
-
gc.collect()
-
Copy code
mqtt.py is used for platform access and message processing:
-
from simple import MQTTClient
-
from machine import Pin
-
import machine
-
import micropython
-
# ESP8266 ESP-12 modules have blue, active-low LED on GPIO2, replace
-
# with something else if needed.
-
led = Pin(2, Pin.OUT, value=1)
-
# Default MQTT server to connect to
-
SERVER = "183.230.40.39"
-
CLIENT_ID = "4070825"
-
TOPIC = b"micropython1"
-
username='76013'
-
password='micropythonespmp01'
-
state = 0
-
def sub_cb(topic, msg):
-
global state
-
print((topic, msg))
-
if msg == b"on":
-
led.value(0)
-
state = 1
-
print("1")
-
elif msg == b"off":
-
led.value(1)
-
state = 0
-
print("0")
-
elif msg == b"toggle":
-
# LED is inversed, so setting it to current state
-
# value will make it toggle
-
led.value(state)
-
state = 1 - state
-
def main(server=SERVER):
-
c = MQTTClient(CLIENT_ID, server,6002,username,password)
-
# Subscribed messages will be delivered to this callback
-
c.set_callback(sub_cb)
-
c.connect()
-
c.subscribe(TOPIC)
-
print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
-
try:
-
while 1:
-
#micropython.mem_info()
-
c.wait_msg()
-
finally:
-
c.disconnect()
-
Copy code
main.py is used to run the program:
-
import mqtt
-
mqtt.main()
Copy code
simple.py does not need to be modified.
[5] Connection test:
Open the uPyLoader terminal window, and after resetting the development board, you can see that the wireless network and OneNET server are successfully connected:
Send the MQTT command. Sending the on command turns on the LED on the development board. Sending the off command turns off the LED on the development board.
Welcome to click to read the original text and communicate with the author.
Follow EEWORLD (Electronic Engineering World) WeChat public account: reply "Submit" and your original work will have a chance to appear in our WeChat push.
To communicate with more netizens in the industry, please click to read the original text and log in to the EEWORLD forum.
WeChat ID : EEWORLD
Hot PostsFollow EEWORLD service account
EE benefits
WeChat ID: EEXINRUI
Chip Information Sharp Interpretation