aboutsummaryrefslogtreecommitdiff
path: root/main/routes/relay.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/routes/relay.c')
-rw-r--r--main/routes/relay.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/main/routes/relay.c b/main/routes/relay.c
new file mode 100644
index 0000000..460e139
--- /dev/null
+++ b/main/routes/relay.c
@@ -0,0 +1,130 @@
+#include "esp_log.h"
+#include "esp_check.h"
+
+#include "routes.h"
+
+static const char *TAG = "routes/relay";
+
+#define GPIO_RELAY_PIN 4
+
+#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
+
+int state = 0;
+
+char* relay_json(void)
+{
+ char* resp = NULL;
+
+ int rc = asprintf(
+ &resp,
+ "{"
+ "\"ison\":%s,"
+ "\"has_timer\":false,"
+ "\"timer_started\":0,"
+ "\"timer_duration\":0,"
+ "\"timer_remaining\":0,"
+ "\"source\":\"http\""
+ "}",
+ state ? "true" : "false"
+ );
+
+ ESP_RETURN_ON_FALSE(resp && rc, NULL, TAG, "buffer alloc failed");
+
+ return resp;
+}
+
+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 = 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) {
+ ESP_LOGI(TAG, "Found URL query parameter => turn=%s", param);
+
+ if (!strcmp("on", param))
+ {
+ ESP_LOGI(TAG, "Turned %i on", GPIO_RELAY_PIN);
+ state = 1;
+ gpio_set_level(GPIO_RELAY_PIN, state);
+ }
+ else if (!strcmp("off", param))
+ {
+ ESP_LOGI(TAG, "Turned %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 %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