Development

» Wifi 31: Update firmware

This example shows how to update firmwares version from Libelium's server. It is necessary to select the correct AP setting and socket in Waspmote where the WiFi module is plugged to.

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.
- The user MUST change the AP setting in the code.
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*
 *  ------Waspmote WIFI_31 Example--------
 *
 *  Explanation: This example shows how to update firmwares version 
 *  from Libelium's server. It is necessary to select the correct 
 *  AP setting and socket in Waspmote where the WiFi module is 
 *  plugged to.
 *
 *  Copyright (C) 2012 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.1
 *  Design:                 David Gascón
 *  Implementation:         Yuri Carmona
 */
 
// Include WIFI library 
#include <WaspWIFI.h>

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

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


// choose FTP settings (DO NOT CHANGE)
///////////////////////////////////////
#define USER "w@libelium.com"
#define PASS "libelium"
///////////////////////////////////////

// Define Firmwares to be updated
#define VERSION "wifly7-232.img" // Version <2.32>
//#define VERSION "wifly7-236.img" // Version <2.36>


char buffer[200];
char updateString[50];
int i=0;
unsigned long previous;


void setup()
{  
  // Call to local function in order to setup the wifi module parameters
  wifiSetup(); 

} 

void loop()
{  

  // Switch ON the WiFi module on the desired socket  
  if( WIFI.ON(socket) == 1 )
  {    
    USB.println(F("Switched ON"));
    
    // print actual version
    WIFI.getVersion();
    delay(500);
        
    if( WIFI.join(ESSID) )
    {

      USB.println(F("Joined AP")); 

      previous=millis();
      serialFlush(socket);      
      
      // set firmware to update 
      sprintf(updateString,"ftp update %s\r",VERSION);
      
      WIFI.send(updateString);
      delay(1000);
      i=0;
      while(1)
      {
        while(serialAvailable(socket))
        {
          buffer[i]=serialRead(socket);
          i++;
          buffer[i]='\0';
        }
        if(strstr(buffer,"UPDATE OK") != NULL)
        {
          // print actual version
          WIFI.send("reboot\r");
          USB.println(F("Update OK"));
          while(1)
          { 
            WIFI.OFF();
            delay(1000);
            WIFI.ON(socket);            
            WIFI.getVersion();
            delay(5000);
          }
        }
        if(millis()-previous>60000)
        {      
          USB.println(F("Update ERROR"));
          USB.println(buffer);
          while(1)
          { 
            WIFI.OFF();
            delay(1000);
            WIFI.ON(socket);            
            WIFI.getVersion();
            delay(5000);
          }
        }

      }

    }
    else
    {
      USB.println(F("ERROR joining"));
    }
  }
  else
  {
    USB.println(F("ERROR switching on"));
  }
}





/**********************************************************
 *
 *  wifiSetup - It sets the proper configuration to the WiFi 
 *  module prior to the attemp of updating the firmware
 *
 ************************************************************/
void wifiSetup()
{
  // Switch the module ON
  WIFI.ON(socket);
  
  // Reset to default values
  WIFI.resetValues();
  
  // 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(CLIENT_SERVER); 
  // 2. Configure the way the modules will resolve the IP address. 
  WIFI.setDHCPoptions(DHCP_ON); 
  // 3. Set the Flush buffer to 700 Bytes 
  WIFI.setCommSize(700);
  // 4. Set the Flush Timer to 50ms
  WIFI.setCommTimer(50);

  // 5 Set the server IP address to Libelium address, ports and FTP mode 
  WIFI.setFTP("46.105.20.38",21,FTP_PASIVE,20); 
   
  // 6. set ftp root directory
  printString("set f d .\r",socket);
  delay(300);

  // 7.Set the server account with the username and password 
  WIFI.openFTP(USER,PASS); 

  // 8. Configure how to connect the AP 
  WIFI.setJoinMode(MANUAL); 

  // 9. Set the AP authentication key
  WIFI.setAuthKey(WPA1,AUTHKEY); 

  // 10. Save Data to module's memory
  WIFI.storeData();

}








Output

H#
Wifi switched ON
Switched ON
Joined AP
Update OK

Quick Publish: