Random Nerd Tutorials
Shares

Request Sensor Data via SMS using Arduino and SIM900 GSM Shield

Shares

In this project we’re going to show you how to request sensor data via SMS with the Arduino. As an example we’re going to request the temperature and humidity from a DHT11 sensor. To send and receive SMS with the Arduino we’re going to use the SIM900 GSM shield. When you send an SMS to the Arduino with the message “STATE”, it replies with the latest temperature and humidity readings.

Before proceeding with this tutorial we recommend the following resources:

First, watch the video demonstration


SIM900 GSM Shield

There are several modules you can use to send and receive SMS with the Arduino. We did this project using the SIM900 GSM shield and that’s the shield we recommend you to get.

The SIM900 GSM shield is shown in figure below:

For an introduction to the GSM shield, and how to set it up, read the Guide to SIM900 GSM GPRS Shield with Arduino.

Parts Required

Here’s a list of all the components needed for this project:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Preliminary steps

Before getting started with your SIM900 GSM module, you need to consider some aspects about the SIM card and the shield power supply.

Prepaid SIM card

We recommend that you use a prepaid plan or a plan with unlimited SMS for testing purposes. Otherwise, if something goes wrong, you may need to pay a huge bill for hundreds of SMS text messages sent by mistake. In this tutorial we’re using a prepaid plan with unlimited SMS.

The shield uses the original SIM card size, not micro or nano. If you have micro or nano you may consider getting a SIM card size adapter.

Turn off the PIN lock

To use the SIM card with the shield, you need to turn off the pin lock. The easiest way to do this, is to insert the SIM card in your smartphone and turn off the pin lock in the phone security settings.

In my case, I need to go through: Settings > Advanced Settings Security > SIM lock and turn off the lock sim card with pin.

Getting the right power supply

The shield has a DC jack for power as shown in figure below.

Next to the power jack there is a toggle switch to select the power source. Next to the toggle switch on the board, there is an arrow indicating the toggle position to use an external power supply – move the toggle switch to use the external power supply as shown above.

To power up the shield, it is advisable to use a 5V power supply that can provide 2A as the one shown below.

You can find the right power adapter for this shield here. Make sure you select the model with 5V and 2A.

Setting up the SIM900 GSM Shield

The following steps show you how to set up the SIM900 GSM shield.

1) Insert the SIM card into the SIM card holder. You need a SIM card with the standard size. The shield is not compatible with micro or nano SIM cards. If you need, you may get a sim card size adapter. Also, it is advisable to use a SIM card with a prepaid plan or unlimited SMS.

2) Confirm the antenna is well connected.

3) On the serial port select, make sure the jumper cap is connected as shown in figure below to use software serial.

 

4) Power the shield using an external 5V power supply. Double-check that you have the external power source selected as we’ve mentioned earlier.

5) To power up/down the shield press the power key for about 2 seconds.

6) Then, the Status LED will light up and the NetLight LED will blink every 800 ms until it finds the network. When it finds the network the NetLight LED will start blinking every three seconds.

7) You can test if the shield is working properly by sending AT commands from the Arduino IDE using an FTDI programmer – as we’ll show below.

Testing the Shield with FTDI programmer

You don’t need to do this step to get the shield working properly. This is an extra step to ensure that you can communicate with your GSM shield and send AT commands from the Arduino IDE serial monitor. For that, you need an FTDI programmer as the one shown in figure below.

1) Connect the FTDI programmer to the GSM shield as shown in figure below.

2) Open the Arduino IDE and select the right COM port.

3) Open the Serial monitor.

4) Select 19200 baud rate – the shield default setting is 19200 – and Carriage return. Write AT at the box highlighted in red and then press enter. See figure below.

5) The shield will respond with OK, if everything is working properly.

Now that you know the shield is working properly, you are ready to start building the project.

Schematics

The figure below shows the circuit schematics for this project.

