#include #include #include #include #include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_http_server.h" #include "esp_netif.h" #include "esp_eth.h" #include "esp_http_client.h" #include "esp_tls.h" #include "protocol_examples_common.h" #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"; 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 char *db_worker_root = "http://192.168.1.107:8431"; struct http_client_t { esp_http_client_handle_t bme280; esp_http_client_handle_t am2302; }; 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); int8_t bme280_readout(); 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 ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); // connect to wifi 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); vTaskDelay( 1000 / portTICK_RATE_MS ); xTaskCreate( &DHT_task, "DHT_readout", 8192, NULL, 5, NULL ); xTaskCreate( &bme280_task, "bme_readout", 8192, NULL, 5, NULL ); } struct bme280_data comp_data; int8_t bme280_readout(){ int8_t ret; char msgbuf[128]; ret = bme280_set_sensor_mode(BME280_NORMAL_MODE, &bme280); bme280.delay_ms(40); ret = bme280_get_sensor_data(BME280_ALL, &comp_data, &bme280); if (ret != BME280_OK) ESP_LOGI(TAG, "bme280_get_sensor_data() returned %d", ret); snprintf(msgbuf, sizeof(msgbuf), "%.1f C, %0.1f hPa, %0.1f%%", comp_data.temperature, comp_data.pressure / 100., comp_data.humidity); ESP_LOGI(TAG, "%s", msgbuf); return ret; } void bme280_task(void *pvParameter){ int8_t ret = 1; char msgbuf[128]; 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.); snprintf(msgbuf, sizeof(msgbuf), "bme280: %.2f C, %.2f %%, %.2f hPa", comp_data.temperature, comp_data.humidity, comp_data.pressure / 100.); ESP_LOGI(TAG, "bme2800: %s", msgbuf); } else { post_error_http(client.bme280, "bme280", "bedroom-bme280-01", ret, "some error"); snprintf(msgbuf, sizeof(msgbuf), "am2302: return code %d", ret); ESP_LOGE(TAG, "bme280: %s", msgbuf); } // read_out every 2 sec vTaskDelay(BME280_READOUT_DELAY / portTICK_RATE_MS); } } void DHT_task(void *pvParameter) { setDHTgpio( 32 ); char msgbuf[128]; while(1) { int ret = readDHT(); errorHandler(ret); 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, "am2302: %s", msgbuf); } else { post_error_http(client.am2302, "am2302", "bedroom-am2302-01", ret, "some error"); snprintf(msgbuf, sizeof(msgbuf), "am2302: return code %d", ret); ESP_LOGE(TAG, "am2302: %s", msgbuf); } // -- wait at least 2 sec before reading again ------------ // The interval of whole process must be beyond 2 seconds !! vTaskDelay( AM2303_READOUT_DELAY / portTICK_RATE_MS ); } } static void post_error_http(esp_http_client_handle_t client, char *sensor, char *sensor_description, int err_code, char *error_details){ char url[80]; snprintf(url, 80, "%s/%s", db_worker_root, sensor); esp_http_client_config_t config = { .url = url, .event_handler = _http_event_handler}; client = esp_http_client_init(&config); esp_http_client_set_method(client, HTTP_METHOD_POST); esp_http_client_set_header(client, "Content-Type", "application/json"); // POST char post_data[256]; snprintf(post_data, sizeof(post_data), "{\"status\": %d, \"error\": \"%s\", \"sensor\": \"%s\"}", err_code, error_details, sensor_description); 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, "%s: HTTP POST Status = %d, content_length = %d", sensor, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { ESP_LOGE(TAG, "%s: HTTP POST request failed: %s", sensor, esp_err_to_name(err)); } esp_http_client_cleanup(client); } static void post_data_http(esp_http_client_handle_t client, char* sensor, char *sensor_description, float temp, float humi, float pres) { char url[80]; snprintf(url, 80, "%s/%s", db_worker_root, sensor); esp_http_client_config_t config = { .url = url, .event_handler = _http_event_handler}; client = esp_http_client_init(&config); esp_http_client_set_method(client, HTTP_METHOD_POST); esp_http_client_set_header(client, "Content-Type", "application/json"); // POST char post_data[256]; snprintf(post_data, sizeof(post_data), "{\"status\": \"ok\", \"sensor\": \"%s\", \"temp\": %.2f, \"humidity\": %.2f, \"pressure\": %.2f}", 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, "%s: HTTP POST Status = %d, content_length = %d", sensor, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { ESP_LOGE(TAG, "%s: HTTP POST request failed: %s", sensor, esp_err_to_name(err)); } esp_http_client_cleanup(client); } esp_err_t _http_event_handler(esp_http_client_event_t *evt) { switch(evt->event_id) { case HTTP_EVENT_ERROR: ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); break; case HTTP_EVENT_ON_CONNECTED: ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); break; case HTTP_EVENT_HEADER_SENT: ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); break; case HTTP_EVENT_ON_HEADER: ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); break; case HTTP_EVENT_ON_DATA: ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); if (!esp_http_client_is_chunked_response(evt->client)) { // Write out data // printf("%.*s", evt->data_len, (char*)evt->data); } break; case HTTP_EVENT_ON_FINISH: ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); break; case HTTP_EVENT_DISCONNECTED: ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); int mbedtls_err = 0; esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL); if (err != 0) { ESP_LOGI(TAG, "Last esp error code: 0x%x", err); ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); } break; } return ESP_OK; }