weather.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @Author: 李建
  3. * @Date: 2025/5/22 14:40
  4. * Description: 接入 wifi 后,实现请求天气接口并显示到 UI界面
  5. * Copyright: Copyright (©) 2025 永续绿建. All rights reserved.
  6. */
  7. #include <cJSON.h>
  8. #include <esp_log.h>
  9. #include "weather.h"
  10. #include "system/net_http.h"
  11. #include "system/miscellaneous_interface.h"
  12. #include "lvgl_port.h"
  13. #include "main.h"
  14. static const char * TAG = "weather";
  15. static void http_register_callback(char *response, void *user_data) {
  16. ESP_LOGD(TAG, "http_register_callback");
  17. if (response != NULL) {
  18. ESP_LOGD(TAG, "http_register_callback1:%s", response);
  19. cJSON * root = cJSON_Parse(response);
  20. cJSON *code = cJSON_GetObjectItemCaseSensitive(root, "code");
  21. if(code != NULL && code->valueint == 0) {
  22. ESP_LOGD(TAG, "http_register_callback2");
  23. cJSON *data = cJSON_GetObjectItem(root, "data");
  24. cJSON *text = cJSON_GetObjectItemCaseSensitive(data, "text");
  25. cJSON *temperature = cJSON_GetObjectItemCaseSensitive(data, "temperature");
  26. cJSON *humidity = cJSON_GetObjectItemCaseSensitive(data, "humidity");
  27. ESP_LOGD(TAG, "%s", text->valuestring);
  28. if(guider_ui.screen)
  29. {
  30. // lvgl_port_lock(-1);
  31. // lv_obj_remove_flag(guider_ui.screen_lab_weather_temp, LV_OBJ_FLAG_HIDDEN);
  32. // lv_obj_remove_flag(guider_ui.screen_labe_weather_name, LV_OBJ_FLAG_HIDDEN);
  33. // lv_obj_remove_flag(guider_ui.screen_lab_weather_hum, LV_OBJ_FLAG_HIDDEN);
  34. // lv_label_set_text_static(guider_ui.screen_labe_weather_name, text->valuestring);
  35. // lv_label_set_text_fmt(guider_ui.screen_lab_weather_temp, "%s°C", temperature->valuestring);
  36. // lv_label_set_text_fmt(guider_ui.screen_lab_weather_hum, "%s%%", humidity->valuestring);
  37. // lvgl_port_unlock();
  38. }
  39. }
  40. }
  41. }
  42. void request_weather() {
  43. char device_serial[16];
  44. get_device_serial(device_serial);
  45. char url[128] = "";
  46. sprintf(url, "%s/%s", API_URL, device_serial);
  47. net_http_config_t config = {
  48. .callback = http_register_callback,
  49. .url = url,
  50. };
  51. net_http_get(config);
  52. }