From e67438bf550a40d634da88f9b963406b01071bc1 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 13 Jul 2021 12:22:32 +0200 Subject: [PATCH] new webserver uri handlers and .gitignore --- .gitignore | 3 + main/main.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..990ba37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/CMakeLists.txt.user* +/build +/sdkconfig.old diff --git a/main/main.cpp b/main/main.cpp index ef65a47..ee99f4c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2,6 +2,7 @@ // system includes #include +#include // esp-idf includes #include @@ -16,6 +17,7 @@ #include #include #include +#include // Arduino includes #include @@ -32,13 +34,17 @@ constexpr const char * const TAG = "MAIN"; espcpputils::mqtt_client mqttClient; -bool lightState{}; -bool switchState{}; +std::atomic lightState; +std::atomic switchState; //espchrono::millis_clock::time_point lastLightToggle; //espchrono::millis_clock::time_point lastSwitchToggle; -esp_err_t webserver_handler(httpd_req_t *req); +esp_err_t webserver_root_handler(httpd_req_t *req); +esp_err_t webserver_on_handler(httpd_req_t *req); +esp_err_t webserver_off_handler(httpd_req_t *req); +esp_err_t webserver_toggle_handler(httpd_req_t *req); +esp_err_t webserver_reboot_handler(httpd_req_t *req); void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data); } // namespace @@ -150,12 +156,63 @@ extern "C" void app_main() httpd_uri_t uri { .uri = "/", .method = HTTP_GET, - .handler = webserver_handler, - .user_ctx = NULL - }; + .handler = webserver_root_handler, + .user_ctx = NULL}; const auto result = httpd_register_uri_handler(httpdHandle, &uri); - ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler(): %s", esp_err_to_name(result)); + ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler() for %s: %s", "/", esp_err_to_name(result)); + //if (result != ESP_OK) + // return result; + } + + { + httpd_uri_t uri { + .uri = "/on", + .method = HTTP_GET, + .handler = webserver_on_handler, + .user_ctx = NULL}; + + const auto result = httpd_register_uri_handler(httpdHandle, &uri); + ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler() for %s: %s", "/on", esp_err_to_name(result)); + //if (result != ESP_OK) + // return result; + } + + { + httpd_uri_t uri { + .uri = "/off", + .method = HTTP_GET, + .handler = webserver_off_handler, + .user_ctx = NULL}; + + const auto result = httpd_register_uri_handler(httpdHandle, &uri); + ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler() for %s: %s", "/off", esp_err_to_name(result)); + //if (result != ESP_OK) + // return result; + } + + { + httpd_uri_t uri { + .uri = "/toggle", + .method = HTTP_GET, + .handler = webserver_toggle_handler, + .user_ctx = NULL}; + + const auto result = httpd_register_uri_handler(httpdHandle, &uri); + ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler() for %s: %s", "/toggle", esp_err_to_name(result)); + //if (result != ESP_OK) + // return result; + } + + { + httpd_uri_t uri { + .uri = "/reboot", + .method = HTTP_GET, + .handler = webserver_reboot_handler, + .user_ctx = NULL}; + + const auto result = httpd_register_uri_handler(httpdHandle, &uri); + ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_register_uri_handler() for %s: %s", "/reboot", esp_err_to_name(result)); //if (result != ESP_OK) // return result; } @@ -287,15 +344,88 @@ extern "C" void app_main() return result; namespace { -esp_err_t webserver_handler(httpd_req_t *req) +esp_err_t webserver_root_handler(httpd_req_t *req) { - std::string_view body{"this is work in progress..."}; + std::string_view body{"this is work in progress...
\n" + "on
\n" + "off
\n" + "toggle
\n" + "reboot"}; CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") CALL_AND_EXIT_ON_ERROR(httpd_resp_send, req, body.data(), body.size()) return ESP_OK; } +esp_err_t webserver_on_handler(httpd_req_t *req) +{ + const bool state = (lightState = true); + + if (mqttClient) { + std::string_view topic = "dahoam/wohnzimmer/deckenlicht1/status"; + std::string_view value = state ? "ON" : "OFF"; + + ESP_LOGI(TAG, "sending %.*s = %.*s", topic.size(), topic.data(), value.size(), value.data()); + mqttClient.publish(topic, value, 0, 1); + } + + std::string_view body{"ON called..."}; + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") + CALL_AND_EXIT_ON_ERROR(httpd_resp_send, req, body.data(), body.size()) + + return ESP_OK; +} + +esp_err_t webserver_off_handler(httpd_req_t *req) +{ + const bool state = (lightState = false); + + if (mqttClient) { + std::string_view topic = "dahoam/wohnzimmer/deckenlicht1/status"; + std::string_view value = state ? "ON" : "OFF"; + + ESP_LOGI(TAG, "sending %.*s = %.*s", topic.size(), topic.data(), value.size(), value.data()); + mqttClient.publish(topic, value, 0, 1); + } + + std::string_view body{"OFF called..."}; + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") + CALL_AND_EXIT_ON_ERROR(httpd_resp_send, req, body.data(), body.size()) + + return ESP_OK; +} + +esp_err_t webserver_toggle_handler(httpd_req_t *req) +{ + const bool state = (lightState = !lightState); + + if (mqttClient) + { + std::string_view topic = "dahoam/wohnzimmer/deckenlicht1/status"; + std::string_view value = state ? "ON" : "OFF"; + + ESP_LOGI(TAG, "sending %.*s = %.*s", topic.size(), topic.data(), value.size(), value.data()); + mqttClient.publish(topic, value, 0, 1); + } + + std::string_view body{"TOGGLE called..."}; + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") + CALL_AND_EXIT_ON_ERROR(httpd_resp_send, req, body.data(), body.size()) + + return ESP_OK; +} + +esp_err_t webserver_reboot_handler(httpd_req_t *req) +{ + std::string_view body{"REBOOT called..."}; + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_type, req, "text/html") + CALL_AND_EXIT_ON_ERROR(httpd_resp_send, req, body.data(), body.size()) + + esp_restart(); + + return ESP_OK; +} + void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { CPP_UNUSED(event_handler_arg) @@ -320,10 +450,10 @@ void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int3 ESP_LOGI(TAG, "%s event_id=%s", event_base, "MQTT_EVENT_CONNECTED"); mqttClient.publish("dahoam/wohnzimmer/deckenlicht1/available", "online", 0, 0); - mqttClient.publish("dahoam/wohnzimmer/deckenlicht1/status", lightState ? "ON" : "OFF", 0, 1); + mqttClient.publish("dahoam/wohnzimmer/deckenlicht1/status", lightState.load() ? "ON" : "OFF", 0, 1); mqttClient.publish("dahoam/wohnzimmer/lichtschalter1/available", "online", 0, 0); - mqttClient.publish("dahoam/wohnzimmer/lichtschalter1/status", switchState ? "ON" : "OFF", 0, 1); + mqttClient.publish("dahoam/wohnzimmer/lichtschalter1/status", switchState.load() ? "ON" : "OFF", 0, 1); mqttClient.subscribe("dahoam/wohnzimmer/deckenlicht1/set", 0);