esp8266-remote-timer

ESP8266 based timer w/ remote control and NTP sync
Index Commits Files Refs README LICENSE
commit e6f37eaa1f6a5966be82ae703f0d8e36b97a584d
parent dd73fee1375fc49cdfb1e9809f4fb84eee9f880f
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Sat, 29 Mar 2025 20:20:11 -0300

enumerate query types to avoid comparing strings

thus improving performance, at least in the uC

Diffstat:
Mdata/main.js | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/main.cpp | 41++++++++++++++++++++++++++++++-----------
2 files changed, 88 insertions(+), 24 deletions(-)
diff --git a/data/main.js b/data/main.js
@@ -14,6 +14,13 @@ const time_formatter = new Intl.DateTimeFormat('en-GB', {
     hour12: false
 });
 
+const query_types = {
+    "MAIN_OUTPUT_STATUS": 0,
+    "MAIN_OUTPUT_TOGGLE": 1,
+    "TIMER_TOGGLE":       2,
+    "TIMER_SET_VALUES":   3
+}
+
 function rssi_to_percentage(rssi) {
     var quality = 0; 
 
@@ -27,11 +34,28 @@ function rssi_to_percentage(rssi) {
     return quality; 
 }
 
-function socket_onmessage_handler(event) {
-    // console.log(event.data);
-    received_data = JSON.parse(event.data);
+function update_checkbox() {
+    main_checkbox.setAttribute("checked", received_data["main-output-enabled"]);
+    timer_checkbox.setAttribute("checked", received_data["timer-enabled"]);
+}
+
+function update_time() {
+    system_time = received_data["system-time"];
+    system_time_elem.innerHTML = time_formatter.format(new Date(system_time*1000))
+        .replaceAll("/", "-")
+        .replaceAll(",", "") + " GMT-3";
+}
 
-    main_output_checkbox.checked = received_data["main-output-enabled"] == "1" ? true : false;
+function update_timer() {
+    console.log(received_data);
+    from_time.value = received_data["from-time"];
+    to_time.value = received_data["to-time"];
+}
+
+function update_all() {
+    update_checkbox();
+    update_time();
+    update_timer();
 
     system_local_domain.href = "http://" + received_data["system-local-domain"] + ".local";
     system_local_domain.innerHTML = "http://" + received_data["system-local-domain"] + ".local";
@@ -39,11 +63,6 @@ function socket_onmessage_handler(event) {
     system_ip_addr.href = received_data["system-ip-addr"];
     system_ip_addr.innerHTML = received_data["system-ip-addr"];
 
-    system_time = received_data["system-time"];
-    system_time_elem.innerHTML = time_formatter.format(new Date(system_time*1000))
-        .replaceAll("/", "-")
-        .replaceAll(",", "") + " GMT-3";
-
     last_ntp_sync = received_data["last-ntp-sync"];
     last_ntp_sync_elem.innerHTML = time_formatter.format(new Date(last_ntp_sync*1000))
         .replaceAll("/", "-")
@@ -54,23 +73,47 @@ function socket_onmessage_handler(event) {
     wifi_rssi.innerHTML = rssi + "dBm (" + rssi_to_percentage(rssi) + "%)";
 }
 
+function socket_onmessage_handler(event) {
+    received_data = JSON.parse(event.data);
+
+    switch(received_data["type"]) {
+        case "all":
+            update_all();
+            break;
+        case "time":
+            update_time();
+            break;
+        case "cb":
+            update_checkbox();
+            break;
+        case "timer":
+            update_timer();
+            break;
+        default:
+            console.log("[SOCKET] received_data: type not known, updating all...");
+            update_all();
+    }
+}
+
 function socket_onopen_handler(event) {
     console.log("[SOCKET] Connection established")
-    socket.send("main-output-status");
+    socket.send(query_types["MAIN_OUTPUT_STATUS"]);
 }
 
 function handle_click(cb) {
     if(cb == 'main-toggle') {
-        socket.send("main-output-toggle");
+        socket.send(query_types["MAIN_OUTPUT_TOGGLE"]);
     } else if(cb == 'timer-toggle') {
-        socket.send("timer-toggle");
+        socket.send(query_types["TIMER_TOGGLE"]);
     } else {
         console.log("handle_click: wrong target");
     }
 }
 
 function update_system_time() {
-    socket.send("main-output-status");
+    socket.send(query_types["MAIN_OUTPUT_STATUS"]);
+}
+
 function timer_set_time() {
     var query_json = new Object();
 
@@ -82,6 +125,8 @@ function timer_set_time() {
     query_json.to.hour = to_time.value.slice(0, 2);
     query_json.to.minute = to_time.value.slice(3, 5);
     console.log(query_json);
+
+    socket.send(query_types["TIMER_SET_VALUES"] + JSON.stringify(query_json));
 }
 
 function query_data() {
diff --git a/src/main.cpp b/src/main.cpp
@@ -187,6 +187,23 @@ void update_all_socket_clients() {
     web_socket.broadcastTXT(data_as_json);
 }
 
+void update_all_clients_checkbox() {
+    JSONVar output_data;
+    output_data["type"] = "cb";
+    output_data["main-output-enabled"] = String(main_output_enabled);
+    output_data["timer-enabled"] = String(timer_enabled);
+    String output_data_as_json = JSON.stringify(output_data);
+    web_socket.broadcastTXT(output_data_as_json);
+}
+
+
+typedef enum {
+    MAIN_OUTPUT_STATUS = 0,
+    MAIN_OUTPUT_TOGGLE,
+    TIMER_TOGGLE,
+    TIMER_SET_VALUES
+} query_t;
+
 void webp_socket_event(uint8_t num, WStype_t type, uint8_t *payload, size_t len) {
     switch(type) {
     case WStype_DISCONNECTED:
@@ -203,22 +220,24 @@ void webp_socket_event(uint8_t num, WStype_t type, uint8_t *payload, size_t len)
         }
         break;
     case WStype_TEXT: {
-            // Serial.printf("[SOCKET](%u) got text: %s\n", num, payload);
+            int query_type = *payload - '0';
 
-            if(!strcmp((char *)payload, "main-output-toggle")) {
+            switch(query_type) {
+            case MAIN_OUTPUT_TOGGLE:
                 main_output_toggle();
-                update_all_socket_clients();
-            }
-
-            if(!strcmp((char *)payload, "main-output-status")) {
+                update_all_clients_checkbox();
+                break;
+            case TIMER_TOGGLE:
+                timer_toggle();
+                update_all_clients_checkbox();
+                break;
+            case TIMER_SET_VALUES:
+            case MAIN_OUTPUT_STATUS:
+            default:
                 update_data();
                 String data_as_json = JSON.stringify(data);
                 web_socket.sendTXT(num, data_as_json);
-            }
-
-            if(!strcmp((char *)payload, "system-time")) {
-                String system_time_data_as_json = JSON.stringify(system_time_data);
-                web_socket.sendTXT(num, system_time_data_as_json);
+                break;
             }
         }
         break;