|  |  | @ -2,9 +2,9 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "esp_event.h" |  |  |  | #include "esp_event.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "esp_log.h" |  |  |  | #include "esp_log.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "esp_system.h" |  |  |  | #include "esp_system.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "nvs_flash.h" |  |  |  | #include "esp_spiffs.h" | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | #include <nvs_flash.h> |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | #include <esp_http_server.h> |  |  |  | #include <esp_http_server.h> | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #include <nvs_flash.h> | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "freertos/FreeRTOS.h" |  |  |  | #include "freertos/FreeRTOS.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "freertos/task.h" |  |  |  | #include "freertos/task.h" | 
			
		
	
	
		
		
			
				
					|  |  | @ -29,7 +29,8 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #define ESP_INTR_FLAG_DEFAULT 0 |  |  |  | #define ESP_INTR_FLAG_DEFAULT 0 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | uint8_t led_state = 0; |  |  |  | uint8_t led_state = 1; | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | volatile uint16_t led_duty = 6075; | 
			
		
	
		
		
			
				
					
					|  |  |  | void connect_to_wifi(char* new_ssid, char* new_psk); |  |  |  | void connect_to_wifi(char* new_ssid, char* new_psk); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Event group
 |  |  |  | // Event group
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -61,12 +62,12 @@ static char* website_lamp_str ="\ | 
			
		
	
		
		
			
				
					
					|  |  |  | 		<script> \ |  |  |  | 		<script> \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 		function sendSlider(data) { \ |  |  |  | 		function sendSlider(data) { \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 			const XHR = new XMLHttpRequest(); \ |  |  |  | 			const XHR = new XMLHttpRequest(); \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 			XHR.open('POST', '/hello'); \ |  |  |  | 			XHR.open('POST', '/lamp_ctrl'); \ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 			XHR.send(\"brightness=\"+data); \
 |  |  |  | 			XHR.send(\"brightness=\"+data); \
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		} \ |  |  |  | 		} \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 		function sendState(data) { \ |  |  |  | 		function sendState(data) { \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 			const XHR = new XMLHttpRequest(); \ |  |  |  | 			const XHR = new XMLHttpRequest(); \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 			XHR.open('POST', '/hello'); \ |  |  |  | 			XHR.open('POST', '/lamp_ctrl'); \ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 			XHR.send(\"lamp_state=\"+data); \
 |  |  |  | 			XHR.send(\"lamp_state=\"+data); \
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		} \ |  |  |  | 		} \ | 
			
		
	
		
		
			
				
					
					|  |  |  | 		</script> \ |  |  |  | 		</script> \ | 
			
		
	
	
		
		
			
				
					|  |  | @ -126,6 +127,41 @@ static esp_err_t web_wifi_post_handler(httpd_req_t *req) | 
			
		
	
		
		
			
				
					
					|  |  |  |     return ESP_OK; |  |  |  |     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 = { |  |  |  | static const httpd_uri_t default_uri = { | 
			
		
	
		
		
			
				
					
					|  |  |  |     .uri       = "/", |  |  |  |     .uri       = "/", | 
			
		
	
		
		
			
				
					
					|  |  |  |     .method    = HTTP_GET, |  |  |  |     .method    = HTTP_GET, | 
			
		
	
	
		
		
			
				
					|  |  | @ -144,6 +180,11 @@ static const httpd_uri_t web_wifi_post_uri = { | 
			
		
	
		
		
			
				
					
					|  |  |  |     .handler   = &web_wifi_post_handler |  |  |  |     .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) |  |  |  | static void IRAM_ATTR gpio_isr_handler(void* arg) | 
			
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  | { | 
			
		
	
	
		
		
			
				
					|  |  | @ -160,6 +201,7 @@ static void button_debounce_task(void* arg){ | 
			
		
	
		
		
			
				
					
					|  |  |  |         vTaskDelay(50 / portTICK_RATE_MS); |  |  |  |         vTaskDelay(50 / portTICK_RATE_MS); | 
			
		
	
		
		
			
				
					
					|  |  |  |         if(gpio_get_level(17)==0){ |  |  |  |         if(gpio_get_level(17)==0){ | 
			
		
	
		
		
			
				
					
					|  |  |  |             led_state^=1; |  |  |  |             led_state^=1; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             printf("setting state = %d\n",led_state); | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |         debounce_active = 0; |  |  |  |         debounce_active = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | @ -171,7 +213,7 @@ static void led_task(void* arg) | 
			
		
	
		
		
			
				
					
					|  |  |  |     printf("started led task!"); |  |  |  |     printf("started led task!"); | 
			
		
	
		
		
			
				
					
					|  |  |  |     for(;;){ |  |  |  |     for(;;){ | 
			
		
	
		
		
			
				
					
					|  |  |  |         if(led_state){ |  |  |  |         if(led_state){ | 
			
		
	
		
		
			
				
					
					|  |  |  |             ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, 1<<11); |  |  |  |             ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, led_duty); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel); |  |  |  |             ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel); | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |         else{ |  |  |  |         else{ | 
			
		
	
	
		
		
			
				
					|  |  | @ -245,12 +287,14 @@ 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() | 
			
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  | { | 
			
		
	
		
		
			
				
					
					|  |  |  |     ledc_timer_config(&ledc_timer); |  |  |  |     ledc_timer_config(&ledc_timer); | 
			
		
	
		
		
			
				
					
					|  |  |  |     ledc_channel_config(&ledc_channel); |  |  |  |     ledc_channel_config(&ledc_channel); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     xTaskCreate(&led_task, "led_task", 2048, NULL, 5, NULL); |  |  |  |     xTaskCreate(&led_task, "led_task", 2048, NULL, 5, NULL); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     gpio_config_t io_conf; |  |  |  |     gpio_config_t io_conf; | 
			
		
	
	
		
		
			
				
					|  |  | @ -266,6 +310,53 @@ void app_main() | 
			
		
	
		
		
			
				
					
					|  |  |  |     gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); |  |  |  |     gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); | 
			
		
	
		
		
			
				
					
					|  |  |  |     gpio_isr_handler_add(17, gpio_isr_handler, (void*) 17); |  |  |  |     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
 |  |  |  | 	// disable the default wifi logging
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	esp_log_level_set("wifi", ESP_LOG_NONE); |  |  |  | 	esp_log_level_set("wifi", ESP_LOG_NONE); | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 |  |  |  | 	 | 
			
		
	
	
		
		
			
				
					|  |  | @ -332,5 +423,6 @@ void app_main() | 
			
		
	
		
		
			
				
					
					|  |  |  |         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_uri); | 
			
		
	
		
		
			
				
					
					|  |  |  |         httpd_register_uri_handler(server, &web_wifi_post_uri); |  |  |  |         httpd_register_uri_handler(server, &web_wifi_post_uri); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         httpd_register_uri_handler(server, &lamp_ctrl_post_uri); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |