Skip to content

Commit

Permalink
Merge pull request #59 from Azure/ewertons/update2esp32pkg301
Browse files Browse the repository at this point in the history
Update ESP32 base samples to work with ESP32 package 3.0.1 (+ Arduino IDE 2.3.2)
  • Loading branch information
ewertons authored Jun 11, 2024
2 parents 3e8d1f0 + 66e4195 commit f424131
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 21 deletions.
84 changes: 64 additions & 20 deletions examples/Azure_IoT_Central_ESP32/Azure_IoT_Central_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ static const char* wifi_password = IOT_CONFIG_WIFI_PASSWORD;
/* --- Function Declarations --- */
static void sync_device_clock_with_ntp_server();
static void connect_to_wifi();

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
#else // ESP_ARDUINO_VERSION_MAJOR
static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event);
#endif // ESP_ARDUINO_VERSION_MAJOR

// This is a logging function used by Azure IoT client.
static void logging_function(log_level_t log_level, char const* const format, ...);
Expand Down Expand Up @@ -127,25 +132,48 @@ static int mqtt_client_init_function(
mqtt_broker_uri_span = az_span_copy(mqtt_broker_uri_span, mqtt_client_config->address);
az_span_copy_u8(mqtt_broker_uri_span, null_terminator);

mqtt_config.uri = mqtt_broker_uri;
mqtt_config.port = mqtt_client_config->port;
mqtt_config.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
mqtt_config.username = (const char*)az_span_ptr(mqtt_client_config->username);

#ifdef IOT_CONFIG_USE_X509_CERT
LogInfo("MQTT client using X509 Certificate authentication");
mqtt_config.client_cert_pem = IOT_CONFIG_DEVICE_CERT;
mqtt_config.client_key_pem = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
#else // Using SAS key
mqtt_config.password = (const char*)az_span_ptr(mqtt_client_config->password);
#endif

mqtt_config.keepalive = 30;
mqtt_config.disable_clean_session = 0;
mqtt_config.disable_auto_reconnect = false;
mqtt_config.event_handle = esp_mqtt_event_handler;
mqtt_config.user_context = NULL;
mqtt_config.cert_pem = (const char*)ca_pem;
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
mqtt_config.broker.address.uri = mqtt_broker_uri;
mqtt_config.broker.address.port = mqtt_client_config->port;
mqtt_config.credentials.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
mqtt_config.credentials.username = (const char*)az_span_ptr(mqtt_client_config->username);

#ifdef IOT_CONFIG_USE_X509_CERT
LogInfo("MQTT client using X509 Certificate authentication");
mqtt_config.credentials.authentication.certificate = IOT_CONFIG_DEVICE_CERT;
mqtt_config.credentials.authentication.certificate_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT);
mqtt_config.credentials.authentication.key = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
mqtt_config.credentials.authentication.key_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY);
#else // Using SAS key
mqtt_config.credentials.authentication.password = (const char*)az_span_ptr(mqtt_client_config->password);
#endif

mqtt_config.session.keepalive = 30;
mqtt_config.session.disable_clean_session = 0;
mqtt_config.network.disable_auto_reconnect = false;
mqtt_config.broker.verification.certificate = (const char*)ca_pem;
mqtt_config.broker.verification.certificate_len = (size_t)ca_pem_len;
#else // ESP_ARDUINO_VERSION_MAJOR
mqtt_config.uri = mqtt_broker_uri;
mqtt_config.port = mqtt_client_config->port;
mqtt_config.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
mqtt_config.username = (const char*)az_span_ptr(mqtt_client_config->username);

#ifdef IOT_CONFIG_USE_X509_CERT
LogInfo("MQTT client using X509 Certificate authentication");
mqtt_config.client_cert_pem = IOT_CONFIG_DEVICE_CERT;
mqtt_config.client_key_pem = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
#else // Using SAS key
mqtt_config.password = (const char*)az_span_ptr(mqtt_client_config->password);
#endif

mqtt_config.keepalive = 30;
mqtt_config.disable_clean_session = 0;
mqtt_config.disable_auto_reconnect = false;
mqtt_config.event_handle = esp_mqtt_event_handler;
mqtt_config.user_context = NULL;
mqtt_config.cert_pem = (const char*)ca_pem;
#endif // ESP_ARDUINO_VERSION_MAJOR

LogInfo("MQTT client target uri set to '%s'", mqtt_broker_uri);

Expand All @@ -158,6 +186,10 @@ static int mqtt_client_init_function(
}
else
{
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY, esp_mqtt_event_handler, NULL);
#endif // ESP_ARDUINO_VERSION_MAJOR

