esp_app_main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "platform_esp32.h"
  22. #include "led.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "freertos/FreeRTOS.h"
  26. #include "driver/gpio.h"
  27. #include "driver/spi_master.h"
  28. #include "freertos/task.h"
  29. #include "esp_system.h"
  30. #include "esp_spi_flash.h"
  31. #include "esp_wifi.h"
  32. #include "esp_system.h"
  33. #include "esp_event_loop.h"
  34. #include "nvs_flash.h"
  35. #include "esp_log.h"
  36. #include "freertos/event_groups.h"
  37. #include "mdns.h"
  38. #include "lwip/api.h"
  39. #include "lwip/err.h"
  40. #include "lwip/netdb.h"
  41. #include "http_server.h"
  42. #include "wifi_manager.h"
  43. static EventGroupHandle_t wifi_event_group;
  44. const int CONNECTED_BIT = BIT0;
  45. #define JOIN_TIMEOUT_MS (10000)
  46. static const char TAG[] = "esp_app_main";
  47. #ifdef CONFIG_SQUEEZEAMP
  48. #define LED_GREEN_GPIO 12
  49. #define LED_RED_GPIO 13
  50. #else
  51. #define LED_GREEN_GPIO 0
  52. #define LED_RED_GPIO 0
  53. #endif
  54. /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
  55. void cb_connection_got_ip(void *pvParameter){
  56. ESP_LOGI(TAG, "I have a connection!");
  57. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  58. led_unpush(LED_GREEN);
  59. }
  60. void cb_connection_sta_disconnected(void *pvParameter){
  61. led_blink_pushed(LED_GREEN, 250, 250);
  62. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  63. }
  64. bool wait_for_wifi(){
  65. bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  66. if(!connected){
  67. ESP_LOGD(TAG,"Waiting for WiFi...");
  68. connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  69. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  70. if(!connected){
  71. ESP_LOGW(TAG,"wifi timeout.");
  72. }
  73. else
  74. {
  75. ESP_LOGI(TAG,"WiFi Connected!");
  76. }
  77. }
  78. return connected;
  79. }
  80. void app_main()
  81. {
  82. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  83. led_config(LED_RED, LED_RED_GPIO, 0);
  84. wifi_event_group = xEventGroupCreate();
  85. /* start the wifi manager */
  86. led_blink(LED_GREEN, 250, 250);
  87. wifi_manager_start();
  88. wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
  89. wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
  90. console_start();
  91. }