Skip to content

Commit 8af67c8

Browse files
feat: update the HTTP weather information retrieval API
Fix the issue of getting stuck when the network connection fails.
1 parent bdb894d commit 8af67c8

File tree

7 files changed

+237
-111
lines changed

7 files changed

+237
-111
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ 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_OVERSEAS
560+
#if CONFIG_REGION_INTERNATIONAL
561561
if (strstr(display_data.text, "Sunny") != NULL) {
562562
epaper_print_string(paper, 145, 5, "Sunny", 16, BLACK);
563563
epaper_display_picture(paper, 141, 25, 48, 48, SUNNY, WHITE);
@@ -577,7 +577,7 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
577577
epaper_print_string(paper, 150, 5, "Haze", 16, BLACK);
578578
epaper_display_picture(paper, 141, 25, 48, 48, HAZE, WHITE);
579579
} else {
580-
epaper_print_string(paper, 140, 5, "No data", 16, BLACK);
580+
epaper_print_string(paper, 140, 5, "No Wifi", 16, BLACK);
581581
epaper_display_picture(paper, 141, 25, 48, 48, NO_WEATHER, WHITE);
582582
}
583583
#else
@@ -600,7 +600,7 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
600600
epaper_print_string(paper, 150, 5, "Haze", 16, BLACK);
601601
epaper_display_picture(paper, 141, 25, 48, 48, HAZE, WHITE);
602602
} else {
603-
epaper_print_string(paper, 140, 5, "No data", 16, BLACK);
603+
epaper_print_string(paper, 140, 5, "No Wifi", 16, BLACK);
604604
epaper_display_picture(paper, 141, 25, 48, 48, NO_WEATHER, WHITE);
605605
}
606606
#endif
@@ -629,7 +629,7 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
629629
wind_speed[strlen(wind_speed) + 1] = '\0';
630630
epaper_print_string(paper, 214, 19, wind_speed, 12, BLACK);
631631

632-
#if CONFIG_REGION_OVERSEAS
632+
#if CONFIG_REGION_INTERNATIONAL
633633
int wind_dir_len = strlen(display_data.wind_dir);
634634
epaper_print_string(paper, 254 - (wind_dir_len * 3), 32, display_data.wind_dir, 12, BLACK);
635635
#else
@@ -659,7 +659,7 @@ esp_err_t epaper_display(epaper_handle_t paper, epaper_display_data_t display_da
659659

660660
epaper_draw_rectangle(paper, 212, 1, 296, 46, BLACK, 0);
661661

662-
#if CONFIG_REGION_OVERSEAS
662+
#if CONFIG_REGION_INTERNATIONAL
663663
int week_len = strlen(display_data.week);
664664
epaper_print_string(paper, 254 - (week_len * 4), 48, display_data.week, 16, BLACK);
665665
#else

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,19 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
108108
return ESP_OK;
109109
}
110110

