Repo for ESP32 Weather Station Development
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

148 lignes
4.7KB

  1. /*
  2. * server.c
  3. *
  4. * Created on: 21 Apr 2020
  5. * Author: Chris
  6. */
  7. #include "server.h"
  8. static const char *TAG = "server";
  9. char cur_value_str[255] = "No measurements yet!";
  10. void server_set_values(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
  11. int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
  12. {
  13. sprintf(cur_value_str, "<html><body><p>%d %d %d</p><p>%d %d %d</p></body></html>", temp_raw, pressure_raw, humidity_raw, temp2_raw, pressure2_raw, humidity2_raw);
  14. }
  15. static esp_err_t get_handler(httpd_req_t *req)
  16. {
  17. char* buf;
  18. size_t buf_len;
  19. /* Get header value string length and allocate memory for length + 1,
  20. * extra byte for null termination */
  21. buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
  22. if (buf_len > 1) {
  23. buf = malloc(buf_len);
  24. /* Copy null terminated value string into buffer */
  25. if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
  26. ESP_LOGI(TAG, "Found header => Host: %s", buf);
  27. }
  28. free(buf);
  29. }
  30. // buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
  31. // if (buf_len > 1) {
  32. // buf = malloc(buf_len);
  33. // if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
  34. // ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
  35. // }
  36. // free(buf);
  37. // }
  38. //
  39. // buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
  40. // if (buf_len > 1) {
  41. // buf = malloc(buf_len);
  42. // if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
  43. // ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
  44. // }
  45. // free(buf);
  46. // }
  47. // /* Read URL query string length and allocate memory for length + 1,
  48. // * extra byte for null termination */
  49. // buf_len = httpd_req_get_url_query_len(req) + 1;
  50. // if (buf_len > 1) {
  51. // buf = malloc(buf_len);
  52. // if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
  53. // ESP_LOGI(TAG, "Found URL query => %s", buf);
  54. // char param[32];
  55. // /* Get value of expected key from query string */
  56. // if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
  57. // ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
  58. // }
  59. // if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
  60. // ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
  61. // }
  62. // if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
  63. // ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
  64. // }
  65. // }
  66. // free(buf);
  67. // }
  68. /* Set some custom headers */
  69. // httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
  70. // httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
  71. /* Send response with custom headers and body set as the
  72. * string passed in user context*/
  73. const char* resp_str = cur_value_str;
  74. httpd_resp_send(req, resp_str, strlen(resp_str));
  75. /* After sending the HTTP response the old HTTP request
  76. * headers are lost. Check if HTTP request headers can be read now. */
  77. // if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
  78. // ESP_LOGI(TAG, "Request headers lost");
  79. // }
  80. return ESP_OK;
  81. }
  82. static httpd_uri_t uri = {
  83. .uri = "/",
  84. .method = HTTP_GET,
  85. .handler = get_handler,
  86. };
  87. static httpd_handle_t start_webserver(void)
  88. {
  89. httpd_handle_t server = NULL;
  90. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  91. // Start the httpd server
  92. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  93. if (httpd_start(&server, &config) == ESP_OK) {
  94. // Set URI handlers
  95. ESP_LOGI(TAG, "Registering URI handlers");
  96. httpd_register_uri_handler(server, &uri);
  97. return server;
  98. }
  99. ESP_LOGI(TAG, "Error starting server!");
  100. return NULL;
  101. }
  102. static void stop_webserver(httpd_handle_t server)
  103. {
  104. // Stop the httpd server
  105. httpd_stop(server);
  106. }
  107. static void connect_handler(void* arg, esp_event_base_t event_base,
  108. int32_t event_id, void* event_data)
  109. {
  110. httpd_handle_t* server = (httpd_handle_t*) arg;
  111. if (*server == NULL) {
  112. ESP_LOGI(TAG, "Starting webserver");
  113. *server = start_webserver();
  114. }
  115. }
  116. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  117. int32_t event_id, void* event_data)
  118. {
  119. httpd_handle_t* server = (httpd_handle_t*) arg;
  120. if (*server) {
  121. ESP_LOGI(TAG, "Stopping webserver");
  122. stop_webserver(*server);
  123. *server = NULL;
  124. }
  125. }
  126. void init_server()
  127. {
  128. start_webserver();
  129. }