Tag Archive: GSM WITH ARDUINO


 

In the previous post part-1 I explained the initial setup of starting a Server on PC on a port , setting the Windows firewall to allow the port for external communication , PORT FORWARDING set up in the Router config, etc..

https://alselectro.wordpress.com/2016/09/25/gsm-connecting-to-a-server-on-a-port-by-tcpip-part-1/

In this part 2 I’ll show a practical demo of sending Temperature data to the Server.

As given in part 1 , start a server on distant PC on port 350 & do the port forwarding settings in Router config as explained.

A LM35 sensor is connected to pin A0 of Arduino & a GSM SIM900A is connected to pins 7 , 8 , Gnd of Arduino. As Soft Serial library is used in the code , pins 7 & 8 are declared as soft Rx & Tx.

The Rx pin goes to Tx of GSM , Tx connected to Rx of GSM , GND is made common.An external power source of 12v 1A is used for the GSM.

Download the Arduino code HERE

————————————-

#include <SoftwareSerial.h>
SoftwareSerial myGsm(7,8);
float temp;
int tempPin = 0;
void setup()
{
myGsm.begin(9600);
Serial.begin(9600);
delay(500);

myGsm.println(“AT+CIPSHUT”);
delay(1000);
printSerialData();

myGsm.println(“AT+CIPMUX=0”);
delay(2000);
printSerialData();

myGsm.println(“AT+CGATT=1”);
delay(1000);
printSerialData();

myGsm.println(“AT+CSTT=\”bsnlnet\”,\”\”,\”\””);//setting the APN,username & password
delay(5000);
printSerialData();

myGsm.println();
myGsm.println(“AT+CIICR”);
delay(6000);
printSerialData();

myGsm.println(“AT+CIFSR”); //get IP address
delay(2000);
printSerialData();

myGsm.println(“AT+CIPSTART=\”TCP\”,\”122.178.80.228\”,\”350\””);
delay(5000);
printSerialData();
delay(5000);

myGsm.println(“AT+CIPSEND”);
delay(2000);
printSerialData();

sendtemp();
delay(3000);
myGsm.println(“AT+CIPCLOSE”);
printSerialData();

myGsm.println(“AT+CIPSHUT”);
delay(1000);
printSerialData();
}

void loop()
{
}

void printSerialData()
{
while(myGsm.available()!=0)
Serial.write(myGsm.read());
}

 

void sendtemp()
{
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.print(“TEMPERATURE = “);
Serial.print(temp);
Serial.print(“*C”);
Serial.println();
delay(5000);
myGsm.println(temp);
delay(3000);
printSerialData();
myGsm.write(0x1A);
delay(3000);
printSerialData();

}

———————————————-

The Analog pin A0 is assigned as tempPin to read the LM 35 sensor data.

Initially any previous IP connection is SHUT  using the command

AT+CIPSHUT

This is essential to start a fresh TCP/IP comnnection.

Single IP connection mode is set by

AT+CIPMUX=0

GPRS is attached using

AT+CGATT=1

Task is started with the command

AT+CSTT=”APN”,”USER NAME”,”PASSWORD”

The Access Point Name of the mobile service provider can be known  by placing the SIM on your Android mobile. It’s shown under

Settings  –> More..  –>  Cellular Networks –> Access Point Names

In my case the APN is bsnlnet , user name & password are empty

AT+CSTT=”bsnlnet”,””,””

 

Then we bring up the Wireless connection with GPRS using

AT+CIICR

This takes a little moment , so a delay of 6 secs is a must in the code.

The IP address assigned by GPRS is obtained by

AT+CIFSR

Now we start up TCP connection with Server IP address & Port number of distant server

AT+CIPSTART=”TCP”,”122.178.80.228″,”350”

Once the connection is established , we send data using

AT+CIPSEND

A CTRL+Z is required to send data which is executed using hex 1A

 

Upload the code to Arduino & open the Serial monitor to watch the AT commands executed

tcp_list

 

Following is the screen shot at the Server side.

 

Image 3

 

 

cooltext753793315

Advertisements

 

 

This post is on retrieving particular Data from a web page using SIM900A & Arduino.

For e.g we shall retrieve the Blog Status hits of my blog in which you’re reading this.

We’ve to create an API link for this data so that data can be retrieved using SIM900A.

To create API link we use the THINGSPEAK.com website.

First let us read the XPATH details of the data wanted as below :

The procedure is related to Google Chrome WebBrowser

 

Image 1

 

Right click on the data to be retrieved & click on INSPECT .

 

inspect

 

A highlighted code will be displayed on which Right click & then select COPY  –> COPY XPATH

XPATH

 

The copied XPATH detail has to be used in THINSPEAK account to create the API link.

Open your thingspeak account & click on APPS –> THINGHTTP

 

thing1

Click on NEW THINGSPEAK to create a new “thing”.

thing2

 

You can provide a suitable Name for the App & under URL provide the website link from where you want the data from.

Method is GET & version is 1.1

thing3

 

Scroll down to bottom & under the PARSE STRING field paste the XPATH copied from the website.

thing4

Click on SAVE & you see the API LINK generated.

thing5

 

You can use this API link in your browser to confirm the data retrieved.

Image 15

Now let us see the practical application using SIN900A GSM & ARDUINO.

The connections between Arduino & GSM are :

Rx of GSM —> pin 8 (Tx) of Arduino   (since we use the SoftSerial in Arduino code , we declare pins 7 & 8 for Serial communication)

Tx of GSM –> pin 7 (Rx) of Arduino

GND –> GND

A separate power source is required for the GSM.

Following are the AT commands used

