|
|
|
@ -1,15 +1,28 @@
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
|
|
|
|
#include "esp_system.h"
|
|
|
|
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
|
|
|
#include <nvs_flash.h>
|
|
|
|
|
|
|
|
#include <esp_http_server.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/ledc.h"
|
|
|
|
#include "driver/ledc.h"
|
|
|
|
#include <esp_http_server.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "esp_netif.h"
|
|
|
|
|
|
|
|
#include "esp_eth.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define WIFI_SSID "AndroidAP"
|
|
|
|
#define WIFI_SSID "AndroidAP"
|
|
|
|
#define WIFI_PASS "uyts6533"
|
|
|
|
#define WIFI_PASS "uyts6533"
|
|
|
|
@ -17,6 +30,7 @@
|
|
|
|
#define ESP_INTR_FLAG_DEFAULT 0
|
|
|
|
#define ESP_INTR_FLAG_DEFAULT 0
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t led_state = 0;
|
|
|
|
uint8_t led_state = 0;
|
|
|
|
|
|
|
|
void connect_to_wifi(char* new_ssid, char* new_psk);
|
|
|
|
|
|
|
|
|
|
|
|
// Event group
|
|
|
|
// Event group
|
|
|
|
static EventGroupHandle_t wifi_event_group;
|
|
|
|
static EventGroupHandle_t wifi_event_group;
|
|
|
|
@ -40,7 +54,7 @@ ledc_channel_config_t ledc_channel =
|
|
|
|
.timer_sel = LEDC_TIMER_1
|
|
|
|
.timer_sel = LEDC_TIMER_1
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static char* resp_str ="\
|
|
|
|
static char* website_lamp_str ="\
|
|
|
|
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"ON\"> \
|
|
|
|
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"ON\"> \
|
|
|
|
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"OFF\"> \
|
|
|
|
<input type=\"submit\" name=\"lamp_state\" onclick=\"sendState(this.value)\" value=\"OFF\"> \
|
|
|
|
<input type=\"range\" min=\"0\" max=\"100\" value=\"0\" oninput=\"sendSlider(this.value)\" class=\"slider\" id=\"myRange\"> \
|
|
|
|
<input type=\"range\" min=\"0\" max=\"100\" value=\"0\" oninput=\"sendSlider(this.value)\" class=\"slider\" id=\"myRange\"> \
|
|
|
|
@ -58,9 +72,57 @@ static char* resp_str ="\
|
|
|
|
</script> \
|
|
|
|
</script> \
|
|
|
|
";
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char* website_wifi_str ="<form action=\"wifi\" method=\"post\">\n<table>\n<tr>\n<td>SSID:</td>\n<td><input type=\"text\" name=\"SSID\"></td>\n</tr>\n<tr>\n<td>PSK :</td>\n<td><input type=\"text\" name=\"PSK\"></td>\n</tr>\n</table>\n<input type=\"submit\" value=\"Submit\">\n</form>";
|
|
|
|
|
|
|
|
|
|
|
|
static esp_err_t default_get_handler(httpd_req_t *req)
|
|
|
|
static esp_err_t default_get_handler(httpd_req_t *req)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
httpd_resp_send(req, resp_str, strlen(resp_str));
|
|
|
|
httpd_resp_send(req, website_lamp_str, strlen(website_lamp_str));
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static esp_err_t web_wifi_get_handler(httpd_req_t *req)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpd_resp_send(req, website_wifi_str, strlen(website_wifi_str));
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static esp_err_t web_wifi_post_handler(httpd_req_t *req)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
int ret, remaining = req->content_len;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char ssid[32];
|
|
|
|
|
|
|
|
char psk[64];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
|
|
|
|
/* Read the data for the request */
|
|
|
|
|
|
|
|
if ((ret = httpd_req_recv(req, buf, MIN(remaining, sizeof(buf)))) <= 0) {
|
|
|
|
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
|
|
|
|
/* Retry receiving if timeout occurred */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining -= ret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* NULL terminal string */
|
|
|
|
|
|
|
|
buf[ret] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *string, *found;
|
|
|
|
|
|
|
|
string = strdup(buf);
|
|
|
|
|
|
|
|
while( (found = strsep(&string,"&")) != NULL ){
|
|
|
|
|
|
|
|
if(strncmp(found, "SSID=", 5)==0){
|
|
|
|
|
|
|
|
memcpy(ssid,&found[5],strlen(&found[5])+1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strncmp(found, "PSK=", 4)==0){
|
|
|
|
|
|
|
|
memcpy(psk,&found[4],strlen(&found[4])+1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
connect_to_wifi(ssid, psk);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpd_resp_send(req, website_wifi_str, strlen(website_wifi_str));
|
|
|
|
return ESP_OK;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -70,6 +132,19 @@ static const httpd_uri_t default_uri = {
|
|
|
|
.handler = &default_get_handler
|
|
|
|
.handler = &default_get_handler
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const httpd_uri_t web_wifi_uri = {
|
|
|
|
|
|
|
|
.uri = "/wifi",
|
|
|
|
|
|
|
|
.method = HTTP_GET,
|
|
|
|
|
|
|
|
.handler = &web_wifi_get_handler
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const httpd_uri_t web_wifi_post_uri = {
|
|
|
|
|
|
|
|
.uri = "/wifi",
|
|
|
|
|
|
|
|
.method = HTTP_POST,
|
|
|
|
|
|
|
|
.handler = &web_wifi_post_handler
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void IRAM_ATTR gpio_isr_handler(void* arg)
|
|
|
|
static void IRAM_ATTR gpio_isr_handler(void* arg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint32_t gpio_num = (uint32_t) arg;
|
|
|
|
uint32_t gpio_num = (uint32_t) arg;
|
|
|
|
@ -113,12 +188,39 @@ static void gpio_task(void* arg)
|
|
|
|
uint32_t io_num;
|
|
|
|
uint32_t io_num;
|
|
|
|
for(;;) {
|
|
|
|
for(;;) {
|
|
|
|
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
|
|
|
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
|
|
|
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
|
|
|
|
|
|
|
|
xTaskCreate(&button_debounce_task, "button_debounce_task", 2048, NULL, 10, NULL);
|
|
|
|
xTaskCreate(&button_debounce_task, "button_debounce_task", 2048, NULL, 10, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void connect_to_wifi(char* new_ssid, char* new_psk){
|
|
|
|
|
|
|
|
printf("ssid: \'%s\'\n", new_ssid);
|
|
|
|
|
|
|
|
printf("psk:\'%s\'\n", new_psk);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_stop());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// configure the wifi connection and start the interface
|
|
|
|
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
|
|
|
|
.sta = {
|
|
|
|
|
|
|
|
.ssid = {*new_ssid},
|
|
|
|
|
|
|
|
.password = {*new_psk}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
strcpy(&(wifi_config.sta.ssid), new_ssid);
|
|
|
|
|
|
|
|
strcpy(&(wifi_config.sta.password), new_psk);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("ssid: \'%s\'\n", wifi_config.sta.ssid);
|
|
|
|
|
|
|
|
printf("psk: \'%s\'\n", wifi_config.sta.password);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -143,7 +245,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
|
|
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Main application
|
|
|
|
// Main application
|
|
|
|
void app_main()
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -229,5 +330,7 @@ void app_main()
|
|
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
|
|
// Set URI handlers
|
|
|
|
// Set URI handlers
|
|
|
|
httpd_register_uri_handler(server, &default_uri);
|
|
|
|
httpd_register_uri_handler(server, &default_uri);
|
|
|
|
|
|
|
|
httpd_register_uri_handler(server, &web_wifi_uri);
|
|
|
|
|
|
|
|
httpd_register_uri_handler(server, &web_wifi_post_uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|