MQTT学习笔记—SIM7080 mosquitto 之旅

 搭建mosquitto_MQTT服务器

也可以公开分享的服务器

域名 端口号
test.mosquitto.org

1883

120.79.2.0

1883

broker.emqx.io

1883

关键指令

  • 激活网络应用
AT+CNACT=0,1
  • 查询网络IP
AT+CNACT?
  • 断开原来的MQTT连接
AT+SMDISC
  • 设置MQTT参数
AT+SMCONF="URL","test.mosquitto.org","1883"

设置完成参数后,如果返回erroe,可能等待数秒左右再发送连接服务器指令;如果还是返回erroe,可重启模块再重新设置

  • 连接服务器
AT+SMCONN
  • 订阅主题
AT+SMSUB="mqtt",1
  • 发送消息
AT+SMPUB="mqtt",5,1,1
  • 断开MQTT连接
AT+SMDISC
  • 断开网络
AT+CNACT=0,0

通信之旅

  • 配置MQTT.fx 参数

 MQTT学习笔记—SIM7080 mosquitto 之旅_第1张图片

  • MQTT.fx 连接MQTT服务器

 MQTT学习笔记—SIM7080 mosquitto 之旅_第2张图片

  • SIM7080连接到同一个MQTT服务器

MQTT学习笔记—SIM7080 mosquitto 之旅_第3张图片

  • 示例程序
#!/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

MQTT学习笔记—SIM7080 mosquitto 之旅_第4张图片

你可能感兴趣的:(mqtt,python,阿里云服务器)