A while back I bought one of these quad band GPRS modems for a job I was working on. The task was simple enough – connect to a data source, wait for outgoing messages, send them out via SMS, report back with success/fail.
The software was installed and “presumably” all is working as expected – as is the usual case you don’t typically hear any feedback if it works, only if it doesn’t work.
Anyway I have this module gathering dust at home and thought I’d write an MQTT to SMS gateway that can run on my Linux server. Once its up and running any other MQTT application could use it to send SMS notifications very simply.
Easy mode would have been to just use the C# code I already had modify to source data from MQTT and run it via Mono on the Linux box. I could have used the Java version in a similar fashion. However the final plan was to run the application on a Raspberry Pi and having the Mono/Java run times really kills you from a memory usage perspective.
I still have plenty of spare time on my hands so I decided to port the application to C++ and make use of the boost libraries. In theory I can compile it to a single executable (+ the shared libraries I guess) and all will be good when running a dozen or so of these little applications.
All GSM Modules are not the same
There are so many of these things around these days. The standard one the Arduino community seems to use is is based on the SIM900 modules. We used one of these in our Alarm Panel device.
It’s really easy to send SMS messages as the library code will do it all for you. The AT commands are simple enough if you want to do it yourself though, as the SIM900 supports “text mode”, so something similar to this.
The module I have contains a HUAWEI MG323B module. Unfortunately it does not support the same “text mode” to send SMS messages, instead you need to use “PDU mode”, something more like this.
Ugly! There are many useful sites on the interwebs that explain PDU mode in great detail. I found a simple library to do this and ported it to C#, Java and now C++ (cant remember the original source or implementation language at the moment – bad).
Boost me up
I haven’t used or looked at the boost libraries for a (very) long time. But a lot of the things you might want to do are pretty much covered so why reinvent. In particular I needed threads, logging, locking schemes, configuration files and serial ports to name a few.
How did it work out? Well mostly fine – once you get going with how each of the libraries work you can get some nice things done quite quickly.
The good – The threads, logging, synchronization schemes and serial port code was reasonably straight forward.
The not so good – configuration files. Here I wanted to reuse the same INI files my windows applications use. A single INI file contains an assortment of settings for system wide settings and for specific applications and components used within these applications.
I might port my configuration file code to C++ just to maintain the simplicity – the boost method just seemed too complex for my liking.
Here is the log of the application running. Basic sequence is start up, read configuration file, create and start the MQTT interface and F1103 interface, connect everything all up with an event queue. MQTT subscribes to the topic, while F1103 starts sending AT commands to get the GPRS device into the correct mode/state.
NOTE: The AT commands are echoed back so the first RX line is actually what I sent.
Later I publish an MQTT message to the topic via the command line.
$ mosquitto_pub -d -h 192.168.156.116 -i test -t /messages/outgoing/sms -m "number:<number> message:<message>"
F1103 tries to initiate the SMS send but fails – thee error indicates that the SIM card is not present.
Next up – Add SIM card and test again.