Skip to content

Commit 27a631d

Browse files
authored
Update to ESP-IDF v5.0 (#176)
1 parent 6ce66ab commit 27a631d

File tree

13 files changed

+102
-69
lines changed

13 files changed

+102
-69
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ jobs:
1919
submodules: recursive
2020

2121
- name: ESP-IDF Build
22-
uses: espressif/[email protected]
22+
uses: espressif/[email protected]
23+
with:
24+
esp_idf_version: v5.0
2325

2426
- name: Upload Application Image
2527
uses: actions/[email protected]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This project is a BLE to MQTT bridge, i.e. it exposes BLE GATT characteristics
44
as MQTT topics for bidirectional communication. It's developed for the ESP32 SoC
5-
and is based on [ESP-IDF](https://github.com/espressif/esp-idf) release v4.4.
5+
and is based on [ESP-IDF](https://github.com/espressif/esp-idf) release v5.0.
66
Note that using any other ESP-IDF version might not be stable or even compile.
77

88
For example, if a device with a MAC address of `a0:e6:f8:50:72:53` exposes the

main/ble2mqtt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <esp_err.h>
1313
#include <esp_log.h>
1414
#include <esp_system.h>
15+
#include <esp_timer.h>
1516
#include <nvs.h>
1617
#include <nvs_flash.h>
1718
#include <mdns.h>
@@ -67,13 +68,13 @@ static void uptime_publish(void)
6768
return;
6869

6970
/* Uptime (in seconds) */
70-
sprintf(buf, "%lld", esp_timer_get_time() / 1000 / 1000);
71+
sprintf(buf, "%" PRId64, esp_timer_get_time() / 1000 / 1000);
7172
snprintf(topic, MAX_TOPIC_LEN, "%s/Uptime", device_name_get());
7273
mqtt_publish(topic, (uint8_t *)buf, strlen(buf), config_mqtt_qos_get(),
7374
config_mqtt_retained_get());
7475

7576
/* Free memory (in bytes) */
76-
sprintf(buf, "%u", esp_get_free_heap_size());
77+
sprintf(buf, "%" PRIu32, esp_get_free_heap_size());
7778
snprintf(topic, MAX_TOPIC_LEN, "%s/FreeMemory", device_name_get());
7879
mqtt_publish(topic, (uint8_t *)buf, strlen(buf), config_mqtt_qos_get(),
7980
config_mqtt_retained_get());
@@ -500,7 +501,7 @@ static uint32_t ble_on_passkey_requested(mac_addr_t mac)
500501
char *s = mactoa(mac);
501502
uint32_t passkey = config_ble_passkey_get(s);
502503

503-
ESP_LOGI(TAG, "Initiating pairing with %s using the passkey %u", s,
504+
ESP_LOGI(TAG, "Initiating pairing with %s using the passkey %" PRIu32, s,
504505
passkey);
505506

506507
return passkey;

main/ble_utils.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "config.h"
33
#include "gatt.h"
44
#include <math.h>
5+
#include <inttypes.h>
56
#include <string.h>
67

78
#define CASE_STR(x) case x: return #x
@@ -297,26 +298,26 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
297298
p += sprintf(p, "%s,", data[i] & 0x01 ? "true" : "false");
298299
break;
299300
case CHAR_TYPE_2BIT:
300-
p += sprintf(p, "%hhu,", data[i] & 0x03);
301+
p += sprintf(p, "%" PRIu8 ",", data[i] & 0x03);
301302
break;
302303
case CHAR_TYPE_4BIT:
303304
case CHAR_TYPE_NIBBLE:
304-
p += sprintf(p, "%hhu,", data[i] & 0x0F);
305+
p += sprintf(p, "%" PRIu8 ",", data[i] & 0x0F);
305306
break;
306307
case CHAR_TYPE_8BIT:
307308
case CHAR_TYPE_UINT8:
308309
case CHAR_TYPE_SINT8:
309310
if (*types == CHAR_TYPE_SINT8)
310-
p += sprintf(p, "%hhd,", data[i]);
311+
p += sprintf(p, "%" PRId8 ",", data[i]);
311312
else
312-
p += sprintf(p, "%hhu,", data[i]);
313+
p += sprintf(p, "%" PRIu8 ",", data[i]);
313314

314315
break;
315316
case CHAR_TYPE_UINT12:
316317
{
317318
uint16_t tmp = (data[i + 1] << 8) | data[i];
318319

319-
p += sprintf(p, "%hu,", tmp & 0x0FFF);
320+
p += sprintf(p, "%" PRIu16 ",", tmp & 0x0FFF);
320321
break;
321322
}
322323
case CHAR_TYPE_16BIT:
@@ -326,9 +327,9 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
326327
uint16_t tmp = (data[i + 1] << 8) | data[i];
327328

328329
if (*types == CHAR_TYPE_SINT16)
329-
p += sprintf(p, "%hd,", tmp);
330+
p += sprintf(p, "%" PRId16 ",", tmp);
330331
else
331-
p += sprintf(p, "%hu,", tmp);
332+
p += sprintf(p, "%" PRIu16 ",", tmp);
332333

333334
break;
334335
}
@@ -339,9 +340,9 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
339340
uint32_t tmp = (data[i + 2] << 16) | (data[i + 1] << 8) | data[i];
340341

341342
if (*types == CHAR_TYPE_SINT24)
342-
p += sprintf(p, "%d,", (int32_t)tmp << 8 >> 8);
343+
p += sprintf(p, "%" PRId32 ",", (int32_t)tmp << 8 >> 8);
343344
else
344-
p += sprintf(p, "%u,", tmp);
345+
p += sprintf(p, "%" PRIu32 ",", tmp);
345346

346347
break;
347348
}
@@ -353,9 +354,9 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
353354
(data[i + 1] << 8) | data[i];
354355

355356
if (*types == CHAR_TYPE_SINT32)
356-
p += sprintf(p, "%d,", tmp);
357+
p += sprintf(p, "%" PRId32 ",", tmp);
357358
else
358-
p += sprintf(p, "%u,", tmp);
359+
p += sprintf(p, "%" PRIu32 ",", tmp);
359360

360361
break;
361362
}
@@ -364,7 +365,7 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
364365
uint64_t tmp = ((uint64_t)data[i + 4] << 32) | (data[i + 3] << 24) |
365366
(data[i + 2] << 16) | (data[i + 1] << 8) | data[i];
366367

367-
p += sprintf(p, "%llu,", tmp);
368+
p += sprintf(p, "%" PRIu64 ",", tmp);
368369

369370
break;
370371
}
@@ -374,7 +375,7 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
374375
((uint64_t)data[i + 4] << 32) | (data[i + 3] << 24) |
375376
(data[i + 2] << 16) | (data[i + 1] << 8) | data[i];
376377

