working iotWebConf,loading animation

This commit is contained in:
coelner 2020-12-26 16:00:29 +01:00
parent 6776269b8a
commit d21301a1e8

View File

@ -4,24 +4,31 @@
#include <IotWebConf.h> #include <IotWebConf.h>
#define MOD(a,b) ((((a)%(b))+(b))%(b)) #define MOD(a,b) ((((a)%(b))+(b))%(b))
// LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6, 12bit resolution default /* Model I
// Light Resistance (at 10 Lux): 5-10 Kohm LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6, 12bit resolution default
// Dark Resistance: 0.5 Mohm Light Resistance (at 10 Lux): 5-10 Kohm
// over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm) Dark Resistance: 0.5 Mohm
over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm)
*/
#define LDR_PIN A6 #define LDR_PIN A6
// How many leds in your strip?
#define colorSaturation 128
#define MINIMAL_BRIGHTNESS 2
#define NUM_LEDS 60 #define NUM_LEDS 60
#define COLOR_ORDER GRB #define COLOR_ORDER GRB
#define DATA_PIN 17 #define DATA_PIN 17
#define SERIAL_BAUD 9600 #define SERIAL_BAUD 115200
// Define the array of leds
NeoPixelBrightnessBus<NeoGrbwFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN); //#define RGBW
//NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN); bool singleSecond = true; //show seconds
//NeoPixelBus<NeoRgbFeature, Neo400KbpsMethod> strip(NUM_LEDS, DATA_PIN); 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 red(colorSaturation, 0, 0, 0);
RgbwColor green(0, colorSaturation, 0, 0); RgbwColor green(0, colorSaturation, 0, 0);
RgbwColor blue(0, 0, colorSaturation, 0); RgbwColor blue(0, 0, colorSaturation, 0);
@ -31,8 +38,27 @@ RgbwColor unknown(30, 48, 40, 0); //darkish dirt
RgbwColor gold(HtmlColor( 0xFFD700 ) ); RgbwColor gold(HtmlColor( 0xFFD700 ) );
RgbwColor orangered(HtmlColor( 0xFF4500 ) ); RgbwColor orangered(HtmlColor( 0xFF4500 ) );
RgbwColor orange(HtmlColor( 0xFFA500 ) ); 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);
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; volatile signed int interruptCounter;
hw_timer_t * timer = NULL; hw_timer_t * timer = NULL;
@ -46,7 +72,7 @@ void IRAM_ATTR onTimer() {
//NTP handler //NTP handler
//const char* ntpServer = "pool.ntp.org"; //const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600; const long gmtOffset_sec = 3600; //ToDo changable from user
const int daylightOffset_sec = 3600; const int daylightOffset_sec = 3600;
struct tm timeinfo; struct tm timeinfo;
volatile int currentSec = 59; volatile int currentSec = 59;
@ -54,35 +80,48 @@ volatile int currentMin = 1;
volatile int currentHour = 1; volatile int currentHour = 1;
// variable for storing the potentiometer value // variable for storing the potentiometer value
volatile int ldrValue = 0; volatile unsigned int ldrValue = 0;
int MAX_BRIGHTNESS = 200;
// WebPortal // WebPortal
const char thingName[] = "NTP-Clock-RGBLED"; const char thingName[] = "NTP-Clock-RGBLED";
const char wifiInitialApPassword[] = "abcdefgh"; const char wifiInitialApPassword[] = "12345678";
#define STRING_LEN 63 #define STRING_LEN 63
#define NUMBER_LEN 4 #define NUMBER_LEN 4
// -- Configuration specific key. The value should be modified if config structure was changed. // -- Configuration specific key. The value should be modified if config structure was changed.
#define CONFIG_VERSION "dem2" #define CONFIG_VERSION "dem3"
DNSServer dnsServer; DNSServer dnsServer;
WebServer server(80); WebServer server(80);
char ntpServerParamValue[STRING_LEN] = "pool.ntp.org"; char ntpServerParamValue[STRING_LEN] = "pool.ntp.org";
char brightnessParamValue[NUMBER_LEN] = "100"; char maxBrightnessParamValue[NUMBER_LEN] = "100";
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION); IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
IotWebConfParameter ntpServerParam = IotWebConfParameter("NTP Server", "ntpServer", ntpServerParamValue, STRING_LEN, "text", "pool.ntp.org", "pool.ntp.org"); IotWebConfParameter ntpServerParam = IotWebConfParameter("NTP Server", "ntpServer", ntpServerParamValue, STRING_LEN, "text", "pool.ntp.org", "pool.ntp.org");
IotWebConfSeparator separator1 = IotWebConfSeparator("LED settings"); IotWebConfSeparator separator1 = IotWebConfSeparator("LED settings");
IotWebConfParameter brightnessParam = IotWebConfParameter("BRIGHTNESS", "brightness", brightnessParamValue, NUMBER_LEN, "number", "10..254", "100", "min='10' max='254' step='1'"); 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);
void printLocalTime() { //ToDo color Picker Second, 12Clock, Minute, Hour
String printLocalTime() {
if (!getLocalTime(&timeinfo)) { if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time"); Serial.println("Failed to obtain time");
return; return "";
} }
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); //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;
} }
/** /**
@ -95,16 +134,31 @@ void handleRoot() {
// -- Captive portal request were already served. // -- Captive portal request were already served.
return; 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\"/>"; String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>RGB LED Clock</title></head><body>RGB LED Clock Config"; s += "<meta http-equiv=\"refresh\" content=\"30\">";
s += "<ul>"; s += "<meta charset=\"UTF-8\">";
s += "<li>NTP Server: \t"; 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 += ntpServerParamValue;
s += "<li>Brightness value: \t"; s += "</td><tr><tr><td>Current Brightness value: </td><td>";
s += atoi(brightnessParamValue); s += String(strip.GetBrightness());
//s += "<li>Float param value: "; s += "</td><tr><tr><td>Max Brightness value: </td><td>";
//s += atof(floatParamValue); s += atoi(maxBrightnessParamValue);
s += "</ul>"; 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 += "Go to <a href='config'>configure page</a> to change values.";
s += "</body></html>\n"; s += "</body></html>\n";
@ -113,7 +167,10 @@ void handleRoot() {
void configSaved() void configSaved()
{ {
//ToDo ntpServerParamValue;
MAX_BRIGHTNESS = atoi(maxBrightnessParamValue);
Serial.println("Configuration was updated."); Serial.println("Configuration was updated.");
} }
boolean formValidator() boolean formValidator()
@ -127,12 +184,11 @@ boolean formValidator()
ntpServerParam.errorMessage = "Please provide at least 3 characters for this test!"; ntpServerParam.errorMessage = "Please provide at least 3 characters for this test!";
valid = false; valid = false;
} }
int b = atoi(brightnessParamValue); int b = atoi(maxBrightnessParamValue);
if (b < MINIMAL_BRIGHTNESS || b > 254) if (b < MINIMAL_BRIGHTNESS || b > 254)
{ {
valid = false; valid = false;
} }
//TODO check brightness
return valid; return valid;
} }
@ -140,11 +196,17 @@ boolean formValidator()
void iotWebConf_Setup() { void iotWebConf_Setup() {
iotWebConf.addParameter(&ntpServerParam); iotWebConf.addParameter(&ntpServerParam);
iotWebConf.addParameter(&separator1); iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&brightnessParam); iotWebConf.addParameter(&maxBrightnessParam);
//iotWebConf.addParameter(&separator2); /*
//iotWebConf.addParameter(&floatParam); iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&singleSecondParam);
iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&allDotsOnParam);
iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&followingHourParam);
*/
iotWebConf.setConfigSavedCallback(&configSaved); iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setWifiConnectionCallback(&syncNTP); iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established
iotWebConf.setFormValidator(&formValidator); iotWebConf.setFormValidator(&formValidator);
iotWebConf.getApTimeoutParameter()->visible = true; iotWebConf.getApTimeoutParameter()->visible = true;
@ -159,18 +221,20 @@ void iotWebConf_Setup() {
}); });
Serial.println("Ready."); Serial.println("Ready.");
Serial.flush();
} }
void syncNTP() { void syncNTP() {
if (iotWebConf.getState() == 4) { if (iotWebConf.getState() == 4) {
//init and get the time //init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue); configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
printLocalTime(); Serial.println(printLocalTime());
//inverted logic - remaining time //inverted logic - remaining time
currentSec = 60 - timeinfo.tm_sec; currentSec = 60 - timeinfo.tm_sec;
currentMin = 60 - timeinfo.tm_min; currentMin = 60 - timeinfo.tm_min;
currentHour = 12 - (timeinfo.tm_hour % 12); currentHour = 12 - (timeinfo.tm_hour % 12);
Serial.println("NTP mapped: " + String(currentHour) + ":" + String(currentMin) + ":" + String(currentSec)); Serial.println("NTP mapped: " + String(currentHour) + ":" + String(currentMin) + ":" + String(currentSec));
setupDone = true;
} }
} }
@ -181,96 +245,126 @@ void setup() {
timerAlarmWrite(timer, 1000000, true); timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer); timerAlarmEnable(timer);
pinMode(LDR_PIN, INPUT); pinMode(LDR_PIN, INPUT_PULLUP);
pinMode(DATA_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT);
Serial.begin(SERIAL_BAUD); Serial.begin(SERIAL_BAUD);
Serial.flush();
Serial.print("CPU Frequency is: ");
Serial.print(getCpuFrequencyMhz()); //Get CPU clock
Serial.print(" Mhz");
strip.Begin(); strip.Begin();
strip.Show(); strip.SetBrightness( MINIMAL_BRIGHTNESS );
strip.ClearTo(white);
//currentSec = 40; currentMin = 1; currentHour = 2; /*
iotWebConf_Setup(); for (int dot = 0; dot < NUM_LEDS; dot++) {
//syncNTP(); strip.SetPixelColor(dot, white);
}
void brightnessSet() {
int BRIGHTNESS = atoi(brightnessParamValue);
int x;
if (ldrValue == 0) {
strip.SetBrightness( BRIGHTNESS );
}
else {
x = int(MINIMAL_BRIGHTNESS + ( (BRIGHTNESS - MINIMAL_BRIGHTNESS) / (ldrValue / 64) + 1) );
if (strip.GetBrightness() != x) {
strip.SetBrightness( x );
} }
} */
Serial.println("Brightness: " + String( x ) + "(" + String(ldrValue) + ")"); strip.Show();
ldrValue = 0; iotWebConf.delay(250);
iotWebConf_Setup();
//Serial.end();
if (followingHour) {
hourOffset = 0 - ((60 - currentMin) / 12); //negative value
}
} }
void loop() void loop()
{ {
// -- doLoop should be called as frequently as possible. // -- doLoop should be called as frequently as possible.
iotWebConf.doLoop(); iotWebConf.doLoop();
while (Serial.available())
if (interruptCounter > 0) { 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); portENTER_CRITICAL(&timerMux);
interruptCounter--; interruptCounter--;
currentSec--; currentSec--;
portEXIT_CRITICAL(&timerMux); portEXIT_CRITICAL(&timerMux);
}
while (setupDone && interruptCounter > 0) {
//brigthness measurement //brigthness measurement
ldrValue = (ldrValue + analogRead(LDR_PIN)) / 2; ldrValue = (ldrValue + analogRead(LDR_PIN));
//Serial.println(ldrValue); //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) { if (currentSec < 0) {
currentSec = 59; currentSec = 59;
currentMin--; currentMin--;
brightnessSet();
if (currentMin < 0) { if (currentMin < 0) {
currentMin = 59; currentMin = 59;
currentHour--; currentHour--;
if (currentHour < 0) { if (currentHour < 0) {
currentHour = 11; currentHour = 11;
//timerAlarmDisable(timer); //timerAlarmDisable(timer);
//ESP.restart();
syncNTP(); syncNTP();
//timerAlarmEnable(timer); //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)); //Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour * 5) + ":" + String(currentMin) + ":" + String(currentSec));
} }
//reset color temp = allDotsOn ? white : black;
strip.SetPixelColor((currentSec + 1) % NUM_LEDS, white); strip.ClearTo(temp);
strip.SetPixelColor((currentMin + 1) % NUM_LEDS, white);
strip.SetPixelColor(((currentHour + 1) * 5 - 1) % NUM_LEDS, white);
strip.SetPixelColor(((currentHour + 1) * 5 + 1) % NUM_LEDS, white);
//1 dot hour marking //1 dot hour marking
for (int dot = 0; dot < NUM_LEDS; dot++) { for (int dot = 0; dot < NUM_LEDS; dot++) {
if (dot % 5 == 0) { if (dot % 5 == 0) {
strip.SetPixelColor(dot, orangered); strip.SetPixelColor(dot, whiter);
} }
} }
//3 dots hour //3 dots hour
if (currentHour == 0) { if (currentHour == 0) {
strip.SetPixelColor(59 % NUM_LEDS, gold); strip.SetPixelColor((59 + hourOffset) % NUM_LEDS, gold);
} }
else { else {
strip.SetPixelColor((currentHour * 5 - 1) % NUM_LEDS, gold); strip.SetPixelColor((currentHour * 5 - 1 + hourOffset) % NUM_LEDS, gold);
} }
strip.SetPixelColor((currentHour * 5) % NUM_LEDS, gold); strip.SetPixelColor((currentHour * 5 + hourOffset) % NUM_LEDS, gold);
strip.SetPixelColor((currentHour * 5 + 1) % NUM_LEDS, gold); strip.SetPixelColor((currentHour * 5 + 1 + hourOffset) % NUM_LEDS, gold);
strip.SetPixelColor(0, blue); //define high noon strip.SetPixelColor(0, white12); //define high noon
//strip.SetPixelColor(NUM_LEDS / 2, CRGB::Green; //define half noon //strip.SetPixelColor(NUM_LEDS / 2, green; //define half noon
strip.SetPixelColor((currentSec + 0) % NUM_LEDS, black); if (singleSecond) {
strip.SetPixelColor((currentMin + 0) % NUM_LEDS, orange); strip.SetPixelColor((currentSec + 0) % NUM_LEDS, (allDotsOn ? black : white));
}
strip.SetPixelColor((currentMin + 0) % NUM_LEDS, darkred);
strip.Show(); strip.Show();
portENTER_CRITICAL(&timerMux);
interruptCounter--;
currentSec--;
portEXIT_CRITICAL(&timerMux);
} }
} }