Skip to content

Commit

Permalink
Show current firmware version in the /update web page.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Dec 30, 2024
1 parent a439fc8 commit 106a19f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
29 changes: 29 additions & 0 deletions create_fwversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

import subprocess

def get_git_version():
"""Retrieve the current version from `git describe --tags`."""
try:
# Run the git command to get the version string
version = subprocess.check_output(
["git", "describe", "--tags", "--dirty", "--always"],
stderr=subprocess.STDOUT,
text=True
).strip()
return version
except subprocess.CalledProcessError:
# Return a default value if the command fails
return "unknown"

def generate_version_header(output_file):
"""Generate the version.h file with the Git version."""
git_version = get_git_version()
with open(output_file, "w") as header:
header.write('// automatically generated using create_fwversion.py\n')
header.write(f'const char* FW_VERSION = "{git_version}";\n\n')
print(f"Generated C file with with GIT_VERSION={git_version}: {output_file}")

# Create the version file
output_file = "fwversion.h"
generate_version_header(output_file)
3 changes: 2 additions & 1 deletion data/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</head>

<body>
<p style="text-align:left"><a href="/"">/home</a></p>
<p style="text-align:left"><a href="/"">/home</a></p>
<p><b>Current version: %fw_version%</b></p>
<form action="/update" method="post">
<fieldset>
<legend>Firmware update over HTTP</legend>
Expand Down
11 changes: 10 additions & 1 deletion fwupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ESP8266httpUpdate.h>

#include "fwupdate.h"
#include "fwversion.h"

static FS *_fs;
static WiFiClientSecure wifiClientSecure;
Expand All @@ -32,9 +33,17 @@ void fwupdate_begin(FS & fs)
Update.runAsync(true);
}

static String template_processor(const String & string)
{
if (string == "fw_version") {
return FW_VERSION;
}
return string;
}

static void handleGet(AsyncWebServerRequest *request)
{
request->send(*_fs, _update_page, "text/html");
request->send(*_fs, _update_page, "text/html", false, template_processor);
}

/*
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ framework = arduino
monitor_speed = 115200
upload_speed = 1000000
board_build.filesystem = littlefs
extra_scripts = pre:create_fsimage.py
extra_scripts =
pre:create_fwversion.py
pre:create_fsimage.py
lib_deps = fastled/[email protected]
alanswx/ESPAsyncWiFiManager@^0.31.0
bblanchon/ArduinoJson@^6.21.5
Expand Down

0 comments on commit 106a19f

Please sign in to comment.