377-
p += sprintf(p, "%llu,", tmp);
378+
p += sprintf(p, "%" PRIu64 ",", tmp);
378379

379380
break;
380381
}
@@ -444,7 +445,7 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)
444445
}
445446

446447
for (; i < len; i++)
447-
p += sprintf(p, "%u,", data[i]);
448+
p += sprintf(p, "%" PRIu8 ",", data[i]);
448449

449450
*(p - 1) = '\0';
450451
return buf;

main/broadcasters.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <stdlib.h>
88
#include <string.h>
99
#include <endian.h>
10-
#include <mbedtls/config.h>
1110
#include <mbedtls/cipher.h>
1211
#include <mbedtls/ccm.h>
1312
#include <mbedtls/error.h>
@@ -294,9 +293,9 @@ static void eddystone_metadata_get(uint8_t *adv_data, size_t adv_data_len,
294293
sprintf(s, "%d.%02ld", (int8_t)(temp >> 8),
295294
lround((temp & 0xff) * 100 / 256.0));
296295
cb("Temperature", s, ctx);
297-
sprintf(s, "%u", be32toh(eddystone->u.tlm.adv_cnt));
296+
sprintf(s, "%" PRIu32, be32toh(eddystone->u.tlm.adv_cnt));
298297
cb("Count", s, ctx);
299-
sprintf(s, "%u", be32toh(eddystone->u.tlm.sec_cnt));
298+
sprintf(s, "%" PRIu32, be32toh(eddystone->u.tlm.sec_cnt));
300299
cb("Uptime", s, ctx);
301300
}
302301
else
@@ -539,7 +538,7 @@ static void mijia_sensor_metadata_get(uint8_t *adv_data, size_t adv_data_len,
539538
{
540539
uint32_t val = mijia_data_entry->data[0]
541540
| (mijia_data_entry->data[1] << 8) | (mijia_data_entry->data[2] << 16);
542-
sprintf(s, "%u", val);
541+
sprintf(s, "%" PRIu32, val);
543542
cb("Illuminance", s, ctx);
544543
}
545544
else if (mijia_data_entry->data_type == MIJIA_SENSOR_DATA_TYPE_COND)
@@ -567,7 +566,7 @@ static void mijia_sensor_metadata_get(uint8_t *adv_data, size_t adv_data_len,
567566
| (mijia_data_entry->data[1] << 8) | (mijia_data_entry->data[2] << 16);
568567
if (device_type == MIJIA_DEVICE_TYPE_CGPR1)
569568
{
570-
sprintf(s, "%u", val);
569+
sprintf(s, "%" PRIu32, val);
571570
cb("Illuminance", s, ctx);
572571
}
573572
else if (device_type == MIJIA_DEVICE_TYPE_MJYD02YL)

main/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ int config_update_begin(config_update_handle_t **handle)
576576
return -1;
577577
}
578578

579-
ESP_LOGI(TAG, "Writing partition type 0x%0x subtype 0x%0x (offset 0x%08x)",
580-
partition->type, partition->subtype, partition->address);
579+
ESP_LOGI(TAG, "Writing partition type 0x%0x subtype 0x%0x (offset 0x%08"
580+
PRIx32 ")", partition->type, partition->subtype, partition->address);
581581

582582
/* Erase partition, needed before writing is allowed */
583583
if (esp_partition_erase_range(partition, 0, partition->size))

main/eth.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <esp_eth.h>
44
#include <esp_event.h>
55
#include <esp_log.h>
6+
#include <esp_mac.h>
7+
#include <esp_netif.h>
8+
#include <esp_rom_gpio.h>
69
#include <driver/gpio.h>
710
#include <arpa/inet.h>
811
#include <stdbool.h>
@@ -13,6 +16,7 @@ static const char *TAG = "Ethernet";
1316
static eth_on_connected_cb_t on_connected_cb = NULL;
1417
static eth_on_disconnected_cb_t on_disconnected_cb = NULL;
1518
static char *eth_hostname = NULL;
19+
static esp_netif_t *eth_netif = NULL;
1620

1721
void eth_set_on_connected_cb(eth_on_connected_cb_t cb)
1822
{
@@ -50,7 +54,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
5054
switch (event_id) {
5155
case ETHERNET_EVENT_START:
5256
if (eth_hostname)
53-
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, eth_hostname);
57+
esp_netif_set_hostname(eth_netif, eth_hostname);
5458
break;
5559
case ETHERNET_EVENT_CONNECTED:
5660
ESP_LOGI(TAG, "Connected");
@@ -61,7 +65,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
6165
on_disconnected_cb();
6266
break;
6367
default:
64-
ESP_LOGD(TAG, "Unhandled Ethernet event (%d)", event_id);
68+
ESP_LOGD(TAG, "Unhandled Ethernet event (%" PRId32 ")", event_id);
6569
}
6670
}
6771
else if (event_base == IP_EVENT)
@@ -78,7 +82,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
7882
break;
7983
}
8084
default:
81-
ESP_LOGD(TAG, "Unhandled IP event (%d)", event_id);
85+
ESP_LOGD(TAG, "Unhandled IP event (%" PRId32 ")", event_id);
8286
break;
8387
}
8488
}
@@ -110,16 +114,17 @@ int eth_connect(eth_phy_t eth_phy, int8_t eth_phy_power_pin)
110114
{
111115
#if CONFIG_ETH_USE_ESP32_EMAC
112116
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
113-
esp_netif_t *eth_netif = esp_netif_new(&cfg);
114117
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
118+
eth_esp32_emac_config_t esp32_mac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
115119
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
116-
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
120+
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_mac_config, &mac_config);
117121
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, NULL);
118122
esp_eth_handle_t eth_handle = NULL;
119123

