Development

» 4G 14: UDP server

This example shows how to open a UDP listening socket and waits for incoming data from UDP clients. After correctly to a UDP client, sending/receiving functions are shown

Required Materials

1 x Waspmote
1 x Battery
1 x 4G module
1 x 4G antenna
1 x SIM card

Notes

- The battery has to be connected.
- This example can be executed in Waspmote v15

Code

/*
    --- 4G_14 - UDP server  ---

    Explanation: This example shows how to open a UDP listening socket
    and waits for incoming data from UDP clients. After correctly  to a 
    UDP client, sending/receiving functions are shown

    Copyright (C) 2016 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 3 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 <http://www.gnu.org/licenses/>.

    Version:           3.0
    Design:            David Gascón
    Implementation:    Alejandro Gállego
*/

#include <Wasp4G.h>
#include <WaspFrame.h>


// APN settings
///////////////////////////////////////
char apn[] = "movistar.es";
char login[] = "movistar";
char password[] = "movistar";
///////////////////////////////////////

// SERVER settings
///////////////////////////////////////
uint16_t local_port = 5000;
uint8_t keep_alive  = 240; // From 0 (disabled) to 240 minutes
///////////////////////////////////////

// define Socket ID (from 'CONNECTION_1' to 'CONNECTION_6'
///////////////////////////////////////
uint8_t connId = Wasp4G::CONNECTION_1;
///////////////////////////////////////


// define variables
uint8_t  error;
uint32_t previous;
uint8_t  socketIndex;
boolean  udp_server_status = false;
uint8_t  socket_state;


void setup()
{
  USB.ON();
  USB.println(F("Start program"));

  //////////////////////////////////////////////////
  // 1. sets operator parameters
  //////////////////////////////////////////////////
  _4G.set_APN(apn, login, password);


  //////////////////////////////////////////////////
  // 2. Show APN settings via USB port
  //////////////////////////////////////////////////
  _4G.show_APN();


  //////////////////////////////////////////////////
  // 3. Switch ON
  //////////////////////////////////////////////////
  error = _4G.ON();

  if (error == 0)
  {
    USB.println(F("4G module ready"));

    ////////////////////////////////////////////////
    // Enter PIN code
    ////////////////////////////////////////////////

    /*
      USB.println(F("Setting PIN code..."));
      if (_4G.enterPIN("****") == 1)
      {
      USB.println(F("PIN code accepted"));
      }
      else
      {
      USB.println(F("PIN code incorrect"));
      }
    */

  }
  else
  {
    // Problem with the communication with the 4G module
    USB.println(F("4G module not started"));
  }
}



