Skip to content

Commit

Permalink
Added OTA firmware update support
Browse files Browse the repository at this point in the history
  • Loading branch information
nullvalue0 committed Dec 8, 2022
1 parent 0f00e65 commit d75eb41
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
60 changes: 57 additions & 3 deletions Firmware/WiRSa/WiRSa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include <ESP8266mDNS.h>
Expand Down Expand Up @@ -132,7 +133,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


// Global variables
String build = "v2.07";
String build = "v2.08";
String cmd = ""; // Gather a new AT command to this string from serial
bool cmdMode = true; // Are we in AT command mode or connected mode
bool callConnected = false; // Are we currently in a call
Expand Down Expand Up @@ -684,6 +685,8 @@ void displayHelp() {
Serial.println("HANGUP.........: ATH"); yield();
Serial.println("ENTER CMD MODE.: +++"); yield();
Serial.println("EXIT CMD MODE..: ATO"); yield();
Serial.println("FIRMWARE CHECK.: ATFC"); yield();
Serial.println("FIRMWARE UPDATE: ATFU"); yield();
Serial.println("EXIT MODEM MODE: ATX"); yield();
Serial.println("QUERY MOST COMMANDS FOLLOWED BY '?'"); yield();
}
Expand Down Expand Up @@ -1474,6 +1477,12 @@ void command()
}
}

else if (upCmd == "ATFC") {
firmwareCheck();
}
else if (upCmd == "ATFU") {
firmwareUpdate();
}
else if (upCmd == "ATX") {
mainMenu(false);
}
Expand Down Expand Up @@ -3147,7 +3156,6 @@ void firmwareCheck() {
//I host on a plain HTTP site. It hits the github API endpoint over HTTPS, parses the JSON and just
//returns the latest version string over HTTP as plain text - this way the ESP8266 can easily check
//the latest version.
//TODO: Add OTA firmware update support

Serial.println("\nChecking firmware version..."); yield();
WiFiClient client;
Expand All @@ -3159,7 +3167,7 @@ void firmwareCheck() {
String version = http.getString();
Serial.println("Latest Version: " + version + ", Device Version: " + build);
if (build!=version)
Serial.println("WiRSa firmware update available, download the latest release at https://github.com/nullvalue0/WiRSa");
Serial.println("WiRSa firmware update available, download the latest release at https://github.com/nullvalue0/WiRSa or use commmand ATFU to apply updates now.");
else
Serial.println("Your WiRSa is running the latest firmware version.");
}
Expand All @@ -3168,6 +3176,52 @@ void firmwareCheck() {
}
}

void firmwareUpdate() {
//Similar to the firmware check, I have a proxy php script calls the github, gets the latest release
//url, downloads the binary and forwards it over plain HTTP so the ESP8266 can download it and use
//it for OTA updates. These php scripts are in the github repository under the firmware folder.

WiFiClient client;

ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
ESPhttpUpdate.onStart(firmwareUpdateStarted);
ESPhttpUpdate.onEnd(firmwareUpdateFinished);
ESPhttpUpdate.onProgress(firmwareUpdateProgress);
ESPhttpUpdate.onError(firmwareUpdateError);

t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://update.retrodisks.com/wirsa-bin-v2.php");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;

case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;

case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}

void firmwareUpdateStarted() {
Serial.println("HTTP update process started");
}

void firmwareUpdateFinished() {
Serial.println("HTTP update process finished...\r\n\r\nPLEASE WAIT, APPLYING UPDATE - DEVICE WILL REBOOT ON IT'S OWN WHEN COMPLETE IN ABOUT 10 SECONDS\r\n\r\n");
}

void firmwareUpdateProgress(int cur, int total) {
Serial.printf("HTTP update process at %d of %d bytes...\r\n", cur, total);
}

void firmwareUpdateError(int err) {
Serial.printf("HTTP update fatal error code %\r\n", err);
}


void modemLoop()
{
// Check flow control
Expand Down
Binary file modified Firmware/WiRSa/WiRSa.ino.d1_mini.bin
Binary file not shown.
32 changes: 32 additions & 0 deletions Firmware/WiRSa/wirsa-bin-v2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, "WiRSa Version Check");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


$json = file_get_contents_curl('http://api.github.com/repos/nullvalue0/WiRSa/releases/latest');
$obj = json_decode($json);
$url = $obj->{'assets'}[0]->{'browser_download_url'};
$bin = file_get_contents_curl($url);
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".strlen($bin));
header("Content-Disposition: attachment; filename=wirsa.bin");
echo $bin

?>

0 comments on commit d75eb41

Please sign in to comment.