server_GPIO.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <string>
  2. #include "string.h"
  3. #include <string.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "driver/gpio.h"
  10. //#include "errno.h"
  11. #include <sys/stat.h>
  12. #include "server_GPIO.h"
  13. #include "ClassLogFile.h"
  14. #include "Helper.h"
  15. esp_err_t handler_switch_GPIO(httpd_req_t *req)
  16. {
  17. if (debug_detail_heap) LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  18. LogFile.WriteToFile("handler_switch_GPIO");
  19. char _query[200];
  20. char _valueGPIO[30];
  21. char _valueStatus[30];
  22. std::string gpio, status, zw;
  23. int gpionum = 0;
  24. gpio_num_t gpio_num;
  25. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  26. {
  27. printf("Query: "); printf(_query); printf("\n");
  28. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  29. {
  30. printf("GPIO is found"); printf(_valueGPIO); printf("\n");
  31. gpio = std::string(_valueGPIO);
  32. }
  33. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  34. {
  35. printf("Status is found"); printf(_valueStatus); printf("\n");
  36. status = std::string(_valueStatus);
  37. }
  38. };
  39. status = toUpper(status);
  40. if (!(status == "HIGH") && !(status == "LOW"))
  41. {
  42. zw = "Status not valid: " + status;;
  43. httpd_resp_sendstr_chunk(req, zw.c_str());
  44. httpd_resp_sendstr_chunk(req, NULL);
  45. return ESP_OK;
  46. }
  47. gpionum = stoi(gpio);
  48. // frei: 16; 12-15; 2; 4
  49. switch (gpionum) {
  50. case 2:
  51. gpio_num = GPIO_NUM_2;
  52. break;
  53. case 4:
  54. gpio_num = GPIO_NUM_4;
  55. break;
  56. case 12:
  57. gpio_num = GPIO_NUM_12;
  58. break;
  59. case 13:
  60. gpio_num = GPIO_NUM_13;
  61. break;
  62. case 14:
  63. gpio_num = GPIO_NUM_14;
  64. break;
  65. case 15:
  66. gpio_num = GPIO_NUM_15;
  67. break;
  68. case 16:
  69. gpio_num = (gpio_num_t) 16;
  70. break;
  71. default:
  72. zw = "GPIO" + std::to_string(gpionum) + " not support - only 2, 4, 12-16 free";
  73. httpd_resp_sendstr_chunk(req, zw.c_str());
  74. httpd_resp_sendstr_chunk(req, NULL);
  75. return ESP_OK;
  76. }
  77. // Init the GPIO
  78. gpio_pad_select_gpio(gpio_num);
  79. /* Set the GPIO as a push/pull output */
  80. gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
  81. if (status == "HIGH")
  82. gpio_set_level(gpio_num, 1);
  83. else
  84. gpio_set_level(gpio_num, 0);
  85. zw = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  86. httpd_resp_sendstr_chunk(req, zw.c_str());
  87. httpd_resp_sendstr_chunk(req, NULL);
  88. return ESP_OK;
  89. };
  90. void register_server_GPIO_uri(httpd_handle_t server)
  91. {
  92. ESP_LOGI(TAGPARTGPIO, "server_GPIO - Registering URI handlers");
  93. httpd_uri_t camuri = { };
  94. camuri.method = HTTP_GET;
  95. camuri.uri = "/GPIO";
  96. camuri.handler = handler_switch_GPIO;
  97. camuri.user_ctx = (void*) "switch GPIO";
  98. httpd_register_uri_handler(server, &camuri);
  99. }