Send Data to Thingspeak Using GSM

In this post we are going to learn step by step how to upload sensor data to Thingspeak server and visually analyze the data in real-time. By the end of this article you will be able to upload any kind of sensor data to Thingspeak IoT platform using (GPRS) GSM module – SIM 800 / SIM 900.

We will see:

  • Quick overview: What we are going to do?
  • What is Thingspeak IoT analytics Platform?
  • What is API Key?
  • How to Sign up for Thingspeak and get API Key
  • What is APN?
  • Block Diagram
  • Circuit Diagram
  • Program code
  • Why we are using GSM instead of ESP8266?
  • How much GPRS data will be consumed?
  • Prototype Image
  • How to operate this circuit

Quick overview: What we are going to do?

  • We will be sending temperature and humidity data of your surroundings to thingspeak platform where you can store, analyse and visualize the data using graphs and much more.
  • We will be using DHT11 sensor and GSM modem for accessing GPRS so that we can send the data to the server and Arduino will act as the brain of the project.
  • Two graphs will be generated on Thingspeak platform as we are sending only two data: temperature and humidity.
  • The data on the thingspeak will be updated every 15 seconds.

What is Thingspeak IoT analytics Platform?

Thingspeak is open source platform made for Internet of Things (IoT) device developers and learners where developers can send and log data to the server, analyse, retrieve and store results using graphs with matlab support.

We can send up to 8 data simultaneously to the thingspeak account; the data uploaded will be converted to graphical representation automatically as illustrated below:

Sending data to Thingspeak
Sending data to Thingspeak

The straight line signifies that there was no temperature or humidity change during data logging. You can hover the mouse cursor on these dots to see the numerical data recorded at a particular local time. Thingspeak updates the data/graph every 15 seconds.

To send your data to thingspeak you need something called API key from your account which needs to be inserted in the given program code.

What is API key in Thingspeak?

API stands for “Application programing Interface”, on thingspeak it is a string of random character contains alphabets of lower and upper case, numbers and even special characters to identify your account and ensures that your sent data doesn’t end on someone else’s account and vice-versa.

There are two API keys generated while creating an account; one is called read API key and another is called write API key. We will be using only the write API key as we are going to write data to the thingspeak.

Now let’s see how to sign up and get the API keys for our project.

How to Sign up for Thingspeak and get API Key

  • Signing up for thingspeak is simple, just click this link: https://thingspeak.com/users/sign_up and fill the necessary fields.
  • Once this is done a verification link will be sent to your e-mail and click the received link.

Now go to your account and click on the API key tab and you can see your read and write API keys. Always keep your write API keys confidential.

Getting API key from Thinkgspeak
Getting API key from Thinkgspeak
    • Now click the channel settings, we will now enable the two channels (graphs) and will be writing necessary labels to make our readings easier. Just do the following changes in your account as shown below:

      Editing Thingspeak Channels
      Editing Thingspeak Channels
    • After making the changes scroll down and press “save”.

      Thingspeak save button
      Thingspeak save button
  • Now you are ready with your thingspeak account and API key.

Now let’s learn about “APN” which is necessary for enabling the GSM modem to access internet.

What is APN?

APN stands for Access Point Name it is the gateway between carrier’s network and internet. GSM modem or any mobile device accessing internet must be configured with APN to present the wireless carrier. APN helps in getting the IP address and decides which security protocols should be used.

We should write the correct APN name for the mobile network / SIM that you have installed on the GSM modem in the program code, only then GSM modem can access the internet and send data to thingspeak platform.

For example:

  • Airtel: airtelgrps.com (Tested)
  • BSNL: bsnlnet (Tested)
  • Vodafone: www
  • Idea: imis

If you installed a BSNL SIM card to your GSM modem you should write “bsnlnet” as APN name in the given program code, similarly for Airtel etc.

NOTE 1: To find the APN names of other SIM mobile carriers, please search on Google.

NOTE 2: Don’t use “Jio” SIM card with GSM modems as they are 4G only network and GSM modem can only access 2G network. Other carriers should work without any issue as they are 2G backward compatible.

