move functions around
This commit is contained in:
95
main/main.c
95
main/main.c
@@ -1,12 +1,3 @@
|
|||||||
/* Simple HTTP + SSL Server Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <esp_event.h>
|
#include <esp_event.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
@@ -18,22 +9,23 @@
|
|||||||
#include "protocol_examples_common.h"
|
#include "protocol_examples_common.h"
|
||||||
|
|
||||||
#include <esp_https_server.h>
|
#include <esp_https_server.h>
|
||||||
|
/* #include "bme280.h" */
|
||||||
|
|
||||||
/* A simple example that demonstrates how to create GET and POST
|
// pin numbers specific to this TTGO board
|
||||||
* handlers and start an HTTPS server.
|
#define SDA_PIN GPIO_NUM_21
|
||||||
*/
|
#define SCL_PIN GPIO_NUM_22
|
||||||
|
|
||||||
static const char *TAG = "example";
|
static const char *TAG = "BME280";
|
||||||
|
|
||||||
|
|
||||||
/* An HTTP GET handler */
|
/* An HTTP GET handler */
|
||||||
static esp_err_t root_get_handler(httpd_req_t *req)
|
static esp_err_t root_get_handler(httpd_req_t *req);
|
||||||
{
|
static httpd_handle_t start_webserver(void);
|
||||||
httpd_resp_set_type(req, "text/html");
|
static void stop_webserver(httpd_handle_t server);
|
||||||
httpd_resp_send(req, "<h1>Hello Secure World!</h1>", -1); // -1 = use strlen()
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void* event_data);
|
||||||
return ESP_OK;
|
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 = {
|
static const httpd_uri_t root = {
|
||||||
.uri = "/",
|
.uri = "/",
|
||||||
@@ -42,6 +34,41 @@ static const httpd_uri_t root = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 httpd_handle_t start_webserver(void)
|
static httpd_handle_t start_webserver(void)
|
||||||
{
|
{
|
||||||
httpd_handle_t server = NULL;
|
httpd_handle_t server = NULL;
|
||||||
@@ -97,31 +124,3 @@ static void connect_handler(void* arg, esp_event_base_t event_base,
|
|||||||
*server = start_webserver();
|
*server = start_webserver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user