/* ============================================
code is placed under the MIT license
Copyright (c) 2026 J-M-L For the arduino forum
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "Debug.h"
#include "NTP.h"
#include "Location.h"
#include <Adafruit_NeoPixel.h>
const char * SSID = "Wokwi-GUEST";
const char * PWD = "";
const uint8_t stripPin = 15;
Location locations[] = {
{"Amsterdam", 52.367984, 4.903561},
{"Paris", 48.856613, 2.352222},
{"London", 51.507351, -0.127758},
{"Rome", 41.902782, 12.496366},
{"Stockholm", 59.329323, 18.068581},
{"Moscow", 55.755825, 37.617298},
{"Singapore", 1.352083, 103.819836},
{"Tokyo", 35.689487, 139.691711},
{"Sydney", -33.868820, 151.209296},
{"New York", 40.712776, -74.005974},
{"Mexico City", 19.432608, -99.133209},
{"Los Angeles", 34.052235, -118.243683},
{"Sao Paulo", -23.550520, -46.633308},
};
constexpr size_t locationsCount = sizeof locations / sizeof * locations;
Adafruit_NeoPixel locationsStrip(locationsCount, stripPin, NEO_GRB + NEO_KHZ800);
void updateSolarInfoEveryDay(time_t nowUTC) {
static int lastDay = -1;
struct tm tmNowUTC;
gmtime_r(&nowUTC, &tmNowUTC); // thread safe
if (tmNowUTC.tm_yday != lastDay) {
lastDay = tmNowUTC.tm_yday;
D_printf("Date (UTC): %04d-%02d-%02d | All times are UTC\n",
tmNowUTC.tm_year + 1900, tmNowUTC.tm_mon + 1, tmNowUTC.tm_mday);
for (auto &loc : locations) {
loc.updateSolar(nowUTC);
loc.printSolarInfo();
}
D_println();
}
}
float ledBrightness(const Location& loc, time_t nowUTC) {
D_printf("%-10s ", loc.name);
struct tm tmNowUTC;
gmtime_r(&nowUTC, &tmNowUTC); // thread safe
if (!loc.solar.valid) {
D_printf("- Solar data invalid, LED off\n");
return 0.0;
}
if (nowUTC < loc.getCivilDawn()) {
D_printf("%-8s - LED fully on\n", "Night");
return 1.0;
}
if (nowUTC < loc.getSunrise()) {
float b = float(loc.getSunrise() - nowUTC) / float(loc.getSunrise() - loc.getCivilDawn());
D_printf("%-8s - LED fading: %.2f %%\n", "Dawn", b);
return b;
}
if (nowUTC < loc.getSunset()) {
D_printf("%-8s - LED off\n", "Day");
return 0.0;
}
if (nowUTC < loc.getCivilDusk()) {
float b = float(nowUTC - loc.getSunset()) / float(loc.getCivilDusk() - loc.getSunset());
D_printf("%-8s - LED fading: %.2f %%\n", "Dusk", b);
return b;
}
D_printf("%-8s - LED fully on\n", "Night");
return 1.0;
}
void setup() {
D_SerialBegin(115200);
ntpBegin(SSID, PWD, 6); // 6 is the WiFi channel for wokwi. remove that param on real ESP32
locationsStrip.begin();
locationsStrip.setBrightness(255);
locationsStrip.clear();
locationsStrip.show();
}
void loop() {
if (!ntpIsSynced()) return;
time_t nowUTC = ntpGetTime();
updateSolarInfoEveryDay(nowUTC); // keeps daily solar up to date and prints info
struct tm tmNowUTC;
gmtime_r(&nowUTC, &tmNowUTC);
D_printf("On %02d/%02d/%04d, at %02d:%02d:%02d UTC\n",
tmNowUTC.tm_mday, tmNowUTC.tm_mon + 1, tmNowUTC.tm_year + 1900,
tmNowUTC.tm_hour, tmNowUTC.tm_min, tmNowUTC.tm_sec);
for (size_t locId = 0; locId < locationsCount; locId++) {
float brightness = ledBrightness(locations[locId], nowUTC);
if (brightness == 0) locationsStrip.setPixelColor(locId, Adafruit_NeoPixel::Color(0, 100, 0)); // led off but to see it I make it light green
else if (brightness == 1) locationsStrip.setPixelColor(locId, Adafruit_NeoPixel::Color(255, 0, 0)); // full on red
else locationsStrip.setPixelColor(locId, Adafruit_NeoPixel::Color(255 * brightness, 255 * brightness, 0)); // yelllow gradient
}
D_println();
locationsStrip.show();
delay(5000);
}
Amsterdam
Paris
London
Rome
Stockholm
Moscow
Singapore
Tokyo
Sydney
New York
Mexico City
Los Angeles
Sao Paulo