void loop()
{

  ///////////////////////////////////////////////////////
  // 1. Open UDP Listen on port
  ///////////////////////////////////////////////////////

  error = _4G.openSocketServer(connId, Wasp4G::UDP, local_port);

  if (error == 0)
  {
    USB.println(F("1. Socket open"));

    USB.println(F("----------------------------"));
    USB.print(F("IP address: "));
    USB.println(_4G._ip);
    USB.print(F("Local port: "));
    USB.println(local_port);
    USB.println(F("----------------------------"));

    // update flag
    udp_server_status = true;
  }
  else
  {
    USB.print(F("1. Error opening socket. Error code: "));
    USB.println(error, DEC);

    // update flag
    udp_server_status = false;
  }


  ///////////////////////////////////////////////////////
  // 2. Wait for incoming data from UDP client
  ///////////////////////////////////////////////////////

  USB.println(F("2. Wait for incoming data from UDP client (30 secs): "));

  error = _4G.manageSockets(30000);

  if (error == 0)
  {
    if (_4G.socketStatus[connId].state == Wasp4G::STATUS_SUSPENDED_DATA)
    {
      USB.println(F("\nData in socket:"));
      USB.println(F("-------------------------------------"));
      USB.print(F("Socket ID: "));
      USB.println(_4G.socketStatus[connId].id, DEC);
      USB.print(F("Socket State: "));
      USB.println(_4G.socketStatus[connId].state, DEC);
      USB.print(F("Socket Local IP: "));
      USB.println(_4G.socketStatus[connId].localIp);
      USB.print(F("Socket Local Port: "));
      USB.println(_4G.socketStatus[connId].localPort, DEC);
      USB.print(F("Socket Remote IP: "));
      USB.println(_4G.socketStatus[connId].remoteIp);
      USB.print(F("Socket Remote Port: "));
      USB.println(_4G.socketStatus[connId].remotePort, DEC);
      USB.println(F("-------------------------------------"));

      // update flag
      udp_server_status = true;
    }
    else
    {
      udp_server_status = false;
      USB.print(F("3. Incorrect Socket State: "));
      USB.println(_4G.socketStatus[connId].state, DEC);      
    }
  }
  else
  {
    USB.print(F("3. No incoming data. Code: "));
    USB.println(error, DEC);

    // update flag
    udp_server_status = false;
  }



  ///////////////////////////////////////////////////////
  // 3. Loop while connected
  ///////////////////////////////////////////////////////

  while (udp_server_status == true)
  {
    //////////////////////////////////////////////
    // 3.1. Get socket info
    //////////////////////////////////////////////
    error = _4G.getSocketInfo(connId);

    if (error == 0)
    {
      USB.println(F("3.1. Socket Info since it was opened:"));
      USB.println(F("-------------------------------------"));
      USB.print(F("Socket ID: "));
      USB.println(_4G.socketInfo[connId].id, DEC);
      USB.print(F("Socket Sent bytes: "));
      USB.println(_4G.socketInfo[connId].sent, DEC);
      USB.print(F("Socket Received bytes: "));
      USB.println(_4G.socketInfo[connId].received, DEC);
      USB.print(F("Socket pending bytes read: "));
      USB.println(_4G.socketInfo[connId].size);
      USB.print(F("Socket bytes sent and not yet acked: "));
      USB.println(_4G.socketInfo[connId].ack, DEC);
      USB.println(F("-------------------------------------"));
    }
    else
    {
      USB.print(F("3.1. Error getting socket info. Erro code: "));
      USB.println(error, DEC);
    }
    
    //////////////////////////////////////////////
    // 3.2. Receive data
    //////////////////////////////////////////////

    // Wait for incoming data from the socket (if the other side responds)
    USB.print(F("3.2. Waiting to receive data (30 secs): "));

    error = _4G.receive(connId, 30000);

    if (error == 0)
    {
      if (_4G.socketInfo[connId].size > 0)
      {
        USB.println(F("\nData received:"));
        USB.println(F("====================================="));
        USB.println(_4G._buffer, _4G._length);
        USB.println(F("====================================="));
      }
      else
      {
        USB.println(F("NO data received"));
      }
    }
    else
    {
      USB.print(F("No data received. Code:"));
      USB.println(error, DEC);
    }

    //////////////////////////////////////////////
    // 3.3. Send data
    //////////////////////////////////////////////
   error = _4G.send(connId, "This is a message from Waspmote UDP server\n");

    if (error == 0)
    {
      USB.println(F("3.3. Data sent via UDP socket"));
    }
    else
    {
      USB.println(F("3.3. Error sending data via UDP socket"));
    }

    //////////////////////////////////////////////
    // 3.4. Check Socket Listen status
    /////////////////////////////////////////////
    error = _4G.getSocketStatus(connId);

    if (error == 0)
    {
      // get state
      socket_state = _4G.socketStatus[connId].state;
      
      USB.print(F("3.4. Get socket status OK: "));
      USB.println(socket_state, DEC);


      // check socket status
      if (socket_state == Wasp4G::STATUS_CLOSED)
      {
        USB.println(F("SOCKET CLOSED"));

        // update flag
        udp_server_status = false;
      }
    }
    else
    {
      USB.print(F("3.4. Error getting socket status. Error code: "));
      USB.println(error, DEC);

      // update flag
      udp_server_status = false;
    }

    USB.println();
  }


  ///////////////////////////////////////////////////////
  // 4. Close socket
  ///////////////////////////////////////////////////////
  error = _4G.closeSocketServer(connId, Wasp4G::UDP);

  if (error == 0)
  {
    USB.println(F("4. Socket closed OK"));
  }
  else
  {
    USB.print(F("4. Error closing socket. Error code: "));
    USB.println(error, DEC);
  }

  USB.println();
}

Output

Start program
*****************************
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
*****************************
4G module ready
1. Socket open
----------------------------
IP address: 80.27.175.252
Local port: 5000
----------------------------
2. Wait for incoming data from UDP client (30 secs):

Data in socket:
-------------------------------------
Socket ID: 1
Socket State: 3
Socket Local IP: 80.27.175.252
Socket Local Port: 5000
Socket Remote IP: 213.97.9.230
Socket Remote Port: 55565
-------------------------------------
3.1. Socket Info since it was opened:
-------------------------------------
Socket ID: 1
Socket Sent bytes: 0
Socket Received bytes: 0
Socket pending bytes read: 17
Socket bytes sent and not yet acked: 0
-------------------------------------
3.2. Waiting to receive data (30 secs):
Data received:
=====================================
Hello from Client
=====================================
3.3. Data sent via UDP socket
3.4. Get socket status OK: 2

3.1. Socket Info since it was opened:
-------------------------------------
Socket ID: 1
Socket Sent bytes: 43
Socket Received bytes: 17
Socket pending bytes read: 0
Socket bytes sent and not yet acked: 0
-------------------------------------
3.2. Waiting to receive data (30 secs):
Data received:
=====================================
Hello from Client for second time
=====================================
3.3. Data sent via UDP socket
3.4. Get socket status OK: 2

3.1. Socket Info since it was opened:
-------------------------------------
Socket ID: 1
Socket Sent bytes: 86
Socket Received bytes: 50
Socket pending bytes read: 0
Socket bytes sent and not yet acked: 0
-------------------------------------
3.2. Waiting to receive data (30 secs):


.......

Quick Publish: