#include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "esp_spiffs.h" #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "freertos/queue.h" #include "driver/gpio.h" #include "driver/ledc.h" #include #include #include #include #include "esp_netif.h" #include "esp_eth.h" #define WIFI_SSID "AndroidAP" #define WIFI_PASS "uyts6533" #define ESP_INTR_FLAG_DEFAULT 0 uint8_t led_state = 1; volatile uint16_t led_duty = 6075; void connect_to_wifi(char* new_ssid, char* new_psk); // Event group static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; static xQueueHandle gpio_evt_queue = NULL; ledc_timer_config_t ledc_timer = { .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty .freq_hz = 5000, // frequency of PWM signal .speed_mode = LEDC_LOW_SPEED_MODE, // timer mode .timer_num = LEDC_TIMER_1, // timer index .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock }; ledc_channel_config_t ledc_channel = { .channel = LEDC_CHANNEL_0, .duty = 0, .gpio_num = 5, .speed_mode = LEDC_LOW_SPEED_MODE, .hpoint = 0, .timer_sel = LEDC_TIMER_1 }; static char* website_lamp_str ="\ \ \ \ \ "; static char* website_wifi_str ="
\n\n\n\n\n\n\n\n\n\n
SSID:
PSK :
\n\n
"; static esp_err_t default_get_handler(httpd_req_t *req) { 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; } static esp_err_t lamp_ctrl_post_handler(httpd_req_t *req) { char buf[100]; int ret, remaining = req->content_len; 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; if(strncmp(buf, "lamp_state=ON", 13)==0){ led_state=1; } if(strncmp(buf, "lamp_state=OFF", 14)==0){ led_state=0; } if(strncmp(buf, "brightness=", 11)==0){ int bright = atoi(&buf[11]); printf("bright=%d\n", bright); int duty = ((1<<13)-1)/100*bright; printf("duty=%d\n\n", duty); led_duty = duty; } } httpd_resp_send(req, NULL, 0); return ESP_OK; } static const httpd_uri_t default_uri = { .uri = "/", .method = HTTP_GET, .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 const httpd_uri_t lamp_ctrl_post_uri = { .uri = "/lamp_ctrl", .method = HTTP_POST, .handler = &lamp_ctrl_post_handler }; static void IRAM_ATTR gpio_isr_handler(void* arg) { uint32_t gpio_num = (uint32_t) arg; xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); } static void button_debounce_task(void* arg){ static uint8_t debounce_active; if(!debounce_active){ debounce_active = 1; printf("debounce active\n"); vTaskDelay(50 / portTICK_RATE_MS); if(gpio_get_level(17)==0){ led_state^=1; printf("setting state = %d\n",led_state); } debounce_active = 0; } vTaskDelete( NULL ); } static void led_task(void* arg) { printf("started led task!"); for(;;){ if(led_state){ ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, led_duty); ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel); } else{ ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, 0); ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel); } vTaskDelay(10 / portTICK_RATE_MS); } } static void gpio_task(void* arg) { printf("GPIO task started\n"); uint32_t io_num; for(;;) { if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { 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 static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { case SYSTEM_EVENT_STA_START: esp_wifi_connect(); break; case SYSTEM_EVENT_STA_GOT_IP: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); break; case SYSTEM_EVENT_STA_DISCONNECTED: xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); printf("disconnected!\n"); break; default: break; } return ESP_OK; } // Main application void app_main() { ledc_timer_config(&ledc_timer); ledc_channel_config(&ledc_channel); xTaskCreate(&led_task, "led_task", 2048, NULL, 5, NULL); gpio_config_t io_conf; io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE; io_conf.pin_bit_mask = (1ULL<<17); io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_down_en = 0; io_conf.pull_up_en = 1; gpio_config(&io_conf); gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); xTaskCreate(&gpio_task, "gpio_task", 2048, NULL, 10, NULL); gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); gpio_isr_handler_add(17, gpio_isr_handler, (void*) 17); //esp_vfs_spiffs_conf_t spiffs_conf = { // .base_path = "/spiffs", // .partition_label = NULL, // .max_files = 5, // .format_if_mount_failed = true //}; //esp_err_t ret = esp_vfs_spiffs_register(&spiffs_conf); //static const char *TAG = "example"; //if (ret != ESP_OK) { // if (ret == ESP_FAIL) { // ESP_LOGE(TAG, "Failed to mount or format filesystem"); // } else if (ret == ESP_ERR_NOT_FOUND) { // ESP_LOGE(TAG, "Failed to find SPIFFS partition"); // } else { // ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret); // } // return; //} //size_t total = 0, used = 0; //ret = esp_spiffs_info(NULL, &total, &used); //if (ret != ESP_OK) { // ESP_LOGE(TAG, "Failed to get SPIFFS partition information"); // //} //ESP_LOGI(TAG, "Reading file"); //FILE* f = fopen("/spiffs/index.html", "r"); //if (f == NULL) { // ESP_LOGE(TAG, "Failed to open file for reading"); // return; //} //char line[64]; //fgets(line, sizeof(line), f); //fclose(f); //// strip newline //char* pos = strchr(line, '\n'); //if (pos) { // *pos = '\0'; //} //ESP_LOGI(TAG, "Read from file: '%s'", line); //// All done, unmount partition and disable SPIFFS //esp_vfs_spiffs_unregister(NULL); //ESP_LOGI(TAG, "SPIFFS unmounted"); // disable the default wifi logging esp_log_level_set("wifi", ESP_LOG_NONE); // initialize NVS ESP_ERROR_CHECK(nvs_flash_init()); // create the event group to handle wifi events wifi_event_group = xEventGroupCreate(); // initialize the tcp stack tcpip_adapter_init(); // initialize the wifi event handler ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); // initialize the wifi stack in STAtion mode with config in RAM 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 = WIFI_SSID, .password = WIFI_PASS, }, }; ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); printf("Connecting to %s\n", WIFI_SSID); EventBits_t uxBits; uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, 5000 / portTICK_PERIOD_MS); if( ( uxBits & CONNECTED_BIT ) != 0 ) { printf("Connected!\n"); } else{ printf("Not connected!\n"); ESP_ERROR_CHECK(esp_wifi_stop()); esp_wifi_set_mode(WIFI_MODE_AP); wifi_config_t ap_config = { .ap = { .ssid = "lamp", .channel = 0, .authmode = WIFI_AUTH_OPEN, .ssid_hidden = 0, .max_connection = 1, .beacon_interval = 100 } }; esp_wifi_set_config(WIFI_IF_AP, &ap_config); 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); httpd_register_uri_handler(server, &web_wifi_uri); httpd_register_uri_handler(server, &web_wifi_post_uri); httpd_register_uri_handler(server, &lamp_ctrl_post_uri); } }