Created
December 12, 2023 13:50
-
-
Save leyyce/c5b429f172d5278626633e9650a5853e to your computer and use it in GitHub Desktop.
IoT-Uebung4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include "esp_event.h" | |
#include "esp_wifi.h" | |
#include "esp_err.h" | |
#include "esp_log.h" | |
#include "nvs_flash.h" | |
#include "esp_http_client.h" | |
#define ESP_WIFI_SSID "LV-PK" | |
#define ESP_WIFI_PASS "wlMDsvlVKr6aAc0cQS" | |
static const char* WIFI_TAG = "WiFi"; | |
static const char* HTTP_TAG = "HTTP-Client"; | |
bool wifi_established; | |
// ESP_EVENT_DECLARE_BASE(TIME_RECIVED); | |
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { | |
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { | |
wifi_established = false; | |
ESP_LOGI(WIFI_TAG, "Connecting to the AP!"); | |
esp_wifi_connect(); | |
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { | |
wifi_established = false; | |
ESP_LOGI(WIFI_TAG, "Retrying to connect to the AP!"); | |
esp_wifi_connect(); | |
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP){ | |
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; | |
ESP_LOGI(WIFI_TAG, "Got IP:" IPSTR, IP2STR(&(event->ip_info.ip))); | |
wifi_established = true; | |
} else { | |
ESP_LOGI (WIFI_TAG , "Unhandled event (%s) with ID %d!" , event_base , event_id); | |
} | |
} | |
esp_err_t http_event_handler (esp_http_client_event_t* evt) { | |
switch ( evt -> event_id ) { | |
case HTTP_EVENT_ERROR : | |
ESP_LOGI (HTTP_TAG , " HTTP_EVENT_ERROR " ) ; | |
break; | |
case HTTP_EVENT_ON_CONNECTED : | |
ESP_LOGI (HTTP_TAG , " HTTP_EVENT_ON_CONNECTED " ) ; | |
break; | |
case HTTP_EVENT_HEADER_SENT : | |
ESP_LOGI (HTTP_TAG , " HTTP_EVENT_HEADER_SENT " ) ; | |
break; | |
case HTTP_EVENT_ON_HEADER : | |
ESP_LOGI (HTTP_TAG , " HTTP_EVENT_ON_HEADER " ) ; | |
printf ("%.*s" , evt -> data_len , ( char *) evt -> data ) ; | |
break; | |
case HTTP_EVENT_ON_DATA : | |
ESP_LOGI ( HTTP_TAG , " HTTP_EVENT_ON_DATA , len =%d" , evt -> data_len ) ; | |
if (!esp_http_client_is_chunked_response ( evt -> client ) ) | |
printf ( "%.*s" , evt -> data_len , ( char *) evt -> data ) ; | |
break ; | |
case HTTP_EVENT_ON_FINISH : | |
ESP_LOGI ( HTTP_TAG , " HTTP_EVENT_ON_FINISH " ) ; | |
break ; | |
case HTTP_EVENT_DISCONNECTED : | |
ESP_LOGI ( HTTP_TAG , " HTTP_EVENT_DISCONNECTED " ) ; | |
break ; | |
} | |
return ESP_OK ; | |
} | |
void wifi_init_sta() { | |
esp_netif_init(); | |
ESP_ERROR_CHECK(esp_event_loop_create_default()); | |
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); | |
ESP_ERROR_CHECK(esp_wifi_init(&config)); | |
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); | |
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL)); | |
// ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &wifi_event_handler, NULL)); | |
wifi_config_t wifi_config = { | |
.sta = { | |
.ssid = ESP_WIFI_SSID, | |
.password = ESP_WIFI_PASS, | |
}, | |
}; | |
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | |
esp_netif_create_default_wifi_sta(); | |
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); | |
ESP_ERROR_CHECK(esp_wifi_start()); | |
} | |
void app_main() { | |
esp_err_t ret = nvs_flash_init(); | |
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { | |
ESP_ERROR_CHECK(nvs_flash_erase()); | |
ret = nvs_flash_init(); | |
} | |
ESP_ERROR_CHECK(ret); | |
wifi_init_sta(); | |
while (!wifi_established) {} | |
esp_http_client_config_t config = { | |
.url = "http://worldtimeapi.org/api/timezone/Europe/Berlin.txt", | |
.event_handler = http_event_handler, | |
}; | |
esp_http_client_handle_t client ; | |
client = esp_http_client_init (& config ) ; | |
esp_err_t err = esp_http_client_perform ( client ) ; | |
if ( err == ESP_OK ) { | |
ESP_LOGI ( HTTP_TAG , " Status = %d , length = %d" , | |
esp_http_client_get_status_code ( client ) , | |
esp_http_client_get_content_length ( client ) ) ; | |
} | |
esp_http_client_cleanup ( client ) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment