Code Crashing when Requesting Data from OpenWeatherMap
Posted: Fri Jul 07, 2023 6:50 pm
Hello Everyone,
I am new to ESP32 and wanted to write a simple application to get weather information from the OpenWeatherMap website.
It works fine for the first time, but when I try to request the data next time, my program crashes and restarts with the following message.
The following is the piece of the code for reference, and it is crashing when "openweathermap_send_request" is called second time, and more specifically while calling the function "esp_err_t err = esp_http_client_perform(client);"
Logs on Terminal when code crashed.
Any sort of help is appreciated.
Thanks, and Regards
I am new to ESP32 and wanted to write a simple application to get weather information from the OpenWeatherMap website.
It works fine for the first time, but when I try to request the data next time, my program crashes and restarts with the following message.
- Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
- // OpenWeatherMap Task
- static void openweathermap_task(void *pvParameters)
- {
- // Initialize the City Names
- strcpy(city_weather[0].city_name, "manali");
- strcpy(city_weather[1].city_name, "kashmir");
- strcpy(city_weather[2].city_name, "jaipur");
- strcpy(city_weather[3].city_name, "delhi");
- for( ; ; )
- {
- openweathermap_send_request();
- vTaskDelay(HTTP_REQ_EXEC_RATE / portTICK_PERIOD_MS);
- }
- vTaskDelete(NULL);
- }
- static void openweathermap_send_request(void)
- {
- char openweathermap_url[200];
- city_weather_index = 0; // added for debugging
- snprintf( openweathermap_url, sizeof(openweathermap_url), \
- "%s%s%s", CLIENT_REQ_PRE, city_weather[city_weather_index].city_name, CLIENT_REQ_POST);
- esp_http_client_config_t config =
- {
- .url = openweathermap_url,
- .method = HTTP_METHOD_GET,
- .event_handler = openweathermap_event_handler,
- };
- ESP_LOGI(TAG, "OpenWeatherMap Task Execution Started");
- esp_http_client_handle_t client = esp_http_client_init(&config);
- esp_http_client_set_header(client, CLIENT_KEY, CLIENT_VALUE);
- esp_err_t err = esp_http_client_perform(client);
- if( err == ESP_OK )
- {
- int status = esp_http_client_get_status_code(client);
- if(status == 200)
- {
- ESP_LOGI(TAG, "City=%s, Message Sent Successfully", city_weather[city_weather_index].city_name);
- city_weather_index++;
- // Reset back to Initial Position
- if( city_weather_index >= NUM_OF_CITIES )
- {
- city_weather_index = 0;
- }
- }
- else
- {
- ESP_LOGI(TAG, "City=%s, Message Sent Failed", city_weather[city_weather_index].city_name);
- }
- }
- else
- {
- ESP_LOGI(TAG, "City=%s, Message Sent Failed", city_weather[city_weather_index].city_name);
- }
- esp_http_client_cleanup(client);
- ESP_LOGI(TAG, "OpenWeatherMap Task Execution Complete");
- }
- static esp_err_t openweathermap_event_handler(esp_http_client_event_t *event)
- {
- switch(event->event_id)
- {
- case HTTP_EVENT_ON_DATA:
- // Resize the Buffer to fit the new chunk of the data
- response_data = realloc(response_data, response_len + event->data_len);
- // Copy the Data
- memcpy(response_data+response_len, event->data, event->data_len);
- // Update the Length
- response_len += event->data_len;
- // Only used for debugging
- // ESP_LOGI("OpenWeatherAPI", "Partial Received Data: %d", event->data_len);
- break;
- case HTTP_EVENT_ON_FINISH:
- all_data_received = true;
- // NOTE: TAG is different here (debugging purpose)
- // ESP_LOGI("OpenWeatherAPI", "Received Data: %s", response_data);
- // Decode/Parse the weather data from the response data
- openweathermap_get_weather(response_data, &city_weather[0]);
- // free up the space, and reset the response length
- free(response_data);
- response_len = 0;
- ESP_LOGI("OpenWeatherAPI", "City=%s, Temp=%f, Pressure=%d, Humidity=%d", \
- city_weather[city_weather_index].city_name, \
- city_weather[city_weather_index].temperature, \
- city_weather[city_weather_index].pressure, \
- city_weather[city_weather_index].humidity);
- ESP_LOGI("OpenWeatherAPI", "Exiting Event Handler");
- break;
- default:
- break;
- }
- return ESP_OK;
- }
- I (27940) WIFI: OpenWeatherMap Task Execution Complete
- I (30920) WIFI: Hello World from Main Task
- I (32940) WIFI: OpenWeatherMap Task Execution Started
- I (35920) WIFI: Hello World from Main Task
- I (35970) OpenWeatherAPI: City=manali, Temp=30.040001, Pressure=1007, Humidity=82
- I (35970) OpenWeatherAPI: Exiting Event Handler
- I (35970) WIFI: City=manali, Message Sent Successfully
- I (35980) WIFI: OpenWeatherMap Task Execution Complete
- I (40920) WIFI: Hello World from Main Task
- I (40980) WIFI: OpenWeatherMap Task Execution Started
- Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Thanks, and Regards