bmp lib fixed

This commit is contained in:
2023-01-14 00:47:20 +01:00
parent 24152cbc0b
commit 1bb827c5db
6 changed files with 177 additions and 108 deletions
+58 -14
View File
@@ -11,7 +11,9 @@
#include "ds18b20.h"
#include "dht.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "circbuf.h"
#include "bmp280.h"
static volatile uint32_t timestamp = 0;
@@ -41,7 +43,7 @@ void hw_timer_callback1s(void *arg)
if (cbuf_full(&rps_cb)) {
cbuf_pop_back(&rps_cb, NULL);
}
cbuf_push(&rps_cb, (void*) &cycle_count);
cbuf_push(&rps_cb, (void *) &cycle_count);
cycle_count = 0;
calculate_wind();
@@ -57,7 +59,7 @@ void calculate_wind()
uint32_t tenmin_sum = 0;
uint32_t numsecs = cbuf_count(&rps_cb);
uint16_t threesec1 = 0, threesec2 = 0;
for(size_t i = 0; i < numsecs; i++) {
for (size_t i = 0; i < numsecs; i++) {
uint16_t *slot = cbuf_ptr_nth(&rps_cb, i);
if (!slot) {
continue;
@@ -71,19 +73,19 @@ void calculate_wind()
threesec1 = threesec2;
threesec2 = slotval;
float gust_avg = (float)gust_sum * (float)20.0f;
float gust_avg = (float) gust_sum * (float) 20.0f;
if (gust_avg > max_gust) {
max_gust = gust_avg;
}
}
rpm_gust = max_gust;
rpm_average = ((float)tenmin_sum / (float)numsecs) * 60.0f;
rpm_average = ((float) tenmin_sum / (float) numsecs) * 60.0f;
}
void meteo_task(void* pvParameters)
void meteo_task(void *pvParameters)
{
cbuf_init(&rps_cb, (void*)history, RPS_BUFFER_LEN, 2); // uint16 fields
cbuf_init(&rps_cb, (void *) history, RPS_BUFFER_LEN, 2); // uint16 fields
// Try to unfuck GPIOs
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
@@ -92,8 +94,8 @@ void meteo_task(void* pvParameters)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15);
// start timer used for timebase
hw_timer_init(hw_timer_callback1s, NULL);
hw_timer_alarm_us(1000000, true); // 1s timer
ESP_ERROR_CHECK(hw_timer_init(hw_timer_callback1s, NULL));
ESP_ERROR_CHECK(hw_timer_alarm_us(1000000, true)); // 1s timer
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_POSEDGE;
@@ -101,21 +103,63 @@ void meteo_task(void* pvParameters)
io_conf.pin_bit_mask = 1 << 14;
io_conf.pull_down_en = 1;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
gpio_install_isr_service(0);
gpio_isr_handler_add(14, gpio_isr_handler, NULL);
ESP_ERROR_CHECK(gpio_config(&io_conf));
ESP_ERROR_CHECK(gpio_install_isr_service(0));
ESP_ERROR_CHECK(gpio_isr_handler_add(14, gpio_isr_handler, NULL));
float dht_hum, dht_temp, ds_temp;
/* I2C driver & peri setup */
int i2c_master_port = I2C_NUM_0;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = 4;
conf.sda_pullup_en = 1;
conf.scl_io_num = 5;
conf.scl_pullup_en = 1;
conf.clk_stretch_tick = 300; // 300 ticks, Clock stretch is about 210us, you can make changes according to the actual situation.
ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
bmp280_params_t bmp_conf;
bmp280_init_default_params(&bmp_conf);
bmp280_t bmp_dev = {
.i2c_dev = {
.addr = BMP280_I2C_ADDRESS_0,
.i2c_num = I2C_NUM_0,
}
};
bmp280_init(&bmp_dev, &bmp_conf);
vTaskDelay(pdMS_TO_TICKS(500));
float dht_hum, dht_temp, ds_temp, bmp_temp, bmp_hum, bmp_press;
while (1) {
dht_hum = 0;
dht_temp = 0;
ds_temp = 0;
bmp_temp = 0;
bmp_press = 0;
// this works ...
ds_temp = ds18b20_measure_and_read(0, DS18B20_ANY);
if (ds_temp != ds_temp) {
printf("DS failed\n");
}
if (!dht_read_float_data(DHT_TYPE_DHT22, 12, &dht_hum, &dht_temp)) {
dht_hum = dht_temp = NAN;
printf("DHT failed\n");
}
printf("Dallas: %.2f °C, DHT %.2f °C, %.1f %%r.H, HALL avg %.1f RPM, gust %.1f RPM\n",
ds_temp, dht_temp, dht_hum, rpm_average, rpm_gust);
if(!bmp280_read_float(&bmp_dev, &bmp_temp, &bmp_press, &bmp_hum)) {
printf("BMP failed\n");
}
printf("Dallas: %.2f °C, ** DHT %.2f °C, %.1f %%r.H, ** WIND avg %.1f, gust %.1f RPM, ** BMP %.2f °C, %f Pa \n",
ds_temp, dht_temp, dht_hum,
rpm_average, rpm_gust,
bmp_temp, bmp_press);
vTaskDelay(pdMS_TO_TICKS(500));
}