143 lines
3.7 KiB
C
143 lines
3.7 KiB
C
#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 <pico/types.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);
|
|
}
|
|
|
|
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++;
|
|
if(!comms_led_state) {
|
|
gpio_put(16, true);
|
|
comms_led_state = true;
|
|
}
|
|
comms_led_off_time = make_timeout_time_ms(1000);
|
|
}
|
|
|
|
void comms_led_update() {
|
|
if (comms_led_state && time_reached(comms_led_off_time)) {
|
|
gpio_put(16, false);
|
|
comms_led_state = false;
|
|
}
|
|
}
|
|
|
|
static pms5003_config pms_config;
|
|
static pms5003_reading current_pms5003_reading;
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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");
|
|
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("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("Sending data to home assistant...\n");
|
|
// TODO: Send data to home assistant
|
|
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 == 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;
|
|
};
|
|
|
|
int main() {
|
|
stdio_init_all();
|
|
|
|
// Initialize communication LED
|
|
comms_led_init();
|
|
|
|
// Setup BME280
|
|
bme280_init(&bem_config, i2c1, 14, 15);
|
|
|
|
// Setup PMS5003
|
|
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);
|
|
absolute_time_t blink_time = make_timeout_time_ms(5000);
|
|
comms_led_blink();
|
|
while (true) {
|
|
if(time_reached(blink_time)) {
|
|
comms_led_blink();
|
|
blink_time = make_timeout_time_ms(5000);
|
|
}
|
|
comms_led_update();
|
|
tight_loop_contents();
|
|
}
|
|
}
|