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.

283 lignes
9.7KB

  1. #include "display.h"
  2. #include "sensors.h"
  3. #include <esp_log.h>
  4. #include <freertos/FreeRTOS.h>
  5. #include <freertos/task.h>
  6. //#include <esp_wifi.h>
  7. //#include "freertos/event_groups.h"
  8. //#include "nvs_flash.h"
  9. //#include <esp_http_server.h>
  10. //#include <sys/param.h>
  11. //#define WIFI_SSID "Netzknecht"
  12. //#define WIFI_PASS "***REMOVED***"
  13. //#define WIFI_RETRIES 10
  14. //
  15. //static EventGroupHandle_t s_wifi_event_group;
  16. //#define WIFI_CONNECTED_BIT BIT0
  17. //#define WIFI_FAIL_BIT BIT1
  18. //
  19. //static const char *TAG = "wifi station";
  20. //static int s_retry_num = 0;
  21. //
  22. //char cur_value_str[255];
  23. //
  24. ///* An HTTP GET handler */
  25. //static esp_err_t hello_get_handler(httpd_req_t *req)
  26. //{
  27. // char* buf;
  28. // size_t buf_len;
  29. //
  30. // /* Get header value string length and allocate memory for length + 1,
  31. // * extra byte for null termination */
  32. // buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
  33. // if (buf_len > 1) {
  34. // buf = malloc(buf_len);
  35. // /* Copy null terminated value string into buffer */
  36. // if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
  37. // ESP_LOGI(TAG, "Found header => Host: %s", buf);
  38. // }
  39. // free(buf);
  40. // }
  41. //
  42. // buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
  43. // if (buf_len > 1) {
  44. // buf = malloc(buf_len);
  45. // if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
  46. // ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
  47. // }
  48. // free(buf);
  49. // }
  50. //
  51. // buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
  52. // if (buf_len > 1) {
  53. // buf = malloc(buf_len);
  54. // if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
  55. // ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
  56. // }
  57. // free(buf);
  58. // }
  59. //
  60. // /* Read URL query string length and allocate memory for length + 1,
  61. // * extra byte for null termination */
  62. // buf_len = httpd_req_get_url_query_len(req) + 1;
  63. // if (buf_len > 1) {
  64. // buf = malloc(buf_len);
  65. // if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
  66. // ESP_LOGI(TAG, "Found URL query => %s", buf);
  67. // char param[32];
  68. // /* Get value of expected key from query string */
  69. // if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
  70. // ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
  71. // }
  72. // if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
  73. // ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
  74. // }
  75. // if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
  76. // ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
  77. // }
  78. // }
  79. // free(buf);
  80. // }
  81. //
  82. // /* Set some custom headers */
  83. // httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
  84. // httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
  85. //
  86. // /* Send response with custom headers and body set as the
  87. // * string passed in user context*/
  88. // const char* resp_str = cur_value_str;
  89. // httpd_resp_send(req, resp_str, strlen(resp_str));
  90. //
  91. // /* After sending the HTTP response the old HTTP request
  92. // * headers are lost. Check if HTTP request headers can be read now. */
  93. // if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
  94. // ESP_LOGI(TAG, "Request headers lost");
  95. // }
  96. // return ESP_OK;
  97. //}
  98. //
  99. //static httpd_uri_t hello = {
  100. // .uri = "/hello",
  101. // .method = HTTP_GET,
  102. // .handler = hello_get_handler,
  103. // /* Let's pass response string in user
  104. // * context to demonstrate it's usage */
  105. // .user_ctx = "Hello World!"
  106. //};
  107. //
  108. //static httpd_handle_t start_webserver(void)
  109. //{
  110. // httpd_handle_t server = NULL;
  111. // httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  112. //
  113. // // Start the httpd server
  114. // ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  115. // if (httpd_start(&server, &config) == ESP_OK) {
  116. // // Set URI handlers
  117. // ESP_LOGI(TAG, "Registering URI handlers");
  118. // httpd_register_uri_handler(server, &hello);
  119. // return server;
  120. // }
  121. //
  122. // ESP_LOGI(TAG, "Error starting server!");
  123. // return NULL;
  124. //}
  125. //
  126. //static void stop_webserver(httpd_handle_t server)
  127. //{
  128. // // Stop the httpd server
  129. // httpd_stop(server);
  130. //}
  131. //
  132. //static void disconnect_handler(void* arg, esp_event_base_t event_base,
  133. // int32_t event_id, void* event_data)
  134. //{
  135. // httpd_handle_t* server = (httpd_handle_t*) arg;
  136. // if (*server) {
  137. // ESP_LOGI(TAG, "Stopping webserver");
  138. // stop_webserver(*server);
  139. // *server = NULL;
  140. // }
  141. //}
  142. //
  143. //static void connect_handler(void* arg, esp_event_base_t event_base,
  144. // int32_t event_id, void* event_data)
  145. //{
  146. // httpd_handle_t* server = (httpd_handle_t*) arg;
  147. // if (*server == NULL) {
  148. // ESP_LOGI(TAG, "Starting webserver");
  149. // *server = start_webserver();
  150. // }
  151. //}
  152. //
  153. //static void event_handler(void* arg, esp_event_base_t event_base,
  154. // int32_t event_id, void* event_data)
  155. //{
  156. // if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  157. // esp_wifi_connect();
  158. // } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  159. // if (s_retry_num < WIFI_RETRIES) {
  160. // esp_wifi_connect();
  161. // s_retry_num++;
  162. // ESP_LOGI(TAG, "retry to connect to the AP");
  163. // } else {
  164. // xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  165. // }
  166. // ESP_LOGI(TAG,"connect to the AP fail");
  167. // } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  168. // ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  169. // ESP_LOGI(TAG, "got ip:%s",
  170. // ip4addr_ntoa(&event->ip_info.ip));
  171. // s_retry_num = 0;
  172. // xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  173. // }
  174. //}
  175. //void wifi_init_sta()
  176. //{
  177. // s_wifi_event_group = xEventGroupCreate();
  178. //
  179. // tcpip_adapter_init();
  180. //
  181. // ESP_ERROR_CHECK(esp_event_loop_create_default());
  182. //
  183. // wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  184. // ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  185. //
  186. // //tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
  187. // //tcpip_adapter_ip_info_t info;
  188. // //ip4_addr_t gw;
  189. // //gw.addr = ipaddr_addr("192.168.0.1");
  190. // //info.gw = gw;
  191. // //ip4_addr_t ip;
  192. // //ip.addr = ipaddr_addr("192.168.0.110");
  193. // //info.ip = ip;
  194. // //ip4_addr_t netmask;
  195. // //netmask.addr = ipaddr_addr("255.255.255.0");
  196. // //info.netmask = netmask;
  197. // //tcpip_adapter_sta_start(0, &info);
  198. //
  199. // ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  200. // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  201. //
  202. // wifi_config_t wifi_config = {
  203. // .sta = {
  204. // .ssid = WIFI_SSID,
  205. // .password = WIFI_PASS
  206. // },
  207. // };
  208. // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  209. // ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  210. // ESP_ERROR_CHECK(esp_wifi_start() );
  211. //
  212. // ESP_LOGI(TAG, "wifi_init_sta finished.");
  213. //
  214. // /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  215. // * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
  216. // EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  217. // WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  218. // pdFALSE,
  219. // pdFALSE,
  220. // portMAX_DELAY);
  221. //
  222. // //tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info);
  223. // /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  224. // * happened. */
  225. // if (bits & WIFI_CONNECTED_BIT) {
  226. // ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  227. // WIFI_SSID, WIFI_PASS);
  228. // } else if (bits & WIFI_FAIL_BIT) {
  229. // ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  230. // WIFI_SSID, WIFI_PASS);
  231. // } else {
  232. // ESP_LOGE(TAG, "UNEXPECTED EVENT");
  233. // }
  234. //
  235. // ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
  236. // ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
  237. // vEventGroupDelete(s_wifi_event_group);
  238. //}
  239. void app_main(void)
  240. {
  241. int32_t temp = -12;
  242. uint32_t pressure = 0;
  243. uint32_t humidity = 0;
  244. int32_t temp2 = 0;
  245. uint32_t pressure2 = 0;
  246. uint32_t humidity2 = 0;
  247. // INIT WIFI
  248. //Initialize NVS
  249. // esp_err_t ret = nvs_flash_init();
  250. // if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  251. // ESP_ERROR_CHECK(nvs_flash_erase());
  252. // ret = nvs_flash_init();
  253. // }
  254. // ESP_ERROR_CHECK(ret);
  255. //
  256. // ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  257. // wifi_init_sta();
  258. // INIT WEBSERVER
  259. // static httpd_handle_t server = NULL;
  260. // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  261. // ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  262. // server = start_webserver();
  263. init_sensors();
  264. init_display();
  265. read_sensor(&temp, &pressure, &humidity);
  266. read_sensor2(&temp2, &pressure2, &humidity2);
  267. display_data(temp, pressure, humidity, temp2, pressure2, humidity2);
  268. while (1) {
  269. read_sensor(&temp, &pressure, &humidity);
  270. read_sensor2(&temp2, &pressure2, &humidity2);
  271. display_data(temp, pressure, humidity, temp2, pressure2, humidity2);
  272. vTaskDelay(1000 / portTICK_PERIOD_MS);
  273. }
  274. }