也可以公开分享的服务器
域名 | 端口号 |
---|---|
test.mosquitto.org | 1883 |
120.79.2.0 | 1883 |
broker.emqx.io | 1883 |
AT+CNACT=0,1
AT+CNACT?
AT+SMDISC
AT+SMCONF="URL","test.mosquitto.org","1883"
设置完成参数后,如果返回erroe,可能等待数秒左右再发送连接服务器指令;如果还是返回erroe,可重启模块再重新设置
AT+SMCONN
AT+SMSUB="mqtt",1
AT+SMPUB="mqtt",5,1,1
AT+SMDISC
AT+CNACT=0,0
配置MQTT.fx 参数
MQTT.fx 连接MQTT服务器
SIM7080连接到同一个MQTT服务器
#!/usr/bin/python
import RPi.GPIO as GPIO
import serial
import time
ser = serial.Serial('/dev/ttyS0',9600)
ser.flushInput()
powerKey = 4
rec_buff = ''
Message = 'www.waveshare.com'
def powerOn(powerKey):
print('SIM7080X is starting:')
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(powerKey,GPIO.OUT)
time.sleep(0.1)
GPIO.output(powerKey,GPIO.HIGH)
time.sleep(1)
GPIO.output(powerKey,GPIO.LOW)
time.sleep(5)
ser.flushInput()
def powerDown(powerKey):
print('SIM7080X is loging off:')
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(powerKey,GPIO.OUT)
GPIO.output(powerKey,GPIO.HIGH)
time.sleep(2)
GPIO.output(powerKey,GPIO.LOW)
time.sleep(5)
print('Good bye')
def sendAt(command,back,timeout):
rec_buff = ''
ser.write((command+'\r\n').encode())
time.sleep(timeout)
if ser.inWaiting():
time.sleep(0.1 )
rec_buff = ser.read(ser.inWaiting())
if rec_buff != '':
if back not in rec_buff.decode():
print(command + ' back:\t' + rec_buff.decode())
return 0
else:
print(rec_buff.decode())
return 1
else:
print(command + ' no responce')
def checkStart():
while True:
# simcom module uart may be fool,so it is better to send much times when it starts.
ser.write( 'AT\r\n'.encode() )
time.sleep(1)
ser.write( 'AT\r\n'.encode() )
time.sleep(1)
ser.write( 'AT\r\n'.encode() )
time.sleep(1)
if ser.inWaiting():
time.sleep(0.01)
recBuff = ser.read(ser.inWaiting())
print('SOM7080X is ready\r\n')
print( 'try to start\r\n' + recBuff.decode() )
if 'OK' in recBuff.decode():
recBuff = ''
break
else:
powerOn(powerKey)
try:
checkStart()
print('wait for signal')
time.sleep(2)
sendAt('AT+CSQ','OK',1)
sendAt('AT+CPSI?','OK',1)
sendAt('AT+CGREG?','+CGREG: 0,1',0.5)
sendAt('AT+CNACT=0,1','OK',1)
sendAt('AT+CNACT?', 'OK',1)
sendAt('AT+SMCONF=\"URL\",120.79.2.0,1883','OK',1)
time.sleep(8)
sendAt('AT+SMCONN','OK',5)
sendAt('AT+SMSUB=\"mqtt\",1','OK',1)
sendAt('AT+SMPUB=\"mqtt\",17,1,0','OK',1)
ser.write(Message.encode())
time.sleep(10);
print('send message successfully!')
sendAt('AT+SMDISC','OK',1)
sendAt('AT+CNACT=0,0', 'OK', 1)
powerDown(powerKey)
except:
if ser != None:
ser.close()
powerDown(powerKey)
GPIO.cleanup()
sudo nano SIM7080_MQTT
sudo python SIM7080_MQTT