SIM800L tracking your IoT device with Arduino
The content here is under the Attribution 4.0 International (CC BY 4.0) license
After a while testing the GPS NEO6 I ordered a new guy to play around with GSM connection and GPS at the same time. The sensor this time is the SIM800L, which is an interface between the SIM card and the Arduino Uno (could be any board, not Arduino Uno only).
The version used is the Arduino Uno, so every time you see Arduino in the text I am talking about the Uno version. For the test I started using the Raspberry Pi (model B+) board but ended up with Arduino, the switch is given by the facility to send the sketch out to the Arduino. While in the Raspberry Pi, you should wait till the board is up and running. Further tests can lead me back to the Pi, but for this post, the examples will be on Arduino.
SIM800L
The board used is the SIM800L without any specific break-board or something like that. The newest versions of the SIM like SIM900 come with an antenna and a bigger board. Besides the board is needed to have a micro SIM card. In my case I had to use an adapter to transform the nano SIM I have into the micro.
Fonte: Mi.com
IMPORTANT: Make sure to have a SIM card with 2G, 3G or 4G enabled (you can test it plugin the SIM card on your phone and try to access any web page), and have in hands the APN, the APN user and APN password.
Wiring
This sensor requires just 4 wires to start with, but there is a gap in the voltage that it operates. The following table holds the pins where the wires should go to.
SIM800L | Arduino Uno |
---|---|
NET | Antenna connection |
VCC | 5v |
RST | - |
RX | 8 |
TX | 9 |
GND | GNG |
To make it easier to understand, the following image illustrates how the wiring should be done.
If you are planning to use your device for a long time, it is good to mention that there is an resistor to add and deliver the 4.2v instead of 5v. For that check this repo and make sure to wire up using the resistor.
As this post has the goal to keep as simple as possible, I am not going to use the resistor.
Sketch (commands AT 101)
IMPORTANT 2: Here I assume that you can compile and run a sketch into the Arduino, if not, please find out how to do that and come back later.
Before we go any deeper into the libraries available out there, the proposal here is to upload a simple sketch to interact with the serial port. The reason behind it is that the SIM800L handles AT (instructions to command a modem) commands into the SIM card, and to understand what is going on we need to understand the AT commands first. The following sketch reads and writes to the serial port.
#include <SoftwareSerial.h>
SoftwareSerial gsm(9, 8);
void setup() {
Serial.begin(9600);
while(!Serial);
gsm.begin(9600);
delay(1000);
Serial.println("Ready to receive commands");
}
void loop() {
if(gsm.available()){
Serial.write(gsm.read());
}
if(Serial.available()){
gsm.write(Serial.read());
}
}
Once we have the sketch up and running in our Arduino, is time to understand the AT commands. The first thing
to send through the serial interface is the command AT
. This is the most basic command
to see if the sensor is up and running, if everything is correct an OK
is printed back.
The next step is to understand what is required to set up the GPRS mode in our SIM card. For that, we are going to rely a lot on the SIM800L official documentation available here.
Enabling GSM location
By default the SIM cards don’t come with any configuration, we should set up manually for every action we would like to do with it. To enable the tracking (that is our goal) we should do the following:
- Enable the GSM location mode
- Authenticate with the APN
- Request the data regarding the SIM location
To enable the GMS location mode is a matter of sending the correct AT
command through the serial.
AT+SAPBR=3,1,"CONTYPE","GPRS"
The next step is to authenticate the APN, we do that in four steps, first setting the APN, followed by the username, the password and finally activating the bearer. Each command should be executed separately and before executing the next one, make sure to get an OK response from the serial.
There is an website with the possible APNS around the world, if you don’t know which one is yours, access this link
AT+SAPBR=3,1,"APN","{here goes your APN}"
After the APN replace the text {here goes your APN}
with your APN provider.
AT+SAPBR=3,1,"USER","{here goes your username}"
After the USER replaces the text {here goes your username}
with your APN username (sometimes it
will be empty, so just leave it as it is).
AT+SAPBR=3,1,"PWD","{here goes your password}"
After the PWD replace the text {here goes your password}
with your password (sometimes it
will be empty, so just leave it as it is).
AT+SAPBR=1,1
By now the LED sensor should start blinking more frequently than it used to, it indicates that a connection has been established and now you can communicate freely with the GSM network. To guarantee that everything is working as expected, there is one command to retrieve the current IP in use by the SIM card.
AT+SAPBR=2,1
The command response should be something similar to the figure below, other than that or only 0.0.0.0
indicates
that something went wrong.
Finally, we are ready to request the latitude and longitude to know where our SIM card is in the world. As you may
have imagined it is done through an AT
command as well.
AT+CIPGSMLOC=1,1
It should return a string containing the latitude, longitude, current date, and current time. The figure below illustrates the response received in the serial monitor.
Edit 04/03/2018
During a few tests with the SIM, it turned out that the accuracy in the latitude and longitude was not as good as expected. Sometimes it gives the same location even with 10 meters of change.
References
- Tracking with GPS NEO 6
- SIM (Subscriber Identity Module)
- SIM Card types
- Arduino Serial
- AT commands
- SIMCOM AT commands PDF
- APN providers worldwide
Changelog
- May 01, 2024 - Grammar fixes
Table of contents
Got a question?
If you have question or feedback, don't think twice and click here to leave a comment. Just want to support me? Buy me a coffee!