#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include #include "driver/gpio.h" #define WIFI_SSID "AndroidAP" #define WIFI_PASS "uyts6533" #define ESP_INTR_FLAG_DEFAULT 0 uint8_t led_state = 0; // Event group static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; static xQueueHandle gpio_evt_queue = NULL; static char* resp_str ="\ \ \ \ \ "; 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 }; 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 gpio_task(void* arg) { printf("GPIO task started\n"); uint32_t io_num; for(;;) { if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num)); } } led_state^=1; if(led_state){ ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, 1<<11); 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); } } // 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() { 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); // 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); } }