Skip to content

Commit 7dd2916

Browse files
committed
fix(mosq): per review comments
1 parent 5808dc6 commit 7dd2916

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

components/mosquitto/examples/serverless_mqtt/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Serverless MQTT Example
1+
# Brokerless MQTT Example
22

33
MQTT served by (two) mosquitto's running on two ESP chips.
44

5-
* Leverages MQTT connectivity between private two networks without cloud premisses.
6-
* Creates two local MQTT servers (on ESP32x's) which are being synchronized over peer to peer connection (established via ICE protocol, by libjuice).
5+
* Leverages MQTT connectivity between two private networks without cloud premisses.
6+
* Creates two local MQTT servers (on ESP32x's) which are being synchronized over peer to peer connection (established via ICE protocol, by [libjuice](https://github.com/paullouisageneau/libjuice)).
77

88
## How it works
99

components/mosquitto/examples/serverless_mqtt/main/Kconfig.projbuild

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ menu "Example Configuration"
5252
help
5353
STUN server hostname.
5454

55+
config EXAMPLE_MQTT_CLIENT_STACK_SIZE
56+
int "Stack size for mqtt client"
57+
default 16384
58+
help
59+
Set stack size for the mqtt client.
60+
Need more stack, since calling juice API from the handler.
61+
62+
config EXAMPLE_MQTT_BROKER_PORT
63+
int "port for the mosquitto to listen to"
64+
default 1883
65+
help
66+
This is a port which the local mosquitto uses.
67+
5568
choice EXAMPLE_SERVERLESS_ROLE
5669
prompt "Choose your role"
5770
default EXAMPLE_SERVERLESS_ROLE_PEER1

components/mosquitto/examples/serverless_mqtt/main/serverless_mqtt.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "freertos/FreeRTOS.h"
88
#include "freertos/event_groups.h"
99
#include "mqtt_client.h"
10+
#include "esp_wifi.h"
1011
#include "esp_log.h"
12+
#include "esp_random.h"
1113
#include "esp_check.h"
1214
#include "esp_sleep.h"
1315
#include "mosq_broker.h"
@@ -50,6 +52,7 @@ static cJSON *s_peer_desc_json = NULL;
5052
static char *s_peer_desc = NULL;
5153
static esp_mqtt_client_handle_t s_local_mqtt = NULL;
5254

55+
char *wifi_get_ipv4(wifi_interface_t interface);
5356
esp_err_t wifi_connect(void);
5457
static esp_err_t sync_peers(void);
5558
static esp_err_t create_candidates(void);
@@ -62,17 +65,17 @@ void app_main(void)
6265
ESP_GOTO_ON_ERROR(wifi_connect(), err, TAG, "Failed to initialize WiFi");
6366
ESP_GOTO_ON_ERROR(create_local_broker(), err, TAG, "Failed to create local broker");
6467
ESP_GOTO_ON_ERROR(create_candidates(), err, TAG, "Failed to create juice candidates");
65-
ESP_GOTO_ON_ERROR(sync_peers(), err, TAG, "Failed to sync with the ohter peer");
66-
EventBits_t bits = xEventGroupWaitBits(s_state, PEER_FAIL | PEER_CONNECTED, pdFALSE, pdFALSE, pdMS_TO_TICKS(20000));
68+
ESP_GOTO_ON_ERROR(sync_peers(), err, TAG, "Failed to sync with the other peer");
69+
EventBits_t bits = xEventGroupWaitBits(s_state, PEER_FAIL | PEER_CONNECTED, pdFALSE, pdFALSE, pdMS_TO_TICKS(90000));
6770
if (bits & PEER_CONNECTED) {
6871
ESP_LOGI(TAG, "Peer is connected!");
6972
ESP_GOTO_ON_ERROR(create_local_client(), err, TAG, "Failed to create forwarding mqtt client");
7073
ESP_LOGI(TAG, "Everything is ready, exiting main task");
7174
return;
7275
}
7376
err:
74-
ESP_LOGE(TAG, "Non recoverable error, going to sleep for some time");
75-
esp_deep_sleep(1000000LL * 30);
77+
ESP_LOGE(TAG, "Non recoverable error, going to sleep for some time (random, max 20s)");
78+
esp_deep_sleep(1000000LL * (esp_random() % 20));
7679
}
7780

7881
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
@@ -161,7 +164,7 @@ static esp_err_t sync_peers(void)
161164
esp_err_t ret = ESP_OK;
162165
esp_mqtt_client_config_t mqtt_cfg = {
163166
.broker.address.uri = CONFIG_EXAMPLE_MQTT_BROKER_URI,
164-
.task.stack_size = 16384,
167+
.task.stack_size = CONFIG_EXAMPLE_MQTT_CLIENT_STACK_SIZE,
165168
};
166169
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
167170
ESP_GOTO_ON_FALSE(client, ESP_ERR_NO_MEM, err, TAG, "Failed to create mqtt client");
@@ -256,6 +259,7 @@ static esp_err_t create_candidates(void)
256259
esp_err_t ret = ESP_OK;
257260
juice_set_log_level(JUICE_LOG_LEVEL_INFO);
258261
juice_config_t config = { .stun_server_host = CONFIG_EXAMPLE_STUN_SERVER,
262+
.bind_address = wifi_get_ipv4(WIFI_IF_STA),
259263
.stun_server_port = 19302,
260264
.cb_state_changed = juice_state,
261265
.cb_candidate = juice_candidate,
@@ -310,8 +314,10 @@ static esp_err_t create_local_client(void)
310314
{
311315
esp_err_t ret = ESP_OK;
312316
esp_mqtt_client_config_t mqtt_cfg = {
313-
.broker.address.uri = "mqtt://192.168.4.1:3333",
314-
.task.stack_size = 16384,
317+
.broker.address.transport = MQTT_TRANSPORT_OVER_TCP,
318+
.broker.address.hostname = wifi_get_ipv4(WIFI_IF_AP),
319+
.broker.address.port = CONFIG_EXAMPLE_MQTT_BROKER_PORT,
320+
.task.stack_size = CONFIG_EXAMPLE_MQTT_CLIENT_STACK_SIZE,
315321
.credentials.client_id = "local_mqtt"
316322
};
317323
s_local_mqtt = esp_mqtt_client_init(&mqtt_cfg);
@@ -356,7 +362,7 @@ static void handle_message(char *client, char *topic, char *payload, int len, in
356362

357363
static void broker_task(void *ctx)
358364
{
359-
struct mosq_broker_config config = { .host = "192.168.4.1", .port = 3333, .handle_message_cb = handle_message };
365+
struct mosq_broker_config config = { .host = wifi_get_ipv4(WIFI_IF_AP), .port = CONFIG_EXAMPLE_MQTT_BROKER_PORT, .handle_message_cb = handle_message };
360366
mosq_broker_run(&config);
361367
vTaskDelete(NULL);
362368
}

components/mosquitto/examples/serverless_mqtt/main/wifi_connect.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ esp_err_t wifi_connect(void)
7171
wifi_config_t wifi_ap_config = {
7272
.ap = {
7373
.ssid = CONFIG_EXAMPLE_AP_SSID,
74-
.password = CONFIG_EXAMPLE_AP_SSID,
74+
.password = CONFIG_EXAMPLE_AP_PASSWORD,
75+
.authmode = WIFI_AUTH_WPA2_PSK,
7576
.max_connection = 4,
7677
},
7778
};
@@ -107,3 +108,15 @@ esp_err_t wifi_connect(void)
107108
return ret;
108109

109110
}
111+
112+
_Thread_local char s_ipv4_addr[4 * 4]; // 4 octets + '.'/term
113+
114+
char *wifi_get_ipv4(wifi_interface_t interface)
115+
{
116+
esp_netif_t *netif = esp_netif_get_handle_from_ifkey(interface == WIFI_IF_AP ? "WIFI_AP_DEF" : "WIFI_STA_DEF");
117+
ESP_RETURN_ON_FALSE(netif, NULL, TAG, "Failed to find default Wi-Fi netif");
118+
esp_netif_ip_info_t ip_info;
119+
ESP_RETURN_ON_FALSE(esp_netif_get_ip_info(netif, &ip_info) == ESP_OK, NULL, TAG, "Failed to get IP from netif");
120+
ESP_RETURN_ON_FALSE(esp_ip4addr_ntoa(&ip_info.ip, s_ipv4_addr, sizeof(s_ipv4_addr)) != NULL, NULL, TAG, "Failed to convert IP");
121+
return s_ipv4_addr;
122+
}

components/mosquitto/port/include/mosq_broker.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ struct mosq_broker_config {
2525
* You can open the respective docs with this idf.py command:
2626
* `idf.py docs -sp api-reference/protocols/esp_tls.html`
2727
*/
28-
void (*handle_message_cb)(char *client, char *topic, char *data, int len, int qos, int retain);
28+
void (*handle_message_cb)(char *client, char *topic, char *data, int len, int qos, int retain); /*!<
29+
* On message callback. If configured, user function is called
30+
* whenever mosquitto processes a message.
31+
*/
2932
};
3033

3134
/**

0 commit comments

Comments
 (0)