Development

» WiFi PRO 15: HTTPS POST

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

#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 url[]  = "getpost_frame_parser.php?";
///////////////////////////////////////


// define trusted Certificate Authority
////////////////////////////////////////////////////////////////////////
char TRUSTED_CA[] =\ 
"-----BEGIN CERTIFICATE-----\r"\
"MIICNjCCAZ+gAwIBAgIJAL5/5O7w2Cm5MA0GCSqGSIb3DQEBCwUAMFMxLTArBgNV\r"\
"BAoMJExpYmVsaXVtIENvbXVuaWNhY2lvbmVzIERpc3RyaWJ1aWRhczELMAkGA1UE\r"\
"BhMCRVMxFTATBgNVBAMMDGxpYmVsaXVtLmNvbTAgFw0xNzAxMjQwOTU1MDJaGA8y\r"\
"MTE2MTIzMTA5NTUwMlowUzEtMCsGA1UECgwkTGliZWxpdW0gQ29tdW5pY2FjaW9u\r"\
"ZXMgRGlzdHJpYnVpZGFzMQswCQYDVQQGEwJFUzEVMBMGA1UEAwwMbGliZWxpdW0u\r"\
"Y29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfI9j2DbbuK1fUrH1RKmnU\r"\
"EQ22r7FAT+R7uxOhSBnx61qlLjtZT9zuA7eMuq9k3tUBSkMxJjai6ebqmvPUpgrU\r"\
"0EoCZg+PrAglcvqAkzv8QDRueEi0hLCa8iTUsuora3viKMefbFR8ROH5uQrFnZK1\r"\
"1aUQxeV0HBL9zIH8ghaLmwIDAQABoxAwDjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3\r"\
"DQEBCwUAA4GBAG2bWPWsfHzSqwlzY+5kJDeAgJ7GfQo51+QXqMq5nnjcPhgkIkvz\r"\
"IVOO2WM01Pnm3LuEQ3YS8eHS1blOL8i7GsxxIMR6aQ8E0XYbcizPvcyL+NAdIodd\r"\
"bSa087amkPIVcwETuGf2JdpbJLEjWayfcE1Ll+DA7UfX9korLzZzfDxX\r"\
"-----END CERTIFICATE-----";
////////////////////////////////////////////////////////////////////////


// example of body for POST request
char body[] = "counter=1&variable1=1&variable2=2&variable3=3&variable4=4&variable5=5&variable6=6&variable7=7&variable8=8&variable9=9&variable10=10&variable11=11&variable12=12&variable13=13&variable14=14&varia15=15";

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. Set URL
  //////////////////////////////////////////////////
  error = WIFI_PRO.setURL( type, host, port, url );

  // check response
  if (error == 0)
  {
    USB.println(F("3. Set URL OK"));           
  }
  else
  {
    USB.println(F("3. Error calling 'setURL' function"));
    WIFI_PRO.printErrorCode();
  }

  //////////////////////////////////////////////////
  // 4. Switch OFF
  //////////////////////////////////////////////////  
  WIFI_PRO.OFF(socket);
  USB.println(F("4. 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.post(body); 
    

    // check response
    if( error == 0 )
    {
      USB.println(F("HTTPS POST 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 'post' function"));
      WIFI_PRO.printErrorCode();
    }
  }
  else
  {
    USB.print(F("2. 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")); 
  delay(10000);
}

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. Set URL OK
4. WiFi switched OFF
Setup done


1. WiFi switched ON
2. WiFi is connected OK Time(ms):5164
HTTPS POST OK
HTTP Time from OFF state (ms):9266
Server answer:method::POST;
POST;counter::1;
POST;variable1::1;
POST;variable2::2;
POST;variable3::3;
POST;variable4::4;
POST;variable5::5;
POST;variable6::6;
POST;variable7::7;
POST;variable8::8;
POST;variable9::9;
POST;variable10::10;
POST;variable11::11;
POST;variable12::12;
POST;variable13::13;
POST;variable14::14;
POST;varia15::15;
error::id_wasp;


3. WiFi switched OFF


1. WiFi switched ON
2. WiFi is connected OK Time(ms):5710
HTTPS POST OK
HTTP Time from OFF state (ms):6778
Server answer:method::POST;
POST;counter::1;
POST;variable1::1;
POST;variable2::2;
POST;variable3::3;
POST;variable4::4;
POST;variable5::5;
POST;variable6::6;
POST;variable7::7;
POST;variable8::8;
POST;variable9::9;
POST;variable10::10;
POST;variable11::11;
POST;variable12::12;
POST;variable13::13;
POST;variable14::14;
POST;varia15::15;
error::id_wasp;


3. WiFi switched OFF

Quick Publish: