Sending Data from GSM module to a web server using Arduino

Sending Data from GSM module to a web server using Arduino: I have seen many people searching on the internet about how to use GSM module to send data to a server. This article will help you send data to a server using GPRS data of GSM module. you can control anything from a server and send any type of data to a server using gsm module using GPRS data of your mobile phone network. you can also do the same task with the help esp826 wifi module and ethernet shield. This is an IoT-based project in this project we send data from SIM900A GSM / GPS module to web server using Arduino. We have already posted an article on sending data to the server using an Arduino. In this embedded systems based project, We enter data using 4×4 keypad and also display on 16×2 LCD in real time. Data can be of any type numeric, alphabetic or a special character.  A web server which is used to receive data is created on Spark fun website. Web server can be publically accessed and used.sending data to server using gsm

Block Diagram of sending data from gsm module to a web serversending data to server using gsm block diagram

Components Required for sending data from gsm module to a web server

Following is a list of components required for sending data from gsm module to a web server using gsm module and Arduino.

  • SIM900A GSM/GPS Module: SIM900A provides both GSM and GPS service in this compact module. We can send SMS, data, Voice, and Fax using this module. Its operating voltages range is 3.2 to 4.8V. It draws the only 1.0mA in sleep mode. Its Operation temperature range is -40°C to +85 °C. you can check this article to know, how to interface a GSM module with Arduino.
  • Arduino Uno: As always it is easy to use. Provides enough pins for my project. Consumes less power, Cheap, user-friendly and it external library is found easily. I recommend you to check a list of Arduino tutorials for beginners.
  • 16×2 LCD: It can display 16 characters in 2 rows. Easy to use with Arduino Uno. Also, operates on the same voltages of Arduino Uno. Check this guide to know how to interface Arduino with LCD?
  • 4×4 Keypad: It consists of 4 rows and 4 columns. It contains 16 buttons which we can define as our requirement in the code. A keypad is used as following in this project.

Circuit diagram for sending data to a web server using Arduino

Circuit diagram for sending data to a web server using Arduino and gsm module is given below: Circuit Diagram for sending data from gsm module to a web server

sending data to server using gsm circuit diagram

Connections for sending data to server using gsm module Arduino

16×2 LCD to Arduino:

  • LCD pin R/S to Arduino pin A0.
  • R/W to ground.
  • LCD pin Enable to Arduino pin A1.
  • LCD pin D4 to Arduino pin A2.
  • LCD pin D5 to Arduino pin A3.
  • LCD pin D6 TO Arduino pin A4.
  • LCD pin D7 TO Arduino pin A5.
  • Others LCD pins are connected as shown in the picture.

4×4 Keypad to Arduino Uno:

Starting from left

  • Keypad pin 1 to Arduino pin D11.
  • Keypad pin 2 to Arduino pin D10.
  • Keypad pin 3 to Arduino pin D9.
  • Keypad pin 4 to Arduino pin D8.
  • Keypad pin 5 to Arduino pin D7.
  • Keypad pin 6 to Arduino pin D6.
  • Keypad pin 7 to Arduino pin D5.
  • Keypad pin 8 to Arduino pin D4.

SIM900A GSM / GPS Module to Arduino:

  • Rx pin of module to Arduino pin D3.
  • Tx pin of module to Arduino pin D2.
  • Connect external 5 volt dc supply to Vcc and Gnd terminal of module.
  • Connect Ground pin of module with Ground pin of Arduino.

How to make Web server on spark fun website

  • Visit the Spark Fun website. Go to the data section.
  • Click on the following option: Create data stream on Spark fun
  • Fill the required information and click on save.
  • Now you will see the following Links: Save the keys for the future use.

Working on sending data to server using gsm and Arduino

First of all, we enter text using the 4×4 keypad and the text is displays on 16×2 LCD in real time. When we finish our text entering then we press D button on the keypad to send the data to web server. The GSM module connects to the web server by using the information defined in the code such as public and private keys. GSM send the data on the web server which we can check by opening the web server. The data can be of any type such as numeric, alphabetic or special character. The only disadvantage is the spark fun only provides 50MB of data which is not enough if you want to use regularly.

Implementation:

Code

