372 lines
12 KiB
C++
372 lines
12 KiB
C++
#include <WiFi.h>
|
|
#include "time.h"
|
|
#include <NeoPixelBrightnessBus.h>
|
|
#include <IotWebConf.h>
|
|
|
|
#define MOD(a,b) ((((a)%(b))+(b))%(b))
|
|
/* Model I
|
|
LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6, 12bit resolution default
|
|
Light Resistance (at 10 Lux): 5-10 Kohm
|
|
Dark Resistance: 0.5 Mohm
|
|
over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm)
|
|
*/
|
|
#define LDR_PIN A6
|
|
|
|
#define NUM_LEDS 60
|
|
#define COLOR_ORDER GRB
|
|
#define DATA_PIN 17
|
|
#define SERIAL_BAUD 115200
|
|
|
|
//#define RGBW
|
|
bool singleSecond = true; //show seconds
|
|
bool allDotsOn = true; //lighten up all leds
|
|
bool followingHour = true; //move hour like an analog one
|
|
bool setupDone = false;
|
|
int volatile hourOffset = 0;
|
|
|
|
#ifdef RGBW
|
|
#define MINIMAL_BRIGHTNESS 5
|
|
#define LDR_SCALE 16
|
|
#define colorSaturation 255
|
|
NeoPixelBrightnessBus<NeoGrbwFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN); //SK6812
|
|
RgbwColor red(colorSaturation, 0, 0, 0);
|
|
RgbwColor green(0, colorSaturation, 0, 0);
|
|
RgbwColor blue(0, 0, colorSaturation, 0);
|
|
RgbwColor white(colorSaturation);
|
|
RgbwColor black(0);
|
|
RgbwColor unknown(30, 48, 40, 0); //darkish dirt
|
|
RgbwColor gold(HtmlColor( 0xFFD700 ) );
|
|
RgbwColor orangered(HtmlColor( 0xFF4500 ) );
|
|
RgbwColor orange(HtmlColor( 0xFFA500 ) );
|
|
RgbwColor darkred(HtmlColor(0x800000));
|
|
RgbwColor temp;
|
|
#else
|
|
#define MINIMAL_BRIGHTNESS 20
|
|
#define LDR_SCALE 16
|
|
#define colorSaturation 192
|
|
//NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
|
|
NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp32I2s1800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
|
|
RgbColor red(colorSaturation, 0, 0);
|
|
RgbColor green(0, colorSaturation, 0);
|
|
RgbColor blue(0, 0, colorSaturation);
|
|
RgbColor realWhite(colorSaturation);
|
|
RgbColor black(0);
|
|
RgbColor white(30, 40, 35); //darkish dirt
|
|
RgbColor whiter(120, 120, 120); //darkish dirt
|
|
RgbColor white12(255, 255, 255); //darkish dirt
|
|
RgbColor gold(HtmlColor( 0xFFD700 ) );
|
|
RgbColor orangered(HtmlColor( 0xFF4500 ) );
|
|
RgbColor orange(HtmlColor( 0xFFA500 ) );
|
|
RgbColor darkred(HtmlColor(0x800000));
|
|
RgbColor temp;
|
|
#endif
|
|
|
|
volatile signed int interruptCounter;
|
|
hw_timer_t * timer = NULL;
|
|
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
void IRAM_ATTR onTimer() {
|
|
portENTER_CRITICAL_ISR(&timerMux);
|
|
interruptCounter++;
|
|
portEXIT_CRITICAL_ISR(&timerMux);
|
|
}
|
|
|
|
//NTP handler
|
|
//const char* ntpServer = "pool.ntp.org";
|
|
const long gmtOffset_sec = 3600; //ToDo changable from user
|
|
const int daylightOffset_sec = 3600;
|
|
struct tm timeinfo;
|
|
volatile int currentSec = 59;
|
|
volatile int currentMin = 1;
|
|
volatile int currentHour = 1;
|
|
|
|
// variable for storing the potentiometer value
|
|
volatile unsigned int ldrValue = 0;
|
|
int MAX_BRIGHTNESS = 200;
|
|
|
|
// WebPortal
|
|
const char thingName[] = "NTP-Clock-RGBLED";
|
|
const char wifiInitialApPassword[] = "12345678";
|
|
#define STRING_LEN 63
|
|
#define NUMBER_LEN 4
|
|
// -- Configuration specific key. The value should be modified if config structure was changed.
|
|
#define CONFIG_VERSION "dem3"
|
|
|
|
DNSServer dnsServer;
|
|
WebServer server(80);
|
|
|
|
char ntpServerParamValue[STRING_LEN] = "pool.ntp.org";
|
|
char maxBrightnessParamValue[NUMBER_LEN] = "100";
|
|
|
|
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
|
|
|
|
IotWebConfParameter ntpServerParam = IotWebConfParameter("NTP Server", "ntpServer", ntpServerParamValue, STRING_LEN, "text", "pool.ntp.org", "pool.ntp.org");
|
|
IotWebConfSeparator separator1 = IotWebConfSeparator("LED settings");
|
|
IotWebConfParameter maxBrightnessParam = IotWebConfParameter("Max BRIGHTNESS", "max brightness", maxBrightnessParamValue, NUMBER_LEN, "number", "20..254", "200", "min='20' max='254' step='1'");
|
|
//IotWebConfParameter singleSecondParam = IotWebConfParameter("Show Seconds", "singleSecond", singleSecondParamValue, STRING_LEN, "text", "Yes", "Yes");
|
|
//IotWebConfParameter allDotsOnParam = IotWebConfParameter("all Dots lighten on", "allDotsOn", allDotsOnParamValue, STRING_LEN, "text", "Yes", "Yes");
|
|
//IotWebConfParameter followingHourParam = IotWebConfParameter("following Hour", "followingHour ", followingHourParamValue, STRING_LEN, "text", "Yes", "Yes");
|
|
|
|
//Version3.X.X IotWebConfCheckboxParameter checkboxParam = IotWebConfCheckboxParameter("Check param", "checkParam", checkboxParamValue, STRING_LEN, true);
|
|
|
|
//ToDo color Picker Second, 12Clock, Minute, Hour
|
|
|
|
String printLocalTime() {
|
|
if (!getLocalTime(&timeinfo)) {
|
|
Serial.println("Failed to obtain time");
|
|
return "";
|
|
}
|
|
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
|
|
|
char timeStringBuff[50]; //50 chars should be enough
|
|
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
|
//Serial.println(timeStringBuff);
|
|
return timeStringBuff;
|
|
|
|
}
|
|
|
|
/**
|
|
Handle web requests to "/" path.
|
|
*/
|
|
void handleRoot() {
|
|
// -- Let IotWebConf test and handle captive portal requests.
|
|
if (iotWebConf.handleCaptivePortal())
|
|
{
|
|
// -- Captive portal request were already served.
|
|
return;
|
|
}
|
|
//char timeStringBuff[50]; //50 chars should be enough
|
|
//strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
|
|
|
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
|
|
s += "<meta http-equiv=\"refresh\" content=\"30\">";
|
|
s += "<meta charset=\"UTF-8\">";
|
|
s += "<title>RGB LED Clock</title></head><body>RGB LED Clock";
|
|
s += "<p>";
|
|
s += "<table>";
|
|
s += "<tr><td>Current Time:</td><td>";
|
|
s += printLocalTime();
|
|
s += "</td></tr><tr></tr><tr><td>NTP Server:</td><td>";
|
|
s += ntpServerParamValue;
|
|
s += "</td><tr><tr><td>Current Brightness value: </td><td>";
|
|
s += String(strip.GetBrightness());
|
|
s += "</td><tr><tr><td>Max Brightness value: </td><td>";
|
|
s += atoi(maxBrightnessParamValue);
|
|
s += "</td></tr><tr><td>Show Seconds: </td><td>";
|
|
s += (singleSecond ? "Yes" : "No");
|
|
s += "</td></tr><tr><td>All Dots On: </td><td>";
|
|
s += (allDotsOn ? "Yes" : "No");
|
|
s += "</td></tr><tr><td>Following Hour: </td><td>";
|
|
s += (followingHour ? "Yes" : "No");
|
|
s += "<td></tr></table>";
|
|
s += "<p>";
|
|
s += "Go to <a href='config'>configure page</a> to change values.";
|
|
s += "</body></html>\n";
|
|
|
|
server.send(200, "text/html", s);
|
|
}
|
|
|
|
void configSaved()
|
|
{
|
|
//ToDo ntpServerParamValue;
|
|
MAX_BRIGHTNESS = atoi(maxBrightnessParamValue);
|
|
Serial.println("Configuration was updated.");
|
|
|
|
}
|
|
|
|
boolean formValidator()
|
|
{
|
|
Serial.println("Validating form.");
|
|
boolean valid = true;
|
|
|
|
int l = server.arg(ntpServerParam.getId()).length();
|
|
if (l < 3)
|
|
{
|
|
ntpServerParam.errorMessage = "Please provide at least 3 characters for this test!";
|
|
valid = false;
|
|
}
|
|
int b = atoi(maxBrightnessParamValue);
|
|
if (b < MINIMAL_BRIGHTNESS || b > 254)
|
|
{
|
|
valid = false;
|
|
}
|
|
|
|
return valid;
|
|
}
|
|
|
|
void iotWebConf_Setup() {
|
|
iotWebConf.addParameter(&ntpServerParam);
|
|
iotWebConf.addParameter(&separator1);
|
|
iotWebConf.addParameter(&maxBrightnessParam);
|
|
/*
|
|
iotWebConf.addParameter(&separator1);
|
|
iotWebConf.addParameter(&singleSecondParam);
|
|
iotWebConf.addParameter(&separator1);
|
|
iotWebConf.addParameter(&allDotsOnParam);
|
|
iotWebConf.addParameter(&separator1);
|
|
iotWebConf.addParameter(&followingHourParam);
|
|
*/
|
|
iotWebConf.setConfigSavedCallback(&configSaved);
|
|
iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established
|
|
iotWebConf.setFormValidator(&formValidator);
|
|
iotWebConf.getApTimeoutParameter()->visible = true;
|
|
|
|
// -- Initializing the configuration.
|
|
iotWebConf.init();
|
|
|
|
// -- Set up required URL handlers on the web server.
|
|
server.on("/", handleRoot);
|
|
server.on("/config", [] { iotWebConf.handleConfig(); });
|
|
server.onNotFound([]() {
|
|
iotWebConf.handleNotFound();
|
|
});
|
|
|
|
Serial.println("Ready.");
|
|
Serial.flush();
|
|
}
|
|
|
|
void syncNTP() {
|
|
if (iotWebConf.getState() == 4) {
|
|
//init and get the time
|
|
configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
|
|
Serial.println(printLocalTime());
|
|
//inverted logic - remaining time
|
|
currentSec = 60 - timeinfo.tm_sec;
|
|
currentMin = 60 - timeinfo.tm_min;
|
|
currentHour = 12 - (timeinfo.tm_hour % 12);
|
|
Serial.println("NTP mapped: " + String(currentHour) + ":" + String(currentMin) + ":" + String(currentSec));
|
|
setupDone = true;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
//interrupt for one second
|
|
timer = timerBegin(0, 80, true);
|
|
timerAttachInterrupt(timer, &onTimer, true);
|
|
timerAlarmWrite(timer, 1000000, true);
|
|
timerAlarmEnable(timer);
|
|
|
|
pinMode(LDR_PIN, INPUT_PULLUP);
|
|
pinMode(DATA_PIN, OUTPUT);
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
Serial.flush();
|
|
Serial.print("\nCPU Frequency is: ");
|
|
Serial.print(getCpuFrequencyMhz()); //Get CPU clock
|
|
Serial.println(" Mhz");
|
|
|
|
strip.Begin();
|
|
strip.SetBrightness( MINIMAL_BRIGHTNESS );
|
|
strip.ClearTo(white);
|
|
/*
|
|
for (int dot = 0; dot < NUM_LEDS; dot++) {
|
|
strip.SetPixelColor(dot, white);
|
|
}
|
|
*/
|
|
strip.Show();
|
|
iotWebConf.delay(250);
|
|
|
|
iotWebConf_Setup();
|
|
//Serial.end();
|
|
if (followingHour) {
|
|
hourOffset = 0 - ((60 - currentMin) / 12); //negative value
|
|
}
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// -- doLoop should be called as frequently as possible.
|
|
iotWebConf.doLoop();
|
|
/*
|
|
while (Serial.available())
|
|
Serial.read();
|
|
*/
|
|
if (!setupDone && interruptCounter > 0) {
|
|
if (currentSec < 0) {
|
|
currentSec = 59;
|
|
currentMin--;
|
|
}
|
|
strip.ClearTo(black);
|
|
strip.SetPixelColor((currentSec + 0) % NUM_LEDS, white);
|
|
strip.SetPixelColor((currentSec - 1) % NUM_LEDS, whiter);
|
|
strip.SetPixelColor((currentSec - 2) % NUM_LEDS, white12);
|
|
strip.Show();
|
|
portENTER_CRITICAL(&timerMux);
|
|
interruptCounter--;
|
|
currentSec--;
|
|
portEXIT_CRITICAL(&timerMux);
|
|
}
|
|
if (setupDone && interruptCounter > 0) {
|
|
//brigthness measurement
|
|
ldrValue = (ldrValue + analogRead(LDR_PIN));
|
|
//Serial.println(ldrValue);
|
|
if (currentSec % 10 == 0) {
|
|
//brigthness begin
|
|
ldrValue = ldrValue / 10;
|
|
int x;
|
|
if (ldrValue == 0) {
|
|
strip.SetBrightness( MAX_BRIGHTNESS );
|
|
}
|
|
else {
|
|
x = int(MINIMAL_BRIGHTNESS + (MAX_BRIGHTNESS - MINIMAL_BRIGHTNESS) / ((ldrValue / LDR_SCALE) + 1) );
|
|
strip.SetBrightness(x);
|
|
}
|
|
//Serial.println("Brightness: " + String(strip.GetBrightness()) + "(" + String(ldrValue) + ")");
|
|
ldrValue = 0;
|
|
//brightness end
|
|
}
|
|
if (currentSec < 0) {
|
|
currentSec = 59;
|
|
currentMin--;
|
|
|
|
if (currentMin < 0) {
|
|
currentMin = 59;
|
|
currentHour--;
|
|
if (currentHour < 0) {
|
|
currentHour = 11;
|
|
//timerAlarmDisable(timer);
|
|
syncNTP();
|
|
//timerAlarmEnable(timer);
|
|
}
|
|
}
|
|
if (followingHour) {
|
|
hourOffset = 0 - ((60 - currentMin) / 12); //negative value
|
|
//Serial.println("hourOffset: " + String(hourOffset));
|
|
}
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour * 5) + ":" + String(currentMin) + ":" + String(currentSec));
|
|
}
|
|
|
|
temp = allDotsOn ? white : black;
|
|
strip.ClearTo(temp);
|
|
//1 dot hour marking
|
|
for (int dot = 0; dot < NUM_LEDS; dot++) {
|
|
if (dot % 5 == 0) {
|
|
strip.SetPixelColor(dot, whiter);
|
|
}
|
|
}
|
|
//3 dots hour
|
|
if (currentHour == 0) {
|
|
strip.SetPixelColor((59 + hourOffset) % NUM_LEDS, gold);
|
|
}
|
|
else {
|
|
strip.SetPixelColor((currentHour * 5 - 1 + hourOffset) % NUM_LEDS, gold);
|
|
}
|
|
strip.SetPixelColor((currentHour * 5 + hourOffset) % NUM_LEDS, gold);
|
|
strip.SetPixelColor((currentHour * 5 + 1 + hourOffset) % NUM_LEDS, gold);
|
|
|
|
strip.SetPixelColor(0, white12); //define high noon
|
|
//strip.SetPixelColor(NUM_LEDS / 2, green; //define half noon
|
|
|
|
if (singleSecond) {
|
|
strip.SetPixelColor((currentSec + 0) % NUM_LEDS, (allDotsOn ? black : white));
|
|
}
|
|
strip.SetPixelColor((currentMin + 0) % NUM_LEDS, darkred);
|
|
strip.Show();
|
|
|
|
portENTER_CRITICAL(&timerMux);
|
|
interruptCounter--;
|
|
currentSec--;
|
|
portEXIT_CRITICAL(&timerMux);
|
|
}
|
|
}
|