http

DOWNLOAD Arduino code HERE

————————————————————

#include <SoftwareSerial.h>
SoftwareSerial myGsm(7,8);

void setup()
{
myGsm.begin(9600);
Serial.begin(9600);
delay(500);

myGsm.println(“AT+CGATT=1”);
delay(200);
printSerialData();

myGsm.println(“AT+SAPBR=3,1,\”CONTYPE\”,\”GPRS\””);//setting the SAPBR,connection type is GPRS
delay(1000);
printSerialData();

myGsm.println(“AT+SAPBR=3,1,\”APN\”,\”\””);//setting the APN,2nd parameter empty works for all networks
delay(5000);
printSerialData();

myGsm.println();
myGsm.println(“AT+SAPBR=1,1”);
delay(10000);
printSerialData();

myGsm.println(“AT+HTTPINIT”); //init the HTTP request
delay(2000);
printSerialData();

myGsm.println(“AT+HTTPPARA=\”URL\”,\”http://api.thingspeak.com/apps/thinghttp/send_request?api_key=VV7WQ9DS19E2BNNI\””);// setting the httppara,
//the second parameter is the website from where you want to access data
delay(1000);
printSerialData();

myGsm.println();
myGsm.println(“AT+HTTPACTION=0”);//submit the GET request
delay(8000);//the delay is important if the return datas are very large, the time required longer.
printSerialData();
myGsm.println(“AT+HTTPREAD=0,20”);// read the data from the website you access
delay(3000);
printSerialData();

myGsm.println(“”);
delay(1000);
myGsm.println(“AT+HTTPTERM”);// terminate HTTP service
printSerialData();

}

void loop()
{
}

void printSerialData()
{
while(myGsm.available()!=0)
Serial.write(myGsm.read());
}

—————————————————————-

AT+CGATT=1  enables the GPRS CONNECTION

AT+SAPBR is used to declare the Connection Type as GPRS

& Then the APN –Access Point Name of the mobile service provider is given.

An empty string also will work.Or you can slide the SIM into your Android mobile

under Settings –. More –> Cellular Networks  –> Access Point Names

you get the APN details

AT+SAPBR=1,1  enables the GPRS & APN settings & then HTTP is initialized with AT+HTTPINIT

HTTP parameters are then provided as “URL” & the API link we created earlier.

Note to use HTTP ( not HTTPS) in the link, as we use AT commands for HTTP only here.

AT+HTTPACTION=0 enables GET action

The GSM respons with

+HTTPACTION:0,200,12

Here 0 means GET action & 200 means OK , 12 bytes are retrieved.

If you get 600 instaed of 200 ,then it means a Network Error.

AT+HTTPREAD command is used to read the Data.

 

Image 11

cooltext753793315   cooltext753790696

 

A6 GSM  is introduced by the makers of ESP8266 , AI-THINKER.

The module is much cheaper than SIM900 & connections are quite simple.In this post we shall see how to connect with Arduino to make a call & send SMS.

A mobile adapter is enough to power up the A6 GSM module.The Vcc pin of GSM must be looped with PWR_KEY pin.This acts as a chip enable.You can leave this connected or remove after a moment.The module just requires a HIGH trigger at PWR_KEY pin while started.

A valid SIM is used at the back side of the module.The SIM slot provided is for a Micro SIM.If you ‘ve a Nano SIM you need to use a converter to fit the slot.

The RxD pin of A6 GSM is connected to Tx of Arduino

The TxD pin of A6 goes to Rx of Arduino.

GND pin of A6 to GND of Arduino.

 

Connection

 

 

PIN_DETAILS_A6

Initial TESTING

Power up the module & then after a moment make a call to the SIM number used in the GSM module from another phone.If you get the RING Back tone , it means the module is ready.As there is no LED indication provided , this initial step is required to confirm working.

 

Making a CALL

 

Connect the USB cable of PC to Arduino & upload this code for making a call.Remember to disconnect connections at Rx/Tx before uploading the code.

Image 1

 

The mobile number to be called is stored in an Array of char variable.

In the void setup() code we begin Serial communication at 9600 baud.Though the default baud rate of A6 GSM is 115200 , it is effective to use 9600 baud .

ATD command followed by the number to be called is used to make a call.

A semicolon at the end is not a must as in SIM900.The call will be made even if you do not use a semicolon.

After a delay of 8 secs ATH command is sent to terminate the call.

You can open the Serial monitor of Arduino & watch the AT commands landing.You should set the baud rate as 9600 in the Serial monitor as well.

The void loop() function is left empty.In case you try the code inside loop , the call will be repeatedly

made for ever.

 

Sending SMS

Now let us see the method to send SMS.

Following screenshot shows the code for sending SMS.

Image 2

 

DOWNLOAD Arduino code HERE

AT+CMGF=1

which will allow you to read and write SMS messages as strings instead of hexadecimal characters.

Serial.println is used to send the AT command with a New line (Enter).

After a delay of 2 seconds next command

AT+CMGS=”phone_no.”

is sent.The target phone number is to be given within double quotes. Note the usage of

Serial.write(0x22)  which is the ASCII equivalent for double quotes.

0D followed by 0A are the ASCII equivalents for  CARRIAGE RETURN & LINE FEED.

The delay of 2 seconds is a must after setting the GSM to text mode AT+CMGF=1

Try this code without the delay , you won’t get response.

After the 0D , 0A a delay of 2 seconds is provided & then the message to be sent is given.

CTRL+Z is finally given to send the message.

This is provided by the ASCII equivalent 1A or char(26)

Watch this video :

 

 

cooltext753793315    cooltext753790696