esp_err_t start_result = esp_mqtt_client_start(mqtt_client);

if (start_result != ESP_OK)
Expand Down Expand Up @@ -400,7 +432,6 @@ void setup()
sync_device_clock_with_ntp_server();

azure_pnp_init();
azure_pnp_set_telemetry_frequency(TELEMETRY_FREQUENCY_IN_SECONDS);

configure_azure_iot();
azure_iot_start(&azure_iot);
Expand Down Expand Up @@ -502,8 +533,18 @@ static void connect_to_wifi()
LogInfo("WiFi connected, IP address: %s", WiFi.localIP().toString().c_str());
}

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
static void esp_mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
(void)handler_args;
(void)base;
(void)event_id;

esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
#else // ESP_ARDUINO_VERSION_MAJOR
static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event)
{
#endif // ESP_ARDUINO_VERSION_MAJOR
switch (event->event_id)
{
int i, r;
Expand Down Expand Up @@ -611,7 +652,10 @@ static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event)
break;
}

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
#else // ESP_ARDUINO_VERSION_MAJOR
return ESP_OK;
#endif // ESP_ARDUINO_VERSION_MAJOR
}

static void logging_function(log_level_t log_level, char const* const format, ...)
Expand Down
41 changes: 41 additions & 0 deletions examples/Azure_IoT_Hub_ESP32/Azure_IoT_Hub_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,18 @@ void receivedCallback(char* topic, byte* payload, unsigned int length)
Serial.println("");
}

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
(void)handler_args;
(void)base;
(void)event_id;

esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
#else // ESP_ARDUINO_VERSION_MAJOR
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
{
#endif // ESP_ARDUINO_VERSION_MAJOR
switch (event->event_id)
{
int i, r;
Expand Down Expand Up @@ -202,7 +212,10 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
break;
}

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
#else // ESP_ARDUINO_VERSION_MAJOR
return ESP_OK;
#endif // ESP_ARDUINO_VERSION_MAJOR
}

static void initializeIoTHubClient()
Expand Down Expand Up @@ -251,6 +264,29 @@ static int initializeMqttClient()

esp_mqtt_client_config_t mqtt_config;
memset(&mqtt_config, 0, sizeof(mqtt_config));

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
mqtt_config.broker.address.uri = mqtt_broker_uri;
mqtt_config.broker.address.port = mqtt_port;
mqtt_config.credentials.client_id = mqtt_client_id;
mqtt_config.credentials.username = mqtt_username;

#ifdef IOT_CONFIG_USE_X509_CERT
LogInfo("MQTT client using X509 Certificate authentication");
mqtt_config.credentials.authentication.certificate = IOT_CONFIG_DEVICE_CERT;
mqtt_config.credentials.authentication.certificate_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT);
mqtt_config.credentials.authentication.key = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
mqtt_config.credentials.authentication.key_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY);
#else // Using SAS key
mqtt_config.credentials.authentication.password = (const char*)az_span_ptr(sasToken.Get());
#endif

mqtt_config.session.keepalive = 30;
mqtt_config.session.disable_clean_session = 0;
mqtt_config.network.disable_auto_reconnect = false;
mqtt_config.broker.verification.certificate = (const char*)ca_pem;
mqtt_config.broker.verification.certificate_len = (size_t)ca_pem_len;
#else // ESP_ARDUINO_VERSION_MAJOR
mqtt_config.uri = mqtt_broker_uri;
mqtt_config.port = mqtt_port;
mqtt_config.client_id = mqtt_client_id;
Expand All @@ -270,6 +306,7 @@ static int initializeMqttClient()
mqtt_config.event_handle = mqtt_event_handler;
mqtt_config.user_context = NULL;
mqtt_config.cert_pem = (const char*)ca_pem;
#endif // ESP_ARDUINO_VERSION_MAJOR

mqtt_client = esp_mqtt_client_init(&mqtt_config);

Expand All @@ -279,6 +316,10 @@ static int initializeMqttClient()
return 1;
}

#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY, mqtt_event_handler, NULL);
#endif // ESP_ARDUINO_VERSION_MAJOR

esp_err_t start_result = esp_mqtt_client_start(mqtt_client);

if (start_result != ESP_OK)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Azure SDK for C
version=1.1.6
version=1.1.7
author=Microsoft Corporation
maintainer=Microsoft Corporation <[email protected]>
sentence=Azure SDK for C library for Arduino.
Expand Down

0 comments on commit f424131

Please sign in to comment.