What's the escape character in AT-commands?

1.5k Views Asked by At

Im using a BG96 modem to connect to AWS iot over MQTT.

I'm trying to set my MQTT Last Will and Testament with the following AT-command:

+QMTCFG:"will",(0-5),(0,1),(0-2),(0,1),"willtopic","willmessage"

Which works great. But now I'm trying to add a JSON formatted string to "willmessage", so I need to add "" (double quotes) in there, which means I need to escape them in my command. But I have no clue if I can escape them or what the escape character is.

Things I tried: \" (backslash) and "" (double double quotes)

I looked in all of the BG96 datasheets, and I don't see it mentioned anywhere.

3

There are 3 best solutions below

0
On

Some special characters have different values between the ASCII character set and the GSM character set.

i.e.

  • In the ASCII character set: \ = 0x5C.
  • In the GSM character set: Ö = 0x5C.

Beyond this point, some special characters must be entered using a specific way, such as a 2-byte representation. I suggest you check the standard/version of AT commands implemented on your hardware (i.e. GSM 07.07, GSM 07.05, manufacturer specific set...).

i.e. I'm using a GPS+GPRS modem from Ai-Thinker called A9G. In this one, to use the AT+MQTTPUB command with data formatted in JSON style, I need to append \x5c\x32\x32. So the module will interpret this as \22 and the server as \".

i.g.

"{\x5c\x32\x32Key\x5c\x32\x32:\x5c\x32\x32Value\x5c\x32\x32}"

at the cloud it will be:

{"Key":"Value"}
0
On

Escaping of " within a string is covered in chapter 5.4.2.2 String constants in the V.250 standard - which is a MUST READ for anyone writing code handling AT commands (read at least all of chapter 5):

String constants shall consist of a sequence of ... except for the characters """ ... . String constants shall be bounded at the beginning and end by the double-quote character (""" ... . ... The double-quote character, used as the beginning and ending string delimiter, shall be represented within a string constant as "\22".

So the escape mechanism is \22 not \x22. This should be universally the case for all modems and not something that is implementation dependent.

I did not find reference to documentation of MQTT and BG96 and you did not link any of your "all of the BG96 datasheets" so I am just providing example syntax for an imaginary command to send a JSON payload of {"key": "value"}:

AT+SOMECOMMAND=...,"{\22key\22: \22value\22}"
1
On

I had the same issue while using MQTT commands on a SIMCOM SIM800c, and I noticed that the regular backslash (\) escapes the quotation marks (as it does in c) when communicating directly with the GSM unit via a USB to TTL converter. To implement this in software I printed the following string to the UART connected to the GSM Modem:

AT+SMPUB=\"testTopicPost\",0,1,\"{\x5c\x22Key\x5c\x22 : \x5c\x22Value\x5c\x22}\"

What this basically does is send the raw \ and " characters to the GSM unit. Hope this solution works for you as well.