working! but could be unstable ..., will debug later

This commit is contained in:
2020-09-15 22:45:25 -05:00
parent 554cf91c8b
commit 5c7341487f
2 changed files with 111 additions and 147 deletions

View File

@@ -1,3 +1,3 @@
idf_component_register(SRCS "bme280.c" "bme280_sup.c"
INCLUDE_DIRS "."
REQUIRES i2c)
)

View File

@@ -20,26 +20,40 @@
#include "bme280.h"
#include "dht22.h"
#define BME280_READOUT_DELAY 2000
#define AM2303_READOUT_DELAY 4000
#define MAX_HTTP_RECV_BUFFER 512
static const char *TAG = "micro_climate";
static esp_err_t bme280_readout_handler(httpd_req_t *req);
static httpd_handle_t start_webserver(void);
static void stop_webserver(httpd_handle_t server);
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data);
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data);
esp_err_t _http_event_handler(esp_http_client_event_t *evt);
static void post_data_http(esp_http_client_handle_t,
char* sensor, char *sensor_description, float temp, float humi, float pres);
static void post_error_http(esp_http_client_handle_t,
char *sensor, char *sensor_description, int err_code, char *error_details);
static const httpd_uri_t bme280_uri = {
.uri = "/bme280",
.method = HTTP_GET,
.handler = bme280_readout_handler
static const char *db_worker_root = "http://192.168.1.107:3000";
struct http_client_t {
esp_http_client_handle_t bme280;
esp_http_client_handle_t am2302;
};
esp_err_t _http_event_handler(esp_http_client_event_t *evt);
static void post_data_http(int status, char *s, float t, float h, float p);
struct http_client_t client;
void init_http_clients();
void init_http_clients(){
char url[80];
snprintf(url, 80, "%s/bme280", db_worker_root);
esp_http_client_config_t config = { .url = url, .event_handler = _http_event_handler};
client.bme280 = esp_http_client_init(&config);
esp_http_client_set_method(client.bme280, HTTP_METHOD_POST);
esp_http_client_set_header(client.bme280, "Content-Type", "application/json");
snprintf(url, 80, "%s/am2302", db_worker_root);
esp_http_client_config_t config2 = { .url = url, .event_handler = _http_event_handler};
client.am2302 = esp_http_client_init(&config2);
esp_http_client_set_method(client.am2302, HTTP_METHOD_POST);
esp_http_client_set_header(client.am2302, "Content-Type", "application/json");
}
void setup_i2c(void);
int8_t init_bme280(void);
@@ -48,6 +62,8 @@ int8_t lastReadoutStatus = 1;
extern struct bme280_dev bme280; // from bme280_sup.c
void DHT_task(void *pvParameter);
void bme280_task(void *pvParameter);
void app_main(void)
{
// initialization
@@ -56,25 +72,20 @@ void app_main(void)
ESP_ERROR_CHECK(esp_event_loop_create_default());
// connect to wifi
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
ESP_ERROR_CHECK(example_connect());
// prepare url
init_http_clients();
// setup the sensors
setup_i2c();
int8_t ret = init_bme280();
printf("BME init result %d\n", ret);
/* while(1) { */
/* lastReadoutStatus = bme280_readout(); */
/* vTaskDelay(1600/portTICK_PERIOD_MS); */
/* } */
vTaskDelay( 1000 / portTICK_RATE_MS );
xTaskCreate( &DHT_task, "DHT_task", 2048, NULL, 5, NULL );
xTaskCreate( &DHT_task, "DHT_readout", 8192, NULL, 5, NULL );
xTaskCreate( &bme280_task, "bme_readout", 8192, NULL, 5, NULL );
}
struct bme280_data comp_data;
@@ -94,69 +105,67 @@ int8_t bme280_readout(){
return ret;
}
void bme280_task(void *pvParameter){
int8_t ret = 1;
while (1) {
ret = bme280_readout();
if (ret == 0) {
post_data_http(client.bme280, "bme280", "bedroom-bme280-01",
comp_data.temperature, comp_data.humidity, comp_data.pressure / 100.);
}
else {
post_error_http(client.bme280, "bme280", "bedroom-bme280-01",
1, "some error");
}
// read_out every 2 sec
vTaskDelay(BME280_READOUT_DELAY / portTICK_RATE_MS);
}
}
void DHT_task(void *pvParameter)
{
setDHTgpio( 32 );
printf( "Starting DHT Task\n\n");
char msgbuf[128];
while(1) {
printf("=== Reading DHT ===\n" );
int ret = readDHT();
errorHandler(ret);
printf( "Hum %.1f\n", getHumidity() );
printf( "Tmp %.1f\n", getTemperature() );
post_data_http(ret, "bedroom-am2302-01", getHumidity(), getTemperature(), -1);
if (ret == DHT_OK) {
snprintf(msgbuf, sizeof(msgbuf), "am2302: %.2f C, %.2f %%",
getTemperature(), getHumidity());
post_data_http(client.am2302, "am2302", "bedroom-am2302-01",
getTemperature(), getHumidity(), -1);
ESP_LOGI(TAG, "%s", msgbuf);
}
else {
post_error_http(client.am2302, "am2302", "bedroom-am2302-01",
1, "some error");
snprintf(msgbuf, sizeof(msgbuf), "am2302: return code %d", ret);
ESP_LOGE(TAG, "%s", msgbuf);
}
// -- wait at least 2 sec before reading again ------------
// The interval of whole process must be beyond 2 seconds !!
vTaskDelay( 4000 / portTICK_RATE_MS );
vTaskDelay( AM2303_READOUT_DELAY / portTICK_RATE_MS );
}
}
static esp_err_t bme280_readout_handler(httpd_req_t *req)
static void post_error_http(esp_http_client_handle_t client,
char *sensor, char *sensor_description, int err_code, char *error_details){
/* snprintf(post_data, sizeof(post_data), "{\"status\": failed, \"error\": \"could not read from sensor\", \"sensor\": \"%s\"}", s); */
}
static void post_data_http(esp_http_client_handle_t client,
char* sensor, char *sensor_description, float temp, float humi, float pres)
{
char buffer[128];
httpd_resp_set_type(req, "text/json");
int8_t ret = bme280_readout();
if (ret == 0) {
snprintf(buffer, sizeof(buffer), "{\"status\": \"ok\", \"sensor\": \"bedroom-bme280-01\", \"temp\": %.2f, \"humidity\": %.2f, \"pressure\": %0.2f}",
comp_data.temperature, comp_data.humidity, comp_data.pressure/100.);
ESP_LOGI(TAG, "%s", buffer);
httpd_resp_send(req, buffer, -1);
}
else {
ESP_LOGE(TAG, "bme280_readout failed");
httpd_resp_send(req, "{\"status\": failed, \"error\": \"could not read from sensor\"}", -1);
}
return ESP_OK;
}
static void post_data_http(int status, char *s, float t, float h, float p)
{
esp_http_client_config_t config = {
.url = "http://192.168.1.107:3000/am2302",
.event_handler = _http_event_handler,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// POST
char post_data[256];
if (status == 0) {
snprintf(post_data, sizeof(post_data),
"{\"status\": \"ok\", \"sensor\": \"%s\", \"temp\": %.2f, \"humidity\": %.2f, \"pressure\": %.2f}",
s, t, h, p);
}
else {
snprintf(post_data, sizeof(post_data), "{\"status\": failed, \"error\": \"could not read from sensor\", \"sensor\": \"%s\"}", s);
}
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/json");
sensor_description, temp, humi, pres);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
@@ -164,51 +173,6 @@ static void post_data_http(int status, char *s, float t, float h, float p)
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
// Start the httpd server
ESP_LOGI(TAG, "Starting server");
httpd_config_t conf = HTTPD_DEFAULT_CONFIG();
esp_err_t ret = httpd_start(&server, &conf);
if (ESP_OK != ret) {
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &bme280_uri);
return server;
}
static void stop_webserver(httpd_handle_t server)
{
httpd_stop(server);
}
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
stop_webserver(*server);
*server = NULL;
}
}
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
*server = start_webserver();
}
}
esp_err_t _http_event_handler(esp_http_client_event_t *evt)