first functional version

This commit is contained in:
coelner 2020-05-03 14:53:41 +02:00
parent b36af62403
commit d72706d4ed

View File

@ -1,17 +1,18 @@
#include <WiFi.h>
#include "time.h"
#include <FastLED.h>
#include <WiFiManager.h>
#include <IotWebConf.h>
#define MOD(a,b) ((((a)%(b))+(b))%(b))
// LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6
// LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6, 12bit resolution default
// over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm)
const int ldrPin = 34;
// How many leds in your strip?
#define MINIMAL_BRIGHTNESS 5
#define NUM_LEDS 60
#define BRIGHTNESS 50
#define COLOR_ORDER GRB
#define DATA_PIN 5
#define SERIAL_BAUD 9600
// Define the array of leds
CRGB leds[NUM_LEDS];
@ -26,7 +27,11 @@ void IRAM_ATTR onTimer() {
portEXIT_CRITICAL_ISR(&timerMux);
}
//NTP handler
//const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
volatile int currentSec = 59;
volatile int currentMin = 1;
volatile int currentHour = 1;
@ -34,14 +39,26 @@ volatile int currentHour = 1;
// variable for storing the potentiometer value
volatile int ldrValue = 0;
WiFiManager wifiManager;
const char* ssid = "koelner Gastzugang";
const char* password = "deltaforce!";
// WebPortal
const char thingName[] = "NTP-Clock-RGBLED";
const char wifiInitialApPassword[] = "abcdefgh";
#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"
DNSServer dnsServer;
WebServer server(80);
char ntpServerParamValue[STRING_LEN] = "pool.ntp.org";
char brightnessParamValue[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'");
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
void printLocalTime()
{
@ -52,38 +69,96 @@ void printLocalTime()
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
void syncNTP() {
Serial.println(WiFi.getMode());
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.enableAP(false);
WiFi.enableSTA(true);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i == 10) {
break;
}
i++;
/**
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;
}
Serial.println(" CONNECTED");
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 += ntpServerParamValue;
s += "<li>Brightness value: \t";
s += atoi(brightnessParamValue);
//s += "<li>Float param value: ";
//s += atof(floatParamValue);
s += "</ul>";
s += "Go to <a href='config'>configure page</a> to change values.";
s += "</body></html>\n";
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
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));
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
server.send(200, "text/html", s);
}
void configSaved()
{
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(brightnessParamValue);
if (b < MINIMAL_BRIGHTNESS || b > 254)
{
valid = false;
}
//TODO check brightness
return valid;
}
void iotWebConf_Setup() {
iotWebConf.addParameter(&ntpServerParam);
iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&brightnessParam);
//iotWebConf.addParameter(&separator2);
//iotWebConf.addParameter(&floatParam);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setWifiConnectionCallback(&syncNTP);
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.");
}
void syncNTP() {
if (iotWebConf.getState() == 4) {
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
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));
}
}
void setup()
{
//interrupt for one second@80Mhz!
@ -92,41 +167,49 @@ void setup()
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
Serial.begin(9600);
Serial.begin(SERIAL_BAUD);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
FastLED.setBrightness(50);
FastLED.clear();
syncNTP();
//currentSec = 40; currentMin = 1; currentHour = 2;
iotWebConf_Setup();
//syncNTP();
}
void LDR() {
//brigthness measurement
ldrValue = analogRead(ldrPin);
void brightnessSet() {
int BRIGHTNESS = atoi(brightnessParamValue);
int x;
if (ldrValue == 0) {
FastLED.setBrightness( BRIGHTNESS );
}
else {
FastLED.setBrightness( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue * 10)) );
//Serial.println("Brightness: " + String( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue * 10))));
x = int(MINIMAL_BRIGHTNESS + ( (BRIGHTNESS - MINIMAL_BRIGHTNESS) / (ldrValue/32) + 1) );
FastLED.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) {
portENTER_CRITICAL(&timerMux);
interruptCounter--;
currentSec--;
portEXIT_CRITICAL(&timerMux);
//brigthness measurement
ldrValue = (ldrValue + analogRead(ldrPin)) / 2;
if (currentSec < 0) {
currentSec = 59;
currentMin--;
LDR();
brightnessSet();
if (currentMin < 0) {
currentMin = 59;
currentHour--;