Last active
October 24, 2018 17:30
-
-
Save MkLHX/fbaee86590972b55a8feeff665ad9579 to your computer and use it in GitHub Desktop.
This is how i do for download the last firmware of an IoT product to the SD card of my ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*2018-10-24 Mickael Lehoux | |
* | |
*PlatformIO project | |
*//main.cpp | |
* | |
*Used to download firmware to ESP32 SD card from private server | |
*Use the ESP32Dev web server to start request for the last firmware by using HTTP request | |
*/ | |
#include "main.h" | |
#include "FS.h" | |
#include "SD.h" | |
#include "HTTPClient.h" | |
#include "ESP32WebServer.h" | |
ESP32WebServer webServer(80); | |
// Functions declarations | |
void wifiManager(char *ssid, char *pwd); | |
void getLastFirmware(); | |
void downloadLastFirmware(String uriParam); | |
// Define CS pin for the SD card module | |
#define SD_CS 5 | |
void setup() | |
{ | |
Serial.begin(115200); | |
// Connect to the WiFi | |
wifiManager("you_ssid", "you_password"); | |
// SD card Initialization | |
// Make SD card always run -> add pull-up on MISO pin | |
#ifdef ESP32 | |
//Serial.println(MISO); | |
pinMode(19, INPUT_PULLUP); | |
#endif | |
SD.begin(SD_CS); | |
if (!SD.begin(SD_CS)) | |
{ | |
Serial.println("Card Mount Failed"); | |
delay(1000); | |
ESP.restart(); | |
} | |
uint8_t cardType = SD.cardType(); | |
if (cardType == CARD_NONE) | |
{ | |
Serial.println("No SD card attached"); | |
delay(1000); | |
ESP.restart(); | |
} | |
Serial.println("Initializing SD card..."); | |
if (!SD.begin(SD_CS)) | |
{ | |
Serial.println("ERROR - SD card initialization failed!"); | |
delay(1000); | |
ESP.restart(); | |
} | |
Serial.println("SD card initialized"); | |
webServer.on("/getLastFirmware", getLastFirmware); | |
webServer.begin(); | |
Serial.println("HTTP Server started"); | |
} | |
void loop() | |
{ | |
delay(1000); | |
Serial.print("."); | |
webServer.handleClient(); | |
} | |
void wifiManager(char *ssid, char *pwd) | |
{ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, pwd); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
Serial.print(F(".")); | |
delay(250); | |
} | |
Serial.println(); | |
Serial.print(F("Connected to Wifi : ")); | |
Serial.println(WiFi.SSID()); | |
Serial.print(F("WiFi IP address: ")); | |
Serial.println(WiFi.localIP()); | |
} | |
void getLastFirmware() | |
{ | |
Serial.println("getLastFirmware function start"); | |
// Check if there are some arguments from the GET request | |
if (webServer.args() > 0) | |
{ | |
// Find uriParam argument from request | |
if (webServer.hasArg("uriParam")) | |
{ | |
downloadLastFirmware(String(webServer.arg(0))); | |
} | |
} | |
} | |
//download last firmware from server and store it on sd card | |
void downloadLastFirmware(String uriParam) | |
{ | |
Serial.println("Enter in downloadLastFirmware function"); | |
String fwUrlLastVersion = "http://my-server-domain/" + uriParam + "/last/release"; | |
HTTPClient httpClient; | |
httpClient.begin(fwUrlLastVersion); | |
int httpCode = httpClient.POST(""); | |
Serial.println("HTTP reponse code :" + httpCode); | |
if (httpCode == HTTP_CODE_OK) | |
{ | |
Serial.print("Current firmware version : "); | |
Serial.println(FW_VERSION); | |
String newFWVersion = httpClient.getString(); | |
Serial.print(F("request response from server :")); | |
Serial.println(newFWVersion); | |
//parsing json response from server | |
DynamicJsonBuffer jsonBuffer; | |
JsonObject &rootJsonResponse = jsonBuffer.parseObject(newFWVersion); | |
if (!rootJsonResponse.success()) | |
{ | |
Serial.println(F("parseObject() failed")); | |
} | |
Serial.println(F("Parsing json response Success")); | |
const char *release = rootJsonResponse["release"]; | |
String newRelease = String(release); | |
Serial.print("Available firmware version : "); | |
Serial.println(newRelease); | |
newRelease.replace(".", ""); | |
int newVersion = newRelease.toInt(); | |
String actualFW = FW_VERSION; | |
actualFW.replace(".", ""); | |
int actualVersion = actualFW.toInt(); | |
if (newVersion > actualVersion) | |
{ | |
WiFiClient fwDlClient; | |
String host = "my-server-domain/"; | |
String url = "/" + uriParam + "/firmware.bin"; | |
if (fwDlClient.connect(host.c_str(), 80)) | |
{ | |
Serial.print("connected to : "); | |
Serial.println(host); | |
// Make a HTTP request to the server | |
fwDlClient.println("GET " + url + " HTTP/1.1"); // change resource to get here | |
fwDlClient.println("Host: " + host); // change resource host here | |
fwDlClient.println("Cache-Control: no-cache"); | |
fwDlClient.println("Connection: close"); | |
fwDlClient.println(); | |
} | |
else | |
{ | |
Serial.println("Connection to " + host + " failed..."); | |
} | |
//delay the available client test | |
delay(250); | |
if (fwDlClient.available()) | |
{ | |
Serial.println("client is available"); | |
// Check to see if the file exists | |
if (SD.exists("/" + uriParam + "/firmware.bin")) | |
{ | |
Serial.println("firmware file exists, deleting from sd card"); | |
SD.remove("/" + uriParam + "/firmware.bin"); | |
} | |
else | |
{ | |
Serial.println("firmware file doesn't exist on sd card. Let's create it!"); | |
} | |
File firmwareFile = SD.open("/" + uriParam + "/firmware.bin", FILE_WRITE); | |
if (firmwareFile) | |
{ | |
Serial.println("Writing file on SD card from server..."); | |
Serial.println("It's will take 2 or 5min!! be patient please...."); | |
size_t currentSize; | |
while (fwDlClient.connected()) | |
{ | |
byte c = fwDlClient.read(); | |
currentSize = firmwareFile.write(c); | |
//Serial.println("..."); | |
} | |
// close the file | |
firmwareFile.close(); | |
Serial.println("firmware file " + String(currentSize) + " upload done"); | |
} | |
else | |
{ | |
// if the file didn't open, print an error | |
Serial.println("error opening file"); | |
} | |
} | |
if (!fwDlClient.connected()) | |
{ | |
Serial.println("disconnecting"); | |
fwDlClient.flush(); | |
fwDlClient.stop(); | |
Serial.println("Finished writing to file"); | |
} | |
} | |
else | |
{ | |
Serial.println("Already on latest version"); | |
} | |
} | |
else | |
{ | |
Serial.print("Firmware version check failed, got HTTP response code : "); | |
Serial.println(httpCode); | |
} | |
httpClient.end(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include "ArduinoJson.h" | |
#include "WiFi.h" | |
#define FW_VERSION "0.0.1" | |
extern String newRelease; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment