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