TCP Client Socket GPRS

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to send and receive data on TCP Client Socket with Flyport GPRS.

FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the code since the pinout is slightly different.

Introduction to data mode

The first step to perform data communication, it's to correctly configure APN parameters (APN, username, password, IPs...).Good practise would be to set APN parameters everytime Flyport GPRS resets (or wakes up from hibernation/sleep)
To properly set the APN parameters, the function APNConfig() must be used:

APNConfig(char* APN, char* login, char* password, char* IP, char* DN1, char* DNS2);

The APN changes according to the operator (generally you can find it on your operator website), the login and password credentials are personal, and are given to the user by the operator when the SIm card is purchased. IP and DNS (1 & 2) can usually be set to "DYNAMIC", in this way the operator will provide Flyport the correct IP and DNS addresses.

NOTE: data connection functions (like TCP/HTTP Client connection, FTP Client file exchange, email sending) will NOT work if APN is not properly set!

How to use it

Everytime the button is pressed Flyport sends a GET Request to download openPicus Wiki logo. The button should be connected between p7 and p22 (Ground) like in the picture below. Flyport detects automatically button pressure and starts the commands sequence to send the GET REQUEST to the Server. After reads data present on TCP Socket receive buffer. All data read from TCP Socket Buffer is shown on Serial Monitor Utility of IDE.
This tutorial shows how to create a firmware with "state machine" execution. The state machine keeps track of your code execution. In this way, if problems occurs, Flyport can automatically start from a certain state. If the connection/data exchange process go wrong for any reason (could be an incoming call or network disconnection etc...) the firmware restarts from APN setup phase.

NOTE: The following example should be configured with your own parameters (APN and email).

Bill of Materials

  • 1x Flyport GPRS Module
  • 1x miniUSB programmer
  • 1x Power Supply
  • 1x Proto Nest Board
  • 1x Button


Schematic

ButtonInput p7 inup.png


Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
TCP_SOCKET sockTcp;
char inBuff[1500];
BOOL sendRequest = FALSE;
int lenTemp = 0;
 
void FlyportTask()
{
    sockTcp.number = INVALID_SOCKET;
 
    vTaskDelay(20);
    UARTWrite(1,"Flyport Task Started...\r\n");
 
    //Wait for GSM Connection successfull
    do
    {
	UpdateConnStatus();
	while(LastExecStat() == OP_EXECUTION);
    }while((LastConnStatus() != REG_SUCCESS)&&(LastConnStatus() != ROAMING));
 
    vTaskDelay(20);
    UARTWrite(1,"Flyport registered on network!\r\n");
 
    // Initialize p7 as digital input with internal pullup resistor
    IOInit(p7, inup);
 
    int actionState = 0;
 
    while(1)
    {
	vTaskDelay(20);
	IOPut(p21, toggle);
 
	// Poll Button input state...
	if(IOGet(p7) == off)
	{
            UARTWrite(1, "Button Pressed!\r\n");
	    if(actionState == 0)
		actionState++;	// Set Step #1 to start
	}
 
	switch(actionState)
	{
	    // Step #0: IDLE operation
	    case 0:
		// Insert here your IDLE code (if needed...) 
		break;
 
	    // Step #1: APN Config
	    case 1:
		UARTWrite(1, "Setup APN params\r\n");
		APNConfig("apn.domain.fly","usernameAPN","passwordAPN",DYNAMIC_IP,DYNAMIC_IP,DYNAMIC_IP);
		vTaskDelay(20);
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		    actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;
 
	    // Step #2: Connect socket to TCP Server
	    case 2:
		UARTWrite(1, "Connecting to TCP Server...");
		TCPClientOpen(&sockTcp, "wiki.openpicus.com","80");
	 	while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors on TCPClientOpen function!\r\n");	
		    actionState = 7; // Close socket and try again	
		}	
		else
		{
		    UARTWrite(1, "\r\n TCPClientOpen OK \r\n");
		    UARTWrite(1, "Socket Number: ");
		    sprintf(inBuff, "%d\r\n", sockTcp.number);
		    UARTWrite(1, inBuff);	
		    actionState++; // execute next step
		}
		break;
 
	    // Step #3: Update TCP_SOCKET Status
	    case 3:
		UARTWrite(1, "Updating TCP_SOCKET Status...");
		TCPStatus(&sockTcp);
	 	while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors on updating TCPStatus!\r\n");	
		    actionState = 7; // Close socket and try again	
		}
		else
		{
		    UARTWrite(1, "\r\n TCP Socket Status:\r\n");
		    sprintf(inBuff, "Status: %d\r\n", sockTcp.status);
		    UARTWrite(1, inBuff);
		    sprintf(inBuff, "RxLen: %d\r\n", sockTcp.rxLen);
		    UARTWrite(1, inBuff);	
		    actionState++; // execute next step
		}
		break;
 
	    // Step #4: Send TCP DATA (HTTP GET Request)
	    case 4:
		UARTWrite(1, "Sending data...");
 
		char data[] = "GET /custom_openpicus/logo_openpicus.png HTTP/1.1\r\nHost: wiki.openpicus.com\r\n\r\n\r\n";
 
		TCPWrite(&sockTcp, data, strlen(data));
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors sending TCP data!\r\n");	
		    actionState = 7; // Close socket and try again	
		}	
		else
		{
		    UARTWrite(1, "\r\n TCP data sent:\r\n");
		    sprintf(inBuff, "Status: %d\r\n\r\n\r\n", sockTcp.status);
		    UARTWrite(1, inBuff);	
		    actionState++; // execute next step
		}
		break;
 
	    // Step #5: Update TCP_SOCKET Status
	    case 5:
		TCPStatus(&sockTcp);
	 	while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors on TCPStatus!\r\n");	
		    actionState = 7; // Close socket and try again
		}	
		else
		{	
		    if(sockTcp.rxLen > 0)
			actionState++; // execute next step
		    else
		    {
			// Action completed (no more data received...), close socket:
			actionState = 7; // force step #7
		    }
		}
		break;
 
	    // Step #6: Read until end of data...
	    case 6:
		lenTemp = sockTcp.rxLen;
		TCPRead(&sockTcp, inBuff, lenTemp);
		inBuff[lenTemp] = '\0';
 
	 	while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors on reading TCP buffer!\r\n");	
		    actionState = 7; // Close socket and try again
		}	
		else
		{
		    UARTWrite(1, inBuff); // Write data to serial Monitor	
		    actionState = 5; // Update again TCP_SOCKET Status
		}
		break;
 
	    // Step #7: Close TCP_SOCKET
	    case 7:
		UARTWrite(1, "\r\n\r\nClosing socket...");
		TCPClientClose(&sockTcp);
 
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(1);
		vTaskDelay(20);
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "Errors on TCPClientClose!\r\n");	
		    actionState = 1; // Repeat from case 1	
		}	
		else
		{
		    UARTWrite(1, "Socket Closed!\r\n");	
		    actionState = 0; // Reset to IDLE State
		}
		break;
	}
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox