Browse Source

insert ota example

lightsleep
Hendrik Langer 7 years ago
parent
commit
004868ef65
  1. 82
      src/XD0OTA.cpp
  2. 53
      src/XD0OTA.h

82
src/XD0OTA.cpp

@ -0,0 +1,82 @@
#include "XD0OTA.h"
XD0OTA::XD0OTA() {
}
String XD0OTA::getMAC() {
uint8_t mac[6];
char result[14];
WiFi.macAddress( mac );
snprintf( result, sizeof( result ), "%02x%02x%02x%02x%02x%02x", mac[ 0 ], mac[ 1 ], mac[ 2 ], mac[ 3 ], mac[ 4 ], mac[ 5 ] );
return String( result );
}
void XD0OTA::update(void) {
int newVersion = checkForUpdates();
if( newVersion > FW_VERSION ) {
Serial.println( "Preparing to update." );
String mac = getMAC();
String fwURL = String( fwUrlBase );
fwURL.concat( mac );
String fwImageURL = fwURL;
fwImageURL.concat( ".bin" );
t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL );
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", 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("[update] Update ok."); // may not called we reboot the ESP
break;
}
} else {
Serial.println( "Already on latest version" );
}
}
int XD0OTA::checkForUpdates() {
int newVersion = -1;
String mac = getMAC();
String fwURL = String( fwUrlBase );
fwURL.concat( mac );
String fwVersionURL = fwURL;
fwVersionURL.concat( ".version" );
Serial.println( "Checking for firmware updates." );
Serial.print( "MAC address: " );
Serial.println( mac );
Serial.print( "Firmware version URL: " );
Serial.println( fwVersionURL );
HTTPClient httpClient;
httpClient.begin( fwVersionURL );
int httpCode = httpClient.GET();
if( httpCode == 200 ) {
String newFWVersion = httpClient.getString();
Serial.print( "Current firmware version: " );
Serial.println( FW_VERSION );
Serial.print( "Available firmware version: " );
Serial.println( newFWVersion );
newVersion = newFWVersion.toInt();
} else {
Serial.print( "Firmware version check failed, got HTTP response code " );
Serial.println( httpCode );
newVersion = -1;
}
httpClient.end();
return newVersion;
}

53
src/XD0OTA.h

@ -0,0 +1,53 @@
#ifndef _XD0OTA_H
#define _XD0OTA_H
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
const int FW_VERSION = 10;
const char* httpsFingerprint = "37 42 61 B9 E6 EE 22 36 D1 59 67 7D 55 53 6E A4 C7 AA 60 26";
class XD0OTA {
public:
XD0OTA();
void update(void);
int checkForUpdates();
private:
const char* fwUrlBase = "https://fwupdate.xd0.de/fota/";
String getMAC();
};
#endif /* _XD0OTA_H */
/*
openssl req -x509 -nodes -days 18263 -newkey rsa:2048 -keyout /etc/ssl/private/xd0-fwupdate-selfsigned.key -out /etc/ssl/certs/xd0-fwupdate-selfsigned.crt
openssl x509 -noout -fingerprint -sha1 -inform pem -in /etc/ssl/certs/xd0-fwupdate-selfsigned.crt
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name fwupdate.xd0.de;
# SSL configuration
ssl_certificate /etc/ssl/certs/xd0-fwupdate-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/xd0-fwupdate-selfsigned.key;
root /var/www/fwupdate;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/fwupdate_access.log;
error_log /var/log/nginx/fwupdate_error.log;
}
*/
Loading…
Cancel
Save