Skip to content

Commit 8b5ff02

Browse files
committed
OTA!!!!
1 parent a94698d commit 8b5ff02

5 files changed

Lines changed: 148 additions & 39 deletions

File tree

Firmware_ESP8266/ESP8266_Utils.h

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include <ArduinoOTA.h>
34
#include <ESP8266WiFi.h>
5+
#include <ESP8266mDNS.h>
6+
#include <WiFiUdp.h>
47

58
#include "EEPROM_Utils.h"
69
#include "basic_defines.h"
@@ -268,6 +271,13 @@ void ESP8266Utils_connectToWifi(const String& ssid, const String& password) {
268271

269272
bool ESP8266Utils_isWifiConnected(void) { return WiFi.status() == WL_CONNECTED; }
270273

274+
String ESP8266Utils_getWifiIP(void) {
275+
if (WiFi.status() == WL_CONNECTED) {
276+
return WiFi.localIP().toString();
277+
}
278+
return String("N/A");
279+
}
280+
271281
int ESP8266Utils_getWifiConnectionPercentage(void) {
272282
if (lastConnectUpdateMs == 0) {
273283
return 0;
@@ -314,14 +324,17 @@ void Wifi_handler(bool inConfigMode) {
314324
return;
315325
}
316326

317-
bool wifiEnabled = settings.wifiSettings.wifi_enabled;
318-
bool wifiConnected = ESP8266Utils_isWifiConnected();
319-
bool WifiTmo = ESP8266Utils_getWifiConnectAttemptTmo() || backFromConfigMode;
320-
bool WeatherTmo = ESP8266Utils_getWifiWeatherAttemptTmo() || backFromConfigMode;
327+
bool wifiEnabled = settings.wifiSettings.wifi_enabled;
328+
bool wifiOTAenabled = settings.wifiSettings.ota_enabled;
329+
bool wifiConnected = ESP8266Utils_isWifiConnected();
330+
bool WifiTmo = ESP8266Utils_getWifiConnectAttemptTmo() || backFromConfigMode;
331+
bool WeatherTmo = ESP8266Utils_getWifiWeatherAttemptTmo() || backFromConfigMode;
321332

322333
// ===== WiFi auto connection handling =====
323334
if (wifiEnabled && wifiConnected && connectAttemptCount > 0) {
324335
Serial.println("Connected to WiFi");
336+
Serial.print("IP: ");
337+
Serial.println(WiFi.localIP());
325338
connectAttemptCount = 0;
326339
wifiError = false;
327340
} else if ((wifiEnabled && !wifiConnected) && WifiTmo) {
@@ -340,6 +353,50 @@ void Wifi_handler(bool inConfigMode) {
340353
wifiError = false;
341354
}
342355

356+
// ===== OTA update handling =====
357+
if (wifiEnabled && wifiConnected && wifiOTAenabled) {
358+
static bool ArduinoOTAInitialized = false;
359+
if (!ArduinoOTAInitialized) {
360+
ArduinoOTAInitialized = true;
361+
362+
ArduinoOTA.setHostname("RollerShutterControlPanel");
363+
364+
ArduinoOTA.onStart([]() {
365+
const char* type = (ArduinoOTA.getCommand() == U_FLASH)
366+
? "sketch"
367+
: "filesystem"; // ESP8266: U_FLASH or U_FS
368+
Serial.print(F("Start updating "));
369+
Serial.println(type);
370+
});
371+
372+
ArduinoOTA.onEnd([]() { Serial.println(F("\nEnd")); });
373+
374+
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
375+
const unsigned int pct
376+
= total ? (progress * 100U) / total : 0U; // safer than progress/(total/100)
377+
Serial.printf("Progress: %u%%\n", pct);
378+
});
379+
380+
ArduinoOTA.onError([](ota_error_t error) {
381+
Serial.printf("Error[%u]: ", error);
382+
if (error == OTA_AUTH_ERROR) {
383+
Serial.println(F("Auth Failed"));
384+
} else if (error == OTA_BEGIN_ERROR) {
385+
Serial.println(F("Begin Failed"));
386+
} else if (error == OTA_CONNECT_ERROR) {
387+
Serial.println(F("Connect Failed"));
388+
} else if (error == OTA_RECEIVE_ERROR) {
389+
Serial.println(F("Receive Failed"));
390+
} else if (error == OTA_END_ERROR) {
391+
Serial.println(F("End Failed"));
392+
}
393+
});
394+
395+
ArduinoOTA.begin();
396+
}
397+
ArduinoOTA.handle();
398+
}
399+
343400
// ===== Weather update handling =====
344401
if (wifiEnabled && wifiConnected && WeatherTmo) {
345402
if (ESP8266Utils_update_WeatherData()) {

Firmware_ESP8266/Firmware_ESP8266.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void loop() {
8989

9090
static Settings previousSettings;
9191
if (memcmp(&settings, &previousSettings, sizeof(Settings)) != 0) {
92-
Serial.println("Cambio de configuración de settings detectado.");
92+
Serial.println("Cambio de configuracion de settings detectado.");
9393
Serial.print("Habilitado: ");
9494
Serial.println(settings.wifiSettings.wifi_enabled ? "Si" : "No");
9595
Serial.print("OTA: ");

Firmware_ESP8266/basic_defines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ enum seleccionMenu {
5050
SELECCION_MENU_CONFIG_VOLUMEN_AJUSTE,
5151
SELECCION_MENU_CONFIG_DEBUG,
5252
SELECCION_MENU_CONFIG_DEBUG_SOFT_RST_COUNT,
53+
SELECCION_MENU_CONFIG_DEBUG_WIFI_SSID,
54+
SELECCION_MENU_CONFIG_DEBUG_WIFI_IP,
5355
SELECCION_MENU_CONFIG_WIFI,
5456
SELECCION_MENU_CONFIG_WIFI_HABILITAR,
5557
SELECCION_MENU_CONFIG_WIFI_OTA_HABILITAR,

Firmware_ESP8266/lcd.h

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,36 @@ void _sendLcdBuffer(String line1, String line2) {
8282
}
8383
}
8484

85+
String _getScrollingText(String text, unsigned int visibleChars) {
86+
static unsigned long lastScrollTime = 0;
87+
static unsigned int scrollOffset = 0;
88+
89+
// Añadir separación al final para el efecto loop
90+
String scrollingSSID = text + String(" "); // espacio entre loops
91+
92+
// Actualizar desplazamiento cada 200ms
93+
if (millis() - lastScrollTime >= LCD_SLIDE_OR_FLASH_SPEED_MS) {
94+
lastScrollTime = millis();
95+
scrollOffset++;
96+
if (scrollOffset >= scrollingSSID.length()) {
97+
scrollOffset = 0;
98+
}
99+
}
100+
101+
// Construir ventana deslizante
102+
String scrolled;
103+
for (unsigned int i = 0; i < visibleChars; ++i) {
104+
int charIndex = (scrollOffset + i) % scrollingSSID.length();
105+
scrolled += scrollingSSID.charAt(charIndex);
106+
}
107+
108+
while (scrolled.length() < visibleChars) {
109+
scrolled += " ";
110+
}
111+
112+
return scrolled;
113+
}
114+
85115
bool pantalla_sendLcdBuffer(String newBuffer) {
86116
static String bufferAnterior;
87117
if ((bufferAnterior != newBuffer) && (newBuffer != "")) {
@@ -375,9 +405,37 @@ void pantalla_handleButtonInMenu(
375405
case BUTTON_STATUS_LEFT:
376406
newMenu = SELECCION_MENU_CONFIG_DEBUG;
377407
break;
408+
case BUTTON_STATUS_RIGHT:
409+
newMenu = SELECCION_MENU_CONFIG_DEBUG_WIFI_SSID;
410+
break;
411+
case BUTTON_STATUS_UP:
412+
case BUTTON_STATUS_DOWN:
413+
buzzer_sound_error();
414+
break;
415+
}
416+
break;
417+
case SELECCION_MENU_CONFIG_DEBUG_WIFI_SSID:
418+
switch (currentButtonPressed) {
419+
case BUTTON_STATUS_LEFT:
420+
newMenu = SELECCION_MENU_CONFIG_DEBUG_SOFT_RST_COUNT;
421+
break;
422+
case BUTTON_STATUS_RIGHT:
423+
newMenu = SELECCION_MENU_CONFIG_DEBUG_WIFI_IP;
424+
break;
378425
case BUTTON_STATUS_UP:
379426
case BUTTON_STATUS_DOWN:
427+
buzzer_sound_error();
428+
break;
429+
}
430+
break;
431+
case SELECCION_MENU_CONFIG_DEBUG_WIFI_IP:
432+
switch (currentButtonPressed) {
433+
case BUTTON_STATUS_LEFT:
434+
newMenu = SELECCION_MENU_CONFIG_DEBUG_WIFI_SSID;
435+
break;
380436
case BUTTON_STATUS_RIGHT:
437+
case BUTTON_STATUS_UP:
438+
case BUTTON_STATUS_DOWN:
381439
buzzer_sound_error();
382440
break;
383441
}
@@ -805,6 +863,23 @@ void pantalla_actualizarMenuConfigDebugSoftRstCount(String* lcdBuffer) {
805863
while (lcdBuffer->length() < 16) {
806864
*lcdBuffer += String(" ");
807865
}
866+
*lcdBuffer += String("< >");
867+
}
868+
869+
void pantalla_actualizarMenuConfigDebugWifiSsid(String* lcdBuffer) {
870+
*lcdBuffer = String("WIFI: ");
871+
*lcdBuffer += _getScrollingText(settings.wifiSettings.ssid_sta, 10);
872+
873+
if (ESP8266Utils_isWifiConnected()) {
874+
*lcdBuffer += String("< CONECTADO >");
875+
} else {
876+
*lcdBuffer += String("< DESCONECTADO >");
877+
}
878+
}
879+
880+
void pantalla_actualizarMenuConfigDebugWifiIp(String* lcdBuffer) {
881+
*lcdBuffer += String("IP: ");
882+
*lcdBuffer += _getScrollingText(ESP8266Utils_getWifiIP(), 12);
808883
*lcdBuffer += String("< ");
809884
}
810885

@@ -912,38 +987,7 @@ void pantalla_actualizarMenuConfigWifiSsid(String* lcdBuffer) {
912987
if (adjustedSSIDindex < 0) {
913988
mySSID = "No hay redes";
914989
}
915-
916-
static unsigned long lastScrollTime = 0;
917-
static unsigned int scrollOffset = 0;
918-
919-
// Espacio visible para SSID
920-
const int visibleChars = 11;
921-
922-
// Añadir separación al final para el efecto loop
923-
String scrollingSSID = mySSID + " "; // espacio entre loops
924-
925-
// Actualizar desplazamiento cada 200ms
926-
if (millis() - lastScrollTime >= LCD_SLIDE_OR_FLASH_SPEED_MS) {
927-
lastScrollTime = millis();
928-
scrollOffset++;
929-
if (scrollOffset >= scrollingSSID.length()) {
930-
scrollOffset = 0;
931-
}
932-
}
933-
934-
// Construir ventana deslizante
935-
String scrolled;
936-
for (int i = 0; i < visibleChars; ++i) {
937-
int charIndex = (scrollOffset + i) % scrollingSSID.length();
938-
scrolled += scrollingSSID.charAt(charIndex);
939-
}
940-
941-
*lcdBuffer += scrolled;
942-
943-
// Asegurar longitud mínima de 16
944-
while (lcdBuffer->length() < 16) {
945-
*lcdBuffer += " ";
946-
}
990+
*lcdBuffer += _getScrollingText(mySSID, 11);
947991

948992
// Agregar navegación
949993
*lcdBuffer += String("< ");
@@ -1026,6 +1070,12 @@ void pantalla_actualizarMenu(uint8_t selectedMenu) {
10261070
case SELECCION_MENU_CONFIG_DEBUG_SOFT_RST_COUNT:
10271071
pantalla_actualizarMenuConfigDebugSoftRstCount(&lcdBuffer);
10281072
break;
1073+
case SELECCION_MENU_CONFIG_DEBUG_WIFI_SSID:
1074+
pantalla_actualizarMenuConfigDebugWifiSsid(&lcdBuffer);
1075+
break;
1076+
case SELECCION_MENU_CONFIG_DEBUG_WIFI_IP:
1077+
pantalla_actualizarMenuConfigDebugWifiIp(&lcdBuffer);
1078+
break;
10291079
case SELECCION_MENU_CONFIG_WIFI:
10301080
pantalla_actualizarMenuConfigWifi(&lcdBuffer);
10311081
break;

platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ board_build.flash_mode = dout
1111
board_build.f_cpu = 80000000L
1212
board_build.f_flash = 40000000L
1313

14-
upload_protocol = esptool
15-
# upload_port = COM7
14+
upload_protocol = espota
15+
upload_port = RollerShutterControlPanel.local
1616
upload_resetmethod = ck
1717
upload_speed = 3000000
1818

0 commit comments

Comments
 (0)