start working with timers
This commit is contained in:
parent
ecb1e00198
commit
2548813231
3 changed files with 143 additions and 46 deletions
114
node1.c
114
node1.c
|
|
@ -1,39 +1,103 @@
|
|||
#include <hardware/uart.h>
|
||||
#include <stdio.h>
|
||||
#include <hardware/i2c.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "bme280.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "pms5003.h"
|
||||
#include <hardware/i2c.h>
|
||||
#include <hardware/uart.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* Balcony Weather Station Node 1
|
||||
* record sensor data and send it to home assistant every 5 minutes
|
||||
*/
|
||||
|
||||
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) {
|
||||
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();
|
||||
|
||||
// Setup BME280
|
||||
bme280_config config;
|
||||
bme280_init(&config, i2c1, 14, 15);
|
||||
bme280_reading current_bem280_reading;
|
||||
bme280_init(&bem_config, i2c1, 14, 15);
|
||||
|
||||
// Setup PMS5003
|
||||
pms5003_config pms_config;
|
||||
pms5003_init(&pms_config, uart1, 20, 21, 18, 19);
|
||||
pms5003_reading current_pms5003_reading;
|
||||
|
||||
struct repeating_timer timer_30;
|
||||
// TODO: change from 5 sec to 30sec
|
||||
add_repeating_timer_ms(30000, cb_30, NULL, &timer_30);
|
||||
|
||||
while (true) {
|
||||
// tight_loop_contents();
|
||||
|
||||
sleep_ms(5000); // wait 5 sec
|
||||
printf("Making reading\n");
|
||||
|
||||
// Read BME280
|
||||
current_bem280_reading = bme280_read(&config);
|
||||
printf("Tempature: %.2f\n", current_bem280_reading.temperature);
|
||||
printf("Pressure: %.2f\n", current_bem280_reading.pressure);
|
||||
printf("Humidity: %.2f\n", current_bem280_reading.humidity);
|
||||
|
||||
// Read PMS5003
|
||||
current_pms5003_reading = pms5003_read(&pms_config);
|
||||
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);
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
71
pms5003.c
71
pms5003.c
|
|
@ -20,14 +20,16 @@ https://shop.pimoroni.com/products/pms5003-particulate-matter-sensor-with-cable
|
|||
https://github.com/raspberrypi/pico-examples/blob/master/uart/hello_uart/hello_uart.c
|
||||
*/
|
||||
|
||||
#define MAX_READINGS 100
|
||||
|
||||
static bool read = false;
|
||||
static uint16_t reading_i = 0;
|
||||
static pms5003_reading readings[MAX_READINGS];
|
||||
|
||||
static uint8_t rx_buf[32] = {0};
|
||||
static uint8_t rx_i = 0;
|
||||
|
||||
static float last_pm1_reading = 0;
|
||||
static float last_pm2_5_reading = 0;
|
||||
static float last_pm10_reading = 0;
|
||||
|
||||
static void extract_pm_values_from_rx_buf() {
|
||||
static pms5003_reading extract_pm_values_from_rx_buf() {
|
||||
uint8_t pm1_low = rx_buf[9];
|
||||
uint8_t pm1_high = rx_buf[10];
|
||||
uint16_t pm1 = ((uint16_t)pm1_high << 8) | pm1_low;
|
||||
|
|
@ -40,13 +42,15 @@ static void extract_pm_values_from_rx_buf() {
|
|||
uint8_t pm10_high = rx_buf[14];
|
||||
uint16_t pm10 = ((uint16_t)pm10_high << 8) | pm10_low;
|
||||
|
||||
last_pm1_reading = (float)pm1;
|
||||
last_pm2_5_reading = (float)pm2_5;
|
||||
last_pm10_reading = (float)pm10;
|
||||
pms5003_reading reading = {
|
||||
.pm1 = (float)pm1, .pm2_5 = (float)pm2_5, .pm10 = (float)pm10};
|
||||
|
||||
return reading;
|
||||
}
|
||||
|
||||
static void on_uart_rx() {
|
||||
while (uart_is_readable(uart1)) {
|
||||
// TODO: make the uart1 dynamic
|
||||
while (read && reading_i < MAX_READINGS && uart_is_readable(uart1)) {
|
||||
uint8_t ch = uart_getc(uart1);
|
||||
// start of a message
|
||||
if (ch == 0x42) {
|
||||
|
|
@ -57,7 +61,8 @@ static void on_uart_rx() {
|
|||
rx_i++;
|
||||
// end of message
|
||||
if (rx_i == 31) {
|
||||
extract_pm_values_from_rx_buf();
|
||||
readings[reading_i] = extract_pm_values_from_rx_buf();
|
||||
reading_i++;
|
||||
}
|
||||
|
||||
// guard
|
||||
|
|
@ -78,11 +83,13 @@ void pms5003_init(pms5003_config *new_config, uart_inst_t *uart, uint8_t tx_pin,
|
|||
irq_set_enabled(UART1_IRQ, true);
|
||||
uart_set_irq_enables(uart, true, false);
|
||||
|
||||
// high level or suspending is normal working status, while low level is sleeping mode.
|
||||
// high level or suspending is normal working status, while low level is
|
||||
// sleeping mode.
|
||||
gpio_init(enable_pin);
|
||||
gpio_set_dir(enable_pin, GPIO_OUT);
|
||||
gpio_put(enable_pin, 0);
|
||||
|
||||
// TODO: add hourly reset?
|
||||
// low resets
|
||||
// gpio_init(reset_pin);
|
||||
// gpio_set_dir(reset_pin, GPIO_OUT);
|
||||
|
|
@ -94,13 +101,37 @@ void pms5003_init(pms5003_config *new_config, uart_inst_t *uart, uint8_t tx_pin,
|
|||
new_config->reset_pin = reset_pin;
|
||||
}
|
||||
|
||||
/**
|
||||
Reads should happen min of 30sec after waking the sensor
|
||||
*/
|
||||
pms5003_reading pms5003_read(pms5003_config *config) {
|
||||
pms5003_reading reading;
|
||||
reading.pm1 = last_pm1_reading;
|
||||
reading.pm2_5 = last_pm2_5_reading;
|
||||
reading.pm10 = last_pm10_reading;
|
||||
return reading;
|
||||
static pms5003_reading compute_avarage_reading() {
|
||||
extern uint16_t reading_i;
|
||||
extern pms5003_reading readings[MAX_READINGS];
|
||||
|
||||
float pm1_sum = 0;
|
||||
float pm2_5_sum = 0;
|
||||
float pm10_sum = 0;
|
||||
|
||||
for (int i = 0; i < reading_i; i++) {
|
||||
pm1_sum += readings[i].pm1;
|
||||
pm2_5_sum += readings[i].pm2_5;
|
||||
pm10_sum += readings[i].pm10;
|
||||
}
|
||||
pms5003_reading average_reading = {.pm1 = pm1_sum / reading_i,
|
||||
.pm2_5 = pm2_5_sum / reading_i,
|
||||
.pm10 = pm10_sum / reading_i};
|
||||
return average_reading;
|
||||
}
|
||||
|
||||
void pms5003_warmup(pms5003_config *config) { gpio_put(config->enable_pin, 1); }
|
||||
|
||||
void pms5003_start_reading(pms5003_config *config) {
|
||||
reading_i = 0;
|
||||
read = true;
|
||||
}
|
||||
|
||||
pms5003_reading pms5003_finish_reading(pms5003_config *config) {
|
||||
printf("readings_i: %d\n", reading_i);
|
||||
read = false;
|
||||
gpio_put(config->enable_pin, 0);
|
||||
pms5003_reading average_reading = compute_avarage_reading();
|
||||
reading_i = 0;
|
||||
return average_reading;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,5 +18,7 @@ typedef struct {
|
|||
void pms5003_init(pms5003_config *config, uart_inst_t *uart, uint8_t tx_pin,
|
||||
uint8_t rx_pin, uint8_t enable_pin, uint8_t reset_pin);
|
||||
|
||||
pms5003_reading pms5003_read(pms5003_config *config);
|
||||
void pms5003_warmup(pms5003_config *config);
|
||||
void pms5003_start_reading(pms5003_config *config);
|
||||
pms5003_reading pms5003_finish_reading(pms5003_config *config);
|
||||
#endif /* PMS5003_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue