From f4845867423422d49ac3c5e90fd24a0e347a82d3 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Mon, 14 Apr 2025 17:10:50 +0200 Subject: [PATCH] add comms led --- node1.c | 65 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/node1.c b/node1.c index bb7bdad..11aaede 100644 --- a/node1.c +++ b/node1.c @@ -1,16 +1,44 @@ #include "bme280.h" #include "pico/stdlib.h" #include "pms5003.h" +#include #include #include +#include +#include #include #include +#define LOOP_INTERVAL_MS 5000 +// #define LOOP_INTERVAL_MS 30000 + /** * Balcony Weather Station Node 1 * record sensor data and send it to home assistant every 5 minutes */ +void comms_led_init() { + gpio_init(16); + gpio_set_dir(16, GPIO_OUT); +} + +alarm_id_t comms_led_alarm_id; +int16_t comms_led_blink_count; +int64_t comms_led_disable(alarm_id_t id, __unused void *user_data) { + gpio_put(16, false); + comms_led_alarm_id = 0; + return 0; +} +void comms_led_blink() { + printf("COMMS LED BLINK COUNT: %d\n", comms_led_blink_count); + comms_led_blink_count++; + gpio_put(16, true); + if (comms_led_alarm_id != 0) { + cancel_alarm(comms_led_alarm_id); + } + comms_led_alarm_id = add_alarm_in_ms(1000, comms_led_disable, NULL, false); +} + static pms5003_config pms_config; static pms5003_reading current_pms5003_reading; @@ -23,16 +51,14 @@ static bme280_reading calculate_average_bme280_reading() { float tempSum = 0; float pressureSum = 0; float humiditySum = 0; - for(int i = 0; i < 10; i++) { + 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 - }; + bme280_reading average_reading = {.temperature = tempSum / 10, + .pressure = pressureSum / 10, + .humidity = humiditySum / 10}; return average_reading; } @@ -40,8 +66,9 @@ static bme280_reading calculate_average_bme280_reading() { * Callback function called every 30 seconds */ static bool cb_30(__unused struct repeating_timer *t) { + comms_led_blink(); printf("cb_30: %d\n", readings_index); - if(readings_index >= 10) { + if (readings_index >= 10) { printf("Preparing data to send\n"); readings_index = 0; @@ -65,19 +92,19 @@ static bool cb_30(__unused struct repeating_timer *t) { 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 == 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); + if (readings_index == 6) { + printf("Finished reading PMSS5003\n"); + current_pms5003_reading = pms5003_finish_reading(&pms_config); } readings_index++; @@ -87,6 +114,9 @@ static bool cb_30(__unused struct repeating_timer *t) { int main() { stdio_init_all(); + // Initialize communication LED + comms_led_init(); + // Setup BME280 bme280_init(&bem_config, i2c1, 14, 15); @@ -94,8 +124,7 @@ int main() { pms5003_init(&pms_config, uart1, 20, 21, 18, 19); struct repeating_timer timer_30; - // TODO: change from 5 sec to 30sec - add_repeating_timer_ms(30000, cb_30, NULL, &timer_30); + add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30); while (true) { tight_loop_contents();