You have to connect the SIM900 GSM shield and the DHT11 temperature and humidity sensor to the Arduino as shown in the figure below.

Installing the DHT library

To read from the DHT sensor, you must have the DHT library installed. If you don’t have the DHT library installed, follow the instructions below:

  1. Click here to download the DHT-sensor-library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT
  4. Move the DHT folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the Adafruit_Sensor library

To use the DHT temperature and humidity sensor, you also need to install the Adafruit_Sensor library. Follow the next steps to install the library in your Arduino IDE:

  1. Click here to download the Adafruit_Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit_Sensor-master folder
  3. Rename your folder from Adafruit_Sensor-master to Adafruit_Sensor
  4. Move the Adafruit_Sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Code

The following code reads the temperature and humidity from the DHT sensor and sends them via SMS when you send an SMS to the Arduino with the message “STATE”.

You need to modify the code provided with the phone number your Arduino should reply the readings to. The code is well commented for you to understand the purpose of each line of code. Don’t upload the code now. Scroll down and read the explanation below the code.

Importing libraries

First, you include the libraries needed for this project: the DHT libary to read from the DHT sensor and the SoftwareSerial library to communicate with the SIM900 GSM module.

#include "DHT.h"
#include <Adafruit_Sensor.h>
#include <SoftwareSerial.h>

DHT sensor

Then, you tell the Arduino that the DHT data pin is connected to pin 2, select the DHT sensor type and create a dht instance. The code is compatible with other DHT sensors as long as you define the one you’re using in the code.

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

You also create float variables to store the temperature and humidity values.

float t; // temperature in celcius
float f; // temperature in fahrenheit
float h; // humidity

GSM shield

The following line configures the software serial on pins 7 and 8. Pin 7 is being configure as RX and pin 8 as TX.

SoftwareSerial SIM900(7, 8);

You also create a char variable to store the incoming SMS characters.

char incomingChar;

setup()

In the setup(), you begin the DHT and the SIM900 shield. The SIM900 shield is set to text mode and you also set the module to send the SMS data to the serial monitor when it receives it. This is done with the following two lines, respectively:

SIM900.print("AT+CMGF=1\r"); 
SIM900.print("AT+CNMI=2,2,0,0,0\r");

Functions

We create a function to read the temperature and humidity called readData(). This function stores the values on the t and h variables. The code uses temperature in Celsius, but it is prepared if you want Fahrenheit instead – the code is commented on where you should make the changes.

We also create a function that checks if the incoming message is equal to STATE – the SMSRequest() function. This functions returns true if the Arduino receives a message with the text STATE and false if not.  You read the SMS incoming characters using:

incomingChar = SIM900.read();

loop()

In the loop(), you check if there was an SMS request with the SMSRequest() function – you check if the Arduino received a STATE message. If true, it will read the temperature and humidity and send it via SMS to you.

The number the Arduino answers to is set at the following line:

SIM900.println("AT + CMGS = \"XXXXXXXXXXXX\"");

Replace the XXXXXXXXXXXX with the recipient’s phone number.

Note: you must add the number according to the international phone number format. For example, in Portugal the number is preceded by +351XXXXXXXXX.

Then, you store the message you want to send in the dataMessage variable. Finally you send the SMS text message using:

SIM900.print(dataMessage);

Demonstration

When you send the STATE message to the Arduino, it replies with the sensor data.

Watch the video at the beginning of the post for a more in depth project demo.

Wrapping Up

This is a great project to get you started with the SIM900 GSM shield. You’ve learned how to read and send SMS text messages with the Arduino.

You can apply the concepts learned in pretty much any project. Here’s some ideas of projects:

  • Surveillance system that sends an SMS when it detects movement
  • Control a relay via SMS
  • Request specific sensor data from a collection of sensors by adding more conditions to the code

If you like Arduino, you’ll certainly like these 25 Arduino Step-by-step Projects.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registration closes soon, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course