11/*
2- * EasyOTA.cpp - library to include to allow Over-The-Air updates of ESP8266
2+ * EasyOTA.cpp - library to include to allow Over-The-Air updates of ESP8266 and ESP32
33 *
44 * Inspired on:
55 * http://simplestuffmatters.com/?p=69
66 * https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS
7+ * https://github.com/espressif/arduino-esp32/blob/master/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
78 *
8- * mDNS monitor (OSX): dns-sd -B _arduino._tcp .
9+ * mDNS monitor (OSX): dns-sd -B _arduino._tcp
910 *
1011 */
1112
1415// Necesary to make Arduino Software autodetect OTA device
1516WiFiServer TelnetServer (8266 );
1617
18+ // Constructor
1719EasyOTA::EasyOTA () {
18- // Constructor
1920}
2021
2122void EasyOTA::onMessage (THandlerFunction_Message fn) {
2223 on_message = fn;
2324}
2425
2526void EasyOTA::setup (char * wifi_ssid, char * wifi_password, char * hostname) {
26- this ->wifi_ssid = wifi_ssid;
27- this ->wifi_password = wifi_password;
28- this ->hostname = hostname;
2927 showMessage (" " , 1 ); // New line in case of using serial output
30- showMessage (" Connecting Wifi:" , 1 );
31- showMessage (this ->wifi_ssid , 2 );
32- WiFi.hostname (this ->hostname );
33- WiFi.begin (this ->wifi_ssid , this ->wifi_password );
28+ String line1 = " Connect WiFi" ;
29+ showMessage (line1, 1 );
30+ showMessage (" SSID: " + String (wifi_ssid), 2 );
31+
32+ WiFi.mode (WIFI_STA);
33+ #ifdef ESP8266
34+ WiFi.hostname (hostname);
35+ #endif
36+ #ifdef ESP32
37+ WiFi.setHostname (hostname);
38+ #endif
39+
40+ WiFi.begin (wifi_ssid, wifi_password);
3441 unsigned long startTime = millis ();
3542 String progressDots = " " ;
3643 while (WiFi.status () != WL_CONNECTED && millis () - startTime < 10000 ) {
37- delay (500 );
38- progressDots += " ." ;
39- showMessage (progressDots, 2 );
44+ delay (1000 );
45+ line1 += " ." ;
46+ showMessage (line1, 1 );
4047 }
41-
4248 if (WiFi.status () == WL_CONNECTED) {
43- showMessage (" IP Address:" , 1 );
44- showMessage (WiFi.localIP ().toString (), 2 );
49+ showMessage (" IP: " + WiFi.localIP ().toString (), 1 );
4550 } else {
4651 showMessage (" Can't connect WiFi" , 1 );
4752 showMessage (" Going into AP mode." , 2 );
4853 WiFi.mode (WIFI_AP);
49- delay (10 );
50- WiFi.softAP (this -> hostname );
51- showMessage (" AP: " + String (this -> hostname ), 1 );
54+ delay (500 ); // Extra delay to show message when using LCD / Oled
55+ WiFi.softAP (hostname);
56+ showMessage (" AP: " + String (hostname), 1 );
5257 showMessage (" IP: " + WiFi.softAPIP ().toString (), 2 );
5358 }
5459
5560 TelnetServer.begin (); // Necesary to make Arduino Software autodetect OTA device
5661
62+ // ArduinoOTA callback functions
5763 ArduinoOTA.onStart ([this ]() {
58- showMessage (" OTA starting..." , 1 );
59- showMessage (" " , 2 ); // If display: Clean any previous error
64+ showMessage (" OTA starting..." , 2 );
6065 });
6166 ArduinoOTA.onEnd ([this ]() {
62- showMessage (" OTA finished. " , 1 );
63- showMessage (" Rebooting..." ,2 );
67+ showMessage (" OTA done. Rebooting.. " , 2 );
68+ // showMessage("Rebooting...",2);
6469 });
6570 ArduinoOTA.onProgress ([this ](unsigned int progress, unsigned int total) {
6671 static unsigned int prevPerc = 100 ;
6772 unsigned int perc = (progress / (total / 100 ));
6873 unsigned int roundPerc = 5 * (int )(perc / 5 );
6974 if ( roundPerc != prevPerc) {
7075 prevPerc = roundPerc;
71- showMessage (" OTA upload " + String (roundPerc) + " %" );
76+ showMessage (" OTA upload " + String (roundPerc) + " %" , 2 );
7277 }
7378 });
7479 ArduinoOTA.onError ([this ](ota_error_t error) {
@@ -81,7 +86,8 @@ void EasyOTA::setup(char* wifi_ssid, char* wifi_password, char* hostname) {
8186 else if (error == OTA_END_ERROR) line2 = " End Failed" ;
8287 showMessage (line2, 2 );
8388 });
84- ArduinoOTA.setHostname (this ->hostname );
89+
90+ ArduinoOTA.setHostname (hostname);
8591 ArduinoOTA.begin ();
8692};
8793
0 commit comments