From 39e11f34f9496bb7a4f297b700ac270fdd0a1dba Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Fri, 24 Jan 2020 03:29:45 +0100 Subject: [PATCH] prototype v1, commiting changes from a weeks ago kind of works, still ugly and insecure --- main/main.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/main/main.c b/main/main.c index 6654c9d..d508102 100644 --- a/main/main.c +++ b/main/main.c @@ -2,9 +2,9 @@ #include "esp_event.h" #include "esp_log.h" #include "esp_system.h" -#include "nvs_flash.h" -#include +#include "esp_spiffs.h" #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -29,7 +29,8 @@ #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); // Event group @@ -61,12 +62,12 @@ static char* website_lamp_str ="\ \ @@ -126,6 +127,41 @@ static esp_err_t web_wifi_post_handler(httpd_req_t *req) 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, @@ -144,6 +180,11 @@ static const httpd_uri_t web_wifi_post_uri = { .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) { @@ -160,6 +201,7 @@ static void button_debounce_task(void* arg){ vTaskDelay(50 / portTICK_RATE_MS); if(gpio_get_level(17)==0){ led_state^=1; + printf("setting state = %d\n",led_state); } debounce_active = 0; } @@ -171,7 +213,7 @@ static void led_task(void* arg) printf("started led task!"); for(;;){ 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); } else{ @@ -245,12 +287,14 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) 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; @@ -266,6 +310,53 @@ void app_main() 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); @@ -332,5 +423,6 @@ void app_main() 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); } }