Skip to content

Commit f424131

Browse files
authored
Merge pull request #59 from Azure/ewertons/update2esp32pkg301
Update ESP32 base samples to work with ESP32 package 3.0.1 (+ Arduino IDE 2.3.2)
2 parents 3e8d1f0 + 66e4195 commit f424131

File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed

examples/Azure_IoT_Central_ESP32/Azure_IoT_Central_ESP32.ino

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ static const char* wifi_password = IOT_CONFIG_WIFI_PASSWORD;
8383
/* --- Function Declarations --- */
8484
static void sync_device_clock_with_ntp_server();
8585
static void connect_to_wifi();
86+
87+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
88+
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
89+
#else // ESP_ARDUINO_VERSION_MAJOR
8690
static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event);
91+
#endif // ESP_ARDUINO_VERSION_MAJOR
8792

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

130-
mqtt_config.uri = mqtt_broker_uri;
131-
mqtt_config.port = mqtt_client_config->port;
132-
mqtt_config.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
133-
mqtt_config.username = (const char*)az_span_ptr(mqtt_client_config->username);
134-
135-
#ifdef IOT_CONFIG_USE_X509_CERT
136-
LogInfo("MQTT client using X509 Certificate authentication");
137-
mqtt_config.client_cert_pem = IOT_CONFIG_DEVICE_CERT;
138-
mqtt_config.client_key_pem = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
139-
#else // Using SAS key
140-
mqtt_config.password = (const char*)az_span_ptr(mqtt_client_config->password);
141-
#endif
142-
143-
mqtt_config.keepalive = 30;
144-
mqtt_config.disable_clean_session = 0;
145-
mqtt_config.disable_auto_reconnect = false;
146-
mqtt_config.event_handle = esp_mqtt_event_handler;
147-
mqtt_config.user_context = NULL;
148-
mqtt_config.cert_pem = (const char*)ca_pem;
135+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
136+
mqtt_config.broker.address.uri = mqtt_broker_uri;
137+
mqtt_config.broker.address.port = mqtt_client_config->port;
138+
mqtt_config.credentials.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
139+
mqtt_config.credentials.username = (const char*)az_span_ptr(mqtt_client_config->username);
140+
141+
#ifdef IOT_CONFIG_USE_X509_CERT
142+
LogInfo("MQTT client using X509 Certificate authentication");
143+
mqtt_config.credentials.authentication.certificate = IOT_CONFIG_DEVICE_CERT;
144+
mqtt_config.credentials.authentication.certificate_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT);
145+
mqtt_config.credentials.authentication.key = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
146+
mqtt_config.credentials.authentication.key_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY);
147+
#else // Using SAS key
148+
mqtt_config.credentials.authentication.password = (const char*)az_span_ptr(mqtt_client_config->password);
149+
#endif
150+
151+
mqtt_config.session.keepalive = 30;
152+
mqtt_config.session.disable_clean_session = 0;
153+
mqtt_config.network.disable_auto_reconnect = false;
154+
mqtt_config.broker.verification.certificate = (const char*)ca_pem;
155+
mqtt_config.broker.verification.certificate_len = (size_t)ca_pem_len;
156+
#else // ESP_ARDUINO_VERSION_MAJOR
157+
mqtt_config.uri = mqtt_broker_uri;
158+
mqtt_config.port = mqtt_client_config->port;
159+
mqtt_config.client_id = (const char*)az_span_ptr(mqtt_client_config->client_id);
160+
mqtt_config.username = (const char*)az_span_ptr(mqtt_client_config->username);
161+
162+
#ifdef IOT_CONFIG_USE_X509_CERT
163+
LogInfo("MQTT client using X509 Certificate authentication");
164+
mqtt_config.client_cert_pem = IOT_CONFIG_DEVICE_CERT;
165+
mqtt_config.client_key_pem = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
166+
#else // Using SAS key
167+
mqtt_config.password = (const char*)az_span_ptr(mqtt_client_config->password);
168+
#endif
169+
170+
mqtt_config.keepalive = 30;
171+
mqtt_config.disable_clean_session = 0;
172+
mqtt_config.disable_auto_reconnect = false;
173+
mqtt_config.event_handle = esp_mqtt_event_handler;
174+
mqtt_config.user_context = NULL;
175+
mqtt_config.cert_pem = (const char*)ca_pem;
176+
#endif // ESP_ARDUINO_VERSION_MAJOR
149177

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

@@ -158,6 +186,10 @@ static int mqtt_client_init_function(
158186
}
159187
else
160188
{
189+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
190+
esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY, esp_mqtt_event_handler, NULL);
191+
#endif // ESP_ARDUINO_VERSION_MAJOR
192+
161193
esp_err_t start_result = esp_mqtt_client_start(mqtt_client);
162194

