Python AT MQTT Commands with SimComm7600 to AWS IOT

234 Views Asked by At

I'm using micropython on a esp32-s2 that is connected over uart to a Simcomm7600g-h module. I'm trying to utilize the MQTT functions of the module but am having troubles. I have provisioned the cellular module with security cert, private key, and amazonroot permission already. However, i cannot get it to connect. I'm suspecting that i have an execution order or syntax error somewhere. I have attempted to break it out as best of possible. Any assistance is appreciated. See below:

def cellular_publish(topic, msg):
    global uart

    try:
        # Start the MQTT service
        if not send_at_command(uart, "AT+CMQTTSTART"):
            print("Error: Failed to start the MQTT service")
            return

        # Acquire an MQTT client
        client_id = "aerial_58cf79a5a6ae"  # Replace "test_device" with the correct AWS Thing topic
        if not send_at_command(uart, f'AT+CMQTTACCQ=0,"{client_id}",1'):
            print("Error: Failed to acquire MQTT client")
            return

        # Set the SSL context
        if not send_at_command(uart, "AT+CMQTTSSLCFG=0,0"):
            print("Error: Failed to set SSL context")
            return

        # Connect to the MQTT server
        mqtt_endpoint = "tcp://blah-blah-blah-blah.amazonaws.com:8883"
        if not send_at_command(uart, f'AT+CMQTTCONNECT=0,"{mqtt_endpoint}",60,1'):
            print("Error: Failed to connect to the MQTT server")
            return
        # Wait for 5 seconds before checking the connection status
        time.sleep(5)

        # Check MQTT connection status
        response = send_at_command(uart, "AT+CMQTTCONNECT?", get_response=True)
        if response is not None:
            match = re.search(r'\+CMQTTCONNECT: (\d)', response)
            if match:
                connection_status = int(match.group(1))
                if connection_status == 2:
                    print("MQTT connection established")
                elif connection_status == 0:
                    print("MQTT connection is closed")
                elif connection_status == 1:
                    print("MQTT connection is in progress")
                elif connection_status == 3:
                    print("MQTT connection is closing")
                elif connection_status == 4:
                    print("MQTT connection is disconnected due to network issues")
                else:
                    print("Unknown MQTT connection status")
            else:
                print("Error: Failed to parse MQTT connection status")
        else:
            print("Error: Failed to retrieve MQTT connection status")

       # Subscribe to the AWS topic
        sub_topic = f"aws/things/{aws_thing_name}/"
        print("Subscribing to topic: " + sub_topic)
        if not send_at_command(uart, f'AT+CMQTTSUBTOPIC=0,31,1 {(sub_topic)}', wait_time=5, expected_response=">"):
            print("Error: Failed to set subscription topic")
            return

        uart.write(sub_topic.encode())
        if not send_at_command(uart, "AT+CMQTTSUB=0"):
            print("Error: Failed to subscribe to the topic")
            return

        # Set the topic
        if not send_at_command(uart, f'AT+CMQTTTOPIC=0,31 {(sub_topic)}', wait_time=5, expected_response=">"):
            print("Error: Failed to set topic")
            return

        uart.write(topic.encode())

        # Set the payload
        if not send_at_command(uart, f'AT+CMQTTPAYLOAD=0,65 {(msg)}', wait_time=5, expected_response=">"):
            print("Error: Failed to set payload")
            return

        uart.write(msg.encode())

        # Publish the message
        if not send_at_command(uart, "AT+CMQTTPUB=0,1,60"):
            print("Error: Failed to publish message")
            return

    except Exception as e:
        print("Error in cellular_publish:", str(e))

    finally:
        # Disconnect from the MQTT server
        send_at_command(uart, "AT+CMQTTDISC=0,60")
        # Release the MQTT client
        send_at_command(uart, "AT+CMQTTREL=0")
        # Stop the MQTT service
        send_at_command(uart, "AT+CMQTTSTOP")

UPDATE: I have stepped back my approach and have decided to attempt the AT Commands directly to the SIMCOM module so i can figure out the execution order. However, i am still encountering issues with publishing to AWS IOT. Here is what i have if anyone can help me out i would appreciate it

+CPIN: READY

SMS DONE

PB DONE
AT+CMQTTSTART
OK

+CMQTTSTART: 0
AT+CMQTTACCQ=0,"aerial_58cf79a5a6ae",1
OK
AT+CMQTTSSLCFG=0,0
OK
AT+CMQTTWILLTOPIC=0,31
>aerial_58cf79a5a6ae
K
AT+CMQTTWILLMSG=0,17,1
>SIMCom Connected!
OK
AT+CSSLCFG="sslversion",0,4
OK
AT+CSSLCFG="authmode",0,2
OK
AT+CSSLCFG="clientcert",0,"clientcert.pem"
OK
AT+CSSLCFG="cacert",0,"cacert.pem"
OK
AT+CSSLCFG="clientkey",0,"clientkey.pem"
OK
AT+CMQTTCONNECT=0,"tcp://blah-blah-blah.iot.us-east-2.amazonaws.com:8883",59,1
OK

+CMQTTCONNECT: 0,0
AT+CMQTTSUBTOPIC=0,31,1
>aerial_58cf79a5a6ae
K
AT+CMQTTSUB=0
OK

+CMQTTSUB: 0,0
AT+CMQTTTOPIC=0,31
>aerial_58cf79a5a6ae
K
AT+CMQTTPAYLOAD=0,38
>{"message":"Hello from SIMCom Module"}
OK
AT+CMQTTPUB=0,1,60,0,0
OK

+CMQTTPUB: 0,0

+CMQTTRXSTART: 0,31,38
+CMQTTRXTOPIC: 0,31
aerial_58cf79a5a6ae
MQTTRXPAYLOAD: 0,38
{"message":"Hello from SIMCom Module"}
+CMQTTRXEND: 0
0

There are 0 best solutions below