server_GPIO.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #define DEBUG_DETAIL_ON
  16. esp_err_t handler_switch_GPIO(httpd_req_t *req)
  17. {
  18. #ifdef DEBUG_DETAIL_ON
  19. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  20. #endif
  21. LogFile.WriteToFile("handler_switch_GPIO");
  22. char _query[200];
  23. char _valueGPIO[30];
  24. char _valueStatus[30];
  25. std::string gpio, status, zw;
  26. int gpionum = 0;
  27. gpio_num_t gpio_num;
  28. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  29. {
  30. printf("Query: "); printf(_query); printf("\n");
  31. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  32. {
  33. printf("GPIO is found"); printf(_valueGPIO); printf("\n");
  34. gpio = std::string(_valueGPIO);
  35. }
  36. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  37. {
  38. printf("Status is found"); printf(_valueStatus); printf("\n");
  39. status = std::string(_valueStatus);
  40. }
  41. };
  42. status = toUpper(status);
  43. if (!(status == "HIGH") && !(status == "LOW"))
  44. {
  45. zw = "Status not valid: " + status;;
  46. httpd_resp_sendstr_chunk(req, zw.c_str());
  47. httpd_resp_sendstr_chunk(req, NULL);
  48. return ESP_OK;
  49. }
  50. gpionum = stoi(gpio);
  51. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 14/15: DMA für SDKarte ???
  52. switch (gpionum) {
  53. case 12:
  54. gpio_num = GPIO_NUM_12;
  55. break;
  56. case 13:
  57. gpio_num = GPIO_NUM_13;
  58. break;
  59. default:
  60. zw = "GPIO" + std::to_string(gpionum) + " not support - only 12 & 13 free";
  61. httpd_resp_sendstr_chunk(req, zw.c_str());
  62. httpd_resp_sendstr_chunk(req, NULL);
  63. return ESP_OK;
  64. }
  65. if (status == "HIGH")
  66. gpio_set_level(gpio_num, 1);
  67. else
  68. gpio_set_level(gpio_num, 0);
  69. zw = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  70. httpd_resp_sendstr_chunk(req, zw.c_str());
  71. httpd_resp_sendstr_chunk(req, NULL);
  72. return ESP_OK;
  73. };
  74. void initGPIO()
  75. {
  76. gpio_config_t io_conf;
  77. //disable interrupt
  78. io_conf.intr_type = GPIO_INTR_DISABLE;
  79. //set as output mode
  80. io_conf.mode = GPIO_MODE_OUTPUT;
  81. //bit mask of the pins that you want to set,e.g.GPIO18/19
  82. // io_conf.pin_bit_mask = ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1));
  83. // io_conf.pin_bit_mask = ((1ULL << GPIO_NUM_12) | (1ULL << GPIO_NUM_2) | (1ULL << GPIO_NUM_4) | (1ULL << GPIO_NUM_12) | (1ULL << GPIO_NUM_13) | (1ULL << GPIO_NUM_14) | (1ULL << GPIO_NUM_15));
  84. io_conf.pin_bit_mask = ((1ULL << GPIO_NUM_12) | (1ULL << GPIO_NUM_13));
  85. //disable pull-down mode
  86. io_conf.pull_down_en = (gpio_pulldown_t) 0;
  87. //disable pull-up mode
  88. io_conf.pull_up_en = (gpio_pullup_t) 0;
  89. //configure GPIO with the given settings
  90. gpio_config(&io_conf);
  91. }
  92. void register_server_GPIO_uri(httpd_handle_t server)
  93. {
  94. ESP_LOGI(TAGPARTGPIO, "server_GPIO - Registering URI handlers");
  95. httpd_uri_t camuri = { };
  96. camuri.method = HTTP_GET;
  97. camuri.uri = "/GPIO";
  98. camuri.handler = handler_switch_GPIO;
  99. camuri.user_ctx = (void*) "switch GPIO";
  100. httpd_register_uri_handler(server, &camuri);
  101. initGPIO();
  102. }