Download html files during OTA
This commit is contained in:
parent
384ec017a8
commit
ff6a35096f
@ -21,11 +21,11 @@ board = d1_mini
|
|||||||
build_unflags =
|
build_unflags =
|
||||||
build_flags = #-O0 -Wl,-Map,output.map
|
build_flags = #-O0 -Wl,-Map,output.map
|
||||||
-D BAUD=${common_env_data.monitor_speed}
|
-D BAUD=${common_env_data.monitor_speed}
|
||||||
#-D ACTIVATE_OTA # TODO: Download html files to FS to reduce image size
|
-D ACTIVATE_OTA
|
||||||
-D ACTIVATE_PUSH
|
-D ACTIVATE_PUSH
|
||||||
#-D SKIP_SLEEPMODE
|
#-D SKIP_SLEEPMODE
|
||||||
-D USE_LITTLEFS=true
|
-D USE_LITTLEFS=true
|
||||||
-D EMBED_HTML
|
#-D EMBED_HTML
|
||||||
-D CFG_APPVER="\"0.2.0\""
|
-D CFG_APPVER="\"0.2.0\""
|
||||||
lib_deps =
|
lib_deps =
|
||||||
# https://github.com/jrowberg/i2cdevlib.git # Using local copy of this library
|
# https://github.com/jrowberg/i2cdevlib.git # Using local copy of this library
|
||||||
@ -43,7 +43,9 @@ upload_speed = ${common_env_data.upload_speed}
|
|||||||
monitor_speed = ${common_env_data.monitor_speed}
|
monitor_speed = ${common_env_data.monitor_speed}
|
||||||
framework = ${common_env_data.framework}
|
framework = ${common_env_data.framework}
|
||||||
platform = ${common_env_data.platform}
|
platform = ${common_env_data.platform}
|
||||||
extra_scripts = script/copy_firmware.py
|
extra_scripts =
|
||||||
|
script/copy_firmware.py
|
||||||
|
script/create_versionjson.py
|
||||||
build_unflags = ${common_env_data.build_unflags}
|
build_unflags = ${common_env_data.build_unflags}
|
||||||
build_flags =
|
build_flags =
|
||||||
${common_env_data.build_flags}
|
${common_env_data.build_flags}
|
||||||
@ -59,7 +61,9 @@ upload_speed = ${common_env_data.upload_speed}
|
|||||||
monitor_speed = ${common_env_data.monitor_speed}
|
monitor_speed = ${common_env_data.monitor_speed}
|
||||||
framework = ${common_env_data.framework}
|
framework = ${common_env_data.framework}
|
||||||
platform = ${common_env_data.platform}
|
platform = ${common_env_data.platform}
|
||||||
extra_scripts = script/copy_firmware.py
|
extra_scripts =
|
||||||
|
script/copy_firmware.py
|
||||||
|
script/create_versionjson.py
|
||||||
build_unflags = ${common_env_data.build_unflags}
|
build_unflags = ${common_env_data.build_unflags}
|
||||||
build_flags =
|
build_flags =
|
||||||
${common_env_data.build_flags}
|
${common_env_data.build_flags}
|
||||||
|
@ -19,16 +19,6 @@ def after_build(source, target, env):
|
|||||||
print( "Copy file : " + source + " -> " + target )
|
print( "Copy file : " + source + " -> " + target )
|
||||||
shutil.copyfile( source, target )
|
shutil.copyfile( source, target )
|
||||||
|
|
||||||
target = dir + "\\bin\\version.json"
|
|
||||||
ver = get_build_flag_value("CFG_APPVER")
|
|
||||||
print( "Creating version.json" )
|
|
||||||
f = open( target, "w" )
|
|
||||||
f.write( "{ \"project\":\"gravmon\", \"version\":" + ver + " }" )
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
print( "Adding custom build step (copy firmware): ")
|
||||||
print( "Adding custom build step: ")
|
|
||||||
#env.AddPreAction("buildprog", after_build)
|
|
||||||
env.AddPostAction("buildprog", after_build)
|
env.AddPostAction("buildprog", after_build)
|
||||||
#env.AddPreAction("upload", after_build)
|
|
||||||
#env.AddPostAction("upload", after_build)
|
|
||||||
|
50
script/create_versionjson.py
Normal file
50
script/create_versionjson.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
Import("env", "projenv")
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
def get_build_flag_value(flag_name):
|
||||||
|
build_flags = env.ParseFlags(env['BUILD_FLAGS'])
|
||||||
|
flags_with_value_list = [build_flag for build_flag in build_flags.get('CPPDEFINES') if type(build_flag) == list]
|
||||||
|
defines = {k: v for (k, v) in flags_with_value_list}
|
||||||
|
return defines.get(flag_name)
|
||||||
|
|
||||||
|
def after_build(source, target, env):
|
||||||
|
print( "Executing custom step " )
|
||||||
|
dir = env.GetLaunchDir()
|
||||||
|
#name = env.get( "PIOENV" )
|
||||||
|
|
||||||
|
# Copy file 1
|
||||||
|
source = dir + "\\data\\index.min.htm"
|
||||||
|
target = dir + "\\bin\\index.min.htm"
|
||||||
|
print( "Copy file : " + source + " -> " + target )
|
||||||
|
shutil.copyfile( source, target )
|
||||||
|
|
||||||
|
# Copy file 2
|
||||||
|
source = dir + "\\data\\device.min.htm"
|
||||||
|
target = dir + "\\bin\\device.min.htm"
|
||||||
|
print( "Copy file : " + source + " -> " + target )
|
||||||
|
shutil.copyfile( source, target )
|
||||||
|
|
||||||
|
# Copy file 3
|
||||||
|
source = dir + "\\data\\config.min.htm"
|
||||||
|
target = dir + "\\bin\\config.min.htm"
|
||||||
|
print( "Copy file : " + source + " -> " + target )
|
||||||
|
shutil.copyfile( source, target )
|
||||||
|
|
||||||
|
# Copy file 4
|
||||||
|
source = dir + "\\data\\about.min.htm"
|
||||||
|
target = dir + "\\bin\\about.min.htm"
|
||||||
|
print( "Copy file : " + source + " -> " + target )
|
||||||
|
shutil.copyfile( source, target )
|
||||||
|
|
||||||
|
target = dir + "\\bin\\version.json"
|
||||||
|
ver = get_build_flag_value("CFG_APPVER")
|
||||||
|
|
||||||
|
print( "Creating version.json" )
|
||||||
|
f = open( target, "w" )
|
||||||
|
f.write( "{ \"project\":\"gravmon\", \"version\":" + ver + ", " )
|
||||||
|
f.write( " \"html\": [ \"index.min.htm\", \"device.min.htm\", \"config.min.htm\", \"about.min.htm\" ] }" )
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
print( "Adding custom build step: (create json)")
|
||||||
|
env.AddPreAction("buildprog", after_build)
|
11
src/main.cpp
11
src/main.cpp
@ -132,16 +132,15 @@ void setup() {
|
|||||||
if( myWifi.isConnected() ) {
|
if( myWifi.isConnected() ) {
|
||||||
Log.notice(F("Main: Connected to wifi ip=%s." CR), myWifi.getIPAddress().c_str() );
|
Log.notice(F("Main: Connected to wifi ip=%s." CR), myWifi.getIPAddress().c_str() );
|
||||||
|
|
||||||
|
#if defined( ACTIVATE_OTA )
|
||||||
|
if( !sleepModeActive && myWifi.checkFirmwareVersion() ) {
|
||||||
|
myWifi.updateFirmware();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
if( !sleepModeActive )
|
if( !sleepModeActive )
|
||||||
if( myWebServer.setupWebServer() )
|
if( myWebServer.setupWebServer() )
|
||||||
Log.notice(F("Main: Webserver is running." CR) );
|
Log.notice(F("Main: Webserver is running." CR) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined( ACTIVATE_OTA )
|
|
||||||
if( !sleepModeActive && myWifi.isConnected() && myWifi.checkFirmwareVersion() ) {
|
|
||||||
myWifi.updateFirmware();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
50
src/wifi.cpp
50
src/wifi.cpp
@ -29,7 +29,6 @@ SOFTWARE.
|
|||||||
#include "tempsensor.h"
|
#include "tempsensor.h"
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266mDNS.h>
|
#include <ESP8266mDNS.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <ESP8266HTTPClient.h>
|
#include <ESP8266HTTPClient.h>
|
||||||
#include <ESP8266httpUpdate.h>
|
#include <ESP8266httpUpdate.h>
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
@ -39,7 +38,6 @@ Wifi myWifi;
|
|||||||
WiFiManager myWifiManager;
|
WiFiManager myWifiManager;
|
||||||
|
|
||||||
// TODO: ADD MDNS setting to WIFI portal.....
|
// TODO: ADD MDNS setting to WIFI portal.....
|
||||||
// TODO: Download html files during OTA update to reduce image size.
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Connect to last known access point or create one if connection is not working.
|
// Connect to last known access point or create one if connection is not working.
|
||||||
@ -112,6 +110,33 @@ bool Wifi::updateFirmware() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Download and save file
|
||||||
|
//
|
||||||
|
void Wifi::downloadFile(const char *fname) {
|
||||||
|
#if LOG_LEVEL==6
|
||||||
|
Log.verbose(F("WIFI: Download file %s." CR), fname);
|
||||||
|
#endif
|
||||||
|
WiFiClient client;
|
||||||
|
HTTPClient http;
|
||||||
|
String serverPath = myConfig.getOtaURL();
|
||||||
|
serverPath += fname;
|
||||||
|
|
||||||
|
// Your Domain name with URL path or IP address with path
|
||||||
|
http.begin( client, serverPath);
|
||||||
|
int httpResponseCode = http.GET();
|
||||||
|
|
||||||
|
if (httpResponseCode==200) {
|
||||||
|
File f = LittleFS.open( fname, "w" );
|
||||||
|
http.writeToStream( &f );
|
||||||
|
f.close();
|
||||||
|
Log.notice(F("WIFI: Downloaded file %s." CR), fname);
|
||||||
|
} else {
|
||||||
|
Log.error(F("WIFI: HTTP Response code %d" CR), httpResponseCode);
|
||||||
|
}
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check what firmware version is available over OTA
|
// Check what firmware version is available over OTA
|
||||||
//
|
//
|
||||||
@ -137,13 +162,13 @@ bool Wifi::checkFirmwareVersion() {
|
|||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL==6
|
||||||
Log.verbose(F("WIFI: Payload %s." CR), payload.c_str());
|
Log.verbose(F("WIFI: Payload %s." CR), payload.c_str());
|
||||||
#endif
|
#endif
|
||||||
DynamicJsonDocument ver(256);
|
DynamicJsonDocument ver(300);
|
||||||
DeserializationError err = deserializeJson(ver, payload);
|
DeserializationError err = deserializeJson(ver, payload);
|
||||||
if( err ) {
|
if( err ) {
|
||||||
Log.error(F("WIFI: Failed to parse json" CR));
|
Log.error(F("WIFI: Failed to parse json, %s" CR), err);
|
||||||
} else {
|
} else {
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL==6
|
||||||
Log.verbose(F("WIFI: Project %s version %s." CR), ver["project"].as<char*>(), ver["version"].as<char*>());
|
Log.verbose(F("WIFI: Project %s version %s." CR), (const char*) ver["project"], (const char*) ver["version"]);
|
||||||
#endif
|
#endif
|
||||||
int newVer[3];
|
int newVer[3];
|
||||||
int curVer[3];
|
int curVer[3];
|
||||||
@ -164,6 +189,19 @@ bool Wifi::checkFirmwareVersion() {
|
|||||||
newFirmware = true;
|
newFirmware = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Download new html files to filesystem if they are present.
|
||||||
|
if( !ver["html"].isNull() && newFirmware ) {
|
||||||
|
Log.notice(F("OTA : Downloading new html files." CR));
|
||||||
|
htmlFiles = ver["html"].as<JsonArray>();
|
||||||
|
for(JsonVariant v : htmlFiles) {
|
||||||
|
String s = v;
|
||||||
|
#if LOG_LEVEL==6
|
||||||
|
Log.verbose(F("OTA : Html file found %s" CR), s.c_str() );
|
||||||
|
#endif
|
||||||
|
downloadFile( s.c_str() );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.error(F("WIFI: HTTP Response code %d" CR), httpResponseCode);
|
Log.error(F("WIFI: HTTP Response code %d" CR), httpResponseCode);
|
||||||
@ -189,8 +227,6 @@ bool Wifi::parseFirmwareVersionString( int (&num)[3], const char *version ) {
|
|||||||
|
|
||||||
strcpy( &temp[0], version );
|
strcpy( &temp[0], version );
|
||||||
|
|
||||||
// TODO: Do some error checking on the string, lenght etc.
|
|
||||||
|
|
||||||
while ((s = strtok_r(p, ".", &p)) != NULL) {
|
while ((s = strtok_r(p, ".", &p)) != NULL) {
|
||||||
num[i++] = atoi( s );
|
num[i++] = atoi( s );
|
||||||
}
|
}
|
||||||
|
@ -26,16 +26,19 @@ SOFTWARE.
|
|||||||
|
|
||||||
// Include
|
// Include
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
// classes
|
// classes
|
||||||
class Wifi {
|
class Wifi {
|
||||||
private:
|
private:
|
||||||
// WIFI
|
// WIFI
|
||||||
bool connectedFlag = false;
|
bool connectedFlag = false;
|
||||||
|
JsonArray htmlFiles;
|
||||||
|
|
||||||
// OTA
|
// OTA
|
||||||
bool newFirmware = false;
|
bool newFirmware = false;
|
||||||
bool parseFirmwareVersionString( int (&num)[3], const char *version );
|
bool parseFirmwareVersionString( int (&num)[3], const char *version );
|
||||||
|
void downloadFile(const char *fname);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// WIFI
|
// WIFI
|
||||||
|
Loading…
Reference in New Issue
Block a user