Fixed OTA firmware update

This commit is contained in:
Mark van Renswoude 2017-04-16 14:25:36 +02:00
parent b39f3ba826
commit c789fd259b
8 changed files with 44 additions and 17 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ src/credentials.h
*.sublime-workspace *.sublime-workspace
web/node_modules/ web/node_modules/
web/update/ web/update/
src/version.h
web/version.js

View File

@ -1,3 +1,3 @@
& .\updateversion.ps1 & .\updateversion.ps1
& platformio run & platformio run
Copy-Item .\.pioenvs\esp12e\firmware.elf .\web\update\ Copy-Item .\.pioenvs\esp12e\firmware.bin .\web\update\

View File

@ -212,11 +212,14 @@ void handleUpdateFirmware(uint8_t* packet)
switch (OTAUpdateEnabled) switch (OTAUpdateEnabled)
{ {
case 1: case 1:
_dln("Checking for update (fixed)");
result = ESPhttpUpdate.update(OTAUpdateFixedHost, OTAUpdateFixedPort, OTAUpdateFixedPath, FirmwareVersion); result = ESPhttpUpdate.update(OTAUpdateFixedHost, OTAUpdateFixedPort, OTAUpdateFixedPath, FirmwareVersion);
break; break;
case 2: case 2:
{ {
_dln("Checking for update (client defined)");
uint16_t port; uint16_t port;
memcpy(&port, packet, sizeof(port)); memcpy(&port, packet, sizeof(port));
packet += sizeof(port); packet += sizeof(port);
@ -251,19 +254,25 @@ void handleUpdateFirmware(uint8_t* packet)
switch (result) switch (result)
{ {
case HTTP_UPDATE_NO_UPDATES: case HTTP_UPDATE_NO_UPDATES:
_dln("No updates");
udpServer.write(Command::UpdateFirmware); udpServer.write(Command::UpdateFirmware);
udpServer.write((uint8_t)0); udpServer.write((uint8_t)0);
break; break;
case HTTP_UPDATE_OK: case HTTP_UPDATE_OK:
_dln("Update OK");
udpServer.write(Command::UpdateFirmware); udpServer.write(Command::UpdateFirmware);
udpServer.write((uint8_t)1); udpServer.write((uint8_t)1);
break; break;
default: default:
_d("Error while updating: ");
_dln(ESPhttpUpdate.getLastError());
_dln(ESPhttpUpdate.getLastErrorString().c_str());
udpServer.write(Command::Error); udpServer.write(Command::Error);
udpServer.write(Command::UpdateFirmware); udpServer.write(Command::UpdateFirmware);
udpServer.write((uint8_t)1); udpServer.write((uint8_t)2);
break; break;
} }
} }

View File

@ -51,25 +51,37 @@ void Stairs::setAll(uint16_t brightness)
uint16_t Stairs::getPWMValue(uint8_t step, uint16_t brightness) uint16_t Stairs::getPWMValue(uint8_t step, uint16_t brightness)
{ {
_d("Getting PWM value for step "); _d(step); _d(", brightness "); _dln(brightness);
if (brightness == IStairs::Off || brightness == IStairs::On) if (brightness == IStairs::Off || brightness == IStairs::On)
{
_dln("Full on/off, returning input");
return brightness; return brightness;
}
if (step < 0 || step >= StepCount) if (step < 0 || step >= StepCount)
{
_dln("Step out of bounds, returning input");
return brightness; return brightness;
}
Range* range = &this->ranges[step]; Range* range = &this->ranges[step];
_d("Start: "); _dln(range->start);
_d("End: "); _dln(range->end);
if (this->useScaling) if (this->useScaling)
{ {
_dln("Using scaling");
float factor = ((range->end - range->start) + 1) * factorBase; float factor = ((range->end - range->start) + 1) * factorBase;
brightness = pow(2, (brightness / factor)) - 1 + range->start; brightness = pow(2, (brightness / factor)) - 1 + range->start;
} }
else else
{ {
_dln("Not using scaling");
if (brightness < range->start) brightness = range->start; if (brightness < range->start) brightness = range->start;
if (brightness > range->end) brightness = range->end; if (brightness > range->end) brightness = range->end;
} }
_d("Output: "); _dln(brightness);
return brightness; return brightness;
} }

View File

@ -1,6 +0,0 @@
#ifndef __Version
#define __Version
static const char* FirmwareVersion = "0.1.0+8";
#endif

View File

@ -1,4 +1,5 @@
var fs = require('fs'); var fs = require('fs');
var md5File = require('md5-file');
var express = require('express'); var express = require('express');
var semverUtils = require('semver-utils') var semverUtils = require('semver-utils')
var client = require('./client'); var client = require('./client');
@ -8,7 +9,7 @@ var httpPort = 3127;
var stairsHost = '10.138.2.25'; var stairsHost = '10.138.2.25';
var stairsUdpPort = 3126; var stairsUdpPort = 3126;
var firmwareFile = './update/firmware.elf'; var firmwareFile = './update/firmware.bin';
function requireNoCache(filename) function requireNoCache(filename)
@ -29,7 +30,7 @@ function isNewer(version1, version2)
if (version1.patch > version2.patch) return true; if (version1.patch > version2.patch) return true;
if (version1.patch < version2.patch) return false; if (version1.patch < version2.patch) return false;
if (version1.build > version2.build) return true; if (parseInt(version1.build, 10) > parseInt(version2.build, 10)) return true;
return false; return false;
} }
@ -102,14 +103,26 @@ app.get('/checkUpdate', function(req, res)
if (isNewer(localVersion, deviceVersion)) if (isNewer(localVersion, deviceVersion))
{ {
res.download(firmwareFile); console.log('Sending update');
md5File(firmwareFile, function(err, hash)
{
if (err)
{
res.sendStatus(500);
return;
}
res.set('Content-Length', fs.statSync(firmwareFile).size);
res.set('x-MD5', hash);
res.download(firmwareFile);
});
} }
else else
{ {
console.log('No update required');
res.sendStatus(304); res.sendStatus(304);
} }
res.send(304);
}); });
app.use(express.static(__dirname + '/static')); app.use(express.static(__dirname + '/static'));

View File

@ -8,6 +8,7 @@
"dependencies": { "dependencies": {
"buffer-reader": "^0.1.0", "buffer-reader": "^0.1.0",
"express": "^4.15.2", "express": "^4.15.2",
"md5-file": "^3.1.1",
"semver-utils": "^1.1.1" "semver-utils": "^1.1.1"
} }
} }

View File

@ -1,4 +0,0 @@
module.exports =
{
Version: "0.1.0+8"
};