diff --git a/main/main.c b/main/main.c
index 24bafb4..6324f01 100644
--- a/main/main.c
+++ b/main/main.c
@@ -2,20 +2,46 @@
 #include "esp_wifi.h"
 #include "esp_system.h"
 #include "esp_event.h"
+#if ESP_IDF_VERSION_MAJOR < 4
 #include "esp_event_loop.h"
+#endif
 #include "nvs_flash.h"
 #include "driver/gpio.h"
 
+#if ESP_IDF_VERSION_MAJOR >= 4
+
+static void wifi_event_handler(void* arg, esp_event_base_t event_base,
+                                int32_t event_id, void* event_data)
+{
+}
+
+static void ip_event_handler(void* arg, esp_event_base_t event_base,
+                                int32_t event_id, void* event_data)
+{
+}
+
+#else
+
 esp_err_t event_handler(void *ctx, system_event_t *event)
 {
     return ESP_OK;
 }
 
+#endif
+
 void app_main(void)
 {
     nvs_flash_init();
     tcpip_adapter_init();
+
+#if ESP_IDF_VERSION_MAJOR >= 4
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
+    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, ESP_EVENT_ANY_ID, &ip_event_handler, NULL));
+#else
     ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
+#endif
+
     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );