Development

Communication / WiFi:

» Wifi 26: HTTP GET

This example shows how to send a HTTP GET request message.

Required Materials

1 x Waspmote
1 x Battery
1 x WiFi Module

Notes

- The battery has to be connected.
- Plug the antenna in the SMA connector.
- Remember to set the correct AP settings
- Make sure you have a php file in the server you set listening to messages

Code

/*
 *  ------Waspmote WIFI_26 Example--------
 *
 *  Explanation: This example shows how to send a HTTP get request 
 *  message. 
 *
 *  Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.3
 *  Design:                 David Gascón
 *  Implementation:         Joaquin Ruiz, Yuri Carmona
 */

// Include WIFI library 
#include <WaspWIFI.h>

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

// WiFi AP settings (CHANGE TO USER'S AP)
/////////////////////////////////
#define ESSID "libelium_AP"
#define AUTHKEY "password"
/////////////////////////////////

// WEB server settings 
/////////////////////////////////
#define WEB_SERVER "pruebas.libelium.com"
/////////////////////////////////


// define variable for communication status
uint8_t status;
uint8_t counter=0;
char sentence[100];
unsigned long previous;


void setup()
{
  if( WIFI.ON(socket) == 1 )
  {    
    USB.println(F("WiFi switched ON"));
  }
  else
  {
    USB.println(F("WiFi did not initialize correctly"));
  }

  // reset to avoid previous configuration stored in the module
  //WIFI.resetValues();

  // 1. Configure the transport protocol (UDP, TCP, FTP, HTTP...)
  WIFI.setConnectionOptions(HTTP|CLIENT_SERVER);
  // 2. Configure the way the modules will resolve the IP address.
  WIFI.setDHCPoptions(DHCP_ON);
  // 3. Configure how to connect the AP 
  WIFI.setJoinMode(MANUAL);   
  // 4. Set the AP authentication key
  WIFI.setAuthKey(WPA1,AUTHKEY); 
  // 5. Save Data to module's memory
  WIFI.storeData();

}


void loop()
{ 
  // switch on the WiFi module  
  WIFI.ON(socket);

  // If it is manual, call join giving the name of the AP     
  if( WIFI.join(ESSID) )
  { 
    USB.println(F("Joined"));

    /////////////////////////////////////////// 
    // Create the sentence
    /////////////////////////////////////////// 
    RTC.ON();
    RTC.getTime();
    counter++;
    sprintf(sentence,"GET$/test-get-post.php?counter=%u&time=%02u:%02u:%02u",counter,RTC.hour,RTC.minute,RTC.second);

    USB.print("sentence:");
    USB.println(sentence);

    /////////////////////////////////////////// 
    // Send the HTTP get/post query (specifying the WEB server so DNS is used)
    /////////////////////////////////////////// 
    status = WIFI.getURL(DNS, WEB_SERVER, sentence);

    if( status == 1)
    {
      USB.println(F("\nHTTP query OK."));
      USB.print(F("WIFI.answer:"));
      USB.println(WIFI.answer);
      
      /*
       * At this point, it could be possible
       * to parse the web server information
       */
    }
    else
    {
      USB.println(F("\nHTTP query ERROR"));
      counter--; 
    }
  } 
  else
  {
    USB.println(F("NOT joined"));
  }
  
  
  // switch off module 
  WIFI.OFF();  
  USB.println(F("***************************"));  
  delay(1000);
} 

Output

D#
WiFi switched ON
Joined
sentence:GET$/test-get-post.php?counter=1&time=18:03:54

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Tue, 04 Feb 2014 17:04:31 GMT
Server: Apache
X-Powered-By: PHP/5.3.28
Connection: close
Content-Type: text/html

method::get;counter::1;time::18:03:54;*CLOS*
***************************
Joined
sentence:GET$/test-get-post.php?counter=2&time=18:04:10

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Tue, 04 Feb 2014 17:04:46 GMT
Server: Apache
X-Powered-By: PHP/5.3.28
Connection: close
Content-Type: text/html

method::get;counter::2;time::18:04:10;*CLOS*
***************************
...

Quick Publish: