files: split sensors out into their own source files

This commit is contained in:
A.M. Rowsell 2026-06-27 21:28:41 -04:00
commit 657ee06eed
5 changed files with 167 additions and 32 deletions

45
main.c
View file

@ -18,8 +18,9 @@
* the readings to Adafruit IO over MQTT using the Pico W.
******************************************************************************/
#include "aht.h"
#include "ens.h"
#include "./aht.h"
#include "./bmp.h"
#include "./ens.h"
#include "./mqtt.h"
#include "hardware/gpio.h"
#include "hardware/i2c.h"
@ -32,8 +33,9 @@
#include <pico/error.h>
#include <stdio.h>
#define AVERAGES 4
#define AVERAGES 8
#define I2C1_SCL_PIN 19
#define I2C1_SDA_PIN 18
// MQTT and WiFi defines
#define ADAFRUIT_IO_HOST "io.adafruit.com"
#define ADAFRUIT_IO_USERNAME "ADAFRUIT_IO_USERNAME"
@ -58,6 +60,14 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg,
printf("MQTT status: %d\n", status);
}
static float average_float(const float *samples, uint8_t count) {
float sum = 0.0;
for (uint8_t i = 0; i < count; i++) {
sum += samples[i];
}
return sum / count;
}
static double average_double(const double *samples, uint8_t count) {
double sum = 0.0;
for (uint8_t i = 0; i < count; i++) {
@ -74,14 +84,17 @@ static uint16_t average_uint16(const uint16_t *samples, uint8_t count) {
return (uint16_t)((sum + (uint32_t)count / 2) / (uint32_t)count);
}
int main() {
uint16_t tvoc, eco2, etoh, avg_tvoc, avg_eco2, avg_etoh;
uint8_t aqi, validData;
double t, h, avg_t, avg_h;
double t, h, avg_t, avg_h, avg_bmp_t, avg_bmp_p;
uint16_t tvoc_avg[AVERAGES], eco2_avg[AVERAGES], etoh_avg[AVERAGES];
double t_avg[AVERAGES], h_avg[AVERAGES];
float bmpt_avg[AVERAGES];
float bmpp_avg[AVERAGES];
char payload[32];
float bmpT;
float bmpP;
// MQTT setup
ensaht_mqtt_t mqtt;
struct mqtt_connect_client_info_t mqtt_client_info = {
@ -134,6 +147,15 @@ int main() {
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN,
GPIO_FUNC_I2C));
i2c_init(i2c1,
100 * 1000); // start i2c1 at 100kHz -- this is the stemma chain
gpio_set_function(I2C1_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(I2C1_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(I2C1_SDA_PIN);
gpio_pull_up(I2C1_SCL_PIN);
bi_decl(bi_2pins_with_func(I2C1_SDA_PIN, I2C1_SCL_PIN, GPIO_FUNC_I2C));
initBMP(i2c1);
printf("\x1b[2J"); // clear screen
uint8_t i = 0;
uint8_t sample_count = 0;
@ -141,15 +163,22 @@ int main() {
while (1) {
// cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
getAHTData(&t, &h);
getBMPData(i2c1, &bmpT, &bmpP);
t_avg[i] = t;
h_avg[i] = h;
bmpt_avg[i] = bmpT;
bmpp_avg[i] = bmpP;
if (sample_count < AVERAGES) {
sample_count++;
}
avg_t = average_double(t_avg, sample_count);
avg_h = average_double(h_avg, sample_count);
printf("\x1b[H\x1b[JTemperature %0.2f Humidity %0.2f\n", avg_t, avg_h);
validData = getENSData(&avg_t, &avg_h, &aqi, &tvoc, &eco2, &etoh);
avg_bmp_t = average_float(bmpt_avg, sample_count);
avg_bmp_p = average_float(bmpp_avg, sample_count);
printf("\x1b[H\x1b[JTemperature %0.2f C Humidity %0.2f%%\n", avg_t, avg_h);
printf("BMP Temperature %0.2f C Pressure %0.2f kPa\n", avg_bmp_t,
avg_bmp_p / 100.0);
validData = getENSData(&avg_bmp_t, &avg_h, &aqi, &tvoc, &eco2, &etoh);
tvoc_avg[i] = tvoc;
eco2_avg[i] = eco2;
etoh_avg[i] = etoh;