Tag Archive: GSM TCP/IP


 

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 tutorial is of 2 parts.

First part explains the following :

–Starting a Server on your PC  & start listening on a port (say 350) . For this we use SOCKET TEST or HERCULES software

— Once the port is listening , we test communication locally using PuTty

— Windows Firewall setting has to be done for a NEW INBOUND RULE allowing port 350 for communication.

— For connecting to this port from external world , PORT FORWARDING to be done in Router’s Virtual Server setting so that any contact to the public IP is routed to the PORT 350 .

 

Second part is practical demo using GSM SIM900 or ESP8266 with Arduino.

 

The following picture shows my simple home network connections.A BINATONE make Router is used to connect to the Internet provider Airtel Broadband.The IP address of the Router is

192.168.1.1

which is the Gateway IP address used to access the settings on Router.My Laptop is connected over WIFI to this Router.The Router assigns a local IP address to my PC which is

192.168.1.150

Apart from these, the Internet Provider assigns a Public IP to my Network which is

122.178.80.228

This is the IP by which external world can contact my network.

Note that all these IP addresses are DYNAMIC & changes every time I power up the Router.

In another post I’ll explain how to make these IP addresses STATIC , so that the IP remain the same always.

set1

 

All the Network Tools I’ve used for this demo can be downloaded HERE.

Let us start  a server on port 350 using the SOCKET TEST tool.

This tool allows us to start a server easily.Click on the SERVER Tab & then feed in the port number where you want to listen.

Click on START LISTENING.

soc1

Now the server is up & running.It starts listening on port 350.

Remember to avoid the standard ports like 80 ( http) , 21 (FTP), 23 (TELNET), 22 (SSH) ,etc which are well known ports ,pre assigned for the particular protocol.

The dynamic range of port number you can assign goes up to 65535 or 2^16-1 (16 bits).

The IP address of the router can be known from the command window

RUN –> CMD –> ipconfig

The Default Gateway IP is the address of the Router.

RUN1

 

LOCAL TESTING TOOLS (optional)

 

Once the port is assigned for the server , you can use the ADVANCED PORT SCANNER tool to verify the port on the particular IP.

In the screen shot below you can see that the port number 350 is discovered as a TCP Server port on my PC whose local IP is 192.168.1.150

 

port_scanner

While the port scanner is scanning , you get messages on your SOCKET TEST window.

Here you can see the Port scanner software connecting to the port & then disconnecting after scanning.

port_message

 

Alternatively you can use a PUTTY serial port tool to test local communication.

Select the connection type as RAW & provide the host IP which is 192.168.1.150 & thtn the port number 350

putty1

Click on open , to see the New client connection on the Socket test window.

putty4

 

Now you can test two way communication between  socket test (server) & putty ( client).

putty2

 

WINDOWS FIREWALL SETTINGS

 

The Windows Firewall settings has to be done to allow the port 350 to listen from outside.

Open the Windows Firewall & Advanced Security window.

Click on Inbound Rules & then New Rule.

windows1

 

Select the Rule Type as PORT & in the next screen select protocol as TCP.

Under Specified local ports feed the port number as 350

windows2

 

Under Action window select Allow the Connection

Next window is Profile window , here tick mark all – Domain,Public & Private.

Provide a name to the Rule & save it.

Now your PORT 350 is allowed to listen by the Windows Firewall.

windows3

 

PORT FORWARDING

This step is final where you redirect the incoming request to the port number of local IP where the server is started.

Here my local PC’s IP is 192.168.1.150   &  server is started on port 350.

Anyone from external world connecting to my public IP has to be directed to the port 350 on local IP 192.168.1.150.

This is called PORT FORWARDING , which is very widely used in CCTV Remote viewing & in Gaming.

The setting has to be done in your Router.

Type in the Gateway IP of your Router , here it is 192.168.1.1 , on your browser & provide the user name & password.

Here is an excellent website showing all screen shots of almost all Routers in the world

https://portforward.com/router.htm

 

Mine is BINATONE Router.

You’ve to search for something called VIRTUAL SERVER on the router setting.

 

ROUTER1

 

For my Router it’s hidden under ADVANCED SETUP –> NAT –> VIRTUAL SERVER

For more details you can visit the above portforward link

Create a new Virtual server listing by entering the

— Port number , which is 350 .Enter same number at all fields like start & stop.

— Local IP address , here you provide the IP where server is started , 192.168.1.150

— save the listing

router3

 

Once saved , the port number 350 is open to outside world.

Online port open test tools are available

www.canyouseeme.org

www.yougetsignal.com

 

Once you open the link , your public IP will be automatically shown.

Fill in the PORT number to be checked & click on CHECK button.

If you see SUCCESS , then you’re done with PORT FORWARDING

 

canyou1

 

port_open

 

Following is the list of AT commands used by the GSM SIM900A to contact the server.

This is for part 2 of the post.PART 2 LINK HERE

gsm1

gsm2

cooltext753793315   cooltext753790696