Block Diagram:

Block Diagram of data sending to Thingspeak
Block Diagram of data sending to Thingspeak

Circuit Diagram:

send data to thingspeak using GSM
send data to thingspeak using GSM

The circuit consists of a GSM modem for accessing GRPS/internet, one DHT11 sensor for sensing the ambient temperature and humidity and an arduino board of your choice.

The DHT11’s output is connected to pin A0 and the sensor is power by 5V from Arduino’s power output pins. You can either power the arduino with USB port or from external 9V input. The GSM modem should be powered from a wall adapter with at-least 1A current capacity, do not power it from Arduino.

Download DHT11 library file here: Click here

Program code: Verified and Error Free

//-----Electronics-project-hub>com------//
#include <SoftwareSerial.h>
#include <dht.h>
dht DHT;
SoftwareSerial gsm(10, 11); // RX, TX
#define DHT11_PIN A0
int chk;
int humi = 0;
int temp = 0;
void setup()
{
  Serial.begin(9600);
  gsm.begin(9600);
  modem_init();
  data_init();
  internet_init();
}
void loop()
{
  chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      break;
    case DHTLIB_ERROR_CONNECT:
      Serial.print("Connect error,\t");
      break;
    case DHTLIB_ERROR_ACK_L:
      Serial.print("Ack Low error,\t");
      break;
    case DHTLIB_ERROR_ACK_H:
      Serial.print("Ack High error,\t");
      break;
      default:
      Serial.print("Unknown error,\t");
      break;
  }
  Serial.print("Humidity: ");
  Serial.print(DHT.humidity, 1);
  Serial.print("%");
  Serial.print(",\t");
  Serial.print("Temperature: ");
  Serial.print(DHT.temperature, 1);
  Serial.println("*C");
  temp = DHT.temperature;
  humi = DHT.humidity;
  delay(2000);
  Send_data();
}
void modem_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  delay(1000);
  gsm.println("AT+CMGF=1");
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
}
void data_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  delay(1000); delay(1000);
  gsm.println("AT+CPIN?");
  delay(1000); delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("contype");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("GPRS");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000); ;
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("APN");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  //------------APN------------//
  gsm.print("bsnlnet"); //APN Here
  //--------------------------//
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("USER");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("  ");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("PWD");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("  ");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(2000);
  gsm.print("AT+SAPBR=1,1");
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(3000);
}
void internet_init()
{
  Serial.println("Please wait.....");
  delay(1000);
  gsm.println("AT+HTTPINIT");
  delay(1000); delay(1000);
  gsm.print("AT+HTTPPARA=");
  gsm.print('"');
  gsm.print("CID");
  gsm.print('"');
  gsm.print(',');
  gsm.println('1');
  delay(1000);
}
void Send_data()
{
  gsm.print("AT+HTTPPARA=");
  gsm.print('"');
  gsm.print("URL");
  gsm.print('"');
  gsm.print(',');
  gsm.print('"');
  gsm.print("http:");
  gsm.print('/');
  gsm.print('/');
  //-----------------------Your API Key Here----------------------//
  //Replace xxxxxxxxxxx with your write API key.
  gsm.print("api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxx&field1="); 
  //---------------------------------------------------------------//
  gsm.print(temp); //>>>>>>  variable 1 (temperature)
  gsm.print("&field2=");
  gsm.print(humi); //>>>>>> variable 2 (Humidity)
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  gsm.println("AT+HTTPACTION=0");
  delay(1000);
}
//-----Electronics-project-hub>com------//

Note 3: Don’t forget to change the API key and APN in the above code.

Advantage of Using GSM Module:

We are utilizing GSM modem to access the internet instead of Wi-Fi boards like ESP8266 so that our projects will be independent of Wi-Fi which has limited range and you may not able to move your project anywhere as you wish or you might need to carry a Wi-Fi hotspot device beside the project.

Say your project need to be place somewhere at the top of a building to collect some data like air quality or atmospheric pressure, with GPRS based internet access you can carry the project anywhere, but if we utilize ESP8266 in the project to access internet you will need a Wi-Fi hotspot device near the project.