111-
esp_err_t http_rest_with_url(char *district_id, char *city, char *name, char *text, char *wind_class,
112-
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)
113112
{
113+
esp_err_t ret = ESP_OK;
114114
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
115115
char weather_http_url[120] = {0};
116-
#ifdef CONFIG_REGION_OVERSEAS
116+
snprintf(weather_data->district_id, sizeof(weather_data->district_id), "%s", CONFIG_CITY_DISTRICT_ID);
117+
#ifdef CONFIG_REGION_INTERNATIONAL
117118
strcat(weather_http_url, "https://api.map.baidu.com/weather_abroad/v1/?district_id=");
118-
strcat(weather_http_url, district_id);
119+
strcat(weather_http_url, weather_data->district_id);
119120
strcat(weather_http_url, "&data_type=all&language=en&ak=uKBj61fKPRDbXzv5w3ecFaVove3ZqwlT");
120-
#elif CONFIG_REGION_DOMESTIC
121+
#elif CONFIG_REGION_CHINA
121122
strcat(weather_http_url, "https://api.map.baidu.com/weather/v1/?district_id=");
122-
strcat(weather_http_url, district_id);
123+
strcat(weather_http_url, weather_data->district_id);
123124
strcat(weather_http_url, "&data_type=all&ak=uKBj61fKPRDbXzv5w3ecFaVove3ZqwlT");
124125
#endif
125126
esp_http_client_config_t config = {
@@ -130,7 +131,11 @@ esp_err_t http_rest_with_url(char *district_id, char *city, char *name, char *te
130131
};
131132
esp_http_client_handle_t client = esp_http_client_init(&config);
132133

133-
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+
}
134139

135140
cJSON *root = cJSON_Parse(local_response_buffer);
136141
cJSON *result = cJSON_GetObjectItem(root, "result");
@@ -145,19 +150,25 @@ esp_err_t http_rest_with_url(char *district_id, char *city, char *name, char *te
145150
char *pre_wind_class = cJSON_GetObjectItem(now, "wind_class")->valuestring;
146151
char *pre_wind_dir = cJSON_GetObjectItem(now, "wind_dir")->valuestring;
147152
char *pre_uptime = cJSON_GetObjectItem(now, "uptime")->valuestring;
148-
*high = cJSON_GetObjectItem(first_forecast, "high")->valueint;
149-
*low = cJSON_GetObjectItem(first_forecast, "low")->valueint;
150153
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;
151156

152-
memcpy(city, pre_city, strlen(pre_city));
153-
memcpy(name, pre_name, strlen(pre_name));
154-
memcpy(text, pre_text, strlen(pre_text));
155-
memcpy(wind_class, pre_wind_class, strlen(pre_wind_class));
156-
memcpy(wind_dir, pre_wind_dir, strlen(pre_wind_dir));
157-
memcpy(uptime, pre_uptime, strlen(pre_uptime));
158-
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;
159166

160167
cJSON_Delete(root);
161-
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+
}
162173
return ESP_OK;
163174
}

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040

4141
ESP_EVENT_DEFINE_BASE(APP_WIFI_EVENT);
4242
static const char *TAG = "app_wifi";
43-
static const int WIFI_CONNECTED_EVENT = BIT0;
44-
static int s_retry_num = 0;
45-
static EventGroupHandle_t wifi_event_group;
46-
static EventGroupHandle_t second_wifi_event_group = NULL;
43+
44+
static EventGroupHandle_t wifi_event_group = NULL;
45+
46+
#define EXAMPLE_ESP_MAXIMUM_RETRY 3
47+
#define WIFI_CONNECT_SUCCESS_EVENT BIT0
48+
#define WIFI_CONNECT_FAIL_EVENT BIT1
4749

4850
#define PROV_QR_VERSION "v1"
4951

@@ -153,6 +155,7 @@ static void app_wifi_print_qr(const char *name, const char *pop, const char *tra
153155
static void event_handler(void* arg, esp_event_base_t event_base,
154156
int32_t event_id, void* event_data)
155157
{
158+
static int retry_cnt = 0;
156159
#ifdef CONFIG_APP_WIFI_RESET_PROV_ON_FAILURE
157160
static int retries = 0;
158161
#endif
@@ -219,11 +222,17 @@ static void event_handler(void* arg, esp_event_base_t event_base,
219222
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
220223
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
221224
ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip));
225+
retry_cnt = 0;
222226
/* Signal main application to continue execution */
223-
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT);
227+
xEventGroupSetBits(wifi_event_group, WIFI_CONNECT_SUCCESS_EVENT);
224228
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
225229
ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
226-
esp_wifi_connect();
230+
retry_cnt++;
231+
if (retry_cnt >= EXAMPLE_ESP_MAXIMUM_RETRY) {
232+
xEventGroupSetBits(wifi_event_group, WIFI_CONNECT_FAIL_EVENT);
233+
} else {
234+
esp_wifi_connect();
235+
}
227236
}
228237
}
229238

@@ -415,6 +424,7 @@ esp_err_t app_wifi_start_timer(void)
415424

