add comms led

This commit is contained in:
Travis Shears 2025-04-14 17:10:50 +02:00
parent a615971be2
commit f484586742

41
node1.c
View file

@ -1,16 +1,44 @@
#include "bme280.h"
#include "pico/stdlib.h"
#include "pms5003.h"
#include <hardware/gpio.h>
#include <hardware/i2c.h>
#include <hardware/uart.h>
#include <pico/time.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#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;
@ -28,11 +56,9 @@ static bme280_reading calculate_average_bme280_reading() {
pressureSum += bem280_readings[i].pressure;
humiditySum += bem280_readings[i].humidity;
}
bme280_reading average_reading = {
.temperature = tempSum / 10,
bme280_reading average_reading = {.temperature = tempSum / 10,
.pressure = pressureSum / 10,
.humidity = humiditySum / 10
};
.humidity = humiditySum / 10};
return average_reading;
}
@ -40,6 +66,7 @@ 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) {
printf("Preparing data to send\n");
@ -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();