feat: preferences should work
This commit is contained in:
parent
fd087b4a80
commit
63ee301d2d
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode
|
@ -10,14 +10,14 @@
|
|||||||
|
|
||||||
// Init const and global objects
|
// Init const and global objects
|
||||||
|
|
||||||
// Preferences
|
// PREFRENCES
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
#define RW_MODE false
|
#define RW_MODE false
|
||||||
#define RO_MODE true
|
#define RO_MODE true
|
||||||
|
|
||||||
// Wifi
|
// WIFI
|
||||||
const char* ssid = "Wokwi-GUEST";
|
const char* ssid = "/tmp/lab";
|
||||||
const char* password = "";
|
const char* password = "bitte2cucung";
|
||||||
const uint8_t wifi_loop_max = 10;
|
const uint8_t wifi_loop_max = 10;
|
||||||
|
|
||||||
// NTP
|
// NTP
|
||||||
@ -27,59 +27,73 @@ const char* ntpServer2 = "time.nist.gov";
|
|||||||
const long gmtOffset_sec = 2 * 3600; // GMT + 2
|
const long gmtOffset_sec = 2 * 3600; // GMT + 2
|
||||||
const int daylightOffset_sec = 0;
|
const int daylightOffset_sec = 0;
|
||||||
|
|
||||||
// Timer
|
// TIMER
|
||||||
hw_timer_t*timer_main = NULL;
|
hw_timer_t*timer_main = NULL;
|
||||||
uint32_t timer_interval = 3000000;
|
uint32_t timer_interval = 3000000;
|
||||||
uint16_t timer_interval_in_secs = timer_interval / 1000000;
|
uint16_t timer_interval_in_secs = timer_interval / 1000000;
|
||||||
;
|
;
|
||||||
|
|
||||||
// Scheduler
|
// SCHEDULER
|
||||||
bool scheduler_flag = true;
|
bool brun_scheduler = true;
|
||||||
uint8_t current_hour = HOUR_DEFAULT;
|
uint8_t current_hour = HOUR_DEFAULT;
|
||||||
uint8_t current_minute = MINUTE_DEFAULT;
|
uint8_t current_minute = MINUTE_DEFAULT;
|
||||||
uint8_t current_second = 0;
|
uint8_t current_second = 0;
|
||||||
uint32_t current_ts = 0;
|
uint32_t current_ts = 0;
|
||||||
uint32_t next_event_ts = 0;
|
uint32_t next_event_ts = 0;
|
||||||
uint16_t scheduler_1[24] = {A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60};
|
int scheduler_1[24] ; // = {A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60};
|
||||||
uint16_t scheduler_2[24] = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00};
|
int scheduler_2[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00};
|
||||||
uint16_t* scheduler_list[] = {
|
int* scheduler_list[] = {
|
||||||
scheduler_1,
|
scheduler_1,
|
||||||
scheduler_2
|
scheduler_2
|
||||||
};
|
};
|
||||||
|
|
||||||
// Pins and Hardware
|
// PINS
|
||||||
unsigned int PIN_1 = 2;
|
unsigned int PIN_1 = 12;
|
||||||
unsigned int PIN_2 = 4;
|
unsigned int PIN_2 = 14;
|
||||||
|
unsigned int PIN_3 = 27;
|
||||||
|
unsigned int PIN_4 = 26;
|
||||||
|
unsigned int PIN_5 = 20;
|
||||||
|
unsigned int PIN_6 = 33;
|
||||||
|
unsigned int PIN_7 = 32;
|
||||||
|
unsigned int PIN_8 = 35;
|
||||||
unsigned int pin_list[2] = {
|
unsigned int pin_list[2] = {
|
||||||
PIN_1,
|
PIN_1,
|
||||||
PIN_2
|
PIN_2
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// HTTP
|
||||||
|
WiFiClient http_client;
|
||||||
|
WiFiServer http_server(80);
|
||||||
|
String request;
|
||||||
|
|
||||||
|
|
||||||
// Local functions
|
// Local functions
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Time helper
|
// Time helper
|
||||||
uint32_t get_timestamp( uint8_t hour, uint8_t minute, uint8_t second){
|
uint32_t get_timestamp( uint8_t hour, uint8_t minute, uint8_t second){
|
||||||
return ( 3600 * hour + 60 * minute + second) % 86400;
|
return ( 3600 * hour + 60 * minute + second) % 86400;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Naive Timestamp based event igniter
|
bool event_keep_waiting( uint32_t current_ts, uint32_t event_ts ){
|
||||||
bool scheduler_event_fire( uint32_t current_ts, uint32_t event_ts ){
|
|
||||||
|
|
||||||
// basic case = ts:11h41m11s versus ev:11h44m0s
|
// basic case
|
||||||
// ts < ev means wait
|
// ts: 11h 41m 11s
|
||||||
|
// ev: 11h 44m 0s
|
||||||
|
// ts < ev => TRUE
|
||||||
if ( current_ts < event_ts ){
|
if ( current_ts < event_ts ){
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
// if time looped at midnight = ts:23h57m21s versus ev:0h1m0s
|
// if time looped at midnight
|
||||||
// in case of high against very low values ts > ev means wait
|
// ts: 23h 57m 21s
|
||||||
|
// ev: 0h 1m 0s
|
||||||
|
// ts > ev => TRUE (for high to very low values )
|
||||||
if( (current_ts - event_ts) > 600 && event_ts < 300 ) {
|
if( (current_ts - event_ts) > 600 && event_ts < 300 ) {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
// ts:11h44m11s versus ev:11h44m0s
|
// ts: 11h 44m 11s
|
||||||
// ts > ev means fire event
|
// ev: 11h 44m 0s
|
||||||
return true;
|
// ts > ev => FALSE
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -93,11 +107,12 @@ void timeavailable(struct timeval *t)
|
|||||||
|
|
||||||
// TIMER Callback
|
// TIMER Callback
|
||||||
void IRAM_ATTR onTimer(){
|
void IRAM_ATTR onTimer(){
|
||||||
scheduler_flag = true;
|
brun_scheduler = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// updater local time variables for NTP and no FTP cases
|
|
||||||
void scheduler_time_update(){
|
void run_scheduler(){
|
||||||
|
// Serial.println("onTimer::run");
|
||||||
// Get the current time via NTP, or downgrade
|
// Get the current time via NTP, or downgrade
|
||||||
if ( sntp_initialized ){
|
if ( sntp_initialized ){
|
||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
@ -124,19 +139,13 @@ void scheduler_time_update(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Scheduler's main loop
|
|
||||||
void scheduler_action(){
|
|
||||||
|
|
||||||
// Serial.println("onTimer::run");
|
|
||||||
scheduler_time_update();
|
|
||||||
|
|
||||||
// If not expected to run exit
|
// If not expected to run exit
|
||||||
current_ts = get_timestamp( current_hour, current_minute, current_second );
|
current_ts = get_timestamp( current_hour, current_minute, current_second );
|
||||||
// Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts);
|
Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts);
|
||||||
if( ! scheduler_event_fire( current_ts, next_event_ts ) ){
|
if( event_keep_waiting( current_ts, next_event_ts ) ){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,11 +156,11 @@ void scheduler_action(){
|
|||||||
// Run the relays reconfiguration
|
// Run the relays reconfiguration
|
||||||
for ( int i = 0; i < 2; i++ ) {
|
for ( int i = 0; i < 2; i++ ) {
|
||||||
// Get a pointer to the current array
|
// Get a pointer to the current array
|
||||||
uint16_t* scheduler = scheduler_list[i];
|
int* scheduler = scheduler_list[i];
|
||||||
unsigned int pin = pin_list[i];
|
unsigned int pin = pin_list[i];
|
||||||
// Set the expected status
|
// Set the expected status
|
||||||
uint8_t minutes_by_5 = (current_minute / 5);
|
uint8_t minutes_by_5 = (current_minute / 5);
|
||||||
uint16_t hourly_12_values = scheduler[current_hour];
|
int hourly_12_values = scheduler[current_hour];
|
||||||
// Serial.printf("Check hour(%d) >> minutes(%d x 5)\n", hourly_12_values, minutes_by_5);
|
// Serial.printf("Check hour(%d) >> minutes(%d x 5)\n", hourly_12_values, minutes_by_5);
|
||||||
|
|
||||||
char buffer[40];
|
char buffer[40];
|
||||||
@ -170,10 +179,6 @@ void scheduler_action(){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// Arduino functions
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
|
|
||||||
// HARDWARE
|
// HARDWARE
|
||||||
@ -182,36 +187,36 @@ void setup(){
|
|||||||
Serial.println("Starting.");
|
Serial.println("Starting.");
|
||||||
pinMode(PIN_1, OUTPUT);
|
pinMode(PIN_1, OUTPUT);
|
||||||
pinMode(PIN_2, OUTPUT);
|
pinMode(PIN_2, OUTPUT);
|
||||||
|
|
||||||
// PREFERENCES
|
// PREFERENCES
|
||||||
Serial.println("Setup::PREFERENCES");
|
Serial.println("Setup::PREFERENCES");
|
||||||
preferences.begin(PREF_NAMESPACE, RW_MODE);
|
preferences.begin(PREF_NAMESPACE, RW_MODE);
|
||||||
bool pref_init = preferences.isKey("init");
|
preferences.clear();
|
||||||
|
|
||||||
|
bool pref_init = preferences.isKey("test");
|
||||||
if (pref_init == false) {
|
if (pref_init == false) {
|
||||||
preferences.putBool ("init", true);
|
Serial.println("preferences not exist");
|
||||||
|
preferences.putBool("test", true);
|
||||||
preferences.putBytes(RELAY_1_SCHEDULE, scheduler_1_default, sizeof(scheduler_1_default));
|
preferences.putBytes(RELAY_1_SCHEDULE, scheduler_1_default, sizeof(scheduler_1_default));
|
||||||
preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default));
|
preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default));
|
||||||
/// ... more to come
|
/// ... more to come
|
||||||
}
|
}
|
||||||
// huh this causes a crash
|
bool test = preferences.getBool("test");
|
||||||
preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE));
|
preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE));
|
||||||
preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE));
|
preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE));
|
||||||
preferences.end();
|
preferences.end();
|
||||||
|
Serial.println("Setup::PREFERENCES::end");
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
Serial.println("Setup::WIFI");
|
Serial.println("Setup::WIFI");
|
||||||
Serial.printf("Connecting to %s ", ssid);
|
Serial.printf("Connecting to %s ", ssid);
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
uint8_t wifi_loop_count = 0;
|
uint8_t wifi_loop_count = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && wifi_loop_count < wifi_loop_max ) {
|
while (WiFi.status() != WL_CONNECTED or wifi_loop_count < wifi_loop_max ) {
|
||||||
delay(500);
|
delay(1000);
|
||||||
wifi_loop_count++;
|
wifi_loop_count++;
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
Serial.print("Connect to IP Address: ");
|
|
||||||
Serial.print("http://");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
// TIMER
|
// TIMER
|
||||||
Serial.println("Setup::Timer");
|
Serial.println("Setup::Timer");
|
||||||
timer_main = timerBegin(0, 80, true);
|
timer_main = timerBegin(0, 80, true);
|
||||||
@ -232,15 +237,63 @@ void setup(){
|
|||||||
|
|
||||||
// HTTP
|
// HTTP
|
||||||
Serial.println("Setup::HTTP");
|
Serial.println("Setup::HTTP");
|
||||||
|
http_server.begin();
|
||||||
|
Serial.print("Connect to IP Address: ");
|
||||||
|
Serial.print("http://");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
// Todo
|
// Todo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void html() {
|
||||||
|
http_client.println("HTTP/1.1 200 OK");
|
||||||
|
http_client.println("Content-Type: text/html");
|
||||||
|
http_client.println("Connection: close");
|
||||||
|
http_client.println();
|
||||||
|
|
||||||
|
http_client.println("<!DOCTYPE HTML>");
|
||||||
|
http_client.println("<html>");
|
||||||
|
http_client.println("</body>");
|
||||||
|
http_client.println("</html>");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
|
|
||||||
if( scheduler_flag == true ){
|
if( brun_scheduler == true ){
|
||||||
scheduler_flag = false;
|
brun_scheduler = false;
|
||||||
scheduler_action();
|
run_scheduler();
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(1000);
|
|
||||||
|
http_client = http_server.available();
|
||||||
|
if (!http_client) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (http_client.connected()) {
|
||||||
|
if (http_client.available()) {
|
||||||
|
char c = http_client.read();
|
||||||
|
|
||||||
|
request += c;
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
String postres = http_client.readString();
|
||||||
|
int indp = postres.indexOf("progo=")+6;
|
||||||
|
Serial.println(postres);
|
||||||
|
Serial.println(request);
|
||||||
|
|
||||||
|
|
||||||
|
html();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(1);
|
||||||
|
request = "";
|
||||||
|
http_client.stop();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user