MQTT is a message protocol based on the publish/subscribe programming mode of binary messages; due to its simple specification, it is very suitable for IoT scenarios that require low power consumption and limited network bandwidth, such as smart homes, smart cities, and medical care; it is widely used by children’s shoes. Favored and loved. Let’s take SIM820X as an example to implement MQTT communication:
Hardware
Sofeware
- minicom
sudo apt-get install minicom
Communication steps
1.Hareware connection
- Connect the NB card and antenna to SIM8200EA-M2_5G_HAT
- Connect Raspberry Pi 4 Model and SIM8200EA-M2_5G_HATvia USB cable
Configure Alibaba Cloud (it is recommended to change SIM7020 to SIM8200)
-
Enter the Alibaba Cloud IoT platform. If you have not purchased the product, you need to purchase the product. The enterprise version is free for the first month.
-
Create a product
-
Set DeviceName, check the device after completion, check the DeivceSecret of the device, and change these values to the values for the MQTT code
-
Triple preparation, the following steps are useful to copy and paste directly in the past
{
"ProductKey": "a1mQpKOF9hp",
"DeviceName": "7600",
"DeviceSecret": "8f663b388dee796b931ebcb789b75a39"
}
-
To register a topic, click Products–>View 7020_test–>Topic Categories–>Edit Topic Category–>Select Publish and Subscribe
-
The triplet gets the password through the Password Tool
Analysis of key instructions
- Open mqtt service
at+cmqttstart
- Apply for a client
at+cmqttaccq=0,"7600|securemode=3,signmethod=hmacsha1|"
- Connect to mqtt server
at+cmqttconnect=0,"tcp://a1zjhbLfSbW.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883",60,1,"7600&a1mQpKOF9hp","D48150E8D550607B15C667E100635EE96EA7D512"
- Subscribe
AT+CMQTTSUB=0,27,1,1
- After the prompt > appears, enter:
/a1mQpKOF9hp/7600/user/7600
- Enter the subject of the message to post
AT+CMQTTTOPIC=0,27
- After the prompt > appears, enter:
/a1mQpKOF9hp/7600/user/7600
- Post a message (send a 5-character message to)
AT+CMQTTPAYLOAD=0,5
- After the prompt > appears, enter:
hello
- Complete sending
AT+CMQTTPUB=0,1,60
- Unsubscribe from topic
AT+CMQTTUNSUB=0,28,1
- Disconnect from the server
AT+CMQTTDISC=0,60
- release a client
AT+CMQTTREL=0
- Close the mqtt service
AT+CMQTTSTOP
Sample program
- Key code
#!/usr/bin/python
import serial
import time
ser = serial.Serial("/dev/ttyUSB2",115200)
ser.flushInput()
rec_buff = ''
mg1='/a1mQpKOF9hp/7600/user/7600'
mg2='/a1mQpKOF9hp/7600/user/7600'
mg3='hello'
def send_at(command,back,timeout):
rec_buff = ''
ser.write((command+'\r\n').encode())
time.sleep(timeout)
if ser.inWaiting():
time.sleep(0.01 )
rec_buff = ser.read(ser.inWaiting())
if back not in rec_buff.decode():
print(command + ' ERROR')
print(command + ' back:\t' + rec_buff.decode())
return 0
else:
print(rec_buff.decode())
return 1
def MQTT():
# connect to Ali cloud
send_at('at+cmqttstart','OK',1)
send_at('at+cmqttaccq=0, \"7600|securemode=3,signmethod=hmacsha1| \"', 'OK', 1)
send_at('at+cmqttconnect=0, \"tcp://a1zjhbLfSbW.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883 \",60,1, \"7600&a1mQpKOF9hp\",\"D48150E8D550607B15C667E100635EE96EA7D5$
send_at('AT+CMQTTSUB=0,27,1,1','OK',1)
ser.write(mg1.encode())
send_at('AT+CMQTTTOPIC=0,27','OK',1)
ser.write(mg2.encode())
send_at('AT+CMQTTPAYLOAD=0,5','OK',1)
ser.write(mg3.encode())
send_at('AT+CMQTTPUB=0,1,60','OK',1)
send_at('AT+CMQTTUNSUB=0,28,1','OK',1)
send_at('AT+CMQTTDISC=0,60','OK',1)
#Stop MQTT
send_at('AT+CMQTTREL=0','OK',1)
send_at('AT+CMQTTSTOP','OK',1)
try:
# print('Sending Message Test:')
MQTT()
except :
if ser != None:
ser.close()
- Operation result