154 lines
4.5 KiB
C
154 lines
4.5 KiB
C
#include <esp_wifi.h>
|
|
#include <esp_event.h>
|
|
#include <esp_log.h>
|
|
#include <esp_system.h>
|
|
#include <nvs_flash.h>
|
|
#include <sys/param.h>
|
|
#include "esp_netif.h"
|
|
#include "esp_eth.h"
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include <esp_http_server.h>
|
|
#include "bme280.h"
|
|
|
|
static const char *TAG = "BME280";
|
|
|
|
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);
|
|
|
|
static const httpd_uri_t bme280_uri = {
|
|
.uri = "/bme280",
|
|
.method = HTTP_GET,
|
|
.handler = bme280_readout_handler
|
|
};
|
|
|
|
|
|
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 app_main(void)
|
|
{
|
|
static httpd_handle_t server = NULL;
|
|
|
|
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
|
|
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_i2c();
|
|
int8_t ret = init_bme280();
|
|
printf("BME init result %d\n", ret);
|
|
|
|
/* while(1) { */
|
|
/* lastReadoutStatus = bme280_readout(); */
|
|
/* vTaskDelay(1600/portTICK_PERIOD_MS); */
|
|
/* } */
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static esp_err_t bme280_readout_handler(httpd_req_t *req)
|
|
{
|
|
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\": \"bme280\", \"temp\": %.1f, \"humidity\": %.1f, \"pressure\": %0.1f}",
|
|
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 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();
|
|
}
|
|
}
|