Article count:10827 Read by:13690842

Featured Content
Account Entry

MicroPython uses MQTT protocol to access OneNET cloud platform

Latest update time:2016-11-23
    Reads:

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: firmware_v1.8.5-103_ESP8266_MPLR_v1.2.zip (Click to read the original text and download)

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:

  1. def do_connect():

  2. import network

  3. sta_if = network.WLAN(network.STA_IF)

  4. ap_if = network.WLAN(network.AP_IF)

  5. if ap_if.active():

  6. ap_if.active(False)

  7. if not sta_if.isconnected():

  8. print('connecting to network...')

  9. sta_if.active(True)

  10. sta_if.connect('MicroPython', '12345678') #wifi SSID and password

  11. while not sta_if.isconnected():

  12. pass

  13. print('network config:', sta_if.ifconfig())

  14. do_connect()

  15. gc.collect()


Copy code


mqtt.py is used for platform access and message processing:

  1. from simple import MQTTClient

  2. from machine import Pin

  3. import machine

  4. import micropython

  5. # ESP8266 ESP-12 modules have blue, active-low LED on GPIO2, replace

  6. # with something else if needed.

  7. led = Pin(2, Pin.OUT, value=1)

  8. # Default MQTT server to connect to

  9. SERVER = "183.230.40.39"

  10. CLIENT_ID = "4070825"

  11. TOPIC = b"micropython1"

  12. username='76013'

  13. password='micropythonespmp01'

  14. state = 0

  15. def sub_cb(topic, msg):

  16. global state

  17. print((topic, msg))

  18. if msg == b"on":

  19. led.value(0)

  20. state = 1

  21. print("1")

  22. elif msg == b"off":

  23. led.value(1)

  24. state = 0

  25. print("0")

  26. elif msg == b"toggle":

  27. # LED is inversed, so setting it to current state

  28. # value will make it toggle

  29. led.value(state)

  30. state = 1 - state

  31. def main(server=SERVER):

  32. c = MQTTClient(CLIENT_ID, server,6002,username,password)

  33. # Subscribed messages will be delivered to this callback

  34. c.set_callback(sub_cb)

  35. c.connect()

  36. c.subscribe(TOPIC)

  37. print("Connected to %s, subscribed to %s topic" % (server, TOPIC))

  38. try:

  39. while 1:

  40. #micropython.mem_info()

  41. c.wait_msg()

  42. finally:

  43. c.disconnect()


Copy code


main.py is used to run the program:

  1. import mqtt

  2. 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 Posts
Follow EEWORLD service account
EE benefits
WeChat ID: EEXINRUI
Chip Information Sharp Interpretation

Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号