From af4b86b966a026dd51c6722791b015de24a91e72 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 21 Dec 2025 18:37:05 +0100 Subject: [PATCH] switch to sending temp/humidity every cb --- event_proxy/main.go | 7 ++- node1.c | 134 +++++++++++++++++--------------------------- 2 files changed, 58 insertions(+), 83 deletions(-) diff --git a/event_proxy/main.go b/event_proxy/main.go index 6aae477..53e1c33 100644 --- a/event_proxy/main.go +++ b/event_proxy/main.go @@ -1,6 +1,7 @@ package main import ( + "io" "log/slog" "net" "os" @@ -49,7 +50,11 @@ func handleConnection(conn *net.TCPConn) { //Read up to 1024 bytes n, err := conn.Read(buffer) if err != nil { - slog.Error("Error reading from connection", "error", err) + if err == io.EOF { + slog.Info("Connection closed by client", "remote_addr", conn.RemoteAddr().String()) + } else { + slog.Error("Error reading from connection", "error", err) + } return } if n == 0 { diff --git a/node1.c b/node1.c index 016be22..2d794f9 100644 --- a/node1.c +++ b/node1.c @@ -34,16 +34,14 @@ * Sensors: BME280, PMS5003 */ +// COMMS LED +absolute_time_t comms_led_off_time; +int16_t comms_led_blink_count = 0; +bool comms_led_state = false; void comms_led_init() { gpio_init(16); gpio_set_dir(16, GPIO_OUT); } - - -absolute_time_t comms_led_off_time; -int16_t comms_led_blink_count = 0; -bool comms_led_state = false; - void comms_led_blink() { printf("COMMS LED BLINK COUNT: %d\n", comms_led_blink_count); comms_led_blink_count++; @@ -51,7 +49,6 @@ void comms_led_blink() { comms_led_off_time = make_timeout_time_ms(1000); gpio_put(16, comms_led_state); } - void comms_led_update() { if (time_reached(comms_led_off_time)) { comms_led_state = false; @@ -70,32 +67,14 @@ static bool cb_24h(__unused struct repeating_timer *t) { static tcp_client_config tcp_config; static bool wifi_connected = false; -static pms5003_config pms_config; -static pms5003_reading current_pms5003_reading; +// static pms5003_config pms_config; +// static pms5003_reading current_pms5003_reading; +static int8_t readings_index = 0; static bme280_config bem_config; static bme280_reading current_bem280_reading; -static bme280_reading bem280_readings[10]; -static int8_t readings_index = 0; -static bme280_reading calculate_average_bme280_reading() { - float tempSum = 0; - float pressureSum = 0; - float humiditySum = 0; - for (int i = 0; i < 10; i++) { - tempSum += bem280_readings[i].temperature; - pressureSum += bem280_readings[i].pressure; - humiditySum += bem280_readings[i].humidity; - } - bme280_reading average_reading = {.temperature = tempSum / 10, - .pressure = pressureSum / 10, - .humidity = humiditySum / 10}; - return average_reading; -} - -/** - * Initialize WiFi connection - */ +// Initialize WiFi connection bool wifi_init(void) { if (cyw43_arch_init()) { printf("Failed to initialize CYW43\n"); @@ -121,70 +100,61 @@ bool wifi_init(void) { static bool cb_30(__unused struct repeating_timer *t) { comms_led_blink(); printf("cb_30: %d\n", readings_index); - if (readings_index >= 10) { - printf("Preparing data to send\n"); - readings_index = 0; + // if (readings_index >= 10) { + // printf("Preparing data to send\n"); + // readings_index = 0; // Calculate average BME280 reading - current_bem280_reading = calculate_average_bme280_reading(); - printf("Tempature: %.2f\n", current_bem280_reading.temperature); - printf("Pressure: %.2f\n", current_bem280_reading.pressure); - printf("Humidity: %.2f\n", current_bem280_reading.humidity); + // printf("Tempature: %.2f\n", current_bem280_reading.temperature); + // printf("Pressure: %.2f\n", current_bem280_reading.pressure); + // printf("Humidity: %.2f\n", current_bem280_reading.humidity); - printf("PM1: %.2f\n", current_pms5003_reading.pm1); - printf("PM2.5: %.2f\n", current_pms5003_reading.pm2_5); - printf("PM10: %.2f\n", current_pms5003_reading.pm10); + // printf("PM1: %.2f\n", current_pms5003_reading.pm1); + // printf("PM2.5: %.2f\n", current_pms5003_reading.pm2_5); + // printf("PM10: %.2f\n", current_pms5003_reading.pm10); // Send to backend server if WiFi is connected - if (wifi_connected) { - char msg[256]; - snprintf(msg, sizeof(msg), - "{\"temperature\":%.2f,\"pressure\":%.2f,\"humidity\":%.2f," - "\"pm1\":%.2f,\"pm2_5\":%.2f,\"pm10\":%.2f}\n", - current_bem280_reading.temperature, - current_bem280_reading.pressure, - current_bem280_reading.humidity, - current_pms5003_reading.pm1, - current_pms5003_reading.pm2_5, - current_pms5003_reading.pm10); - - printf("Sending data to backend server...\n"); - 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"); - } - } else { - printf("WiFi not connected, skipping send\n"); - } - - return true; - } // Read BME280 printf("Making BME280 Reading\n"); current_bem280_reading = bme280_read(&bem_config); - bem280_readings[readings_index] = current_bem280_reading; - if (readings_index == 2) { - printf("Warming up PMSS5003\n"); - pms5003_warmup(&pms_config); + + // if (readings_index == 2) { + // printf("Warming up PMSS5003\n"); + // pms5003_warmup(&pms_config); + // } + + // if (readings_index == 4) { + // printf("Starting reads on PMSS5003\n"); + // pms5003_start_reading(&pms_config); + // } + + // if (readings_index == 6) { + // printf("Finished reading PMSS5003\n"); + // current_pms5003_reading = pms5003_finish_reading(&pms_config); + // } + + // readings_index++; + if (wifi_connected) { + char msg[256]; + snprintf(msg, sizeof(msg), + "M001,%.2f,%.2f%.2f\n", + current_bem280_reading.temperature, + current_bem280_reading.pressure, + current_bem280_reading.humidity); + printf("Sending data to backend server...\n"); + 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"); + } + } else { + printf("WiFi not connected, skipping send\n"); } - if (readings_index == 4) { - printf("Starting reads on PMSS5003\n"); - pms5003_start_reading(&pms_config); - } - - if (readings_index == 6) { - printf("Finished reading PMSS5003\n"); - current_pms5003_reading = pms5003_finish_reading(&pms_config); - } - - readings_index++; return true; }; @@ -218,7 +188,7 @@ int main() { bme280_init(&bem_config, i2c1, 14, 15); // Setup PMS5003 - pms5003_init(&pms_config, uart1, 20, 21, 18, 19); + // pms5003_init(&pms_config, uart1, 20, 21, 18, 19); struct repeating_timer timer_30; add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30);