start webserver

2-sided-pcb
Eggert Jung 6 years ago
parent c20226748c
commit e760559fcf

@ -4,9 +4,10 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include <esp_http_server.h>
#define WIFI_SSID "AndroidAP" #define WIFI_SSID "AndroidAP"
#define WIFI_PASS "uyts6533" #define WIFI_PASS "uyts6533"
@ -16,6 +17,41 @@
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0; const int CONNECTED_BIT = BIT0;
static char* resp_str ="\
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"ON\"> \
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"OFF\"> \
<label class=\"switch\">\
<input type=\"checkbox\">\
<span class=\"slider\"></span>\
</label>\
</form> \
\
<input type=\"range\" min=\"0\" max=\"100\" value=\"0\" oninput=\"sendSlider(this.value)\" class=\"slider\" id=\"myRange\"> \
<script> \
function sendSlider(data) { \
const XHR = new XMLHttpRequest(); \
XHR.open('POST', '/hello'); \
XHR.send(\"brightness=\"+data); \
} \
function sendState(data) { \
const XHR = new XMLHttpRequest(); \
XHR.open('POST', '/hello'); \
XHR.send(\"lamp_state=\"+data); \
} \
</script> \
";
static esp_err_t default_get_handler(httpd_req_t *req)
{
httpd_resp_send(req, resp_str, strlen(resp_str));
return ESP_OK;
}
static const httpd_uri_t default_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = &default_get_handler
};
// Wifi event handler // Wifi event handler
static esp_err_t event_handler(void *ctx, system_event_t *event) static esp_err_t event_handler(void *ctx, system_event_t *event)
@ -32,6 +68,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
printf("disconnected!\n");
break; break;
default: default:
@ -41,28 +78,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
return ESP_OK; return ESP_OK;
} }
// Main task
void main_task(void *pvParameter)
{
// wait for connection
printf("Main task: waiting for connection to the wifi network... ");
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
printf("connected!\n");
// print the local IP address
tcpip_adapter_ip_info_t ip_info;
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
printf("IP Address: %s\n", ip4addr_ntoa(&ip_info.ip));
printf("Subnet mask: %s\n", ip4addr_ntoa(&ip_info.netmask));
printf("Gateway: %s\n", ip4addr_ntoa(&ip_info.gw));
while(1) {
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
// Main application // Main application
void app_main() void app_main()
{ {
@ -98,9 +113,6 @@ void app_main()
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
printf("Connecting to %s\n", WIFI_SSID); printf("Connecting to %s\n", WIFI_SSID);
// start the main task
//xTaskCreate(&main_task, "main_task", 2048, NULL, 5, NULL);
EventBits_t uxBits; EventBits_t uxBits;
uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, 5000 / portTICK_PERIOD_MS); uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, 5000 / portTICK_PERIOD_MS);
if( ( uxBits & CONNECTED_BIT ) != 0 ) if( ( uxBits & CONNECTED_BIT ) != 0 )
@ -125,4 +137,13 @@ void app_main()
esp_wifi_set_config(WIFI_IF_AP, &ap_config); esp_wifi_set_config(WIFI_IF_AP, &ap_config);
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
} }
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Start the httpd server
printf("Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
httpd_register_uri_handler(server, &default_uri);
}
} }

Loading…
Cancel
Save