How to Send Data to Thingspeak Using Arduino

In this post we are going to learn step by step how to upload sensor data to thingspeak and visually analyse 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.

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:

Circuit Diagram for sending data to thinkspeak
Circuit Diagram for sending data to thinkspeak

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:

https://drive.google.com/open?id=1aWuF115pMaZbySQS2CIHBxJiPSM1ezhE

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.

Why we are using GSM instead of ESP8266 in this project?

You may have also referred other sites with similar concept of sending data to thingspeak and you may have noticed they have utilized ESP8266 instead of a GSM modem.

We are utilizing GSM modem to access the internet instead of 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 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.

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