416425
esp_err_t app_first_time_wifi_start(app_wifi_pop_type_t pop_type)
417426
{
427+
esp_err_t err = ESP_OK;
418428
/* Configuration for the provisioning manager */
419429
wifi_prov_mgr_config_t config = {
420430
/* What is the Provisioning Scheme that we want ?
@@ -547,35 +557,47 @@ esp_err_t app_first_time_wifi_start(app_wifi_pop_type_t pop_type)
547557
custom_mfg_data_len = 0;
548558
}
549559
/* Wait for Wi-Fi connection */
550-
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
551-
return ESP_OK;
560+
EventBits_t uxBits;
561+
uxBits = xEventGroupWaitBits(wifi_event_group,
562+
WIFI_CONNECT_SUCCESS_EVENT | WIFI_CONNECT_FAIL_EVENT,
563+
pdFALSE,
564+
pdFALSE,
565+
portMAX_DELAY);
566+
if (uxBits & WIFI_CONNECT_SUCCESS_EVENT) {
567+
ESP_LOGI(TAG, "Wi-Fi connected successfully!");
568+
err = ESP_OK;
569+
} else if (uxBits & WIFI_CONNECT_FAIL_EVENT) {
570+
ESP_LOGE(TAG, "Wi-Fi connection failed!");
571+
err = ESP_FAIL;
572+
}
573+
return err;
552574
}
553575

554576
static void second_time_event_handler(void* arg, esp_event_base_t event_base,
555577
int32_t event_id, void* event_data)
556578
{
579+
static int retry_cnt = 0;
557580
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
558581
esp_wifi_connect();
559582
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
560-
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
583+
if (retry_cnt < EXAMPLE_ESP_MAXIMUM_RETRY) {
561584
esp_wifi_connect();
562-
s_retry_num++;
585+
retry_cnt++;
563586
ESP_LOGI(TAG, "retry to connect to the AP");
564587
} else {
565-
xEventGroupSetBits(second_wifi_event_group, WIFI_FAIL_BIT);
588+
xEventGroupSetBits(wifi_event_group, WIFI_CONNECT_FAIL_EVENT);
566589
}
567-
ESP_LOGI(TAG, "connect to the AP fail");
568590
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
569591
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
570592
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
571-
s_retry_num = 0;
572-
xEventGroupSetBits(second_wifi_event_group, WIFI_CONNECTED_BIT);
593+
retry_cnt = 0;
594+
xEventGroupSetBits(wifi_event_group, WIFI_CONNECT_SUCCESS_EVENT);
573595
}
574596
}
575597

576598
void app_second_time_wifi_init(void)
577599
{
578-
second_wifi_event_group = xEventGroupCreate();
600+
wifi_event_group = xEventGroupCreate();
579601

580602
ESP_ERROR_CHECK(esp_netif_init());
581603

@@ -630,21 +652,21 @@ esp_err_t app_second_time_wifi_start(void)
630652
esp_wifi_set_ps(WIFI_PS_NONE);
631653
ESP_LOGI(TAG, "wifi_init_sta finished.");
632654

633-
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
655+
/* Waiting until either the connection is established (WIFI_CONNECT_SUCCESS_EVENT) or connection failed for the maximum
634656
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
635-
EventBits_t bits = xEventGroupWaitBits(second_wifi_event_group,
636-
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
657+
EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
658+
WIFI_CONNECT_SUCCESS_EVENT | WIFI_CONNECT_FAIL_EVENT,
637659
pdFALSE,
638660
pdFALSE,
639661
portMAX_DELAY);
640662

641663
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
642664
* happened. */
643-
if (bits & WIFI_CONNECTED_BIT) {
665+
if (bits & WIFI_CONNECT_SUCCESS_EVENT) {
644666
ret = ESP_OK;
645667
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
646668
wifi_config.sta.ssid, wifi_config.sta.password);
647-
} else if (bits & WIFI_FAIL_BIT) {
669+
} else if (bits & WIFI_CONNECT_FAIL_EVENT) {
648670
ret = ESP_FAIL;
649671
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
650672
wifi_config.sta.ssid, wifi_config.sta.password);

examples/ulp/lp_cpu/lp_environment_sensor/components/app_wifi/include/app_http.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,33 @@ extern "C" {
1212

1313
#define MAX_HTTP_OUTPUT_BUFFER 2048
1414

15-
esp_err_t http_rest_with_url(char *district_id, char *city, char *name, char *text, char *wind_class,
16-
char *wind_dir, char *uptime, char *week, int *high, int *low);
15+
typedef struct {
16+
char district_id[20]; // Unique identifier for the district
17+
char city_name[10]; // City name for weather information retrieval
18+
char district_name[10]; // District name for weather information retrieval
19+
char text[10]; // Weather description (e.g., sunny, cloudy)
20+
char wind_class[10]; // Classification of wind speed
21+
char wind_dir[15]; // Wind direction (e.g., north, south)
22+
char uptime[15]; // Timestamp of the last weather data update
23+
char week[10]; // Day of the week (e.g., Monday, Tuesday)
24+
int temp_high; // High temperature of the day in Celsius
25+
int temp_low; // Low temperature of the day in Celsius
26+
} http_weather_data_t;
27+
28+
/**
29+
* @brief Retrieve weather data via HTTP request using the provided URL
30+
*
31+
* This function sends an HTTP request to the specified URL to retrieve weather data and stores the
32+
* data in the provided `weather_data` structure.
33+
*
34+
* @param[in] weather_data Pointer to an `http_weather_data_t` structure where the retrieved weather data will be stored
35+
*
36+
* @return
37+
* - ESP_OK: Successfully retrieved and stored weather data
38+
* - ESP_FAIL on error
39+
*/
40+
esp_err_t get_http_weather_data_with_url(http_weather_data_t *weather_data);
41+
1742
#ifdef __cplusplus
1843
}
1944
#endif

examples/ulp/lp_cpu/lp_environment_sensor/components/app_wifi/include/app_wifi.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ extern "C" {
2525

2626
#define MFG_DATA_DEVICE_EXTRA_CODE 0x00
2727

28-
#define EXAMPLE_ESP_MAXIMUM_RETRY 3
29-
#define WIFI_CONNECTED_BIT BIT0
30-
#define WIFI_FAIL_BIT BIT1
31-
3228
/** ESP RainMaker Event Base */
3329
ESP_EVENT_DECLARE_BASE(APP_WIFI_EVENT);
3430

examples/ulp/lp_cpu/lp_environment_sensor/main/Kconfig.projbuild

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ menu "LP Environment Sensor Configuration"
2626

2727
choice
2828
prompt "Select Region Type"
29-
default REGION_DOMESTIC
29+
default REGION_CHINA
3030
depends on UPDATE_WEATHER_DATA
3131

32-
config REGION_OVERSEAS
33-
bool "Overseas"
32+
config REGION_INTERNATIONAL
33+
bool "International"
3434

35-
config REGION_DOMESTIC
36-
bool "Domestic"
35+
config REGION_CHINA
36+
bool "China"
3737
endchoice
3838

3939
config CITY_DISTRICT_ID
@@ -43,16 +43,16 @@ endchoice
4343
help
4444
Specify the district ID to get weather data from HTTP.
4545

46-
config DOMESTIC_CITY_NAME
47-
string "Domestic City Name"
48-
depends on REGION_DOMESTIC
46+
config CHINA_CITY_NAME
47+
string "China City Name"
48+
depends on REGION_CHINA
4949
default "Shanghai"
5050
help
5151
Specify the name of the domestic city (e.g., Shanghai).
5252

53-
config DOMESTIC_DISTRICT_NAME
54-
string "Domestic District Name"
55-
depends on REGION_DOMESTIC
53+
config CHINA_DISTRICT_NAME
54+
string "China District Name"
55+
depends on REGION_CHINA
5656
default "Pudong"
5757
help
5858
Specify the name of the domestic district (e.g., Pudong).

0 commit comments

Comments
 (0)