2016-07-07 15:04:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "espressif/esp_common.h"
|
|
|
|
#include "esp/uart.h"
|
|
|
|
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
|
|
|
|
2016-08-15 05:36:09 +00:00
|
|
|
#include "i2c/i2c.h"
|
2016-07-07 15:04:17 +00:00
|
|
|
#include "bmp280/bmp280.h"
|
|
|
|
|
2016-07-07 20:39:25 +00:00
|
|
|
// In forced mode user initiate measurement each time.
|
2017-09-01 09:29:32 +00:00
|
|
|
// In normal mode measurement is done continuously with specified standby time.
|
2016-07-07 20:39:25 +00:00
|
|
|
// #define MODE_FORCED
|
2017-09-01 09:29:32 +00:00
|
|
|
const uint8_t i2c_bus = 0;
|
2016-08-15 05:36:09 +00:00
|
|
|
const uint8_t scl_pin = 0;
|
|
|
|
const uint8_t sda_pin = 2;
|
2016-07-07 15:04:17 +00:00
|
|
|
|
2016-07-07 20:39:25 +00:00
|
|
|
#ifdef MODE_FORCED
|
|
|
|
static void bmp280_task_forced(void *pvParameters)
|
|
|
|
{
|
|
|
|
bmp280_params_t params;
|
2016-08-15 05:36:09 +00:00
|
|
|
float pressure, temperature, humidity;
|
2016-07-07 20:39:25 +00:00
|
|
|
|
|
|
|
bmp280_init_default_params(¶ms);
|
|
|
|
params.mode = BMP280_MODE_FORCED;
|
|
|
|
|
2016-08-15 05:36:09 +00:00
|
|
|
bmp280_t bmp280_dev;
|
2017-09-01 09:29:32 +00:00
|
|
|
bmp280_dev.i2c_dev.bus = i2c_bus;
|
|
|
|
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
|
2016-08-15 05:36:09 +00:00
|
|
|
|
2016-07-07 20:39:25 +00:00
|
|
|
while (1) {
|
2016-08-15 05:36:09 +00:00
|
|
|
while (!bmp280_init(&bmp280_dev, ¶ms)) {
|
2016-07-07 20:39:25 +00:00
|
|
|
printf("BMP280 initialization failed\n");
|
2016-11-05 10:04:03 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-07-07 20:39:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 05:36:09 +00:00
|
|
|
bool bme280p = bmp280_dev.id == BME280_CHIP_ID;
|
|
|
|
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
|
|
|
|
2016-07-07 20:39:25 +00:00
|
|
|
while(1) {
|
2016-11-05 10:04:03 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-08-15 05:36:09 +00:00
|
|
|
if (!bmp280_force_measurement(&bmp280_dev)) {
|
2016-07-07 20:39:25 +00:00
|
|
|
printf("Failed initiating measurement\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-08-15 05:36:09 +00:00
|
|
|
// wait for measurement to complete
|
|
|
|
while (bmp280_is_measuring(&bmp280_dev)) {};
|
2016-07-07 15:04:17 +00:00
|
|
|
|
2016-08-15 05:36:09 +00:00
|
|
|
if (!bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity)) {
|
2016-07-07 20:39:25 +00:00
|
|
|
printf("Temperature/pressure reading failed\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-08-15 05:36:09 +00:00
|
|
|
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
|
|
|
|
if (bme280p)
|
|
|
|
printf(", Humidity: %.2f\n", humidity);
|
|
|
|
else
|
|
|
|
printf("\n");
|
2016-07-07 20:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void bmp280_task_normal(void *pvParameters)
|
2016-07-07 15:04:17 +00:00
|
|
|
{
|
|
|
|
bmp280_params_t params;
|
2016-08-15 05:36:09 +00:00
|
|
|
float pressure, temperature, humidity;
|
2016-07-07 15:04:17 +00:00
|
|
|
|
|
|
|
bmp280_init_default_params(¶ms);
|
2016-08-15 05:36:09 +00:00
|
|
|
|
|
|
|
bmp280_t bmp280_dev;
|
2017-09-01 09:29:32 +00:00
|
|
|
bmp280_dev.i2c_dev.bus = i2c_bus;
|
|
|
|
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
|
2016-08-15 05:36:09 +00:00
|
|
|
|
2016-07-07 15:04:17 +00:00
|
|
|
while (1) {
|
2016-08-15 05:36:09 +00:00
|
|
|
while (!bmp280_init(&bmp280_dev, ¶ms)) {
|
2016-07-07 15:04:17 +00:00
|
|
|
printf("BMP280 initialization failed\n");
|
2016-11-05 10:04:03 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-07-07 15:04:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 05:36:09 +00:00
|
|
|
bool bme280p = bmp280_dev.id == BME280_CHIP_ID;
|
|
|
|
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
|
|
|
|
2016-07-07 15:04:17 +00:00
|
|
|
while(1) {
|
2016-11-05 10:04:03 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-08-15 05:36:09 +00:00
|
|
|
if (!bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity)) {
|
2016-07-07 15:04:17 +00:00
|
|
|
printf("Temperature/pressure reading failed\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-08-15 05:36:09 +00:00
|
|
|
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
|
|
|
|
if (bme280p)
|
|
|
|
printf(", Humidity: %.2f\n", humidity);
|
|
|
|
else
|
|
|
|
printf("\n");
|
2016-07-07 15:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-07 20:39:25 +00:00
|
|
|
#endif
|
2016-07-07 15:04:17 +00:00
|
|
|
|
|
|
|
void user_init(void)
|
|
|
|
{
|
|
|
|
uart_set_baud(0, 115200);
|
|
|
|
|
|
|
|
// Just some information
|
|
|
|
printf("\n");
|
|
|
|
printf("SDK version : %s\n", sdk_system_get_sdk_version());
|
|
|
|
printf("GIT version : %s\n", GITSHORTREV);
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_init(i2c_bus, scl_pin, sda_pin, I2C_FREQ_400K);
|
2016-08-15 05:36:09 +00:00
|
|
|
|
2016-07-07 20:39:25 +00:00
|
|
|
#ifdef MODE_FORCED
|
2016-10-21 09:40:36 +00:00
|
|
|
xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL);
|
2016-07-07 20:39:25 +00:00
|
|
|
#else
|
2016-10-21 09:40:36 +00:00
|
|
|
xTaskCreate(bmp280_task_normal, "bmp280_task", 256, NULL, 2, NULL);
|
2016-07-07 20:39:25 +00:00
|
|
|
#endif
|
2016-07-07 15:04:17 +00:00
|
|
|
}
|