151 lines
4.2 KiB
C
151 lines
4.2 KiB
C
#include "bme280.h"
|
|
#include "pico/stdlib.h"
|
|
#include "pms5003.h"
|
|
#include "mqtt_client.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>
|
|
#include <string.h>
|
|
|
|
// 5 sec loop is for testing
|
|
// #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
|
|
* Sensors: BME280, PMS5003
|
|
*/
|
|
|
|
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 mqtt_client_config mqtt_config;
|
|
|
|
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);
|
|
// char msg[100];
|
|
char msg[200];
|
|
snprintf(msg, sizeof(msg), "{\"temp\": %.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 home assistant...\n");
|
|
mqtt_client_pub_message(&mqtt_config, msg);
|
|
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();
|
|
|
|
mqtt_client_init(&mqtt_config, "homeassistant/sensor/bws/node1/state", "bws-node1");
|
|
|
|
// 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);
|
|
while (true) {
|
|
comms_led_update();
|
|
sleep_us(100);
|
|
tight_loop_contents();
|
|
mqtt_client_do_network_stuff(&mqtt_config);
|
|
}
|
|
}
|