Skip to content

Commit 2fa346b

Browse files
Merge branch 'fix/fix_lp_environment_sensor' into 'master'
fix: fix lp_environment_sensor example See merge request ae_group/esp-iot-solution!1161
2 parents 1aeb70a + 8af67c8 commit 2fa346b

File tree

10 files changed

+361
-133
lines changed

10 files changed

+361
-133
lines changed

examples/ulp/lp_cpu/lp_environment_sensor/components/app_epaper/app_epaper.c

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,30 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
557557
epaper_print_string(paper, 30, 78, "Humidity", 12, BLACK);
558558
epaper_draw_line(paper, 1, 92, 119, 92, BLACK);
559559

560+
#if CONFIG_REGION_INTERNATIONAL
561+
if (strstr(display_data.text, "Sunny") != NULL) {
562+
epaper_print_string(paper, 145, 5, "Sunny", 16, BLACK);
563+
epaper_display_picture(paper, 141, 25, 48, 48, SUNNY, WHITE);
564+
} else if (strstr(display_data.text, "Partly Cloudy") != NULL) {
565+
epaper_print_string(paper, 128, 5, "Partly Cloudy", 12, BLACK);
566+
epaper_display_picture(paper, 141, 25, 48, 48, PARTLY_CLOUDY, WHITE);
567+
} else if (strstr(display_data.text, "Cloudy") != NULL) {
568+
epaper_print_string(paper, 140, 5, "Cloudy", 16, BLACK);
569+
epaper_display_picture(paper, 141, 25, 48, 48, CLOUDY, WHITE);
570+
} else if (strstr(display_data.text, "Snowy") != NULL) {
571+
epaper_print_string(paper, 145, 5, "Snowy", 16, BLACK);
572+
epaper_display_picture(paper, 141, 25, 48, 48, SNOWY, WHITE);
573+
} else if (strstr(display_data.text, "Rain") != NULL) {
574+
epaper_print_string(paper, 150, 5, "Rain", 16, BLACK);
575+
epaper_display_picture(paper, 141, 25, 48, 48, RAIN, WHITE);
576+
} else if (strstr(display_data.text, "Haze") != NULL) {
577+
epaper_print_string(paper, 150, 5, "Haze", 16, BLACK);
578+
epaper_display_picture(paper, 141, 25, 48, 48, HAZE, WHITE);
579+
} else {
580+
epaper_print_string(paper, 140, 5, "No Wifi", 16, BLACK);
581+
epaper_display_picture(paper, 141, 25, 48, 48, NO_WEATHER, WHITE);
582+
}
583+
#else
560584
if (strstr(display_data.text, "晴") != NULL) {
561585
epaper_print_string(paper, 145, 5, "Sunny", 16, BLACK);
562586
epaper_display_picture(paper, 141, 25, 48, 48, SUNNY, WHITE);
@@ -572,10 +596,14 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
572596
} else if (strstr(display_data.text, "雨") != NULL) {
573597
epaper_print_string(paper, 150, 5, "Rain", 16, BLACK);
574598
epaper_display_picture(paper, 141, 25, 48, 48, RAIN, WHITE);
599+
} else if (strstr(display_data.text, "霾") != NULL) {
600+
epaper_print_string(paper, 150, 5, "Haze", 16, BLACK);
601+
epaper_display_picture(paper, 141, 25, 48, 48, HAZE, WHITE);
575602
} else {
576-
epaper_print_string(paper, 140, 5, "No data", 16, BLACK);
603+
epaper_print_string(paper, 140, 5, "No Wifi", 16, BLACK);
577604
epaper_display_picture(paper, 141, 25, 48, 48, NO_WEATHER, WHITE);
578605
}
606+
#endif
579607

580608
char high_temp[10];
581609
char low_temp[10];
@@ -585,9 +613,11 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
585613
strcat(temp_str, high_temp);
586614
strcat(temp_str, low_temp);
587615

616+
int city_len = strlen(display_data.city);
617+
int district_len = strlen(display_data.district);
588618
epaper_print_string(paper, 132, 75, temp_str, 16, BLACK);
589-
epaper_print_string(paper, 134, 92, display_data.city, 16, BLACK);
590-
epaper_print_string(paper, 140, 108, display_data.district, 16, BLACK);
619+
epaper_print_string(paper, 166 - (city_len * 4), 92, display_data.city, 16, BLACK);
620+
epaper_print_string(paper, 166 - (district_len * 4), 108, display_data.district, 16, BLACK);
591621

592622
epaper_draw_rectangle(paper, 122, 1, 210, 128, BLACK, 0);
593623

@@ -599,66 +629,78 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
599629
wind_speed[strlen(wind_speed) + 1] = '\0';
600630
epaper_print_string(paper, 214, 19, wind_speed, 12, BLACK);
601631

