0

i am using Sim808 GPS Module to send lat-long and using MQTT to send it to subscriber. I followed tutorial from here to publish MQTT message over Sim808 but still no luck. Here is my sample code:

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

At main (I created dummy data for hex-byte to see if it works):

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))

And the output:

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'

In Subscriber's side only show this but message from Publisher does not appear (The Connected with result code 0 appeared after message published):

Connecting to broker..
Connected with result code 0

Anyone familiar with this case? Any help would be appreciated. Thank you.

  • I know this isn't a "perfect match" for your situation, but I think there may be something that could help here: https://www.youtube.com/watch?v=dcpMa2UwEeA&t=513s – Monty Jan 16 '21 at 10:41
  • 1
    I think the problem is that you make a TCP connection with the broker, but don't send the mqtt connect package. Here is an example of this package: https://www.digi.com/resources/documentation/Digidocs/90001541/reference/r_example_mqtt.htm If you already solved the problem, I am eager to know the solution as I am currently trying something similar. Hope this was any help. – stene-oh Mar 02 '21 at 08:44

1 Answers1

0

I had the same issue in the beginning. I started to dig deeper into the specification of the MQTT standard here which helped a lot. Make sure to read the yellow marked paragraphs as they contain important exceptions to handle.

I agree with the comment from @stene-oh to first connect to the broker using the Connect Packet (see here). With that, a keep_alive value is passed. The connection to the broker will be lost after the timout of that value. Prevent this by sending a PINGREQ Packet regularly. After connecting to the broker you are allowed to publish and subscribe. Also stated in the specification, almost every protocol violation leads to a disconnect initiated by the server. So whenever you are being disconnected, check the packet precisely.

For better debugging I use the paho library in Python and trace the network traffic with an analyzer like Wireshark to compare the packets I created with the ones sent by the library. Of course it can show the data as hexadecimal numbers too.

Wiresharks Network Analyzer shows MQTT packet fields

CodeSocke
  • 91
  • 1
  • 7