add comms led
This commit is contained in:
parent
a615971be2
commit
f484586742
1 changed files with 47 additions and 18 deletions
65
node1.c
65
node1.c
|
|
@ -1,16 +1,44 @@
|
||||||
#include "bme280.h"
|
#include "bme280.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "pms5003.h"
|
#include "pms5003.h"
|
||||||
|
#include <hardware/gpio.h>
|
||||||
#include <hardware/i2c.h>
|
#include <hardware/i2c.h>
|
||||||
#include <hardware/uart.h>
|
#include <hardware/uart.h>
|
||||||
|
#include <pico/time.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define LOOP_INTERVAL_MS 5000
|
||||||
|
// #define LOOP_INTERVAL_MS 30000
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Balcony Weather Station Node 1
|
* Balcony Weather Station Node 1
|
||||||
* record sensor data and send it to home assistant every 5 minutes
|
* 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_config pms_config;
|
||||||
static pms5003_reading current_pms5003_reading;
|
static pms5003_reading current_pms5003_reading;
|
||||||
|
|
||||||
|
|
@ -23,16 +51,14 @@ static bme280_reading calculate_average_bme280_reading() {
|
||||||
float tempSum = 0;
|
float tempSum = 0;
|
||||||
float pressureSum = 0;
|
float pressureSum = 0;
|
||||||
float humiditySum = 0;
|
float humiditySum = 0;
|
||||||
for(int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
tempSum += bem280_readings[i].temperature;
|
tempSum += bem280_readings[i].temperature;
|
||||||
pressureSum += bem280_readings[i].pressure;
|
pressureSum += bem280_readings[i].pressure;
|
||||||
humiditySum += bem280_readings[i].humidity;
|
humiditySum += bem280_readings[i].humidity;
|
||||||
}
|
}
|
||||||
bme280_reading average_reading = {
|
bme280_reading average_reading = {.temperature = tempSum / 10,
|
||||||
.temperature = tempSum / 10,
|
.pressure = pressureSum / 10,
|
||||||
.pressure = pressureSum / 10,
|
.humidity = humiditySum / 10};
|
||||||
.humidity = humiditySum / 10
|
|
||||||
};
|
|
||||||
return average_reading;
|
return average_reading;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,8 +66,9 @@ static bme280_reading calculate_average_bme280_reading() {
|
||||||
* Callback function called every 30 seconds
|
* Callback function called every 30 seconds
|
||||||
*/
|
*/
|
||||||
static bool cb_30(__unused struct repeating_timer *t) {
|
static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
|
comms_led_blink();
|
||||||
printf("cb_30: %d\n", readings_index);
|
printf("cb_30: %d\n", readings_index);
|
||||||
if(readings_index >= 10) {
|
if (readings_index >= 10) {
|
||||||
printf("Preparing data to send\n");
|
printf("Preparing data to send\n");
|
||||||
readings_index = 0;
|
readings_index = 0;
|
||||||
|
|
||||||
|
|
@ -65,19 +92,19 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
current_bem280_reading = bme280_read(&bem_config);
|
current_bem280_reading = bme280_read(&bem_config);
|
||||||
bem280_readings[readings_index] = current_bem280_reading;
|
bem280_readings[readings_index] = current_bem280_reading;
|
||||||
|
|
||||||
if(readings_index == 2) {
|
if (readings_index == 2) {
|
||||||
printf("Warming up PMSS5003\n");
|
printf("Warming up PMSS5003\n");
|
||||||
pms5003_warmup(&pms_config);
|
pms5003_warmup(&pms_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(readings_index == 4) {
|
if (readings_index == 4) {
|
||||||
printf("Starting reads on PMSS5003\n");
|
printf("Starting reads on PMSS5003\n");
|
||||||
pms5003_start_reading(&pms_config);
|
pms5003_start_reading(&pms_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(readings_index == 6) {
|
if (readings_index == 6) {
|
||||||
printf("Finished reading PMSS5003\n");
|
printf("Finished reading PMSS5003\n");
|
||||||
current_pms5003_reading = pms5003_finish_reading(&pms_config);
|
current_pms5003_reading = pms5003_finish_reading(&pms_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
readings_index++;
|
readings_index++;
|
||||||
|
|
@ -87,6 +114,9 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
|
|
||||||
|
// Initialize communication LED
|
||||||
|
comms_led_init();
|
||||||
|
|
||||||
// Setup BME280
|
// Setup BME280
|
||||||
bme280_init(&bem_config, i2c1, 14, 15);
|
bme280_init(&bem_config, i2c1, 14, 15);
|
||||||
|
|
||||||
|
|
@ -94,8 +124,7 @@ int main() {
|
||||||
pms5003_init(&pms_config, uart1, 20, 21, 18, 19);
|
pms5003_init(&pms_config, uart1, 20, 21, 18, 19);
|
||||||
|
|
||||||
struct repeating_timer timer_30;
|
struct repeating_timer timer_30;
|
||||||
// TODO: change from 5 sec to 30sec
|
add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30);
|
||||||
add_repeating_timer_ms(30000, cb_30, NULL, &timer_30);
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
tight_loop_contents();
|
tight_loop_contents();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue