While the buzzword “IoT “is relatively new, there has been a long time “internet of things” in operation. Those devices are called the far less sexy term “M2M” or machine-to-machine. These devices, around since the 90s, contain a microprocessor, some sensors, sometimes electromechanical hardware, and a cellular radio. These M2M devices were (and still are!) the early “Internet of Things.” Thanks to Hologram.io, you can join this new/old trend for free.
I decided it was time to up my IoT game with a GPRS, or 2G, data radio. In this post, I show the hardware I am using to create a 2G-based GPS tracker. It includes a Teensy 3.2 (Arduino) connected to a SIM808 Module (FONA) using a Hologram.io Developer SIM. It transmits GPS coordinates to an Adafruit.io Dashboard, which displays them on a map.
This post is not going to be a tidy tutorial. Instead, it is all the steps (and notes) I went through. I will cover:
- Hardware pieces I am using
- How to verify SIM808 (FONA) module is connected through Hologram via Serial Commands
- How to send HTTP/POST requests (including SSL) with the SIM808 (FONA)
- My (brute force) changes to the Adafruit FONA Library
- Code for sending MQTT payload (GPS Coords) to a dafruit.io dashboard
When done, you will be able to build something like a battery powered GPS Tracker, that updates over cellular. If you are in a rush, grab all the code from the FONA GPS Tracker Github Project.
Here are the individual hardware pieces I am using to make this GPS Tracker work. Given all the strings and ASCII text you need to communicate with the SIM808, I highly recommend a microcontroller with at least 8k of RAM. While I think this project could work on a 328p based Arduino, I am not sure the challenge is worth it.
I have two reasons for selecting the Teeny 3.2 for this experiment. One of the reasons is a real engineering trade off while the other is a real life compromise. The real-life trade-off is that I had a bunch of them and wanted a reason to use them. (Wait, that might qualify as an engineering reason.)
The engineering reason is power consumption. I compared the 32-bit Teensy 3.2 to one based on an 8-bit Teensy 2.0. I found the 3.2 drew less current. Not bad for an ARM-based processor!
One modification I had to make to my Teensy is disconnecting VIN from VUSB. I needed the ability to connect the USB cable to the Teeny for communication and programming. This mod lets me power the Teeny from the FONA’s power supply. That supply could be USB or a LiPo battery. This configuration means two USB connections during development. Fortunately, the Teensy has a small trace you can cut with a sharp knife, severing its connect to VBUS. PJRC thought through the hardware design of teeny boards. Oooooh, I get it now.
FYI, the Teensy 3.2 is a 72MHz ARM-M4 with 64K of RAM, 256K of Flash and many flexible I/O options. Amazingly, the board is only about $20. Learn more about the Teensy series on PJRC.com.
The hardware design of the FONA is deceptively dull. The board contains a SIM808 module, a battery charger IC, a voltage regulator, some passive components, and connectors. However, that SIM800 is one hell of a module. It is a SoC that makes interfacing with cellular networks a breeze. Want to build a phone? You can. Want to create your own texting pager? You can. Wish to develop an IoT device? You can.
$50 for the FONA might seem high. But, value wise, these are a fantastic piece of kit. Do not overlook the fact you are getting a pre-certified cellular radio. That process is not cheap, fast, or easy. It is incredible that you can just buy this module and connect to a mobile network–no questions asked. (Well, after adding a SIM card.)
The FONA interfaces with AT-style commands over UART serial. Operations like querying the network, getting GPS coordinates and sending HTTP requests are simple. It is possible to do all of that with just a handful of commands from a serial window. Below I will show you some examples to try out. Or you could program a microcontroller to do it. Like the aforementioned Teensy. (Code for that too.)
Antennas for FONA
The FONA module does not come with any antennas. If you are not going to use the GPS module, you can skip it. However, for a reliable cellular connection, you will need that one. I used the flexible sticker-style GSM antenna with no signal problems even indoors. For the GPS, I used a passive GPS antenna. Indoors, I was able to acquire a 3D fix, for most of the time, while sitting on my desk. I cannot wait to see how well it works when I use it outside.
For the FONA module to work correctly, you need a SIM card. That SIM card needs to come from a data service. Hologram supports 2G, 3G, and 4G/LTE networks. I have several SIMs from Hologram laying around my lab. And it is not because I am internet famous (I am not). Hologram gives out their SIMs to developers for free. Well, you do need to pay for shipping. However, even that is not a big deal since you get 1 MB of data per month, again, for free. If you think 1 megabyte of data sounds small, you should keep in mind IoT devices send small payloads.
Whether you are developing an actual IoT product or you just want to learn more about how GPRS works, this is a great option. They have got two maps of Zone coverage (Zone 2 overlaps Zone 1) which covers the majority of the world. Side note, I did kind of laugh that the United States is in Zone 1, but Canada is in Zone 2.
When the FONA connects to the network, it returns service status “5 – roaming.” If you are using the Adafruit example code, there is a conflict here. The example expects the connect status “1,” which means the home network. From what I understand, there are no additional charges for “roaming” on Hologram. One of my modifications to the Adafruit library is accepting either a status of “1” or “5.”
The FONA and MQTT
At first, I had significant trouble getting MQTT to work with my FONA module. I followed the Adafruit examples but got strings of errors. I came across some partial solutions that fixed some problems, but nothing could reliably send MQTT packets. For a bit, I thought it had to do with the time of day. Eventually, I determined there were two problems:
- FONA module was not initialized correctly, and
- You HAVE to connect the VIO pin.
No joke, I completely missed #2. It is the IO reference for the FONA. The pin allows the SIM808 to communicate with either 3.3-volt or 5.0-volt logic if it is connected. The VIO pin solved repeatability. However, I was still getting this dreaded “DEAC” error, on occasion. After many searches and reading the SIM808 documentation, I found that these AT-commands initialize the FONA, for Hologram.io.
// check network status and signal AT+COPS? AT+CSQ // close active actions AT+CIPSHUT //attach to GPRS AT+CGATT=1 // single connection at a time AT+CIPMUX=0 // manually read data AT+CIPRXGET=1 // setup APN AT+CSTT="hologram","","" // turn on GPRS AT+CIICR // get IP address (this fails sometimes, if so, re do the entire sequence) AT+CIFSR
I created a method in the Adafruit FONA library to send those command. It accepts either a status “1” or status “5” for the network type. This change means the code will work whether the cellular module is roaming or not. Click the button below to get the method from Github.
Here are some of the notes I made while working with the hardware. It is in the order I did things. Like I said before, I do not mean this to be a tidy tutorial, but a list of what I learned. Please let me know in the comments if any of this helped saved you time.
Testing FONA with HTTPS
The SIM808 handles HTTP/S connections internally. If your application just needs to use the HTTP POST method, this feature simplifies your code. From a project perspective, it makes it easy to test your connections. So in this case, I connect an FTDI adapter to the FONA.
- TX to RX
- RX to TX
- GND (obviously)
- FONA VIO to VCC on the FTDI 🙂
Open up your favorite serial terminal, here are a few suggestions, and connect to the FTDI. Send the “AT” command a few times to wake up the FONA. (On power-up, you need to press the “Key” button to turn it on.)
The following commands will connect to GPRS, provided you get appropriate responses, and then it will grab a test page from hologram.io. Originally, I found some instructions on a SeeedStudio tutorial, but they are slightly wrong. Hhologram.io redirects standard HTTP to HTTPS. So you need to enable SSL support on the SIM808 before connecting. (This support is present, despite some Adafruit documentation that says it isn’t.)
AT+CSQ +CSQ: 10,0 AT+CGATT? +CGATT: 1 AT+SAPBR=3,1,"CONTYPE","GPRS" AT+SAPBR=3,1,"APN","hologram" AT+SAPBR=1,1 AT+HTTPINIT AT+HTTPPARA="URL",”hologram.io/test.html" AT+HTTPPARA="CID",1 # many tutorials miss this bit AT+HTTPSSL=1 AT+HTTPACTION=0 +HTTPACTION:0,200,79 AT+HTTPREAD +HTTPREAD:79 ... response (in html)
If you are getting status code 200 and the “it works” HTML message, then your FONA is communicating with the internet through a cellular connection. Pretty sweet, huh?
Other cool FONA Commands
The FONA can grab the current time from the cellular network, and it can give you your GPS coordinates. Here’s the AT commands to make both of those happen. These are fun things to play with while connected by serial. First, here’s how to verify your connection.
Check Network and Strength
#network AT+COPS? #signal strength AT+CSQ
The first command identifies the network and the second command gives a signal strength. To be honest, I do not know what the numbers should be. Shame on me, I know. I got a number around 20.
There is a counter-intuitive step to make network time work. You need to turn on a register, save it to non-volatile memory, and then reboot the module. After that, the SIM808 will always get the current time from the network. Seriously, power cycle your FONA.
#Enable Network Time AT+CLTS=1 at&w (restart) #Check the time AT+CCLK? +CCLK: "18/02/05,02:25:43-16”
The last command returns the time. I did not have much success with NTP. Once I got network time working, I was happy to keep using that.
Most of my experience with GPS came from Geocaching and my work on MacCaching. Do not expect a GPS receiver to work indoors.You need to have the antenna outside with a clear view of the sky, or at least, near a window.
# is GPS radio on? AT+CGNSPWR? +CGNSPWR: 0 # turns it on AT+CGNSPWR=1 # is there a GPS fix? AT+CGPSSTATUS? # Get Location (if available) AT+CGNSINF +CGNSINF: 1, 1, 20180101193334.000, 39.352471, -132.181413, 149.300, 0.43, 228.4, 1, , 1.6, 1.9, 1.0, ,9, 6, , ,27, ,
If you get many zeros with the last command, there is no fix available. With the status command, you may get a 2D or 3D fix. In theory, a 2D fix will give your latitude and longitude. My experience has been, a 2D fix means the GPS has a poor connection. Wait for a 3D fix before using the data.
GPS_STATUS: 1, FIX_STATUS: 1, TIMESTAMP: 20180101193334.000, LATITUDE: 39.352471, LONGITUDE: -132.181413, LONG_DIRECTION: 149.300, ALTITUDE: 0.43, SPEED: 228.4, HEADING, 1, SPEED_KNOTS ...
I did not decode all of the fields. The first one is the time, then latitude, then longitude, some kind of heading, and the altitude. The rest, to me, are “don’t cares.” FYI, once you get the FONA library connected, it decodes the string for you.
GPS Tracker Code
Instead of providing the code in this post, here is a link to my FONA GPS Tracker GitHub project. There I have uploaded the application code I am using along with the modified methods for the Adafruit FONA library. In general, I have followed the Adafruit MQTT Library FONA Example code. In setup(), I connect to the cellular with the Hologram.io SIM and enable the GPS. I use the Teensy’s onboard LED to know once setup() is done.
Instead of loop(), I use a millis() timer check to update the GPS coordinates once per 10 seconds. If the SIM808 says it has a lock, I turn the pin 13 LED back on and publish two MQTT updates. The first one is the GPS coordinates and the second is altitude. These are posted to an Adafruit IO dashboard that I created.
If MQTT fails to update more than three times, I call my FONA connect method again. This action resets the module, reconnecting it to the network. This failsafe catches a rare condition where the FONA fails to get an IP address. Perhaps in the future, I will make the connection code more robust. For now, this works
This hardware setup was relatively easy to throw together. I have acquired these pieces over the past year. I am happy that they connected so quickly. Regarding prototyping a cellular-based IoT device, this has been an interesting exercise.
Moving forward, I am working on a 3d-printable case so that I can clip this “device” to my bag. To track my progress (see what I did there?) follow my project on hackster.io
Question: What would you build with a FONA cellular module and a Hologram.io SIM card? You can leave a comment by clicking here.