turning off and on via switch works

2-sided-pcb
Eggert Jung 6 years ago
parent 0a02f17a5f
commit d39ab9c064

@ -7,8 +7,9 @@
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include <esp_http_server.h>
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/ledc.h"
#include <esp_http_server.h>
#define WIFI_SSID "AndroidAP" #define WIFI_SSID "AndroidAP"
#define WIFI_PASS "uyts6533" #define WIFI_PASS "uyts6533"
@ -22,6 +23,23 @@ static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0; const int CONNECTED_BIT = BIT0;
static xQueueHandle gpio_evt_queue = NULL; 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* resp_str ="\ static char* resp_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\"> \
@ -58,6 +76,37 @@ static void IRAM_ATTR gpio_isr_handler(void* arg)
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); 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;
}
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, 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);
}
vTaskDelay(10 / portTICK_RATE_MS);
}
}
static void gpio_task(void* arg) static void gpio_task(void* arg)
{ {
printf("GPIO task started\n"); printf("GPIO task started\n");
@ -65,17 +114,9 @@ static void gpio_task(void* arg)
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)); 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);
} }
} }
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 // Wifi event handler
@ -106,6 +147,11 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
// Main application // Main application
void app_main() 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; gpio_config_t io_conf;
io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE; io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE;
io_conf.pin_bit_mask = (1ULL<<17); io_conf.pin_bit_mask = (1ULL<<17);

Loading…
Cancel
Save