diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino
index b3895a9..f7a761f 100644
--- a/esp32-timed-switch.ino
+++ b/esp32-timed-switch.ino
@@ -7,13 +7,15 @@
#include "scheduler.h"
#include "scheduler_prefs.h"
+// #include "wifi_prefs.h"
+
// --------------------------------------------
// Init const and global objects
// --------------------------------------------
-const char* html_app="
ESP32 timed SwitchScheduler
";
+const char* html_app="ESP32 timed SwitchScheduler
";
// PREFERENCES
@@ -22,8 +24,6 @@ Preferences preferences;
#define RO_MODE true
// WIFI
-const char* ssid = "/tmp/lab";
-const char* password = "bitte2cucung";
// const char* ssid = "Wokwi-GUEST";
// const char* password = "";
const uint8_t wifi_loop_max = 10;
@@ -239,19 +239,45 @@ const char* getPreferenceName( int plug_id){
}
}
-void saveSchedule( int plug_id, unsigned int data[24]){
+void saveSchedule( int plug_id, uint16_t data[24]){
const char* preference_name = getPreferenceName( plug_id );
- Serial.print("saving to ");
- Serial.println(preference_name);
- preferences.putBytes(preference_name, data, sizeof(data));
+ preferences.putBytes(preference_name, data, 48);
+ Serial.print("saving size:");
+ Serial.print(sizeof(data));
+ Serial.print(" to ");
+ Serial.println(preference_name);
+
+
+ uint16_t buf[24];
+ preferences.getBytes(preference_name, buf, 48);
+ for ( int iterator=0; iterator < 24; iterator++){
+
+ Serial.print(iterator);
+ Serial.print(" : ");
+ Serial.print(data[iterator]);
+ Serial.print(" / ");
+ Serial.println(buf[iterator]);
+ }
+ delay(2000);
}
void sendSchedule(WiFiClient &client, int plug_id) {
- int buf[24];
+ uint16_t buf[24];
const char* preference_name = getPreferenceName( plug_id );
- preferences.getBytes(preference_name, buf, preferences.getBytesLength(preference_name));
- client.write((const uint8_t*)buf, 24 * sizeof(int));
+ Serial.print("sending ");
+ Serial.println(preference_name);
+
+ preferences.getBytes(preference_name, buf, 48);
+
+ for ( int iterator=0; iterator < 24; iterator++){
+ Serial.print(iterator);
+ Serial.print(" : ");
+ Serial.println(buf[iterator]);
+ }
+ Serial.println("---------------------------");
+
+ client.write((const uint8_t*)buf, 24 * sizeof(uint16_t));
}
// --------------------------------------------
@@ -266,27 +292,38 @@ void setup(){
Serial.println("Starting.");
pinMode(PIN_1, OUTPUT);
pinMode(PIN_2, OUTPUT);
+ pinMode(PIN_3, OUTPUT);
+ pinMode(PIN_4, OUTPUT);
+ pinMode(PIN_5, OUTPUT);
+ pinMode(PIN_6, OUTPUT);
+ pinMode(PIN_7, OUTPUT);
+ pinMode(PIN_8, OUTPUT);
+
+
// PREFERENCES
Serial.println("Setup::PREFERENCES");
preferences.begin(PREF_NAMESPACE, RW_MODE);
- preferences.clear();
+ // preferences.clear();
+
+
bool pref_init = preferences.isKey("test");
- if (pref_init == false) {
+ if (true || pref_init == false) {
Serial.println("preferences not exist");
preferences.putBool("test", true);
- /*
saveSchedule(1,scheduler_default_deactivate);
+
saveSchedule(2,scheduler_default_deactivate);
saveSchedule(3,scheduler_default_deactivate);
saveSchedule(4,scheduler_default_deactivate);
+ /*
saveSchedule(5,scheduler_default_deactivate);
saveSchedule(6,scheduler_default_deactivate);
saveSchedule(7,scheduler_default_deactivate);
saveSchedule(8,scheduler_default_deactivate);
*/
}
- /*
+
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_3_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_3_SCHEDULE));
@@ -295,7 +332,7 @@ void setup(){
preferences.getBytes(RELAY_6_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_6_SCHEDULE));
preferences.getBytes(RELAY_7_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_7_SCHEDULE));
preferences.getBytes(RELAY_8_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_8_SCHEDULE));
- */
+
preferences.end();
Serial.println("Setup::PREFERENCES::end");
@@ -403,25 +440,36 @@ void loop(){
// Force a new line
client.readStringUntil('\n');
// Read the next line which should have the POST body/content
- String preBody ;
- while ( preBody = client.readStringUntil('\n') ){
- if( preBody.isEmpty() ){
+ String header ;
+ while ( header = client.readStringUntil('\n') ){
+ Serial.print("header: '");
+ Serial.print(header);
+ Serial.print(" / ");
+ Serial.print(header.length());
+ Serial.println("'");
+ if( header.length() < 2 ){
+ Serial.println("parsed headers");
break;
}
}
Serial.println("Done reading. Going for binary.");
- char post_body[24];
- client.readBytes(post_body, 24);
+ char post_body[48];
+ client.readBytes(post_body, 48);
Serial.print("Read post_body: ");
Serial.println(post_body);
// Update the schedule for the specified plug
if (plug_id >= 0 && plug_id < 8) {
- unsigned int byte_value[24];
+ uint16_t byte_value[24];
for (int i=0; i < 24; i++) {
- byte_value[i] = (int) post_body[i];
+ byte_value[i] = (char) post_body[i*2] * 256 + post_body[(i*2)+1];
+ Serial.print("Byte ");
+ Serial.print(i);
+ Serial.print(" : ");
+ Serial.print(byte_value[i]);
+ Serial.println("");
}
saveSchedule(plug_id, byte_value );
diff --git a/html/index.html b/html/index.html
index fcf4e0d..05c1352 100644
--- a/html/index.html
+++ b/html/index.html
@@ -28,7 +28,7 @@ class Schedule {
// Load from binary data
var view = new DataView(buffer);
for (var i = 0; i < 24; i++) {
- this.schedule[i] = view.getUint16(i * 2, true);
+ this.schedule[i] = view.getUint16(i * 2);
}
};
@@ -102,8 +102,7 @@ class Schedule {
var buffer = new ArrayBuffer(24 * 2);
var view = new DataView(buffer);
for (var i = 0; i < 24; i++) {
- view.setUint8( i*2 , (this.schedule[i] & 65280) >> 8, true);
- view.setUint8( i*2 + 1, this.schedule[i] & 255 , true);
+ view.setUint16( i*2, this.schedule[i]);
}
return buffer;
};
diff --git a/html/mockup.py b/html/mockup.py
index ef18aa4..84cb194 100755
--- a/html/mockup.py
+++ b/html/mockup.py
@@ -37,7 +37,7 @@ def binary_stream_to_ints(binary_data):
integers = []
# Every 32-bit integer is represented by 2 bytes
for i in range(0, len(binary_data), 2):
- integers.append(int.from_bytes(binary_data[i:i + 2]) & 0b111111111111) # Extract 32-bit value
+ integers.append(int.from_bytes(binary_data[i:i + 2], byteorder='big') & 0b111111111111) # Extract 32-bit value
return integers
@@ -53,7 +53,7 @@ def get_schedule(plug_id):
# Ensure integer is in 12-bit range
integer &= 0b111111111111
# Convert to bytes and append
- yield integer.to_bytes(2) # 2 bytes per integer
+ yield integer.to_bytes(2, byteorder='big') # 2 bytes per integer
# Return as a binary stream
return Response(ints_to_binary_stream(schedule), status=200, content_type='application/octet-stream')
diff --git a/scheduler.h b/scheduler.h
index 90c836b..90f5cc5 100644
--- a/scheduler.h
+++ b/scheduler.h
@@ -1,3 +1,4 @@
+#include
#define A00 0b000000000000
#define A60 0b111111111111
@@ -13,7 +14,7 @@
// one hour is represented by 1 unsigned int
// each bit of the hour represent one BI_MINUTE.
// the output of the scheduler is ON if one bit is set, OFF otherwise.
-unsigned int scheduler_1_default[24] = {
+uint16_t scheduler_1_default[24] = {
EVERY_2_INV,
EVERY_2,
EVERY_2_INV,
@@ -39,7 +40,7 @@ unsigned int scheduler_1_default[24] = {
EVERY_2_INV,
EVERY_2,
};
-unsigned int scheduler_default_deactivate[24] = {
+uint16_t scheduler_default_deactivate[24] = {
A00,
A00,
A00,
@@ -65,7 +66,7 @@ unsigned int scheduler_default_deactivate[24] = {
A00,
A00,
};
-unsigned int scheduler_2_default[24] = {
+uint16_t scheduler_2_default[24] = {
EVERY_2,
EVERY_2_INV,
EVERY_2,