124+
eth_netif = esp_netif_new(&cfg);
120125
if (eth_phy_power_pin >= 0)
121126
{
122-
gpio_pad_select_gpio(eth_phy_power_pin);
127+
esp_rom_gpio_pad_select_gpio(eth_phy_power_pin);
123128
gpio_set_direction(eth_phy_power_pin, GPIO_MODE_OUTPUT);
124129
gpio_set_level(eth_phy_power_pin, 1);
125130
vTaskDelay(pdMS_TO_TICKS(10));
@@ -137,7 +142,7 @@ int eth_connect(eth_phy_t eth_phy, int8_t eth_phy_power_pin)
137142
break;
138143
case PHY_LAN8720:
139144
ESP_LOGI(TAG, "PHY config: LAN8720");
140-
config.phy = esp_eth_phy_new_lan8720(&phy_config);
145+
config.phy = esp_eth_phy_new_lan87xx(&phy_config);
141146
break;
142147
case PHY_DP83848:
143148
ESP_LOGI(TAG, "PHY config: DP83848");

main/idf_component.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
idf:
4+
version: ">=5.0"
5+
espressif/mdns: "^1.0.7"

main/mqtt.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,12 @@ static void mqtt_message_cb(const char *topic, size_t topic_len,
247247
}
248248
}
249249

