181 lines
5.5 KiB
C
181 lines
5.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_https_server.h>
|
|
#include "bme280.h"
|
|
|
|
static const char *TAG = "BME280";
|
|
|
|
/* An HTTP GET handler */
|
|
static esp_err_t root_get_handler(httpd_req_t *req);
|
|
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 root = {
|
|
.uri = "/",
|
|
.method = HTTP_GET,
|
|
.handler = root_get_handler
|
|
};
|
|
|
|
/* static const httpd_uri_t bme280_url = { */
|
|
/* .uri = "/bme280", */
|
|
/* .method = HTTP_GET, */
|
|
/* .handler = bme280_readout_handler */
|
|
/* }; */
|
|
|
|
|
|
void setup_i2c(void);
|
|
int8_t init_bme280(void);
|
|
int8_t bme280_readout();
|
|
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) {
|
|
// take BME280 readings.
|
|
bme280_readout();
|
|
vTaskDelay(1600/portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
struct bme280_data comp_data;
|
|
int8_t bme280_readout(){
|
|
int8_t rslt;
|
|
char msgbuf[128];
|
|
rslt = bme280_set_sensor_mode(BME280_NORMAL_MODE, &bme280);
|
|
bme280.delay_ms(40);
|
|
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &bme280);
|
|
if (rslt != BME280_OK)
|
|
ESP_LOGI(TAG, "bme280_get_sensor_data() returned %d", rslt);
|
|
|
|
snprintf(msgbuf, sizeof(msgbuf), "%.1f C, %0.2f hPa, %0.1f%%",
|
|
comp_data.temperature, comp_data.pressure / 100., comp_data.humidity);
|
|
ESP_LOGI(TAG, "%s", msgbuf);
|
|
|
|
return rslt;
|
|
}
|
|
|
|
static esp_err_t root_get_handler(httpd_req_t *req)
|
|
{
|
|
httpd_resp_set_type(req, "text/html");
|
|
httpd_resp_send(req, "<h1>Hello Secure World!</h1>", -1); // -1 = use strlen()
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t bme280_readout_handler(httpd_req_t *req)
|
|
{
|
|
/* char buffer[128]; */
|
|
httpd_resp_set_type(req, "text/json");
|
|
|
|
/* if (sht31_readTempHum()) { */
|
|
/* float h = sht31_readHumidity(); */
|
|
/* float t = sht31_readTemperature(); */
|
|
/* ESP_LOGI(TAG, "Humidity, Temp(c) : %.f, %.f", h, t); */
|
|
/* sprintf(buffer, "{\"sensor\": \"sht31\", \"values\": { \"humidity\": %f, \"temp\": %f, \"units\": \"celsius\" } }", h, t); */
|
|
/* httpd_resp_send(req, buffer, -1); */
|
|
/* } else { */
|
|
/* ESP_LOGI(TAG, "sht31_readTempHum : failed"); */
|
|
/* httpd_resp_send(req, "{\"error\":\"failed to read 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_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
|
|
|
|
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
|
|
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
|
|
conf.cacert_pem = cacert_pem_start;
|
|
conf.cacert_len = cacert_pem_end - cacert_pem_start;
|
|
|
|
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
|
|
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
|
|
conf.prvtkey_pem = prvtkey_pem_start;
|
|
conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
|
|
|
|
esp_err_t ret = httpd_ssl_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, &root);
|
|
return server;
|
|
}
|
|
|
|
static void stop_webserver(httpd_handle_t server)
|
|
{
|
|
// Stop the httpd server
|
|
httpd_ssl_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();
|
|
}
|
|
}
|