Help with the right formatting of code error invalid format

Need some advice on the correct formatting please as I am not quite there.

Using a SIM7600 and trying to connect to HiveMQ port 8883 and set the topic will and msg.

Write Command
AT+CMQTTWILLTOPIC=<client_index>,<req_length>

Expected output

AT+CMQTTWILLTOPIC=0,17
>temp/temperature
OK

Example
image

My code below. If I use IDE serial or TeraTerm I can use the following commands successfully

AT+CSSLCFG="sslversion",0,4
AT+CSSLCFG="authmode",0,2
AT+CSSLCFG="cacert",0,"isrgrootx1.pem"
AT+CMQTTSTART
AT+CMQTTACCQ=0,"client1",1
AT+CMQTTSSLCFG=0,0
AT+CMQTTSTART
AT+CMQTTACCQ=0,"client1",1
AT+CMQTTSSLCFG=0,0
AT+CMQTTWILLTOPIC=0,17
test/temperature/
AT+CMQTTWILLMSG=0,2,1
29

I see my issue to be the code AT+CMQTTWILLTOPIC the waiting for the prompy of > then sending the willtopic of test/temperature/ my logs at the bottom of this post dont look quite correct.

e.g., AT+CMQTTWILLTOPIC=1,17\r", 1000, "\r\n> rather than AT+CMQTTWILLTOPIC=0,17\r\n where it waits for the prompt >

 // modem.sendAT("+CMQTTWILLTOPIC=0,17,\"temp/temperature\"");
 // if (!modem.waitResponse(1000)) {
 //   Serial.println("CMQTTWILLTOPIC command failed with /");
 //   return false;
 // }

 
  modem.sendAT("+CMQTTWILLTOPIC=0,17\r\n");
  modem.waitResponse(1000);
  Serial.println("temp/temperature\r\n");
  modem.waitResponse(1000);
  
  if (!modem.waitResponse(1000)) {
      Serial.println("CMQTTWILLTOPIC command failed");
      return false;
  }


  // Send AT+CMQTTWILLMSG=0,25 temp/temperature then 29
  modem.sendAT("+CMQTTWILLMSG=0,2,1,\"17\"");
  if (!modem.waitResponse(1000)) {
    Serial.println("CMQTTWILLMSG command failed");
    return false;
  }

  delay(1000);

Log output with this code

18:36:39.526 -> AT+IPADDR
18:36:39.526 -> 
18:36:39.526 -> +IPADDR: xxx.xxx.xxx.xxx
18:36:39.574 -> 
18:36:39.574 -> OK
18:36:39.621 -> Connecting to xxx.s1.eu.hivemq.cloud
18:36:39.621 -> 
18:36:39.621 -> AT+CNSMOD?
18:36:39.621 -> 
18:36:39.621 -> +CNSMOD: 0,8
18:36:39.621 -> 
18:36:39.668 -> OK
18:36:39.668 -> AT+CSSLCFG="sslversion",0,4
18:36:39.668 -> 
18:36:39.668 -> OK
18:36:39.668 -> AT+CSSLCFG="authmode",0,1
18:36:39.668 -> 
18:36:39.715 -> OK
18:36:39.715 -> AT+CCERTLIST
18:36:39.715 -> 
18:36:39.715 -> +CCERTLIST: "isrgrootx1.pem"
18:36:39.761 -> 
18:36:39.808 -> OK
18:36:39.808 -> AT+CSSLCFG="cacert",0,"isrgrootx1.pem"
18:36:39.808 -> 
18:36:39.808 -> OK
18:36:39.808 -> AT+CMQTTREL=0
18:36:39.853 -> 
18:36:39.853 -> OK
18:36:39.853 -> AT+CMQTTSTOP
18:36:39.853 -> 
18:36:39.853 -> +CMQTTSTOP: 0
18:36:39.901 -> 
18:36:39.901 -> OK
18:36:39.901 -> AT+CMQTTSTART
18:36:39.949 -> 
18:36:39.949 -> +CMQTTSTART: 0
18:36:39.949 -> 
18:36:39.949 -> OK
18:36:39.995 -> AT+CMQTTACCQ=0,"Client1",1,4
18:36:39.995 -> 
18:36:39.995 -> OK
18:36:39.995 -> AT+CMQTTSSLCFG=0,0
18:36:39.995 -> 
18:36:40.041 -> OK
18:36:40.041 -> AT+CMQTTWILLTOPIC=0,25
18:36:40.041 -> 
18:36:40.041 -> 
18:36:40.041 -> >temp/temperature
18:36:41.033 -> 
18:36:42.015 -> AT+CMQTTWILLMSG=0,2,1,"29"
18:36:42.062 -> 
18:36:42.062 -> OK
18:36:43.037 -> AT+CMQTTCONNECT=0,"tcp://xxx.s1.eu.hivemq.cloud:8883",60,1,"xxx","xxx"
18:36:44.063 -> MQTT Connect success
18:36:44.063 -> AT+NETOPEN?
18:36:44.063 -> 
18:36:44.110 -> +CMQTTCONNECT: 0,12

