main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <sys/cdefs.h>
  2. #include <stdio.h>
  3. #include <nvs_flash.h>
  4. #include <esp_netif.h>
  5. #include "esp_log.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "app/include/wifi.h"
  9. #include "lcd_st7701.h"
  10. #include "lvgl_port.h"
  11. #include "framework_compat.h"
  12. #include "time_sync.h"
  13. #include "ota.h"
  14. #include "custom.h"
  15. #include "beep.h"
  16. #include "sht30.h"
  17. #include "modbus_master.h"
  18. #include "system/miscellaneous_interface.h"
  19. #include "setting.h"
  20. #include "heatpump_controller.h"
  21. static const char *TAG = "main";
  22. lv_ui guider_ui;
  23. system_setting_t system_setting = {};
  24. bool cloud_connected = false;
  25. void iot_event_monitor(int event) {
  26. switch (event) {
  27. case IOT_CONNECT_CLOUD_FAILT:
  28. ESP_LOGI(TAG, "IOT_CONNECT_CLOUD_FAILT");
  29. cloud_connected = false;
  30. lvgl_port_lock(-1);
  31. lv_obj_add_flag(guider_ui.screen_lab_icon_cloud, LV_OBJ_FLAG_HIDDEN);
  32. lvgl_port_unlock();
  33. break;
  34. case IOT_CONNECT_CLOUD_SUCCESS:
  35. ESP_LOGI(TAG, "IOT_CONNECT_CLOUD_SUCCESS");
  36. cloud_connected = true;
  37. lvgl_port_lock(-1);
  38. lv_obj_clear_flag(guider_ui.screen_lab_icon_cloud, LV_OBJ_FLAG_HIDDEN);
  39. lv_obj_clear_flag(guider_ui.screen_img_wifi, LV_OBJ_FLAG_HIDDEN);
  40. lvgl_port_unlock();
  41. break;
  42. case IOT_LOGIN_FAILT:
  43. ESP_LOGI(TAG, "IOT_LOGIN_FAILT");
  44. break;
  45. case IOT_LOGIN_SUCCESS:
  46. ESP_LOGI(TAG, "IOT_LOGIN_SUCCESS");
  47. break;
  48. case IOT_REGISTER_FAILT:
  49. ESP_LOGI(TAG, "IOT_REGISTER_FAILT");
  50. break;
  51. case IOT_REGISTER_SUCCESS:
  52. ESP_LOGI(TAG, "IOT_REGISTER_SUCCESS");
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. /**
  59. * wifi连接成功后执行的回调函数
  60. */
  61. void on_wifi_connected(void) {
  62. // //request_weather();
  63. // 开启时间同步
  64. time_sync_start();
  65. }
  66. _Noreturn void app_main(void) {
  67. // esp_log_level_set("*", ESP_LOG_ERROR);
  68. // esp_log_level_set("wifi-setting", ESP_LOG_DEBUG);
  69. // esp_log_level_set("ac_controller", ESP_LOG_DEBUG);
  70. // esp_log_level_set("mqtt_client", ESP_LOG_DEBUG);
  71. // esp_log_level_set("UI", ESP_LOG_DEBUG);
  72. // esp_log_level_set("system_setting", ESP_LOG_DEBUG);
  73. // 初始化NVS
  74. esp_err_t err = nvs_flash_init(); //nvs初始化
  75. if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  76. ESP_ERROR_CHECK(nvs_flash_erase()); //擦除nvs
  77. nvs_flash_init(); //重新初始化
  78. }
  79. modbus_master_init(); // 初始化 modbus master
  80. ESP_ERROR_CHECK(esp_netif_init());
  81. read_system_setting(&system_setting); // 读取nvs中存储的系统设置
  82. heatpump_controller_init(&system_setting);
  83. iotx_event_regist_cb(&iot_event_monitor); // 注册网关事件
  84. beep_init(); // 蜂鸣器初始化
  85. ESP_ERROR_CHECK(lcd_st7701_init());
  86. // 如果上次为熄屏状态
  87. uint8_t *s_on_off = nvs_get_uint8(NVS_SCREEN_ON_OFF);
  88. if (s_on_off != NULL) {
  89. if (*s_on_off == 1) {
  90. ESP_ERROR_CHECK(lcd_st7701_backlight_off());
  91. } else {
  92. ESP_ERROR_CHECK(lcd_st7701_backlight_on());
  93. }
  94. } else {
  95. ESP_ERROR_CHECK(lcd_st7701_backlight_on());
  96. }
  97. setup_ui(&guider_ui);
  98. app_wifi_init(on_wifi_connected);
  99. sht30_init(); // 初始化温湿度传感器
  100. // 初始化OTA功能
  101. ota_init();
  102. // 熄屏定时器初始化
  103. screen_off_timer_init(&system_setting);
  104. for (;;) {
  105. vTaskDelay(10 / portTICK_PERIOD_MS);
  106. }
  107. }