/** * OTA handle StromLog * Version 5.00 * 04.11.2022 P. Rebesky * author Creator P.Rebesky * Copyright (©): 2022-2024 by Peter Rebesky * This code can use in private cases only. Every business or companies using of this codes or codes parts is required an approval of us (me) * Every private using can exchange some parts of code or modify some code-lines. This code is allowed change for private use only. * This software is basicly owned by Peter Rebesky and any comercial using is forbidden without approval of us (me). **/ #ifndef OTA_HANDLE_H_ #define OTA_HANDLE_H_ #include #include #include "global.h" extern ESP8266WiFiMulti WiFiMulti; //*** functions in this file **************************// String getFilesFromServer(IPAddress ip, String updateServer); String getFiles(String updateServer); String setUpdatefile(String file); void updateStromLog(String file, String updateServer); //*** declaring end ***********************************// //*** deliver filenames from server to Web client *****// String getFiles(String updateServer){ String ret = HTTP_ANSWER; ret += "Your StromLog versions number: "+Version+"
"; ret += "Check for new firmware now.
"; ret += getFilesFromServer(WiFi.localIP(),updateServer); ret += HTTP_TEXT_END; return ret; } //*** end *** //*** initial update file to StromLog *****************// String setUpdatefile(String file){ int valueBegin = file.indexOf("?"); String ret = HTTP_ANSWER; if(valueBegin > 0){ int valueEnd = file.indexOf("HTTP"); String newFile = file.substring(valueBegin+1,valueEnd-1); ret += "Update will be start now.
"; ret += "Update-file: "+newFile+"
"; ret += "Don't turn off power during update!

"; ret += "After update, the reconnection only via IP, without arguments behind it!

"; ret += "Click here for reconnect after update
"; } else { ret += "Error: Missing name of update file!
"; ret += "Update canceled.
"; // argument =""; } ret += HTTP_TEXT_END; return ret; } //*** end *** int updateBegin(String file, String updateServer){ if(file){ int valueBegin = file.indexOf("?"); if(valueBegin > 0){ int valueEnd = file.indexOf("HTTP"); file = file.substring(valueBegin+1,valueEnd-1); delay(1000); //wait one second updateStromLog(file, updateServer); // start update now return 0; } } return -1; } //*** end *** //*** get available files ****************************************/ String getFilesFromServer(IPAddress ip,String actVersion, String updateServer){ String files = ""; if (WiFiMulti.run() == WL_CONNECTED) { WiFiClient client; HTTPClient http; String target = "http://"; target += updateServer; target += "/firmware/getFiles.php"; if (http.begin(client,target)){ http.addHeader("Content-Type","application/json;charset=utf-8"); int httpCode = http.POST("{\"userIP\":\""+ip.toString()+"\",\"build\":\""+actVersion+"\"}"); if(httpCode >= 100){ files = http.getString(); } else files = "Error: no connection."; } http.end(); } return files; } //********** update, input file-name ************************************/ void updateStromLog(String file, String updateServer){ ESPhttpUpdate.rebootOnUpdate(false); // remove automatic update digitalWrite(_LED,_ON); WiFiClient client; t_httpUpdate_return ret = ESPhttpUpdate.update(client, updateServer, 80, "/firmware/"+file); 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: break; case HTTP_UPDATE_OK: String result = "Update successful."; delay(1000); // Wait a second and restart ESP.restart(); break; } } //*********** update end *************************************************/ #endif //*** OTA_HANDLE_H_