Write command is AT+CMQTTCONNECT AT+CMQTTCONNECT=<client_index>,<server_addr>,<keepalive_time>,<clean_session>[,<user_name>[,<pass_word>]

Return should be +CMQTTCONNECT: 0,0 not +CMQTTCONNECT: 0,12 which is 'invalid format'

Do you have USB-to-TTL-converter that you could use to listen to the sended data between Arduino and modem?

Do you have a 24 MHz 8 channel logic analyser that you could use for listening?

Post your complete sketch not just a snippet?

echo everything that you send to the modem / receive from the modem to the serial monitor 1 to 1. Add a leading "#" and a trailing "#" to catch even linefeed and carrieage return

Thanks code over 2k is lines been working on it for a long time.

Yes - Do you have a 24 MHz 8 channel logic analyser that you could use for listening, just haven't hooked it up yet

Add a leading "#" and a trailing "#" to catch even linefeed and carriage return

Revised code

  modem.sendAT("+CMQTTWILLTOPIC=0,25\r\n", 1000, "#");
  modem.waitResponse(1000, "#");
  Serial.println("#temp/temperature\r\n#");
  modem.waitResponse(3000, "#");

  modem.sendAT("+CMQTTWILLMSG=0,2,0\r\n", 1000, "#");
  modem.waitResponse(1000);
  Serial.println("#29\r\n#");

Output

20:06:29.052 -> AT+CMQTTSTART
20:06:29.098 -> 
20:06:29.098 -> +CMQTTSTART: 0
20:06:29.145 -> 
20:06:29.145 -> OK
20:06:29.145 -> AT+CMQTTACCQ=0,"Client1",1,4
20:06:29.145 -> 
20:06:29.145 -> OK
20:06:29.192 -> AT+CMQTTSSLCFG=0,0
20:06:29.192 -> 
20:06:29.192 -> OK
20:06:29.192 -> AT+CMQTTWILLTOPIC=0,25
20:06:29.192 -> 1000#
20:06:29.192 -> 
20:06:29.192 -> >#temp/temperature
20:06:30.213 -> #
20:06:34.184 -> AT+CMQTTWILLMSG=0,2,0
20:06:34.184 -> 1000#
20:06:34.230 -> 
20:06:34.230 -> OK
20:06:34.230 -> #29
20:06:34.230 -> #
20:06:38.211 -> AT+CMQTTCONNECT=0,"tcp://xxx.s1.eu.hivemq.cloud:8883",60,1,"xxx","xxx"
20:06:39.239 -> MQTT Connect success
20:06:39.239 -> AT+NETOPEN?
20:06:39.239 -> 
20:06:39.239 -> +CMQTTCONNECT: 0,12

AT+CMQTTWILLTOPIC is meant to return OK
AT+CMQTTWILLMSG is also meant to return OK

Error ends in +CMQTTCONNECT: 0,12
12 is invalid format but suspect the way I am sending CMQTTWILLTOPIC and not getting a OK back is where the +CMQTTCONNECT: 0,12 errors comes from.

then you are writing that sending this command

AT+CMQTTCONNECT AT+CMQTTCONNECT=<client_index>,<server_addr>,<keepalive_time>,<clean_session>[,<user_name>[,<pass_word>]`

so instead of receiving the answer

+CMQTTCONNECT: 0,0

you get the answer

+CMQTTCONNECT: 0,12

I don't understand any more of what you have posted because it is unclear to me what are you sending and what are you receiving

So I guess I have to describe it more precise what I mean with echoing to the serial monitor

storing all your commands into char-arrays and then send the content of the char-arrays to

modem.sendAT(myCommand);
Serial.print("#");
Serial.print(myCommand);
Serial.println("#");

modem.sendAT(myCommand); // <<== char-array
instead of
modem.sendAT("+CMQTTWILLTOPIC=0,17\r\n") //<<<== hardcoded characters

this ensures that you send the exact same character-sequence to sendAT and to Serial.

Your initial posting that you can successfully send commands with the serial monitor does not include the command

Did you test this command that causes an error
works if you send it "by hand" with IDE-Serial monitor or not?

Thanks code is over 2k is lines been working on it for a long time.

2kByte of code is really nothing for the forum-software.
The forum-software can take 120.000 characters which is 117 kB

Found my code error for +CMQTTCONNECT: 0,12 was a extra AT so removed and minimized the code and variables.

Code

  // Show current network system mode
  modem.sendAT("+CNSMOD?");
  if (!modem.waitResponse(1000)) {
    Serial.println("CNSMOD network system mode command failed");
    return false;
  }
  
  // Set SSL version and authentication mode
  modem.sendAT("+CSSLCFG=\"sslversion\",0,4"); //ALL Set the SSL version of the first SSL context
  if (!modem.waitResponse(1000)) {
    Serial.println("CSSLCFG SSLVersion command failed");
    return false;
  }

  modem.sendAT("+CSSLCFG=\"authmode\",0,1"); //Server authentication. It needs the root CA of the serverSet the authentication mode(verify server) of the first SSL context
  if (!modem.waitResponse(1000)) {
    Serial.println("CSSLCFG Authmode command failed");
    return false;
  }


  // Set server root CA
  modem.sendAT("+CSSLCFG=\"cacert\",0,\"isrgrootx1.pem\""); //Set the server root CA of the first SSL context
  if (!modem.waitResponse(1000)) {
    Serial.println("CSSLCFG CACert command failed");
    return false;
  }
 

  modem.sendAT("+CMQTTSSLCFG?");
  modem.waitResponse(1000);

   
  // Connect to MQTT server
  modem.sendAT("+CMQTTCONNECT=0,\"tcp://xxx.s1.eu.hivemq.cloud:8883\",60,1,\"xxx\",\"xxx\"");
  if (!modem.waitResponse(120000)) {
    Serial.println("CMQTTCONNECT command failed");
    return false;
  }

  modem.sendAT("+CMQTTCONNECT?");
  modem.waitResponse(1000);


  delay(5000); //Allow sufficient time for the SSL handshake to finish
  Serial.println("done!");

Logs

18:03:34.127 -> AT+NETOPEN?
18:03:34.174 -> 
18:03:34.174 -> +NETOPEN: 1
18:03:34.174 -> 
18:03:34.174 -> OK
18:03:34.221 -> AT+IPADDR
18:03:34.221 -> 
18:03:34.221 -> +IPADDR: xx.xx.xx.xx
18:03:34.268 -> 
18:03:34.268 -> OK
18:03:34.268 -> Connecting to xxx.s1.eu.hivemq.cloud
18:03:34.268 -> 
18:03:34.268 -> AT+CNSMOD?
18:03:34.316 -> 
18:03:34.316 -> +CNSMOD: 0,8
18:03:34.316 -> 
18:03:34.316 -> OK
18:03:34.361 -> AT+CSSLCFG="sslversion",0,4
18:03:34.361 -> 
18:03:34.361 -> OK
18:03:34.361 -> AT+CSSLCFG="authmode",0,1
18:03:34.361 -> 
18:03:34.361 -> OK
18:03:34.408 -> AT+CSSLCFG="cacert",0,"isrgrootx1.pem"
18:03:34.408 -> 
18:03:34.408 -> OK
18:03:34.408 -> AT+CMQTTSSLCFG=0,0
18:03:34.408 -> 
18:03:34.455 -> OK
18:03:34.455 -> AT+CMQTTSSLCFG?
18:03:34.455 -> 
18:03:34.455 -> +CMQTTSSLCFG: 0,0
18:03:34.502 -> +CMQTTSSLCFG: 1,
18:03:34.548 -> 
18:03:34.548 -> 
18:03:34.548 -> OK
18:03:34.548 -> AT+CMQTTCONNECT=0,"tcp://xxx.s1.eu.hivemq.cloud:8883",60,1,"xxx","xxx"
18:03:34.594 -> 
18:03:34.594 -> OK
18:03:39.600 -> done!
18:03:39.600 -> AT+CIPRXGET=4,0
18:03:39.600 -> 
18:03:39.600 -> +CIPRXGET: 4,0,0
18:03:39.649 -> 
18:03:39.649 -> OK
18:03:39.649 -> AT+CIPCLOSE?
18:03:39.649 -> 
18:03:39.694 -> +CIPCLOSE: 0,0,0,0,0,0,0,0,0,0
18:03:39.694 -> 
18:03:39.694 -> OK
18:03:39.838 -> AT+CGREG?
18:03:39.838 -> 
18:03:39.838 -> +CGREG: 0,1
18:03:39.887 -> 
18:03:39.887 -> OK
18:03:39.887 -> MQTT NOT CONNECTED! 
18:03:39.887 -> Disconnecting from: xxx.s1.eu.hivemq.cloud
18:03:39.887 -> AT+CIPSEND=0,2

Command AT+CMQTTCONNECT returns ok but not +CMQTTCONNECT: 0,0 as per the manual?

AT+CMQTTCONNECT=0,"tcp://hooleeping.com:8883",60,1
OK
+CMQTTCONNECT: 0,0
20:22:47.382 -> 
20:22:47.382 -> 
20:22:47.382 -> OK
20:22:47.382 -> AT+CMQTTCONNECT=0,"tcp://xxx.s1.eu.hivemq.cloud:8883",60,1,"xxx","xxx"
20:22:47.428 -> 
20:22:47.428 -> OK
20:22:47.428 -> AT+CMQTTCONNECT?
20:22:47.428 -> 
20:22:47.428 -> +CMQTTCONNECT: 0
20:22:47.476 -> +CMQTTCONNECT: 1
20:22:47.523 -> 
20:22:47.523 -> OK
20:22:52.554 -> done!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.