If you already own a GSM modem you no need to buy an ESP8266 module to access internet.

How much GPRS data will be consumed?

From our experiments and observation we found that, if the GSM modem sends data to thingspeak for straight 6 hours the data consumed is less than 10 KB.

You absolutely need not to worry that you will run out of mobile data. You can buy an entry level data pack plan from your carrier and you can run the project for several weeks, your bottleneck will your data pack’s validity period and not your data.

Prototype Image:

Prototype
Prototype

How to operate this project:

  • With completed hardware setup install a valid SIM card with a data plan from your mobile carrier.
  • Get the correct APN name for your SIM network carrier and paste it in the given program code.
  • Paste the “write API key” from your thingspeak account to the given program code.
  • Now upload the code to Arduino board.
  • Open Serial monitor, you can see the sensor data.
  • Now open the “private view” tab in the thingspeak site you will see dots appearing after 30 seconds, which is your temperature and humidity data from the sensor.
  • You can logout of the thingspeak account from your PC and still the data will be uploading to the platform.

Not able to Upload data to Thingspeak? Here is the solution:

1) Problem: Your GSM is modem is not working correctly.

Soltuion: You must always check your GSM modem’s working functionality before any GSM based project. This can be done by powering ON the GSM modem with a valid SIM card inserted and dialing the number from another phone. You should hear ring.

Another way to test: There will be 3 LEDs in a GSM modem 1) Power 2) Status 3) Network. The Network LED flashes once every 3 second if it is latched in to a cellular network. It flashes a little faster in the beginning when it is trying to connect to a cellular network.

When GSM modem is connected to Internet, the Network LED flashes faster (twice every second). This is the point where data is sent to Thingsepeak.

2) Problem: Connection Error (No data from DHT11 sensor) / Sensor errors:

Solution: When you get “connection error” or any other error on serial monitor, this could be because you have connected the sensor terminals in wrong way or data pin is disconnected from A0 pin of Arduino. In the circuit diagram, DHT11 sensor’s terminals ( OUT, Vcc, GND) connection was in that way for us, but for you the terminals could be different.

Yes, for the same sensor type the pin configuration could be different, this is because the sensors are sourced from different manufacturer. We personally damaged a DHT11 sensor because we could not identify the terminals correctly and the terminals were not marked properly.

It is your task to identify the correct terminals for DHT11 and don’t blindly follow the circuit diagram. Powering the sensor incorrectly leads to damage like what happened to us.

3) Problem: Incorrect APN

Solution: Incorrect APN in the code will not connect your GSM modem to internet and the APN must be inside the ” ” mark. For example, APN for BSNL cellular provider:

APN
APN

What is my network provider’s APN?

A quick google search will tell you the APN. If not ask the customer support, they will provide you the right information.

4) Problem: You did not insert your write API key in the code correctly.

Solution: You need to insert the write API key like this:

Write API key
Write API key

The “xxxxxxxxxx” must be replaced with your write API key. The key must be inserted between ‘=’ and ‘&’ like this:

API key Insertion
API key Insertion

A vertical line was nothing but cursor in the screen shot.

5) Problem: Not activated a valid data plan for the SIM you inserted in GSM modem.

Solution: You have to activate a internet data plan for the SIM you have inserted in your GSM modem. You can do this by inserting the SIM to any phone and activating it. Please confirm it when you get the service SMS.

6) Problem: You did not configure your Thingspeak account as we mentioned in the beginning.

Solution: You need to label and enable only two fields in a channel as we mentioned.

7) Problem: Connected Tx and Rx pins incorrectly.

Solution: Check Tx and Rx pin connections as per the circuit and don’t forget to connect GND of GSM to GND of Arduino.

8) Problem: GSM modem is not getting adequate power.

Solution: You should not power the GSM modem from Arduino’s power pin even if modem has a 5V input pin. You should always use 9 to 12V wall adapter rated at 1A minimum. The GSM modem consumes up to 1A peak.

If you have any questions regarding this project feel free to express in the comment section, you will get a guaranteed reply from us.

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.