250-
static esp_err_t mqtt_event_cb(esp_mqtt_event_handle_t event)
250+
static void mqtt_event_cb(void *handler_args, esp_event_base_t base,
251+
int32_t event_id, void *event_data)
251252
{
252-
switch (event->event_id) {
253+
esp_mqtt_event_handle_t event = event_data;
254+
255+
switch ((esp_mqtt_event_id_t)event_id) {
253256
case MQTT_EVENT_CONNECTED:
254257
ESP_LOGI(TAG, "MQTT client connected");
255258
is_connected = 1;
@@ -272,8 +275,6 @@ static esp_err_t mqtt_event_cb(esp_mqtt_event_handle_t event)
272275
default:
273276
break;
274277
}
275-
276-
return ESP_OK;
277278
}
278279

279280
int mqtt_connect(const char *host, uint16_t port, const char *client_id,
@@ -283,27 +284,43 @@ int mqtt_connect(const char *host, uint16_t port, const char *client_id,
283284
uint8_t lwt_retain)
284285
{
285286
esp_mqtt_client_config_t config = {
286-
.event_handle = mqtt_event_cb,
287-
.host = resolve_host(host),
288-
.port = port,
289-
.client_id = client_id,
290-
.username = username,
291-
.password = password,
292-
.transport = ssl ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP,
293-
.cert_pem = server_cert,
294-
.client_cert_pem = client_cert,
295-
.client_key_pem = client_key,
296-
.lwt_topic = lwt_topic,
297-
.lwt_msg = lwt_msg,
298-
.lwt_qos = lwt_qos,
299-
.lwt_retain = lwt_retain,
287+
.broker = {
288+
.address = {
289+
.hostname = resolve_host(host),
290+
.port = port,
291+
.transport =
292+
ssl ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP,
293+
},
294+
.verification = {
295+
.certificate = server_cert,
296+
},
297+
},
298+
.credentials = {
299+
.client_id = client_id,
300+
.username = username,
301+
.authentication = {
302+
.password = password,
303+
.certificate = client_cert,
304+
.key = client_key,
305+
}
306+
},
307+
.session = {
308+
.last_will = {
309+
.topic = lwt_topic,
310+
.msg = lwt_msg,
311+
.qos = lwt_qos,
312+
.retain = lwt_retain,
313+
},
314+
},
300315
};
301316

302317
ESP_LOGI(TAG, "Connecting MQTT client");
303318
if (mqtt_handle)
304319
esp_mqtt_client_destroy(mqtt_handle);
305320
if (!(mqtt_handle = esp_mqtt_client_init(&config)))
306321
return -1;
322+
esp_mqtt_client_register_event(mqtt_handle, ESP_EVENT_ANY_ID, mqtt_event_cb,
323+
NULL);
307324
esp_mqtt_client_start(mqtt_handle);
308325
return 0;
309326
}

0 commit comments

Comments
 (0)