2018-07-03 06:57:41 -04:00
|
|
|
/* Persistent Sockets 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>
|
2019-07-05 04:58:04 -04:00
|
|
|
#include <esp_event.h>
|
2018-07-03 06:57:41 -04:00
|
|
|
#include <esp_log.h>
|
|
|
|
#include <esp_system.h>
|
|
|
|
#include <nvs_flash.h>
|
2019-08-31 10:19:21 -04:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 11:42:37 -05:00
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "protocol_examples_common.h"
|
2018-07-03 06:57:41 -04:00
|
|
|
|
2018-10-24 05:52:15 -04:00
|
|
|
#include <esp_http_server.h>
|
2018-07-03 06:57:41 -04:00
|
|
|
|
|
|
|
/* An example to demonstrate persistent sockets, with context maintained across
|
|
|
|
* multiple requests on that socket.
|
|
|
|
*/
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static const char *TAG = "example";
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-07-03 06:57:41 -04:00
|
|
|
/* Function to free context */
|
2018-11-20 11:42:37 -05:00
|
|
|
static void adder_free_func(void *ctx)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "/adder Free Context function called");
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This handler keeps accumulating data that is posted to it into a per
|
|
|
|
* socket/session context. And returns the result.
|
|
|
|
*/
|
2018-11-20 11:42:37 -05:00
|
|
|
static esp_err_t adder_post_handler(httpd_req_t *req)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
/* Log total visitors */
|
|
|
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
|
|
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
|
|
|
|
|
|
char buf[10];
|
|
|
|
char outbuf[50];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Read data received in the request */
|
|
|
|
ret = httpd_req_recv(req, buf, sizeof(buf));
|
2018-10-09 08:37:38 -04:00
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
httpd_resp_send_408(req);
|
|
|
|
}
|
2018-07-03 06:57:41 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[ret] = '\0';
|
|
|
|
int val = atoi(buf);
|
|
|
|
ESP_LOGI(TAG, "/adder handler read %d", val);
|
|
|
|
|
|
|
|
/* Create session's context if not already available */
|
|
|
|
if (! req->sess_ctx) {
|
|
|
|
ESP_LOGI(TAG, "/adder allocating new session");
|
|
|
|
req->sess_ctx = malloc(sizeof(int));
|
|
|
|
req->free_ctx = adder_free_func;
|
|
|
|
*(int *)req->sess_ctx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the received data to the context */
|
|
|
|
int *adder = (int *)req->sess_ctx;
|
|
|
|
*adder += val;
|
|
|
|
|
|
|
|
/* Respond with the accumulated value */
|
|
|
|
snprintf(outbuf, sizeof(outbuf),"%d", *adder);
|
2020-05-11 04:34:24 -04:00
|
|
|
httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
|
2018-07-03 06:57:41 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This handler gets the present value of the accumulator */
|
2018-11-20 11:42:37 -05:00
|
|
|
static esp_err_t adder_get_handler(httpd_req_t *req)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
/* Log total visitors */
|
|
|
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
|
|
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
|
|
|
|
|
|
char outbuf[50];
|
|
|
|
|
|
|
|
/* Create session's context if not already available */
|
|
|
|
if (! req->sess_ctx) {
|
|
|
|
ESP_LOGI(TAG, "/adder GET allocating new session");
|
|
|
|
req->sess_ctx = malloc(sizeof(int));
|
|
|
|
req->free_ctx = adder_free_func;
|
|
|
|
*(int *)req->sess_ctx = 0;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "/adder GET handler send %d", *(int *)req->sess_ctx);
|
|
|
|
|
|
|
|
/* Respond with the accumulated value */
|
|
|
|
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
|
2020-05-11 04:34:24 -04:00
|
|
|
httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
|
2018-07-03 06:57:41 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This handler resets the value of the accumulator */
|
2018-11-20 11:42:37 -05:00
|
|
|
static esp_err_t adder_put_handler(httpd_req_t *req)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
/* Log total visitors */
|
|
|
|
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
|
|
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
|
|
|
|
|
|
char buf[10];
|
|
|
|
char outbuf[50];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Read data received in the request */
|
|
|
|
ret = httpd_req_recv(req, buf, sizeof(buf));
|
2018-10-09 08:37:38 -04:00
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
httpd_resp_send_408(req);
|
|
|
|
}
|
2018-07-03 06:57:41 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[ret] = '\0';
|
|
|
|
int val = atoi(buf);
|
|
|
|
ESP_LOGI(TAG, "/adder PUT handler read %d", val);
|
|
|
|
|
|
|
|
/* Create session's context if not already available */
|
|
|
|
if (! req->sess_ctx) {
|
|
|
|
ESP_LOGI(TAG, "/adder PUT allocating new session");
|
|
|
|
req->sess_ctx = malloc(sizeof(int));
|
|
|
|
req->free_ctx = adder_free_func;
|
|
|
|
}
|
|
|
|
*(int *)req->sess_ctx = val;
|
|
|
|
|
|
|
|
/* Respond with the reset value */
|
|
|
|
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
|
2020-05-11 04:34:24 -04:00
|
|
|
httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN);
|
2018-07-03 06:57:41 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Maintain a variable which stores the number of times
|
|
|
|
* the "/adder" URI has been visited */
|
|
|
|
static unsigned visitors = 0;
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static const httpd_uri_t adder_post = {
|
2018-07-03 06:57:41 -04:00
|
|
|
.uri = "/adder",
|
|
|
|
.method = HTTP_POST,
|
|
|
|
.handler = adder_post_handler,
|
|
|
|
.user_ctx = &visitors
|
|
|
|
};
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static const httpd_uri_t adder_get = {
|
2018-07-03 06:57:41 -04:00
|
|
|
.uri = "/adder",
|
|
|
|
.method = HTTP_GET,
|
|
|
|
.handler = adder_get_handler,
|
|
|
|
.user_ctx = &visitors
|
|
|
|
};
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static const httpd_uri_t adder_put = {
|
2018-07-03 06:57:41 -04:00
|
|
|
.uri = "/adder",
|
|
|
|
.method = HTTP_PUT,
|
|
|
|
.handler = adder_put_handler,
|
|
|
|
.user_ctx = &visitors
|
|
|
|
};
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static httpd_handle_t start_webserver(void)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
|
|
// Start the httpd server
|
2018-09-02 17:01:44 -04:00
|
|
|
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
2018-07-03 06:57:41 -04:00
|
|
|
httpd_handle_t server;
|
|
|
|
|
|
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
|
|
// Set URI handlers
|
|
|
|
ESP_LOGI(TAG, "Registering URI handlers");
|
|
|
|
httpd_register_uri_handler(server, &adder_get);
|
|
|
|
httpd_register_uri_handler(server, &adder_put);
|
|
|
|
httpd_register_uri_handler(server, &adder_post);
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Error starting server!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
static void stop_webserver(httpd_handle_t server)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
// Stop the httpd server
|
|
|
|
httpd_stop(server);
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 11:42:37 -05:00
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
2018-11-20 11:42:37 -05:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server) {
|
|
|
|
ESP_LOGI(TAG, "Stopping webserver");
|
|
|
|
stop_webserver(*server);
|
|
|
|
*server = NULL;
|
2018-07-03 06:57:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 11:42:37 -05:00
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
2018-11-20 11:42:37 -05:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server == NULL) {
|
|
|
|
ESP_LOGI(TAG, "Starting webserver");
|
|
|
|
*server = start_webserver();
|
|
|
|
}
|
2018-07-03 06:57:41 -04:00
|
|
|
}
|
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-07-03 06:57:41 -04:00
|
|
|
{
|
|
|
|
static httpd_handle_t server = NULL;
|
2018-11-20 11:42:37 -05:00
|
|
|
|
2018-07-03 06:57:41 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 11:42:37 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* 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());
|
|
|
|
|
|
|
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
|
|
|
* and re-start it upon connection.
|
|
|
|
*/
|
|
|
|
#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
|
|
|
|
|
|
|
|
/* Start the server for the first time */
|
|
|
|
server = start_webserver();
|
2018-07-03 06:57:41 -04:00
|
|
|
}
|