带有 Mqtt 的 Sim808(AT+CIPSEND)

问题描述 投票:0回答:2

我正在使用 Sim808 GPS 模块发送经纬度并使用 MQTT 将其发送给订阅者。 我按照 here 中的教程通过 Sim808 发布 MQTT 消息,但仍然没有成功。 这是我的示例代码:

def cmd(cmd, ser):
    out = b''; prev = b"101001011"
    ser.flushInput(); ser.flushOutput()
    ser.write(cmd + b'\r');

    while True :
        out += ser.read(1)
        if prev == out:
            return out
        prev = out
    return out

def gprs():
    command1 = cmd(b'AT', ser)  # Check module status
    print(str(command1.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    sleep(1)

    command2 = cmd(b'AT+CPIN?', ser)    # Check if module ready for sms, call,gps, etc.
    print(str(command2.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    sleep(1)

    command3 = cmd(b'AT+CFUN=1', ser)   # Activate GPRS
    print(str(command3.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    sleep(1)

    command4 = cmd(b'AT+CSTT="Internet","",""', ser)    # Setting APN
    print(str(command4.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    while b"ERROR" in command4:
    sleep(3)

    command5 = cmd(b'AT+CIICR', ser)    # Start connection with GPRS
    print(str(command5.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    sleep(1)

    command6 = cmd(b'AT+CIFSR', ser)    # IP Adress from carrier
    print(str(command6.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    sleep(1)

    command7 = cmd(b'AT+CIPSTART="TCP","test.mosquitto.org","1883"', ser)
    print(str(command7.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))

def gprs_send(pub):
    command = cmd(b'AT+CIPSEND', ser)
    print(str(command.replace(b"\r", b"").replace(b"\n", b' '), 'utf-8'))
    if b">" in command:
        pub = cmd(pub, ser)
        return pub

main
(我为
hex-byte
创建了虚拟数据以查看它是否有效):

if __name__ == '__main__' :
    gprs()
    sleep(3)
    pub_msg = b'\x10\x0F\x00\x04\x4D\x51\x54\x54\x04\x02\x00\x3C\x00\x03\x41\x42\x43\x30\x18\x00\x0F\x77\x70\x2D\x69\x6F\x74\x2F\x6F\x62\x64\x32\x2F\x72\x70\x6D\x00\x04\x38\x38\x38\x38\x1A'

    print(gprs_send(pub_msg))

输出:

AT OK 
AT+CPIN? +CPIN: READY  OK  
AT+CFUN=1 OK
AT+CSTT="Internet","","" OK 
AT+CIICR
AT+CIFSR 10.241.141.152 
AT+CIPSTART="TCP","test.mosquitto.org","1883" OK 
AT+CIPSEND > 
b'\x10\x0f\x00\x04MQTT\x04\x02\x00<\x00\x03ABC0\x18\x00\x0fwp-iot/obd2/rpm\x00\x048888\x1a\r'

Subscriber这边只显示这个但是来自Publisher的消息没有出现(消息发布后

Connected with result code 0
出现):

Connecting to broker..
Connected with result code 0

有人熟悉这个案例吗? 任何帮助,将不胜感激。 谢谢。

python mqtt at-command sim800 sim800l
2个回答
0
投票

我一开始也有同样的问题。我开始深入研究 MQTT 标准的规范here,这很有帮助。请务必阅读黄色标记的段落,因为它们包含需要处理的重要异常。

我同意@stene-oh 的评论,即首先使用连接数据包连接到代理(见此处)。这样,传递了一个 keep_alive 值。在该值超时后,与代理的连接将丢失。通过定期发送 PINGREQ 数据包来防止这种情况。 连接到代理后,您可以发布和订阅。规范中还指出,几乎每次违反协议都会导致服务器发起断开连接。因此,无论何时断开连接,请准确检查数据包。

为了更好地调试,我使用 Python 中的

paho
库,并使用 Wireshark 等分析器跟踪网络流量,以将我创建的数据包与库发送的数据包进行比较。当然它也可以将数据显示为十六进制数。


0
投票

嗨,也许你已经找到了解决方案,但我本人非常不成功,最后出于绝望,我尝试了 ChatGPT(是的,你没看错!)...它给出了很多不正确的解决方案,但最终它确实提出了正确的解决方案一个......猜猜你使用的关键字很重要......我也强烈推荐,如果你正在寻找一个,https://mqtthq.com/client 作为一个免费的在线 mqtt,它是非常直观的代理/客户端/ sub 用于检查发布/订阅。反正我不隶属于这个

import machine
import time
import ujson
from umqtt.robust import MQTTClient

# Configure UART2 to communicate with SIM800
uart = machine.UART(2, baudrate=9600, timeout=1000)

# Define MQTT parameters
mqtt_host = "your-mqtt-broker-hostname"
mqtt_port = 1883
mqtt_topic = "your-mqtt-topic"
mqtt_username = "your-mqtt-username"
mqtt_password = "your-mqtt-password"

# Define MQTT message payload
mqtt_payload = {"temperature": 25.5, "humidity": 60}

# Function to send AT command and return response
def send_at_command(cmd):
    uart.write(cmd.encode())
    time.sleep(0.5)
    response = uart.read().decode()
    return response.strip()

# Send AT command to configure SIM800 for MQTT
send_at_command("AT+SMCONF=\"CLIENTID\",\"micropython\"\r\n")
send_at_command("AT+SMCONF=\"USERNAME\",\"" + mqtt_username + "\"\r\n")
send_at_command("AT+SMCONF=\"PASSWORD\",\"" + mqtt_password + "\"\r\n")

# Define MQTT client object
mqtt_client = MQTTClient("micropython", mqtt_host, mqtt_port, mqtt_username, mqtt_password)

# Define callback function for MQTT message received
def on_message(topic, message):
    print("Received MQTT message on topic {}: {}".format(topic, message))

# Connect to MQTT broker and subscribe to topic
mqtt_client.set_callback(on_message)
mqtt_client.connect()
mqtt_client.subscribe(mqtt_topic)

# Publish MQTT message with payload
mqtt_message = ujson.dumps(mqtt_payload)
mqtt_client.publish(mqtt_topic, mqtt_message)

# Wait for MQTT messages
while True:
    mqtt_client.check_msg()
© www.soinside.com 2019 - 2024. All rights reserved.