Fully switch to PrettyOTA
This commit is contained in:
		
							parent
							
								
									14759902be
								
							
						
					
					
						commit
						bcb5208e4d
					
				
							
								
								
									
										56
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								src/main.cpp
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -2,10 +2,9 @@
 | 
			
		|||
#include <WiFiManager.h>
 | 
			
		||||
#include <WiFi.h>
 | 
			
		||||
#include <HTTPClient.h>
 | 
			
		||||
#include <ArduinoOTA.h>
 | 
			
		||||
#include <PrettyOTA.h>
 | 
			
		||||
 | 
			
		||||
#define FIRMWARE "2.1b"
 | 
			
		||||
#define BOARDFIRMWARE "2.2"
 | 
			
		||||
 | 
			
		||||
#define BUTTONPIN 23
 | 
			
		||||
#define DEBOUNCEMS 15
 | 
			
		||||
| 
						 | 
				
			
			@ -38,12 +37,37 @@ unsigned long sendDeviceStatus() {
 | 
			
		|||
  HTTPClient http;
 | 
			
		||||
  http.begin(client, statusURL.c_str());
 | 
			
		||||
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
 | 
			
		||||
  String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + FIRMWARE + "&service=" + SERVICE;
 | 
			
		||||
  String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + BOARDFIRMWARE + "&service=" + SERVICE;
 | 
			
		||||
  int httpResponseCode = http.POST(httpData);
 | 
			
		||||
  http.end();
 | 
			
		||||
  return millis();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gets called when update starts
 | 
			
		||||
// updateMode can be FILESYSTEM or FIRMWARE
 | 
			
		||||
void OnOTAStart(NSPrettyOTA::UPDATE_MODE updateMode) {
 | 
			
		||||
  Serial.println("OTA update started");
 | 
			
		||||
  if(updateMode == NSPrettyOTA::UPDATE_MODE::FIRMWARE)
 | 
			
		||||
    Serial.println("Mode: Firmware");
 | 
			
		||||
  else if(updateMode == NSPrettyOTA::UPDATE_MODE::FILESYSTEM)
 | 
			
		||||
    Serial.println("Mode: Filesystem");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gets called while update is running
 | 
			
		||||
// currentSize: Number of bytes already processed
 | 
			
		||||
// totalSize: Total size of new firmware in bytes
 | 
			
		||||
void OnOTAProgress(uint32_t currentSize, uint32_t totalSize) {
 | 
			
		||||
  Serial.printf("OTA Progress Current: %u bytes, Total: %u bytes\n", currentSize, totalSize);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Gets called when update finishes
 | 
			
		||||
void OnOTAEnd(bool successful) {
 | 
			
		||||
  if (successful)
 | 
			
		||||
    Serial.println("OTA update finished successfully");
 | 
			
		||||
  else
 | 
			
		||||
    Serial.println("OTA update failed");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
  pinMode(BUTTONPIN, INPUT_PULLUP);
 | 
			
		||||
| 
						 | 
				
			
			@ -51,30 +75,13 @@ void setup() {
 | 
			
		|||
  WiFiManager wifiManager;
 | 
			
		||||
  wifiManager.autoConnect("LEDsActionButton");
 | 
			
		||||
  lastStatusTime = sendDeviceStatus();
 | 
			
		||||
  /*ArduinoOTA.onStart([]() {
 | 
			
		||||
    String type;
 | 
			
		||||
    if ( ArduinoOTA.getCommand() == U_FLASH )
 | 
			
		||||
      type = "sketch";
 | 
			
		||||
    else // U_SPIFFS
 | 
			
		||||
      type = "filesystem";
 | 
			
		||||
    Serial.println("Start OTA updating " + type);
 | 
			
		||||
  });
 | 
			
		||||
  ArduinoOTA.onEnd([]() {
 | 
			
		||||
      Serial.println("\nOTA Update Completed");
 | 
			
		||||
  });
 | 
			
		||||
  ArduinoOTA.onError([](ota_error_t error) {
 | 
			
		||||
      Serial.printf("OTA Update Error[%u]: ", error);
 | 
			
		||||
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
 | 
			
		||||
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
 | 
			
		||||
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
 | 
			
		||||
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
 | 
			
		||||
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
 | 
			
		||||
  });
 | 
			
		||||
  ArduinoOTA.begin();*/
 | 
			
		||||
  OTAUpdates.Begin(&server);
 | 
			
		||||
  OTAUpdates.SetHardwareID(SERVICE);
 | 
			
		||||
  OTAUpdates.OverwriteAppVersion(FIRMWARE);
 | 
			
		||||
  OTAUpdates.OverwriteAppVersion(BOARDFIRMWARE);
 | 
			
		||||
  PRETTY_OTA_SET_CURRENT_BUILD_TIME_AND_DATE();
 | 
			
		||||
  OTAUpdates.OnStart(OnOTAStart);
 | 
			
		||||
  OTAUpdates.OnProgress(OnOTAProgress);
 | 
			
		||||
  OTAUpdates.OnEnd(OnOTAEnd);
 | 
			
		||||
  server.begin();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -104,5 +111,4 @@ void loop() {
 | 
			
		|||
    }
 | 
			
		||||
  }
 | 
			
		||||
  lastButtonState = buttonCheck;
 | 
			
		||||
  ArduinoOTA.handle();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user