#include <SoftwareSerial.h> // serial software library for interfacing gsm module
SoftwareSerial Serial1(2, 3); // RX, TX // connect gsm Tx at D2 and Rx at D3
#include<LiquidCrystal.h> // LCD library for interfacing LCD
LiquidCrystal lcd(14,15,16,17,18,19); // connect rs,en,d4,d5,d6,d7 respectevely
String pvtKey="wY9DPG5vzpH99KNrNkx2"; // private key for posting data to sparkfun
String publicKey="w5nXxM6rp0tww5YVYg3G"; // public key for open page of sparkfun 
String url="";
String ip="\"data.sparkfun.com\""; // sparkfun server ip or url
int port=80; // sparkfun server port
String SendCmd="AT+CIPSEND="; // sending number of byte command
String Start="AT+CIPSTART=\"TCP\""; // TCPIP start command 
// strings and variables
//String msg="";
String instr="";
String str_sms="";
String str1="";
int i=0,temp=0;
#include <Keypad.h> // keypad library for interfacing keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int x=0;
int y=0;
int n=0;
int minValue=0;
int maxValue=0;
char keyPress=0;
int keyPressTime=100;
String msg="";
char hexaKeys[ROWS][COLS] = 
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
String alpha="[email protected]_$%?ABC2DEF3GHI4JKL5MNO6PQRS7TUV8WXYZ9* 0#"; 
void setup() 
{
Serial1.begin(9600); // init serial1 for GSM
lcd.begin(16,2); // init LCD
lcd.print("MICROCONTROLLERS");
lcd.setCursor(0,1);
lcd.print(" LAB ");
delay(2000);

lcd.clear();
lcd.print("Sending Data ");
lcd.setCursor(0,1);
lcd.print("to Server");
delay(2000);
lcd.clear();

lcd.print("Initializing GSM");
initGSM(); // init GSM module
lcd.clear();
lcd.print("Initializing GPRS");
initGPRS(); // init GPRS in GSM Module
lcd.clear();
lcd.print("System Ready");
delay(2000);
}
void loop()
{
int n=0;
lcd.clear();
lcd.noCursor();
while(1)
{
lcd.cursor();
char key = customKeypad.getKey();
if(key=='1')
getkey(0, 6, key);
if(key=='2')
getkey(7, 10, key);
else if(key=='3')
getkey(11, 14, key); 
else if(key=='4')
getkey(15, 18, key);
else if(key=='5')
getkey(19, 22, key);
else if(key=='6')
getkey(23, 26, key);
else if(key=='7')
getkey(27, 31, key);
else if(key=='8')
getkey(32,35, key);
else if(key=='9')
getkey(36, 40, key);
else if(key=='*')
getkey(41, 41, key);
else if(key=='0')
getkey(42, 43, key);
else if(key=='#')
getkey(44, 44, key);

else if(key == 'C')
{
x--;
lcd.setCursor(x,y);
lcd.print(" ");
n--;
msg[n]=' '; 
lcd.setCursor(x,y);
}
else if(key == 'D')
{
lcd.clear();
lcd.noBlink();
lcd.print("Data Sending");
lcd.setCursor(0,1);
lcd.print("To Server");
url="GET /input/";
url+=publicKey;
url+="?private_key=";
url+=pvtKey;
url+="&log=";
url+=msg;
url+=" HTTP/1.0\r\n\r\n";
String svr=Start+","+ip+","+port;
delay(1000);
connectGSM(svr,"CONNECT");
delay(1000);
int len = url.length();
String str="";
str=SendCmd+len;
sendToServer(str);
Serial1.print(url);
delay(1000);
Serial1.write(0x1A);
delay(1000);
lcd.clear();
lcd_status();
// clearmsg();
n=0;
i=0;
x=0;
y=0;
msg="";
}
}
}
void getkey(int minValue, int maxValue, char keyPress)
{
int ch=minValue;
int pressed=1;
char key=keyPress;
lcd.noBlink();
for(int i=0;i<keyPressTime;i++)
{ 
if(key==keyPress)
{
lcd.setCursor(x,y);
lcd.print(alpha[ch]);
ch++;
if(ch>maxValue)
ch=minValue;
i=0; 
}
key=customKeypad.getKey();
delay(10);
}
if(pressed)
{ 
x++;
msg+=alpha[ch-1];
n++;
if(x>15)
{
x=0;
y=1;
}
}
pressed=0;
lcd.blink();
}
void lcd_status()
{
lcd.clear();
lcd.print("Data Sent to");
lcd.setCursor(0,1);
lcd.print("Server");
delay(2000);
lcd.clear();
}
void sendToServer(String str)
{
Serial1.println(str);
delay(1000);
}
void initGSM()
{
connectGSM("AT","OK");
connectGSM("ATE1","OK");
connectGSM("AT+CPIN?","READY");
}
void initGPRS()
{
connectGSM("AT+CIPSHUT","OK");
connectGSM("AT+CGATT=1","OK");
connectGSM("AT+CSTT=\"airtelgprs.com\",\"\",\"\"","OK");
connectGSM("AT+CIICR","OK");
delay(1000);
Serial1.println("AT+CIFSR");
delay(1000);
}
void connectGSM (String cmd, char *res)
{
while(1)
{
Serial.println(cmd);
Serial1.println(cmd);
delay(500);
while(Serial1.available()>0)
{
if(Serial1.find(res))
{
delay(1000);
return;
}
}
delay(1000);
}
}
/*
Public URL
http://data.sparkfun.com/streams/w5nXxM6rp0tww5YVYg3G
Public Key
w5nXxM6rp0tww5YVYg3G
Private Key
wY9DPG5vzpH99KNrNkx2
Keep this key secret, and in a safe place. You will not be able to retrieve it.
Delete Key
xxxxxxxxxxxxx
This key can only be used once. Keep this key secret, and in a safe place. You will not be able to retrieve it.
Logging using query string parameters
Format:
http://data.sparkfun.com/input/[publicKey]?private_key=[privateKey]&log=[value]
Example:
http://data.sparkfun.com/input/w5nXxM6rp0tww5YVYg3G?private_key=wY9DPG5vzpH99KNrNkx2&log=22.21
*/

7 thoughts on “Sending Data from GSM module to a web server using Arduino”

  1. Hello Sir.. can you please help me .. How to transfer data using sim 900a and arduino uno to a particular own created server … i.e we don’t have public and private key… just the ip address to send the data…
    please guide me

    Reply
    • Did u got your solution, If yes plz share it with me. we are doing the same thing for our final year project. It would be of great help.

      Reply
  2. hello sir, please can you help me with the code. i am also not able to connect to the server. your support will be highly appreciated.

    thank you.

    Reply
  3. i used API link to connect to the server. To link from arduino to the database server we have to give the IP address of ur server and then give the specific path to connect to the database

    Reply

Leave a Comment

shares