Development

Communication / WiFi PRO:

» WiFi PRO 14: HTTPS GET

This example shows how to perform HTTPS GET requests. It is mandatory to provide the trusted Certificate Authority (CA) in order to securely identify the server

Required Materials

1 x Waspmote
1 x Battery
1 x WiFi PRO Module

Notes

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

Code

/*  
 *  ------ WIFI Example -------- 
 *  
 *  Explanation: This example shows how to perform HTTPS GET requests.
 *  It is mandatory to provide the trusted Certificate Authority (CA)
 *  in order to securely identify the server.
 *  
 *  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 Gascon 
 *  Implementation:    Yuri Carmona
 */

// Put your libraries here (#include ...)
#include <WaspWIFI_PRO.h>


// choose socket (SELECT USER'S SOCKET)
///////////////////////////////////////
uint8_t socket = SOCKET0;
///////////////////////////////////////


// SERVER settings
///////////////////////////////////////
char type[] = "https";
char host[] = "test.libelium.com";
char port[] = "443";
char link[] = "getpost_frame_parser.php?counter=1&varA=1&varB=2&varC=3&varE=4&varE=5";
///////////////////////////////////////


// define trusted Certificate Authority
////////////////////////////////////////////////////////////////////////
char TRUSTED_CA[] =\ 
"-----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-----";
////////////////////////////////////////////////////////////////////////


uint8_t error;
uint8_t status;
unsigned long previous;



void setup() 
{
  USB.println(F("Start program"));  
  USB.println(F("***************************************"));  
  USB.println(F("Once the module is set with one or more"));
  USB.println(F("AP settings, it attempts to join the AP"));
  USB.println(F("automatically once it is powered on"));    
  USB.println(F("Refer to example 'WIFI_PRO_01' to configure"));  
  USB.println(F("the WiFi module with proper settings"));
  USB.println(F("***************************************"));

  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////  
  error = WIFI_PRO.ON(socket);

  if (error == 0)
  {    
    USB.println(F("1. WiFi switched ON"));
  }
  else
  {
    USB.println(F("1. WiFi did not initialize correctly"));
  }


  //////////////////////////////////////////////////
  // 2. Set Trusted CA
  //////////////////////////////////////////////////  
  error = WIFI_PRO.setCA(TRUSTED_CA);

  if (error == 0)
  {    
    USB.println(F("2. Trusted CA set OK"));
  }
  else
  {
    USB.println(F("2. Error calling 'setCA' function"));
    WIFI_PRO.printErrorCode();
  }
  
  
  //////////////////////////////////////////////////
  // 3. Switch OFF
  //////////////////////////////////////////////////  
  WIFI_PRO.OFF(socket);
  USB.println(F("3. WiFi switched OFF"));
  USB.println(F("Setup done\n\n"));
}





void loop()
{   
  // get actual time
  previous = millis();

  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////  
  error = WIFI_PRO.ON(socket);

  if (error == 0)
  {    
    USB.println(F("1. WiFi switched ON"));
  }
  else
  {
    USB.println(F("1. WiFi did not initialize correctly"));
  }


  //////////////////////////////////////////////////
  // 2. Join AP
  //////////////////////////////////////////////////  

  // check connectivity
  status =  WIFI_PRO.isConnected();

  // Check if module is connected
  if (status == true)
  {    
    USB.print(F("2. WiFi is connected OK"));
    USB.print(F(" Time(ms):"));    
    USB.println(millis()-previous);

    //////////////////////////////////////////////// 
    // http request
    //////////////////////////////////////////////// 
    error = WIFI_PRO.getURL( type, host, port, link);

    // check response
    if (error == 0)
    {
      USB.println(F("HTTPS GET OK"));
      USB.print(F("HTTP Time from OFF state (ms):"));
      USB.println(millis()-previous);
      
      USB.print(F("Server answer:"));
      USB.println(WIFI_PRO._buffer, WIFI_PRO._length);
    }
    else
    {
      USB.println(F("Error calling 'getURL' function"));
      WIFI_PRO.printErrorCode();
    }


  }
  else
  {
    USB.print(F("3. WiFi is connected ERROR")); 
    USB.print(F(" Time(ms):"));    
    USB.println(millis()-previous);  
  }


  //////////////////////////////////////////////////
  // 3. Switch OFF
  //////////////////////////////////////////////////  
  WIFI_PRO.OFF(socket);
  USB.println(F("3. WiFi switched OFF\n\n\n")); 
  delay(5000);
}

Output

H#
Start program
***************************************
Once the module is set with one or more
AP settings, it attempts to join the AP
automatically once it is powered on
Refer to example 'WIFI_PRO_01' to configure
the WiFi module with proper settings
***************************************
1. WiFi switched ON
2. Trusted CA set OK
3. WiFi switched OFF
Setup done


1. WiFi switched ON
2. WiFi is connected OK Time(ms):5724
HTTPS GET OK
HTTP Time from OFF state (ms):9434
Server answer:method::GET;
counter::1;
varA::1;
varB::2;
varC::3;
varE::5;
error::id_wasp;


3. WiFi switched OFF



1. WiFi switched ON
2. WiFi is connected OK Time(ms):5153
HTTPS GET OK
HTTP Time from OFF state (ms):5818
Server answer:method::GET;
counter::1;
varA::1;
varB::2;
varC::3;
varE::5;
error::id_wasp;


3. WiFi switched OFF

Quick Publish: