#include <Adafruit_NeoPixel.h>
#include <math.h>
#include <WiFi.h>
#include <Wire.h>
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
#define PIN_WS2812B 26 // The ESP32 pin GPIO16 connected to WS2812B
#define NUM_PIXELS 8 // The number of LEDs (pixels) on WS2812B LED strip
int area_num = 3, area1 = 8, area2 = 8, area3 = 12;
int horaActual = 0;
int minActual = 0;
int secActual = 0;
int horaDespertador = 7;
int minDespertador = 0;
int amanecerRGB[30][3] = {
{10, 0, 30}, {20, 0, 50}, {30, 0, 70}, {40, 10, 90},
{50, 20, 100}, {60, 30, 110}, {70, 40, 120}, {80, 50, 130},
{90, 60, 140}, {100, 70, 150}, {110, 80, 130}, {120, 90, 110},
{130, 100, 90}, {140, 110, 70}, {150, 120, 50}, {160, 130, 30},
{170, 140, 20}, {180, 150, 10}, {190, 160, 0}, {200, 170, 0},
{210, 180, 0}, {220, 190, 10}, {230, 200, 30}, {240, 210, 50},
{250, 220, 70}, {255, 230, 90}, {255, 240, 120}, {255, 245, 150},
{255, 250, 180}, {255, 255, 200}
};
int atardecerRGB[30][3] = {
{255, 255, 200}, {255, 250, 180}, {255, 245, 150}, {255, 240, 120},
{255, 230, 90}, {250, 220, 70}, {240, 210, 50}, {230, 200, 30},
{220, 190, 10}, {210, 180, 0}, {200, 170, 0}, {190, 160, 0},
{180, 150, 10}, {170, 140, 20}, {160, 130, 30}, {150, 80, 40},
{160, 70, 50}, {170, 60, 60}, {180, 50, 50}, {190, 40, 40},
{200, 30, 30}, {210, 20, 20}, {220, 15, 15}, {230, 10, 10},
{240, 5, 5}, {200, 0, 0}, {150, 0, 0}, {100, 0, 0}, {50, 0, 0},
{20, 0, 0}
};
Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
pinMode (PIN_WS2812B, OUTPUT);
ws2812b.begin(); // initialize WS2812B strip object (REQUIRED)
ws2812b.clear();
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
ws2812b.setPixelColor(0, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ws2812b.setPixelColor(1, ws2812b.Color(0, 255, 0)); //Conectado
ws2812b.setPixelColor(2, ws2812b.Color(0, 255, 0)); //Actualizando hora
ws2812b.show();
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
delay(1000);
}
void loop() {
localTime();
ws2812b.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called
ws2812b.show();
delay(2000);
if (secActual < 29) {
for (int i = 0; i < 30; i++) {
// Establecer color RGB para los LEDs
for (int pixel = 0; pixel < area1; pixel++) { // for each pixel
ws2812b.setPixelColor(pixel, ws2812b.Color(atardecerRGB[i][0], atardecerRGB[i][1], atardecerRGB[i][2])); // it only takes effect if pixels.show() is called
}
ws2812b.show(); // Actualizar los LEDs
delay(100); // Esperar un minuto (60000 ms)
}
} else {
for (int pixel = 0; pixel < area1; pixel++) { // for each pixel
ws2812b.setPixelColor(pixel, ws2812b.Color(0, 255, 0));
}
ws2812b.show(); // Actualizar los LEDs
}
delay(1000);
}
void localTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Connection Err");
return;
}
char buffer[3]; // Para almacenar la hora como texto de 2 caracteres + terminador nulo
strftime(buffer, sizeof(buffer), "%H", &timeinfo); // Obtener la hora como cadena en formato HH
horaActual = atoi(buffer);
strftime(buffer, sizeof(buffer), "%M", &timeinfo); // Obtener la hora como cadena en formato HH
minActual = atoi(buffer);
strftime(buffer, sizeof(buffer), "%S", &timeinfo); // Obtener la hora como cadena en formato HH
secActual = atoi(buffer);
Serial.println(&timeinfo, "%H:%M:%S");
Serial.println(&timeinfo, "%d/%m/%Y %Z");
}