Development v12

Warning - Product Update

All the resources present on this section belong to Waspmote v12, Waspmote Plug & Sense! v12 and Meshlium v3.8.

If you have a Waspmote v15, Waspmote Plug & Sense! v15 or Meshlium v4.0 please go to the new Development Section.

Check what is your version and what are the differences between the old and the new one in this document.

» Wifi 26c: HTTP GET (to Meshlium)

This example shows how to send a Waspmote Frame to Meshlium using an HTTP query. The user only has to insert the IP address (or the URL), the port and then use the proper function indicating the frame.buffer pointer and frame.length. When the Meshlium Parser receives the info, the different sensor values are stored in the Data Base automatically.

Required Materials

1 x Waspmote
1 x Battery
1 x WiFi Module
1 x Meshlium

Notes

- The battery has to be connected.
- Plug the antenna in the SMA connector.
- Remember to set the correct AP settings
- Make sure the Meshlium is updated with the last Manager System version
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*
 *  ------Waspmote WIFI_26c Example--------
 *
 *  Explanation: This example shows how to send a Waspmote Frame to
 *  Meshlium using an HTTP query. The user only has to insert the IP 
 *  address (or the URL), the port and then use the proper function 
 *  indicating the frame.buffer pointer and frame.length. When the 
 *  Meshlium Parser receives the info, the different sensor values 
 *  are stored in the Data Base automatically.
 *
 *  Copyright (C) 2015 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.2
 *  Design:                 David Gascón
 *  Implementation:         Yuri Carmona
 */

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

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

// WiFi AP settings (CHANGE TO USER'S AP)
/////////////////////////////////
char ESSID[] = "libelium_AP";
char AUTHKEY[] = "password";
/////////////////////////////////

// MESHLIUM settings
///////////////////////////////////////////////////////////////
char ADDRESS[] = "10.10.10.1";
int REMOTE_PORT = 80;
///////////////////////////////////////////////////////////////


// define variable for communication status
uint8_t status;



void setup()
{
  // set Waspmote ID
  frame.setID("WIFI_001");
  
  
  // Switch ON the WiFi module on the desired socket
  if( WIFI.ON(socket) == 1 )
  {    
    USB.println(F("WiFi switched ON"));
  }
  else
  {
    USB.println(F("WiFi did not initialize correctly"));
  }

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

  USB.println(F("WiFi setup done"));


}


void loop()
{ 
  // Switch ON the WiFi module on the desired socket
  if( WIFI.ON(socket) == 1 )
  {    
    USB.println(F("WiFi switched ON"));
  }
  else
  {
    USB.println(F("WiFi did not initialize correctly"));
  }

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

    ///////////////////////////////////////////
    // Sensor measurements
    ///////////////////////////////////////////  
    RTC.ON();
    RTC.getTime();

    ///////////////////////////////////////////
    // Create ASCII frame
    ///////////////////////////////////////////  

    // create new frame
    frame.createFrame(ASCII);  

    // add frame fields
    frame.addSensor(SENSOR_TIME, RTC.hour, RTC.minute, RTC.second ); 
    frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel()); 

    // show created frame
    frame.showFrame();    

    /////////////////////////////////////////// 
    // Send the HTTP query (specifying the url address)
    ///////////////////////////////////////////     
    status = WIFI.sendHTTPframe(IP, ADDRESS, REMOTE_PORT, frame.buffer, frame.length);  

    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("HTTP query ERROR"));
    }     

  } 
  else
  {
    USB.println(F("NOT joined"));
  }


  WIFI.OFF();
  USB.println(F("\n******************************\n"));
  delay(1000);


} 

Output

H#
WiFi switched ON
WiFi setup done
WiFi switched ON
Joined AP
===============================
Current ASCII Frame:
Length: 47
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 34 39 38 33 23 57 49 46 49 5F 30 30 31 23 30 23 54 49 4D 45 3A 32 2D 32 38 2D 32 36 23 42 41 54 3A 37 34 23
String: <=>€#382544983#WIFI_001#0#TIME:2-28-26#BAT:74#
===============================

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Wed, 03 Sep 2014 15:11:43 GMT
Server: Apache/2.2.9 (Debian)
X-Powered-By: PHP/5.2.6-1+lenny8
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html

*CLOS*

******************************

WiFi switched ON
Joined AP
===============================
Current ASCII Frame:
Length: 47
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 34 39 38 33 23 57 49 46 49 5F 30 30 31 23 31 23 54 49 4D 45 3A 32 2D 32 38 2D 34 33 23 42 41 54 3A 37 34 23
String: <=>€#382544983#WIFI_001#1#TIME:2-28-43#BAT:74#
===============================

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Wed, 03 Sep 2014 15:12:01 GMT
Server: Apache/2.2.9 (Debian)
X-Powered-By: PHP/5.2.6-1+lenny8
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html

*CLOS*

******************************

WiFi switched ON
Joined AP
===============================
Current ASCII Frame:
Length: 46
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 34 39 38 33 23 57 49 46 49 5F 30 30 31 23 32 23 54 49 4D 45 3A 32 2D 32 39 2D 30 23 42 41 54 3A 37 35 23
String: <=>€#382544983#WIFI_001#2#TIME:2-29-0#BAT:75#
===============================

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Wed, 03 Sep 2014 15:12:18 GMT
Server: Apache/2.2.9 (Debian)
X-Powered-By: PHP/5.2.6-1+lenny8
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html

*CLOS*

******************************

WiFi switched ON
Joined AP
===============================
Current ASCII Frame:
Length: 47
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 34 39 38 33 23 57 49 46 49 5F 30 30 31 23 33 23 54 49 4D 45 3A 32 2D 32 39 2D 31 37 23 42 41 54 3A 37 35 23
String: <=>€#382544983#WIFI_001#3#TIME:2-29-17#BAT:75#
===============================

HTTP query OK.
WIFI.answer:HTTP/1.1 200 OK
Date: Wed, 03 Sep 2014 15:12:36 GMT
Server: Apache/2.2.9 (Debian)
X-Powered-By: PHP/5.2.6-1+lenny8
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html

*CLOS*

******************************

WiFi switched ON
Joined AP
===============================
Current ASCII Frame:
Length: 47
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 34 39 38 33 23 57 49 46 49 5F 30 30 31 23 34 23 54 49 4D 45 3A 32 2D 32 39 2D 33 35 23 42 41 54 3A 37 34 23
String: <=>€#382544983#WIFI_001#4#TIME:2-29-35#BAT:74#
===============================

Quick Publish: