Development

Communication / 4G:

» 4G 15: SSL sockets

This example shows how to use the SSL commands so as to open a TCP client socket to the specified server address and port

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_15 - SSL functions for TCP sockets  ---

    Explanation: This example shows how to use the SSL commands so as
    to open a TCP client socket to the specified server address and port

    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.1
    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
///////////////////////////////////////
char host[] = "test.libelium.com";
uint16_t remote_port = 443;
///////////////////////////////////////

// define data to send through TCP socket
///////////////////////////////////////
char data[] =
  "GET /getpost_frame_parser.php?counter=1 HTTP/1.1\r\n"\
  "Host: test.libelium.com\r\n"\
  "Content-Length: 0\r\n\r\n";
///////////////////////////////////////

// define Socket ID (mandatory CONNECTION_1)
///////////////////////////////////////
uint8_t socketId = Wasp4G::CONNECTION_1;
///////////////////////////////////////


// define certificate for SSL
////////////////////////////////////////////////////////////////////////
char certificate[] =//
  "-----BEGIN CERTIFICATE-----\r"\
  "MIICGzCCAYQCCQCWP2pnP2wL+TANBgkqhkiG9w0BAQQFADBSMSwwKgYDVQQKDCNM\r"\
  "aWJlbGl1bSBDb211bmlhY2lvbmVzIERpc3RyaWJ1aWRhczELMAkGA1UEBhMCRVMx\r"\
  "FTATBgNVBAMMDGxpYmVsaXVtLmNvbTAeFw0xNTA3MDYxMTAyNDJaFw0yNTA3MDMx\r"\
  "MTAyNDJaMFIxLDAqBgNVBAoMI0xpYmVsaXVtIENvbXVuaWFjaW9uZXMgRGlzdHJp\r"\
  "YnVpZGFzMQswCQYDVQQGEwJFUzEVMBMGA1UEAwwMbGliZWxpdW0uY29tMIGfMA0G\r"\
  "CSqGSIb3DQEBAQUAA4GNADCBiQKBgQCznwc4Rt6HF4CumZqDkMPL9Wn73kyoVDiT\r"\
  "kTST6Gj8IUsqnwftnu959Oqlow5X80foNu/o88zj8bbrSpXsaqaD9Wjt2zDJkdSL\r"\
  "42uElTDY+BuyUEY84L3JJ7InDPSyduayhXlKquNlhjP1SGX/q8WNVPzL05Sw1pPR\r"\
  "QPSt0ow82wIDAQABMA0GCSqGSIb3DQEBBAUAA4GBALEtLedkIjtsCHXxEZeuUA2t\r"\
  "DhIPBt2rIUUKOjcdOtC0AeQzalX1ln279KOoD86NShCRMkKl24SCFgXwQ0e8TcQ3\r"\
  "9Le1A24vGmZtJvc+MFh1bS/b2KmHYOj0ie8QmHBSMVxMIc/opFy3BAmLC9V/90hj\r"\
  "BziFIf5Ff7pZvzBoimQi\r"\
  "-----END CERTIFICATE-----";
////////////////////////////////////////////////////////////////////////


// define variables
uint8_t  error;
uint32_t previous;
uint8_t  socketIndex;


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();


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

  if (error == 0)
  {
    USB.println(F("1. 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"));
      }
    */


    ////////////////////////////////////////////////
    // 2. Set CA certificate
    ////////////////////////////////////////////////

    error = _4G.manageSSL(socketId, Wasp4G::SSL_ACTION_STORE, Wasp4G::SSL_TYPE_CA_CERT, certificate);

    if (error == 0)
    {
      USB.println(F("2. Set CA certificate OK"));
    }
    else
    {
      USB.print(F("2. Error setting CA certificate. Error="));
      USB.println(error, DEC);
    }
  }


  ////////////////////////////////////////////////
  // 3. Powers off the 4G module
  ////////////////////////////////////////////////
  _4G.OFF();

  USB.println(F("3. Switch OFF 4G module\n\n"));
}




void loop()
{
  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////
  error = _4G.ON();

  if (error == 0)
  {
    USB.println(F("1. 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"));
      }
    */


    ////////////////////////////////////////////////
    // 2. TCP socket
    ////////////////////////////////////////////////

    error = _4G.openSocketSSL(socketId, host, remote_port);

    if (error == 0)
    {
      USB.println(F("2.1. Opening a socket... done!"));


      //////////////////////////////////////////////
      // 2.2. Send data through socket
      //////////////////////////////////////////////

      error = _4G.sendSSL(socketId, data);
      if (error == 0)
      {
        USB.println(F("2.2. Sending data... done!"));
      }
      else
      {
        USB.print(F("2.2. Error sending data. Code: "));
        USB.println(error, DEC);
      }

      //////////////////////////////////////////////
      // 2.3. Receive data
      //////////////////////////////////////////////

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

      error = _4G.receiveSSL(socketId, 60000);

      if (error == 0)
      {
        if (_4G._length > 0)
        {
          USB.println(F("\n-----------------------------------"));
          USB.print(F("Data received:"));
          USB.println(_4G._buffer, _4G._length);
          USB.println(F("-----------------------------------"));
        }
        else
        {
          USB.println(F("NO Data received"));
        }
      }
      else
      {
        USB.print(F("No data received. Error code:"));
        USB.println(error, DEC);
      }

      //////////////////////////////////////////////
      // 2.4. Close socket
      //////////////////////////////////////////////
      error = _4G.closeSocketSSL(socketId);

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

    }
    else
    {
      USB.print(F("2.1. Error opening socket. Error code: "));
      USB.println(error, DEC);
    }

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

  ////////////////////////////////////////////////
  // 3. Powers off the 4G module
  ////////////////////////////////////////////////
  USB.println(F("3. Switch OFF 4G module"));
  _4G.OFF();


  ////////////////////////////////////////////////
  // 4. Sleep
  ////////////////////////////////////////////////
  USB.println(F("4. Enter deep sleep..."));
  PWR.deepSleep("00:00:00:10", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

  USB.ON();
  USB.println(F("5. Wake up!!\n\n"));

}

Output

Start program
*****************************
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
*****************************
1. 4G module ready...
2. Set CA certificate OK
3. Switch OFF 4G module


1. 4G module ready...
2.1. Opening a socket... done!
2.2. Sending data... done!
2.3. Waiting to receive data...No data received. Error code:3
2.4. Socket closed OK
3. Switch OFF 4G module
4. Enter deep sleep...
5. Wake up!!


Quick Publish: