webupdate - dl3ebb/OpenIot GitHub Wiki

Web update

Webupdate is a simple method for deploying updates to a device, especially when it's already installed or when managing a large number of devices.

The web update process can be configured on the devices. Each device calls a script on the update server every minute, providing the application name. The server returns the highest version number available. The device compares this version with the currently installed version. If the version on the server is newer, the update can be performed. If auto-update is not enabled, the update can be triggered manually via the web frontend. When auto-update is enabled, the device automatically updates and restarts.

An update consists of two files: The binary file contains the compiled program code, and the image file is a compressed image of the filesystem, which typically includes files like web pages.

To try the following example, you need to set up an update server with a newer image. The setup for the update server is described on the Management server page. Here is the code for the example:

/**
 * Demo of the Web Update Server
 * 
 * To use this example you must 
 *   1. set up a web update server 
 *   2. Deploy a version of your application to this server. 
 * 
 */

#define SWITCH_PIN 25
#define LED_PIN    32
#define WLAN_SSID   "your_ssid"
#define WLAN_PASSWD "your_passwd"
#define WEB_UPDATE_SERVER "your_update_server"

#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
#include <Logger/SerialLogger.h>
#include <Modules/WebUpdate.h>

#include <Modules/Wifi.h>

void setup() {
    serialLogger->setBaudrate(115200);
    openIot.setLogger(serialLogger);
    
    wifi->setEnablePrimary(true);
    wifi->setSsid1(WLAN_SSID);
    wifi->setPassword1(WLAN_PASSWD);

    webUpdate->setEnable(true);
    webUpdate->setServer(WEB_UPDATE_SERVER);
    webUpdate->setAutoUpdate(true);

    /**
     * Create the Elements and register them to the connectors
     */
    BoolElement *switch1 = new BoolElement("Switch1");
    digitalInConnector->registerElement(switch1, SWITCH_PIN, true);

    BoolElement *led1 = new BoolElement("Led1");
    digitalOutConnector->registerElement(led1, LED_PIN);
    
    eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
        led1->setValue(event->newValue);
    });

    openIot.setup("OpenIotWebUpdate", 1);
}

void loop() {
    openIot.loop();
}

The web update is enabled and configured with these three lines:

webUpdate->setEnable(true);
webUpdate->setServer(WEB_UPDATE_SERVER);
webUpdate->setAutoUpdate(true);

The line:

openIot.setup("OpenIotWebUpdate", 1);

passes the application name and version number to the OpenIot core. To upload a new version for a software to the server, I use the following script:

#!/bin/bash

APP="OpenIotWebUpdate"
VERSION="2"
SERVER="[email protected]"
REMOTE_PATH="/var/www/html/update/img"
PROJECT_DIR="c:/Users/<user>/<project_dir>"
PLATFORMIO_BIN="c:/Users/<user>/.platformio/penv/Scripts/platformio.exe"

#---Compile project ---------------------------------
cd "$PROJECT_DIR" || { echo "Could not find project directory !"; exit 1; }

echo "Compile project."
if "$PLATFORMIO_BIN" run; then
    echo "Build successful"
else
    echo "Build failed !"
    exit 1
fi

#--- SPIFFS-Image erstellen ---------------------------------
echo "Create filesystem image."
if "$PLATFORMIO_BIN" run --target buildfs; then
    echo "Build successful"
else
    echo "Build failed !"
    exit 1
fi

#--- Copy files to server ---------------------------------
REMOTE_FILE="${REMOTE_PATH}/${APP}_${VERSION}.img"
scp .pio/build/esp32dev/spiffs.bin "${SERVER}:${REMOTE_FILE}"

REMOTE_FILE="${REMOTE_PATH}/${APP}_${VERSION}.bin"
scp .pio/build/esp32dev/firmware.bin "${SERVER}:${REMOTE_FILE}"

At the top of the script, the application name and version are specified. This will create OpenIotWebUpdate Version 2

The server is the user and server for the later scp command.

REMOTE_PATH is the path to the image directory on the update server. This should match if the server was installed according to my instructions.

PROJECT_DIR and PLATFORMIO_BIN must be adapted to your local environment.

First, the binary file is generated, then the SPIFFS filesystem is created. Both files are then copied to the update server.

Dont forget to change

openIot.setup("OpenIotWebUpdate", 1);

to

openIot.setup("OpenIotWebUpdate", 2);

Before generating the image or you will create an update loop


| ← Previous Page (NTP) | ↑ Tutorial Main Page | Next Page (MQTT Status) → |

⚠️ **GitHub.com Fallback** ⚠️