partially working

This commit is contained in:
2020-09-15 20:07:41 -05:00
parent 828f2ee07b
commit 554cf91c8b

View File

@@ -4,15 +4,25 @@
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_http_server.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include "esp_http_client.h"
#include "esp_tls.h"
#include <esp_http_server.h>
#include "protocol_examples_common.h"
#include "bme280.h"
#include "dht22.h"
static const char *TAG = "BME280";
#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);
@@ -28,6 +38,8 @@ static const httpd_uri_t bme280_uri = {
.handler = bme280_readout_handler
};
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);
void setup_i2c(void);
int8_t init_bme280(void);
@@ -35,54 +47,23 @@ int8_t bme280_readout();
int8_t lastReadoutStatus = 1;
extern struct bme280_dev bme280; // from bme280_sup.c
void DHT_task(void *pvParameter)
{
setDHTgpio( 32 );
printf( "Starting DHT Task\n\n");
while(1) {
printf("=== Reading DHT ===\n" );
int ret = readDHT();
errorHandler(ret);
printf( "Hum %.1f\n", getHumidity() );
printf( "Tmp %.1f\n", getTemperature() );
// -- wait at least 2 sec before reading again ------------
// The interval of whole process must be beyond 2 seconds !!
vTaskDelay( 3000 / portTICK_RATE_MS );
}
}
void DHT_task(void *pvParameter);
void app_main(void)
{
static httpd_handle_t server = NULL;
// initialization
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* Register event handlers to start server when Wi-Fi or Ethernet is connected,
* and stop server when disconnection happens.
*/
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
// 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));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
// setup the sensors
setup_i2c();
int8_t ret = init_bme280();
printf("BME init result %d\n", ret);
@@ -113,6 +94,27 @@ int8_t bme280_readout(){
return ret;
}
void DHT_task(void *pvParameter)
{
setDHTgpio( 32 );
printf( "Starting DHT Task\n\n");
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);
// -- wait at least 2 sec before reading again ------------
// The interval of whole process must be beyond 2 seconds !!
vTaskDelay( 4000 / portTICK_RATE_MS );
}
}
static esp_err_t bme280_readout_handler(httpd_req_t *req)
{
char buffer[128];
@@ -132,6 +134,38 @@ static esp_err_t bme280_readout_handler(httpd_req_t *req)
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");
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),
esp_http_client_get_content_length(client));
} 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;
@@ -176,3 +210,43 @@ static void connect_handler(void* arg, esp_event_base_t event_base,
*server = start_webserver();
}
}
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;
}