From 9f4a163b532bf13e9723d8713deddf5f0569ac89 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 18 Jan 2026 07:49:01 +0100 Subject: [PATCH] do work including wifi stuff in main loop instead of callback "); "); ", readings_index); "); "); "); "); --- node1.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/node1.c b/node1.c index cef9f37..47b2412 100644 --- a/node1.c +++ b/node1.c @@ -16,7 +16,7 @@ // 5 sec loop is for testing // #define LOOP_INTERVAL_MS 5000 -#define LOOP_INTERVAL_MS 10000 +#define LOOP_INTERVAL_MS 20000 // #define LOOP_INTERVAL_MS 30000 /** @@ -87,11 +87,22 @@ bool wifi_init(void) { return true; } +// Send a message over TCP to homeassistant event proxy +static void send_msg(char *msg) { + bool success = tcp_client_send_message(&tcp_config, msg); + if (success) { + printf("✓ Data sent successfully\n"); + comms_led_blink(); + } else { + printf("✗ Failed to send data\n"); + } +} + /** * Callback function called every 30 seconds */ -static bool cb_30(__unused struct repeating_timer *t) { - comms_led_blink(); +char msg_to_send[256]; +static bool do_work(void) { printf("cb_30: %d\n", readings_index); // Read BME280 @@ -122,7 +133,7 @@ static bool cb_30(__unused struct repeating_timer *t) { current_bem280_reading.temperature, current_bem280_reading.pressure, current_bem280_reading.humidity); printf("Sending temperature, pressure, and humidity to backend server...\n"); - // printf("MSG: %s", msg_to_send); + send_msg(msg_to_send); // send PM readings if (readings_index == 6) { @@ -130,13 +141,16 @@ static bool cb_30(__unused struct repeating_timer *t) { current_pms5003_reading.pm1, current_pms5003_reading.pm2_5, current_pms5003_reading.pm10); printf("Sending particulate matter readings to backend server...\n"); - // printf("MSG: %s", msg); + send_msg(msg_to_send); } return true; }; bool cb_30_trigger = true; +static bool cb_30(__unused struct repeating_timer *t) { + cb_30_trigger = true; +} int main() { stdio_init_all(); @@ -172,16 +186,9 @@ int main() { watchdog_update(); tight_loop_contents(); // Iterate over msgs_to_send and send them if not empty - if (msg_to_send[0] != '\0') { - bool success = tcp_client_send_message(&tcp_config, msg_to_send); - if (success) { - printf("✓ Data sent successfully\n"); - comms_led_blink(); - // Clear the message after sending - msg_to_send[0] = '\0'; - } else { - printf("✗ Failed to send data\n"); - } + if (cb_30_trigger) { + do_work(); + cb_30_trigger = false; } } }