Refactor WiFi connection handling: move PIN definitions, enhance connection logic, and integrate MQTT initialization in setup
This commit is contained in:
@@ -6,4 +6,10 @@
|
||||
#define SSID "DIGIFIBRA-D2ys"
|
||||
#define WIFI_PASSWORD "4EEATsyTcZ"
|
||||
|
||||
int WiFi_Init();
|
||||
#define PIN_R 12
|
||||
#define PIN_G 13
|
||||
#define PIN_B 14
|
||||
|
||||
void WiFi_Init();
|
||||
void WiFi_Handle();
|
||||
bool WiFi_IsConnected();
|
||||
@@ -1,10 +1,10 @@
|
||||
#include <WifiConnection.hpp>
|
||||
|
||||
#define PIN_R 12
|
||||
#define PIN_G 13
|
||||
#define PIN_B 14
|
||||
|
||||
|
||||
WiFiClient wifiClient;
|
||||
static bool wifiConnected = false;
|
||||
static TaskTimer wifiTimer{0, 500};
|
||||
|
||||
void setColor(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
@@ -25,7 +25,6 @@ void setupLED()
|
||||
ledcAttachPin(PIN_B, 2);
|
||||
}
|
||||
|
||||
// hue cycle
|
||||
void hueCycle(uint8_t pos)
|
||||
{
|
||||
uint8_t r = (uint8_t)(sin((pos + 0) * 0.024) * 127 + 128);
|
||||
@@ -34,53 +33,54 @@ void hueCycle(uint8_t pos)
|
||||
setColor(r, g, b);
|
||||
}
|
||||
|
||||
int WiFi_Init()
|
||||
void WiFi_Init()
|
||||
{
|
||||
setupLED();
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, WIFI_PASSWORD);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("Conectando a la red WiFi: ");
|
||||
Serial.print(SSID);
|
||||
Serial.print("🟡 Intentando conectar a WiFi: ");
|
||||
Serial.println(SSID);
|
||||
#endif
|
||||
}
|
||||
|
||||
int hue = 0;
|
||||
uint32_t start = millis();
|
||||
const uint32_t timeout = 15000;
|
||||
bool WiFi_IsConnected()
|
||||
{
|
||||
return wifiConnected;
|
||||
}
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - start < timeout)
|
||||
void WiFi_Handle()
|
||||
{
|
||||
static uint8_t hue = 0;
|
||||
uint32_t now = millis();
|
||||
|
||||
if (!wifiConnected)
|
||||
{
|
||||
hueCycle(hue++);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print(".");
|
||||
#endif
|
||||
|
||||
delay(30);
|
||||
}
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
setColor(0, 255, 0);
|
||||
|
||||
if (!wifiConnected)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Conectado a la red WiFi");
|
||||
Serial.print("Dirección IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println("🟢 Conectado a la red WiFi");
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
setColor(0, 255, 0);
|
||||
wifiConnected = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
||||
if (now - wifiRetryTimer.lastRun >= wifiRetryTimer.interval)
|
||||
{
|
||||
setColor(255, 0, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("No se pudo conectar a la red WiFi");
|
||||
Serial.println("🔁 Reintentando conexión WiFi...");
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
WiFi.disconnect(true);
|
||||
WiFi.begin(SSID, WIFI_PASSWORD);
|
||||
wifiRetryTimer.lastRun = now;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ BME280Data_t bme280Data;
|
||||
GPSData_t gpsData;
|
||||
#endif
|
||||
|
||||
static bool mqttStarted = false;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@@ -33,16 +35,11 @@ void setup()
|
||||
Serial.println("Iniciando...");
|
||||
#endif
|
||||
|
||||
while(WiFi_Init() != WL_CONNECTED)
|
||||
{
|
||||
Serial.println("Esperando conexión WiFi...");
|
||||
delay(1000);
|
||||
}
|
||||
MQTT_Init(MQTT_URI, MQTT_PORT);
|
||||
|
||||
WiFi_Init();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
GROUP_ID = getGroupId(DEVICE_ID);
|
||||
BME280_Init();
|
||||
@@ -72,6 +69,22 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
WiFi_Handle();
|
||||
|
||||
if(!WiFi_IsConnected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mqttStarted)
|
||||
{
|
||||
MQTT_Init(MQTT_SERVER, MQTT_PORT);
|
||||
mqttStarted = true;
|
||||
#ifdef DEBUG
|
||||
Serial.println("Iniciando conexión MQTT...");
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t now = millis();
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
|
||||
Reference in New Issue
Block a user