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>
#define MOD(a,b) ((((a)%(b))+(b))%(b))
// 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)
/* 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
// How many leds in your strip?
#define colorSaturation 128
#define MINIMAL_BRIGHTNESS 2
#define NUM_LEDS 60
#define COLOR_ORDER GRB
#define DATA_PIN 17
#define SERIAL_BAUD 9600
// Define the array of leds
#define SERIAL_BAUD 115200
NeoPixelBrightnessBus<NeoGrbwFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
//NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
//NeoPixelBus<NeoRgbFeature, Neo400KbpsMethod> strip(NUM_LEDS, DATA_PIN);
//#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);
@ -31,8 +38,27 @@ 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);
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;
@ -46,7 +72,7 @@ void IRAM_ATTR onTimer() {
//NTP handler
//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;
struct tm timeinfo;
volatile int currentSec = 59;
@ -54,35 +80,48 @@ volatile int currentMin = 1;
volatile int currentHour = 1;
// variable for storing the potentiometer value
volatile int ldrValue = 0;
volatile unsigned int ldrValue = 0;
int MAX_BRIGHTNESS = 200;
// WebPortal
const char thingName[] = "NTP-Clock-RGBLED";
const char wifiInitialApPassword[] = "abcdefgh";
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 "dem2"
#define CONFIG_VERSION "dem3"
DNSServer dnsServer;
WebServer server(80);
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);
IotWebConfParameter ntpServerParam = IotWebConfParameter("NTP Server", "ntpServer", ntpServerParamValue, STRING_LEN, "text", "pool.ntp.org", "pool.ntp.org");
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)) {
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.
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 += "<title>RGB LED Clock</title></head><body>RGB LED Clock Config";
s += "<ul>";
s += "<li>NTP Server: \t";
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 += "<li>Brightness value: \t";
s += atoi(brightnessParamValue);
//s += "<li>Float param value: ";
//s += atof(floatParamValue);
s += "</ul>";
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";
@ -113,7 +167,10 @@ void handleRoot() {
void configSaved()
{
//ToDo ntpServerParamValue;
MAX_BRIGHTNESS = atoi(maxBrightnessParamValue);
Serial.println("Configuration was updated.");
}
boolean formValidator()
@ -127,12 +184,11 @@ boolean formValidator()
ntpServerParam.errorMessage = "Please provide at least 3 characters for this test!";
valid = false;
}
int b = atoi(brightnessParamValue);
int b = atoi(maxBrightnessParamValue);
if (b < MINIMAL_BRIGHTNESS || b > 254)
{
valid = false;
}
//TODO check brightness
return valid;
}
@ -140,11 +196,17 @@ boolean formValidator()
void iotWebConf_Setup() {
iotWebConf.addParameter(&ntpServerParam);
iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&brightnessParam);
//iotWebConf.addParameter(&separator2);
//iotWebConf.addParameter(&floatParam);
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);
iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established
iotWebConf.setFormValidator(&formValidator);
iotWebConf.getApTimeoutParameter()->visible = true;
@ -159,18 +221,20 @@ void iotWebConf_Setup() {
});
Serial.println("Ready.");
Serial.flush();
}
void syncNTP() {
if (iotWebConf.getState() == 4) {
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
printLocalTime();
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;
}
}
@ -181,96 +245,126 @@ void setup() {
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
pinMode(LDR_PIN, INPUT);
pinMode(LDR_PIN, INPUT_PULLUP);
pinMode(DATA_PIN, OUTPUT);
Serial.begin(SERIAL_BAUD);
Serial.flush();
Serial.print("CPU Frequency is: ");
Serial.print(getCpuFrequencyMhz()); //Get CPU clock
Serial.print(" 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);
//currentSec = 40; currentMin = 1; currentHour = 2;
iotWebConf_Setup();
//syncNTP();
}
void brightnessSet() {
int BRIGHTNESS = atoi(brightnessParamValue);
int x;
if (ldrValue == 0) {
strip.SetBrightness( BRIGHTNESS );
//Serial.end();
if (followingHour) {
hourOffset = 0 - ((60 - currentMin) / 12); //negative value
}
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) + ")");
ldrValue = 0;
}
void loop()
{
// -- doLoop should be called as frequently as possible.
iotWebConf.doLoop();
if (interruptCounter > 0) {
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);
}
while (setupDone && interruptCounter > 0) {
//brigthness measurement
ldrValue = (ldrValue + analogRead(LDR_PIN)) / 2;
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--;
brightnessSet();
if (currentMin < 0) {
currentMin = 59;
currentHour--;
if (currentHour < 0) {
currentHour = 11;
//timerAlarmDisable(timer);
//ESP.restart();
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));
}
//reset color
strip.SetPixelColor((currentSec + 1) % NUM_LEDS, white);
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);
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, orangered);
strip.SetPixelColor(dot, whiter);
}
}
//3 dots hour
if (currentHour == 0) {
strip.SetPixelColor(59 % NUM_LEDS, gold);
strip.SetPixelColor((59 + hourOffset) % NUM_LEDS, gold);
}
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 + 1) % NUM_LEDS, gold);
strip.SetPixelColor((currentHour * 5 + hourOffset) % NUM_LEDS, gold);
strip.SetPixelColor((currentHour * 5 + 1 + hourOffset) % NUM_LEDS, gold);
strip.SetPixelColor(0, blue); //define high noon
//strip.SetPixelColor(NUM_LEDS / 2, CRGB::Green; //define half noon
strip.SetPixelColor(0, white12); //define high noon
//strip.SetPixelColor(NUM_LEDS / 2, green; //define half noon
strip.SetPixelColor((currentSec + 0) % NUM_LEDS, black);
strip.SetPixelColor((currentMin + 0) % NUM_LEDS, orange);
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);
}
}