diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6cb0774..8394b05 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -60,10 +60,11 @@ build_esp8266_demo: - chmod 600 ~/.ssh/id_rsa - echo -e "Host gitlab.espressif.cn\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config - git --version - - git clone --recursive --depth 1 $GITLAB_SSH_SERVER/sdk/ESP8266_RTOS_SDK.git + - git clone -b v3.3 --recursive --depth 1 $GITLAB_SSH_SERVER/sdk/ESP8266_RTOS_SDK.git - cd ESP8266_RTOS_SDK - source tools/ci/configure_ci_environment.sh - cd .. + - git submodule update --init --recursive - export IDF_PATH=$CI_PROJECT_DIR/ESP8266_RTOS_SDK - cd examples/iothub_client_sample_mqtt - make defconfig diff --git a/README.md b/README.md index 2d77471..43db37a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,18 @@ - [Monitoring Results](#monitoring) - [Troubleshooting](#troubleshooting) +## 2021 Update + + + +Since this library has been published, Microsoft has created newer versions of the Azure SDK for usage with the Espressif ESP32. This new library is better suited for microcontrollers, great for composition with your own network stack and officially supported by Microsoft. + +The first one, [Azure IoT middleware for FreeRTOS](https://github.com/Azure/azure-iot-middleware-freertos), is based on ESP-IDF and FreeRTOS and it has [samples](https://github.com/Azure-Samples/iot-middleware-freertos-samples) for IoT Hub and IoT Central using the device provisioning service (DPS). + +The second one is based on [Azure IoT for C library for Arduino](https://github.com/Azure/azure-sdk-for-c-arduino) and also has samples for IoT Hub. + +If you can, **avoid using this library for any new projects**. + ## Introduction diff --git a/component.mk b/component.mk index 6099655..1709535 100644 --- a/component.mk +++ b/component.mk @@ -173,7 +173,7 @@ COMPONENT_SRCDIRS += azure-iot-sdk-c/certs endif CFLAGS += -Wno-unused-function -Wno-missing-braces -Wno-missing-field-initializers -DHSM_TYPE_X509 -DHSM_TYPE_SAS_TOKEN -DHSM_TYPE_SYMM_KEY -CFLAGS += -Wno-error=maybe-uninitialized -Wno-error=char-subscripts +CFLAGS += -Wno-error=maybe-uninitialized -Wno-error=char-subscripts -Wno-unknown-pragmas ifdef CONFIG_DEVICE_COMMON_NAME CFLAGS += -DUSE_PROV_MODULE diff --git a/examples/iothub_client_sample_mqtt/main/azure_main.c b/examples/iothub_client_sample_mqtt/main/azure_main.c index 1097d30..1e2290f 100644 --- a/examples/iothub_client_sample_mqtt/main/azure_main.c +++ b/examples/iothub_client_sample_mqtt/main/azure_main.c @@ -14,7 +14,11 @@ #include "esp_system.h" #include "esp_system.h" #include "esp_wifi.h" +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) #include "esp_event_loop.h" +#else +#include "esp_event.h" +#endif #include "esp_log.h" #include "nvs_flash.h" @@ -35,6 +39,7 @@ const int CONNECTED_BIT = BIT0; static const char *TAG = "azure"; +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { @@ -55,12 +60,37 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) } return ESP_OK; } +#else +static void 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) { + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + /* This is a workaround as ESP platform WiFi libs don't currently + auto-reassociate. */ + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + } +} +#endif static void initialise_wifi(void) { +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); +#else + ESP_ERROR_CHECK( esp_netif_init() ); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_create_default() ); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); +#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) ); @@ -71,6 +101,13 @@ static void initialise_wifi(void) }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); + +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) +#else + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) ); +#endif + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); diff --git a/examples/iothub_devicetwin_samples_and_methods/main/azure_main.c b/examples/iothub_devicetwin_samples_and_methods/main/azure_main.c index bfbe9d3..b192827 100644 --- a/examples/iothub_devicetwin_samples_and_methods/main/azure_main.c +++ b/examples/iothub_devicetwin_samples_and_methods/main/azure_main.c @@ -14,7 +14,11 @@ #include "esp_system.h" #include "esp_system.h" #include "esp_wifi.h" +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) #include "esp_event_loop.h" +#else +#include "esp_event.h" +#endif #include "esp_log.h" #include "nvs_flash.h" @@ -37,6 +41,7 @@ static const char *TAG = "azure"; extern int iothhub_devicetwin_init(void); +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { @@ -57,12 +62,37 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) } return ESP_OK; } +#else +static void 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) { + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + /* This is a workaround as ESP platform WiFi libs don't currently + auto-reassociate. */ + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + } +} +#endif static void initialise_wifi(void) { +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); +#else + ESP_ERROR_CHECK( esp_netif_init() ); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_create_default() ); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); +#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) ); @@ -73,6 +103,13 @@ static void initialise_wifi(void) }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); + +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) +#else + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) ); +#endif + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); diff --git a/examples/prov_dev_client_ll_sample/main/azure_main.c b/examples/prov_dev_client_ll_sample/main/azure_main.c index 158d8f0..f700115 100644 --- a/examples/prov_dev_client_ll_sample/main/azure_main.c +++ b/examples/prov_dev_client_ll_sample/main/azure_main.c @@ -14,7 +14,11 @@ #include "esp_system.h" #include "esp_system.h" #include "esp_wifi.h" +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) #include "esp_event_loop.h" +#else +#include "esp_event.h" +#endif #include "esp_log.h" #include "nvs_flash.h" @@ -35,6 +39,7 @@ const int CONNECTED_BIT = BIT0; static const char *TAG = "azure"; +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { @@ -55,12 +60,37 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) } return ESP_OK; } +#else +static void 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) { + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + /* This is a workaround as ESP platform WiFi libs don't currently + auto-reassociate. */ + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + } +} +#endif static void initialise_wifi(void) { +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); +#else + ESP_ERROR_CHECK( esp_netif_init() ); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_create_default() ); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); +#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) ); @@ -71,6 +101,13 @@ static void initialise_wifi(void) }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); + +#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)) +#else + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) ); +#endif + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); diff --git a/port/CMakeLists.txt b/port/CMakeLists.txt index 5b82bbd..b9caf56 100644 --- a/port/CMakeLists.txt +++ b/port/CMakeLists.txt @@ -131,6 +131,7 @@ component_compile_options ( -Wno-unused-function -Wno-missing-braces -Wno-missing-field-initializers + -Wno-unknown-pragmas ) component_compile_definitions ( diff --git a/port/src/agenttime_esp.c b/port/src/agenttime_esp.c index 0c25967..21152d9 100644 --- a/port/src/agenttime_esp.c +++ b/port/src/agenttime_esp.c @@ -5,19 +5,11 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "esp_attr.h" +#include "lwip/apps/sntp.h" -#include "lwip/err.h" #include "azure_c_shared_utility/agenttime.h" #include "azure_c_shared_utility/xlogging.h" -#include "lwip/apps/sntp.h" - void initialize_sntp(void) { if (!sntp_enabled()) { @@ -67,7 +59,7 @@ time_t get_time(time_t* currentTime) } double get_difftime(time_t stopTime, time_t startTime) -{ +{ return (double)stopTime - (double)startTime; }