esp_app_main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "nvs_utilities.h"
  42. #include "http_server.h"
  43. #include "wifi_manager.h"
  44. #include "squeezelite-ota.h"
  45. static EventGroupHandle_t wifi_event_group;
  46. const int CONNECTED_BIT = BIT0;
  47. #define JOIN_TIMEOUT_MS (10000)
  48. static const char TAG[] = "esp_app_main";
  49. char * fwurl = NULL;
  50. #ifdef CONFIG_SQUEEZEAMP
  51. #define LED_GREEN_GPIO 12
  52. #define LED_RED_GPIO 13
  53. #else
  54. #define LED_GREEN_GPIO 0
  55. #define LED_RED_GPIO 0
  56. #endif
  57. static bool bWifiConnected=false;
  58. /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
  59. void cb_connection_got_ip(void *pvParameter){
  60. ESP_LOGI(TAG, "I have a connection!");
  61. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  62. bWifiConnected=true;
  63. led_unpush(LED_GREEN);
  64. }
  65. void cb_connection_sta_disconnected(void *pvParameter){
  66. led_blink_pushed(LED_GREEN, 250, 250);
  67. bWifiConnected=false;
  68. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  69. }
  70. bool wait_for_wifi(){
  71. bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  72. if(!connected){
  73. ESP_LOGD(TAG,"Waiting for WiFi...");
  74. connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  75. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  76. if(!connected){
  77. ESP_LOGW(TAG,"wifi timeout.");
  78. }
  79. else
  80. {
  81. ESP_LOGI(TAG,"WiFi Connected!");
  82. }
  83. }
  84. return connected;
  85. }
  86. void app_main()
  87. {
  88. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  89. led_config(LED_RED, LED_RED_GPIO, 0);
  90. wifi_event_group = xEventGroupCreate();
  91. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  92. /* start the wifi manager */
  93. led_blink(LED_GREEN, 250, 250);
  94. wifi_manager_start();
  95. wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
  96. wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
  97. char * fwurl = get_nvs_value_alloc(NVS_TYPE_STR, "fwurl");
  98. if(fwurl){
  99. while(!bWifiConnected){
  100. wait_for_wifi();
  101. }
  102. ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
  103. start_ota(fwurl);
  104. }
  105. console_start();
  106. }