@@ -83,7 +83,12 @@ static const char* wifi_password = IOT_CONFIG_WIFI_PASSWORD;
83
83
/* --- Function Declarations --- */
84
84
static void sync_device_clock_with_ntp_server ();
85
85
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
86
90
static esp_err_t esp_mqtt_event_handler (esp_mqtt_event_handle_t event);
91
+ #endif // ESP_ARDUINO_VERSION_MAJOR
87
92
88
93
// This is a logging function used by Azure IoT client.
89
94
static void logging_function (log_level_t log_level, char const * const format, ...);
@@ -127,25 +132,48 @@ static int mqtt_client_init_function(
127
132
mqtt_broker_uri_span = az_span_copy (mqtt_broker_uri_span, mqtt_client_config->address );
128
133
az_span_copy_u8 (mqtt_broker_uri_span, null_terminator);
129
134
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
149
177
150
178
LogInfo (" MQTT client target uri set to '%s'" , mqtt_broker_uri);
151
179
@@ -158,6 +186,10 @@ static int mqtt_client_init_function(
158
186
}
159
187
else
160
188
{
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
+
161
193
esp_err_t start_result = esp_mqtt_client_start (mqtt_client);
162
194
163
195
if (start_result != ESP_OK)
@@ -400,7 +432,6 @@ void setup()
400
432
sync_device_clock_with_ntp_server ();
401
433
402
434
azure_pnp_init ();
403
- azure_pnp_set_telemetry_frequency (TELEMETRY_FREQUENCY_IN_SECONDS);
404
435
405
436
configure_azure_iot ();
406
437
azure_iot_start (&azure_iot);
@@ -502,8 +533,18 @@ static void connect_to_wifi()
502
533
LogInfo (" WiFi connected, IP address: %s" , WiFi.localIP ().toString ().c_str ());
503
534
}
504
535
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
505
545
static esp_err_t esp_mqtt_event_handler (esp_mqtt_event_handle_t event)
506
546
{
547
+ #endif // ESP_ARDUINO_VERSION_MAJOR
507
548
switch (event->event_id )
508
549
{
509
550
int i, r;
@@ -611,7 +652,10 @@ static esp_err_t esp_mqtt_event_handler(esp_mqtt_event_handle_t event)
611
652
break ;
612
653
}
613
654
655
+ #if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
656
+ #else // ESP_ARDUINO_VERSION_MAJOR
614
657
return ESP_OK;
658
+ #endif // ESP_ARDUINO_VERSION_MAJOR
615
659
}
616
660
617
661
static void logging_function (log_level_t log_level, char const * const format, ...)
0 commit comments