4

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.

Jelmer
  • 41
  • 3
  • 1
    did you try with ***"{'key':'value'}"*** ??? – ΦXocę 웃 Пepeúpa ツ Mar 13 '20 at 08:08
  • 1
    Forgive my basic question, but what happens if you simply add the inner double quotes without escaping them? – Roberto Caboni Mar 14 '20 at 13:26
  • @ΦXocę웃Пepeúpaツ Single quotes work, but isn't valid JSON, so it's not recognized by AWS on the other side. – Jelmer Mar 14 '20 at 19:38
  • @000 the input cuts off. For example `+QMTCFG:"will",0,0,0,0,"test\topic","{"key":"value"}"` will result in a message with a single `{` in it on the other side. – Jelmer Mar 14 '20 at 19:40
  • Ok, I see. It's something very implementation dependant. Other devices, as far as I know, have different behavior. Or at least designed an _online_interface for binary data (including special chars like `"`). One last attempt: could you try escaping the hex ASCII value with `\x22`? – Roberto Caboni Mar 14 '20 at 21:27
  • @000 no difference between `\x22` and actual quotes `"`. – Jelmer Mar 15 '20 at 22:11
  • @000 if I actually include `\x22` in the message as text that gets send to AWS, it also doesn't get translated to `"` by AWS. Good idea though. – Jelmer Mar 15 '20 at 22:17

3 Answers3

0

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.

0

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}"
hlovdal
  • 26,565
  • 10
  • 94
  • 165
0

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"}