163195
if (start_result != ESP_OK)
@@ -400,7 +432,6 @@ void setup()
400432
sync_device_clock_with_ntp_server();
401433

402434
azure_pnp_init();
403-
azure_pnp_set_telemetry_frequency(TELEMETRY_FREQUENCY_IN_SECONDS);
404435

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

536+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
537+
static void esp_mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
538+
{
539+
(void)handler_args;
540+
(void)base;
541+
(void)event_id;
542+
543+
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
544+
#else // ESP_ARDUINO_VERSION_MAJOR
505545
static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event)
506546
{
547+
#endif // ESP_ARDUINO_VERSION_MAJOR
507548
switch (event->event_id)
508549
{
509550
int i, r;
@@ -611,7 +652,10 @@ static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event)
611652
break;
612653
}
613654

655+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
656+
#else // ESP_ARDUINO_VERSION_MAJOR
614657
return ESP_OK;
658+
#endif // ESP_ARDUINO_VERSION_MAJOR
615659
}
616660

617661
static void logging_function(log_level_t log_level, char const* const format, ...)

examples/Azure_IoT_Hub_ESP32/Azure_IoT_Hub_ESP32.ino

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,18 @@ void receivedCallback(char* topic, byte* payload, unsigned int length)
141141
Serial.println("");
142142
}
143143

144+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
145+
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
146+
{
147+
(void)handler_args;
148+
(void)base;
149+
(void)event_id;
150+
151+
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
152+
#else // ESP_ARDUINO_VERSION_MAJOR
144153
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
145154
{
155+
#endif // ESP_ARDUINO_VERSION_MAJOR
146156
switch (event->event_id)
147157
{
148158
int i, r;
@@ -202,7 +212,10 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
202212
break;
203213
}
204214

215+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
216+
#else // ESP_ARDUINO_VERSION_MAJOR
205217
return ESP_OK;
218+
#endif // ESP_ARDUINO_VERSION_MAJOR
206219
}
207220

208221
static void initializeIoTHubClient()
@@ -251,6 +264,29 @@ static int initializeMqttClient()
251264

252265
esp_mqtt_client_config_t mqtt_config;
253266
memset(&mqtt_config, 0, sizeof(mqtt_config));
267+
268+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
269+
mqtt_config.broker.address.uri = mqtt_broker_uri;
270+
mqtt_config.broker.address.port = mqtt_port;
271+
mqtt_config.credentials.client_id = mqtt_client_id;
272+
mqtt_config.credentials.username = mqtt_username;
273+
274+
#ifdef IOT_CONFIG_USE_X509_CERT
275+
LogInfo("MQTT client using X509 Certificate authentication");
276+
mqtt_config.credentials.authentication.certificate = IOT_CONFIG_DEVICE_CERT;
277+
mqtt_config.credentials.authentication.certificate_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT);
278+
mqtt_config.credentials.authentication.key = IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY;
279+
mqtt_config.credentials.authentication.key_len = (size_t)sizeof(IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY);
280+
#else // Using SAS key
281+
mqtt_config.credentials.authentication.password = (const char*)az_span_ptr(sasToken.Get());
282+
#endif
283+
284+
mqtt_config.session.keepalive = 30;
285+
mqtt_config.session.disable_clean_session = 0;
286+
mqtt_config.network.disable_auto_reconnect = false;
287+
mqtt_config.broker.verification.certificate = (const char*)ca_pem;
288+
mqtt_config.broker.verification.certificate_len = (size_t)ca_pem_len;
289+
#else // ESP_ARDUINO_VERSION_MAJOR
254290
mqtt_config.uri = mqtt_broker_uri;
255291
mqtt_config.port = mqtt_port;
256292
mqtt_config.client_id = mqtt_client_id;
@@ -270,6 +306,7 @@ static int initializeMqttClient()
270306
mqtt_config.event_handle = mqtt_event_handler;
271307
mqtt_config.user_context = NULL;
272308
mqtt_config.cert_pem = (const char*)ca_pem;
309+
#endif // ESP_ARDUINO_VERSION_MAJOR
273310

274311
mqtt_client = esp_mqtt_client_init(&mqtt_config);
275312

@@ -279,6 +316,10 @@ static int initializeMqttClient()
279316
return 1;
280317
}
281318

319+
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
320+
esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY, mqtt_event_handler, NULL);
321+
#endif // ESP_ARDUINO_VERSION_MAJOR
322+
282323
esp_err_t start_result = esp_mqtt_client_start(mqtt_client);
283324

284325
if (start_result != ESP_OK)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Azure SDK for C
2-
version=1.1.6
2+
version=1.1.7
33
author=Microsoft Corporation
44
maintainer=Microsoft Corporation <[email protected]>
55
sentence=Azure SDK for C library for Arduino.

0 commit comments

Comments
 (0)