GSM/GPRS 

Example

: Sockets TCP

This example shows how to open a TCP socket and send data through it.


File:
"WaspGPRS_4_socketsTCP.pde"


Code



/*
 *  ------Waspmote GPRS Sockets Example--------
 *
 *  Explanation: This example shows how to open a TCP socket and send
 *  data through it.
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:    Alberto Bielsa
 */

void setup(){  
  
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");

  // setup for GPRS seial port
  GPRS.ON();
  USB.println("GPRS module ready...");
    
  // waiting while GPRS connects to the network
  while(!GPRS.check());
  USB.println("GPRS connected to the network");
  
  // configure SMS and Incoming Calls
  if(GPRS.setInfoIncomingCall()) USB.println("Info Incoming Call OK");
  if(GPRS.setInfoIncomingSMS()) USB.println("Info Incoming SMS OK");
  if(GPRS.setTextModeSMS()) USB.println("Text Mode SMS OK");
}

char* test="0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999";
char command[30];
uint8_t n=0;

void loop(){

  // Configure GPRS Connection
  if(GPRS.configureGPRS()) USB.println("Configured OK");
  if(GPRS.createSocket("www.libelium.com","80",GPRS_CLIENT)){
    USB.println("Socket Opened OK");
    USB.print("Session Number: ");
    USB.println(GPRS.socket_ID-'0',DEC);
  }
  delay(2000);
  
  
  /********************** UPLOAD *****************************************/
  
  // Send data via socket
  n=0;
  while(n<10){
    if(GPRS.sendData(test,GPRS.socket_ID)) USB.println("Data sent");
    n++;
  }

  
  /********************** DOWNLOAD *****************************************/
  
  // Get data from socket
  sprintf(command,"%s%c%c","GET / HTTP/1.0",'\r','\n');
  if(GPRS.sendData(command,GPRS.socket_ID)) USB.println("Data sent");  
  delay(2000);
  n=0;
  while(!n){
    n=GPRS.readData(GPRS.socket_ID,"90");
  }
  USB.print("Bytes Read: ");
  USB.println(GPRS.data_read,DEC);
  
  // Close socket
  if(GPRS.closeSocket(GPRS.socket_ID)) USB.println("Socket closed");

  // Delete socket
  if(GPRS.deleteSocket(GPRS.socket_ID)) USB.println("Socket deleted");
  
}
 

Download code

You can download the code of this example.


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use