diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2024-04-22 16:26:50 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2024-04-22 16:26:50 +0200 |
commit | 2da71ab755d1f444f2867b4dd75fe3e21e2c7395 (patch) | |
tree | 37ffdda6c59746dc9980b90b7bcab950e9577a47 /main/routes/relay.cpp | |
parent | accf08f86a636f59dad3af110a8e914f12f59498 (diff) | |
download | ShelSP-2da71ab755d1f444f2867b4dd75fe3e21e2c7395.tar.gz ShelSP-2da71ab755d1f444f2867b4dd75fe3e21e2c7395.zip |
cleanup
Diffstat (limited to 'main/routes/relay.cpp')
-rw-r--r-- | main/routes/relay.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/main/routes/relay.cpp b/main/routes/relay.cpp new file mode 100644 index 0000000..8dbeb02 --- /dev/null +++ b/main/routes/relay.cpp @@ -0,0 +1,108 @@ +#include "esp_log.h" +#include "esp_check.h" + +#include "relay.h" +#include "routes.h" + +static const char *TAG = "routes/relay"; + +#define GPIO_RELAY_PIN (gpio_num_t)CONFIG_SHELLSP_RELAY_PIN + +#if !CONFIG_IDF_TARGET_LINUX +#include "driver/gpio.h" + +static gpio_config_t* get_gpio(void) +{ + static gpio_config_t io_conf; + + if (!io_conf.pin_bit_mask) + { + //disable interrupt + io_conf.intr_type = GPIO_INTR_DISABLE; + //set as output mode + io_conf.mode = GPIO_MODE_OUTPUT; + //bit mask of the pins that you want to set,e.g. GPIO4 + io_conf.pin_bit_mask = (1ULL << GPIO_RELAY_PIN); + //disable pull-down mode + io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; + //disable pull-up mode + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; + //configure GPIO with the given settings + ESP_ERROR_CHECK(gpio_config(&io_conf)); + } + + return &io_conf; +} +#else /* CONFIG_IDF_TARGET_LINUX */ + +#define gpio_set_level(...) +#define gpio_get_level(...) 0 + +#endif + + + +esp_err_t relay_handler(httpd_req_t *req) +{ + char* resp = NULL; + +#if !CONFIG_IDF_TARGET_LINUX + get_gpio(); +#endif + + size_t query_len = httpd_req_get_url_query_len(req) + 1; + if (query_len > 1) + { + char* buf = (char*)malloc(query_len); + ESP_RETURN_ON_FALSE(buf, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); + + if (httpd_req_get_url_query_str(req, buf, query_len) == ESP_OK) { + char param[8]; + + if (httpd_query_key_value(buf, "turn", param, sizeof(param)) == ESP_OK) { + + if (!strcmp("on", param)) + { + ESP_LOGI(TAG, "Turned GPIO %i on", GPIO_RELAY_PIN); + state = 1; + gpio_set_level(GPIO_RELAY_PIN, state); + } + else if (!strcmp("off", param)) + { + ESP_LOGI(TAG, "Turned GPIO %i off", GPIO_RELAY_PIN); + state = 0; + gpio_set_level(GPIO_RELAY_PIN, state); + } + else if (!strcmp("toggle", param)) + { + state = !state; + ESP_LOGI(TAG, "Toggled GPIO %i", GPIO_RELAY_PIN); + gpio_set_level(GPIO_RELAY_PIN, state); + } + else + { + resp = strdup("Bad turn!"); + } + } + + if (httpd_query_key_value(buf, "timer", param, sizeof(param)) == ESP_OK) { + ESP_LOGI(TAG, "Found URL query parameter => timer=%s", param); + } + } + + free(buf); + } + + if (!resp) + { + httpd_resp_set_type(req, "application/json"); + resp = relay_json(); + ESP_RETURN_ON_FALSE(resp, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); + } + + httpd_resp_sendstr(req, resp); + + free(resp); + + return ESP_OK; +}
\ No newline at end of file |