602-
if (strstr(display_data.wind_dir, "北风") != NULL) {
603-
epaper_print_string(paper, 214, 32, "Direction:N", 12, BLACK);
632+
#if CONFIG_REGION_INTERNATIONAL
633+
int wind_dir_len = strlen(display_data.wind_dir);
634+
epaper_print_string(paper, 254 - (wind_dir_len * 3), 32, display_data.wind_dir, 12, BLACK);
635+
#else
636+
char wind_direction[15] = {0};
637+
if (strstr(display_data.wind_dir, "西北风") != NULL) {
638+
strcat(wind_direction, "Northwest");
604639
} else if (strstr(display_data.wind_dir, "东北风") != NULL) {
605-
epaper_print_string(paper, 214, 32, "Direction:NE", 12, BLACK);
606-
} else if (strstr(display_data.wind_dir, "东风") != NULL) {
607-
epaper_print_string(paper, 214, 32, "Direction:E", 12, BLACK);
640+
strcat(wind_direction, "Northeast");
641+
} else if (strstr(display_data.wind_dir, "西南风") != NULL) {
642+
strcat(wind_direction, "Southwest");
608643
} else if (strstr(display_data.wind_dir, "东南风") != NULL) {
609-
epaper_print_string(paper, 214, 32, "Direction:SE", 12, BLACK);
644+
strcat(wind_direction, "Southeast");
645+
} else if (strstr(display_data.wind_dir, "北风") != NULL) {
646+
strcat(wind_direction, "North");
610647
} else if (strstr(display_data.wind_dir, "南风") != NULL) {
611-
epaper_print_string(paper, 214, 32, "Direction:S", 12, BLACK);
612-
} else if (strstr(display_data.wind_dir, "西南风") != NULL) {
613-
epaper_print_string(paper, 214, 32, "Direction:SW", 12, BLACK);
648+
strcat(wind_direction, "South");
614649
} else if (strstr(display_data.wind_dir, "西风") != NULL) {
615-
epaper_print_string(paper, 214, 32, "Direction:W", 12, BLACK);
616-
} else if (strstr(display_data.wind_dir, "西北风") != NULL) {
617-
epaper_print_string(paper, 214, 32, "Direction:NW", 12, BLACK);
650+
strcat(wind_direction, "West");
651+
} else if (strstr(display_data.wind_dir, "东风") != NULL) {
652+
strcat(wind_direction, "East");
618653
} else {
619-
epaper_print_string(paper, 214, 32, "Direction:..", 12, BLACK);
654+
strcat(wind_direction, "No data");
620655
}
656+
int wind_dir_len = strlen(wind_direction);
657+
epaper_print_string(paper, 254 - (wind_dir_len * 3), 32, wind_direction, 12, BLACK);
658+
#endif
621659

622660
epaper_draw_rectangle(paper, 212, 1, 296, 46, BLACK, 0);
623661

662+
#if CONFIG_REGION_INTERNATIONAL
663+
int week_len = strlen(display_data.week);
664+
epaper_print_string(paper, 254 - (week_len * 4), 48, display_data.week, 16, BLACK);
665+
#else
666+
char week_str[15] = {0};
624667
if (strstr(display_data.week, "一") != NULL) {
625-
epaper_print_string(paper, 230, 48, "Monday", 16, BLACK);
668+
strcat(week_str, "Monday");
626669
} else if (strstr(display_data.week, "二") != NULL) {
627-
epaper_print_string(paper, 226, 48, "Tuesday", 16, BLACK);
670+
strcat(week_str, "Tuesday");
628671
} else if (strstr(display_data.week, "三") != NULL) {
629-
epaper_print_string(paper, 218, 48, "Wednesday", 16, BLACK);
672+
strcat(week_str, "Wednesday");
630673
} else if (strstr(display_data.week, "四") != NULL) {
631-
epaper_print_string(paper, 226, 48, "Thursday", 16, BLACK);
674+
strcat(week_str, "Thursday");
632675
} else if (strstr(display_data.week, "五") != NULL) {
633-
epaper_print_string(paper, 230, 48, "Friday", 16, BLACK);
676+
strcat(week_str, "Friday");
634677
} else if (strstr(display_data.week, "六") != NULL) {
635-
epaper_print_string(paper, 222, 48, "Saturday", 16, BLACK);
678+
strcat(week_str, "Saturday");
636679
} else {
637-
epaper_print_string(paper, 230, 48, "Sunday", 16, BLACK);
680+
strcat(week_str, "Sunday");
638681
}
682+
int week_len = strlen(week_str);
683+
epaper_print_string(paper, 254 - (week_len * 4), 48, week_str, 16, BLACK);
684+
#endif
639685

640686
char uptime_str[12];
641687
uint8_t index = 0;
642688
for (int i = 0; i < 8; i++) {
643-
uptime_str[index] = display_data.uptime[i + 4];
689+
uptime_str[index] = display_data.uptime[i];
644690
index++;
645-
if (index == 2) {
646-
uptime_str[index] = '.';
647-
index++;
648-
}
649-
if (index == 5) {
650-
uptime_str[index] = ' ';
691+
if (index == 4) {
692+
uptime_str[index] = '/';
651693
index++;
652694
}
653-
if (index == 8) {
654-
uptime_str[index] = ':';
695+
if (index == 7) {
696+
uptime_str[index] = '/';
655697
index++;
656698
}
657-
if (index == 11) {
699+
if (index == 10) {
658700
uptime_str[index] = '\0';
659701
}
660702
}
661-
epaper_print_string(paper, 222, 66, uptime_str, 12, BLACK);
703+
epaper_print_string(paper, 225, 66, uptime_str, 12, BLACK);
662704
epaper_display_picture(paper, 236, 85, 36, 36, ESP_LOGO, WHITE);
663705
epaper_draw_rectangle(paper, 212, 48, 296, 128, BLACK, 0);
664706

examples/ulp/lp_cpu/lp_environment_sensor/components/app_epaper/include/esp_ths_image.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const unsigned char SNOWY[288] = {
3030
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0x7C, 0x1E, 0x1E, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0F, 0x0F, 0x1E, 0x1E, 0x7C, 0xF8, 0xF8, 0xE0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0x70, 0x70, 0x70, 0x7F, 0x7F, 0x3F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3F, 0x7F, 0x7F, 0x70, 0x70, 0x70, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0xF0, 0xFE, 0xFF, 0xFF, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xFF, 0xFF, 0xFC, 0xE0, 0x01, 0x0F, 0x1F, 0x3F, 0x7C, 0xF8, 0xF0, 0xE0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xF0, 0xF8, 0x7C, 0x3F, 0x1F, 0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x61, 0xF1, 0xF9, 0xF9, 0xF9, 0xF9, 0xF9, 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x21, 0xF1, 0xF9, 0xFD, 0xF9, 0xF9, 0xF9, 0xC1, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xF8, 0xFE, 0xFE, 0xFC, 0xFC, 0x7C, 0x20, 0x00, 0x01, 0x01, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x18, 0x78, 0x7E, 0xFE, 0xFE, 0xFE, 0x3E, 0x30, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3131
};
3232

33+
const unsigned char HAZE[288] = {
34+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x71, 0xF1, 0x71, 0x71, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x71, 0xF1, 0x71, 0x71, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xF1, 0xF1, 0xF1, 0x71, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x71, 0xF1, 0x71, 0x71, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x0E, 0x1E, 0x1F, 0x1E, 0x0C, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x0E, 0x1E, 0x1E, 0x0C, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x0E, 0x1E, 0x1E, 0x0E, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35+
};
36+
3337
const unsigned char NO_WEATHER[288] = {
3438
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF1, 0xF3, 0xE7, 0x4F, 0x1E, 0x3C, 0x78, 0xF2, 0xE6, 0xCF, 0x8F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x1F, 0x1E, 0x3E, 0x3E, 0x3C, 0x7C, 0x78, 0xF8, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x07, 0x03, 0x03, 0x01, 0xC0, 0xE0, 0xE0, 0xF0, 0xF8, 0x7D, 0x73, 0x27, 0x0F, 0x1F, 0x3C, 0x78, 0xF2, 0xE6, 0xCE, 0x9E, 0x1E, 0x1E, 0x1E, 0x3E, 0x3E, 0x7C, 0x7C, 0xF8, 0xF8, 0xF0, 0xE0, 0xC0, 0x01, 0x03, 0x03, 0x07, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xE0, 0xF0, 0xF8, 0xF8, 0x7C, 0x3C, 0x3C, 0x39, 0x33, 0x27, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xE0, 0x80, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xF0, 0xF8, 0xF8, 0xF0, 0x60, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0F, 0x0E, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3539
};

examples/ulp/lp_cpu/lp_environment_sensor/components/app_wifi/app_http.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <string.h>
78
#include "cJSON.h"
89
#include "esp_crt_bundle.h"
910
#include "esp_log.h"
@@ -107,13 +108,21 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
107108
return ESP_OK;
108109
}
109110

110-
esp_err_t http_rest_with_url(char *district_id, char *text, char *wind_class,
111-
char *wind_dir, char *uptime, char *week, int *high, int *low)
111+
esp_err_t get_http_weather_data_with_url(http_weather_data_t *weather_data)
112112
{
113+
esp_err_t ret = ESP_OK;
113114
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
114-
char weather_http_url[120] = "https://api.map.baidu.com/weather/v1/?district_id=";
115-
strcat(weather_http_url, district_id);
115+
char weather_http_url[120] = {0};
116+
snprintf(weather_data->district_id, sizeof(weather_data->district_id), "%s", CONFIG_CITY_DISTRICT_ID);
117+
#ifdef CONFIG_REGION_INTERNATIONAL
118+
strcat(weather_http_url, "https://api.map.baidu.com/weather_abroad/v1/?district_id=");
119+
strcat(weather_http_url, weather_data->district_id);
120+
strcat(weather_http_url, "&data_type=all&language=en&ak=uKBj61fKPRDbXzv5w3ecFaVove3ZqwlT");
121+
#elif CONFIG_REGION_CHINA
122+
strcat(weather_http_url, "https://api.map.baidu.com/weather/v1/?district_id=");
123+
strcat(weather_http_url, weather_data->district_id);
116124
strcat(weather_http_url, "&data_type=all&ak=uKBj61fKPRDbXzv5w3ecFaVove3ZqwlT");
125+
#endif
117126
esp_http_client_config_t config = {
118127
.url = weather_http_url,
119128
.event_handler = _http_event_handler,
@@ -122,29 +131,44 @@ esp_err_t http_rest_with_url(char *district_id, char *text, char *wind_class,
122131
};
123132
esp_http_client_handle_t client = esp_http_client_init(&config);
124133

125-
esp_http_client_perform(client);
134+
ret = esp_http_client_perform(client);
135+
if (ret != ESP_OK) {
136+
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(ret));
137+
return ret;
138+
}
126139

127140
cJSON *root = cJSON_Parse(local_response_buffer);
128141
cJSON *result = cJSON_GetObjectItem(root, "result");
142+
cJSON *location = cJSON_GetObjectItem(result, "location");
129143
cJSON *now = cJSON_GetObjectItem(result, "now");
130144
cJSON *forecasts = cJSON_GetObjectItem(result, "forecasts");
131145
cJSON *first_forecast = cJSON_GetArrayItem(forecasts, 0);
132146

147+
char *pre_city = cJSON_GetObjectItem(location, "city")->valuestring;
148+
char *pre_name = cJSON_GetObjectItem(location, "name")->valuestring;
133149
char *pre_text = cJSON_GetObjectItem(now, "text")->valuestring;
134150
char *pre_wind_class = cJSON_GetObjectItem(now, "wind_class")->valuestring;
135151
char *pre_wind_dir = cJSON_GetObjectItem(now, "wind_dir")->valuestring;
136152
char *pre_uptime = cJSON_GetObjectItem(now, "uptime")->valuestring;
137-
*high = cJSON_GetObjectItem(first_forecast, "high")->valueint;
138-
*low = cJSON_GetObjectItem(first_forecast, "low")->valueint;
139153
char *pre_week = cJSON_GetObjectItem(first_forecast, "week")->valuestring;
154+
int pre_high = cJSON_GetObjectItem(first_forecast, "high")->valueint;
155+
int pre_low = cJSON_GetObjectItem(first_forecast, "low")->valueint;
140156

141-
memcpy(text, pre_text, strlen(pre_text));
142-
memcpy(wind_class, pre_wind_class, strlen(pre_wind_class));
143-
memcpy(wind_dir, pre_wind_dir, strlen(pre_wind_dir));
144-
memcpy(uptime, pre_uptime, strlen(pre_uptime));
145-
memcpy(week, pre_week, strlen(pre_week));
157+
snprintf(weather_data->city_name, sizeof(weather_data->city_name), "%s", pre_city);
158+
snprintf(weather_data->district_name, sizeof(weather_data->district_name), "%s", pre_name);
159+
snprintf(weather_data->text, sizeof(weather_data->text), "%s", pre_text);
160+
snprintf(weather_data->wind_class, sizeof(weather_data->wind_class), "%s", pre_wind_class);
161+
snprintf(weather_data->wind_dir, sizeof(weather_data->wind_dir), "%s", pre_wind_dir);
162+
snprintf(weather_data->uptime, sizeof(weather_data->uptime), "%s", pre_uptime);
163+
snprintf(weather_data->week, sizeof(weather_data->week), "%s", pre_week);
164+
weather_data->temp_high = pre_high;
165+
weather_data->temp_low = pre_low;
146166

147167
cJSON_Delete(root);
148-
esp_http_client_cleanup(client);
168+
ret = esp_http_client_cleanup(client);
169+
if (ret != ESP_OK) {
170+
ESP_LOGE(TAG, "HTTP cleanup failed: %s", esp_err_to_name(ret));
171+
return ret;
172+
}
149173
return ESP_OK;
150174
}

0 commit comments

Comments
 (0)