Open jorgie0 opened 8 months ago
You can use MQTT like a general Wi-Fi device. MQTT connects to the MQTT broker directly.
You can use MQTT like a general Wi-Fi device. MQTT connects to the MQTT broker directly.
Thank you for a very unhelpful answer. The request remains extant: "how to utilise MQTT while running ESP MESH LITE?" Please feel free to provide a link to how to implement a solution
@jorgie0 just to help out a tad here, basicaly treat the esp mesh just like wifi connect it should be a drop in replacement this is an example from the migration guide `` /Event handler for catching system events / static void event_handler(void arg, esp_event_base_t event_base, int32_t event_id, void event_data) { if (event_base == WIFI_PROV_EVENT) { switch (event_id) { case WIFI_PROV_START: ESP_LOGI(TAG, "Provisioning started"); break; case WIFI_PROV_CRED_RECV: { wifi_sta_config_t wifi_sta_cfg = (wifi_sta_config_t )event_data; ESP_LOGI(TAG, "Received Wi-Fi credentials" "\n\tSSID : %s\n\tPassword : %s", (const char ) wifi_sta_cfg->ssid, (const char ) wifi_sta_cfg->password);
mesh_lite_sta_config_t config;
memcpy((char*)config.ssid, (char*)wifi_sta_cfg->ssid, sizeof(config.ssid));
memcpy((char*)config.password, (char*)wifi_sta_cfg->password, sizeof(config.password));
config.bssid_set = wifi_sta_cfg->bssid_set;
if (config.bssid_set) {
memcpy((char*)config.bssid, (char*)wifi_sta_cfg->bssid, sizeof(config.bssid));
}
esp_mesh_lite_set_router_config(&config);
esp_mesh_lite_connect();
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
esp_wifi_set_config(ESP_IF_WIFI_STA, (wifi_config_t*)wifi_sta_cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_disconnect();
esp_wifi_connect();
break;
}
}
}
} `` all you need to do is use the mesh lite to get the wifi going instead of the normal wifi, and the exising in this case mqtt example will just run as before, just through other nodes vs each one connecting to router
@ThatBigPrint Thanks, that does provide an indication of the direction I need to go. While I am a software engineer I'm not a C coder. I can follow established patterns to get the functionality that I want, I'm next to useless with generating code when trying to expand on snippets of code. It would still be nice if Espressif were able to provide some more advanced examples.
@ThatBigPrint
After a few hours of playing around I have managed to get a working example of MQTT over ESP MESH LITE, there is an initial hiccup at the start:
I (6976) bridge_wifi: SoftAP IP network segment has changed, deauth all station I (6984) [vendor_ie]: RTC store: ssid:ESP_Bridge_0c39ad; bssid:e8:9f:6d:0c:39:ad crc:2216604720 E (8363) esp-tls: couldn't get hostname for :mqtt.eclipseprojects.io: getaddrinfo() returns 202, addrinfo=0x0 E (8364) transport_base: Failed to open a new connection: 32769 E (8370) mqtt_client: Error transport connect I (8375) local_control: MQTT_EVENT_ERROR E (8379) local_control: Last error reported from esp-tls: 0x8001 I (8386) local_control: Last errno string (Success)
After that it correctly connects to the broker and sends and received messages as expected
Full code is:
`
static int g_sockfd = -1; static const char *TAG = "local_control";
static void log_error_if_nonzero(const char *message, int error_code) { if (error_code != 0) { ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); } }
/*
@param event_data The data for the event, esp_mqtt_event_handle_t. / static void mqtt_event_handler(void handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_DISCONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); break;
case MQTT_EVENT_SUBSCRIBED: ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; case MQTT_EVENT_UNSUBSCRIBED: ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); break; case MQTT_EVENT_PUBLISHED: ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); break; case MQTT_EVENT_DATA: ESP_LOGI(TAG, "MQTT_EVENT_DATA"); printf("TOPIC=%.s\r\n", event->topic_len, event->topic); printf("DATA=%.s\r\n", event->data_len, event->data); break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); break; } }
static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = CONFIG_BROKER_URL, };
char line[128];
if (strcmp(mqtt_cfg.broker.address.uri, "FROM_STDIN") == 0) {
int count = 0;
printf("Please enter url of mqtt broker\n");
while (count < 128) {
int c = fgetc(stdin);
if (c == '\n') {
line[count] = '\0';
break;
} else if (c > 0 && c < 127) {
line[count] = c;
++count;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
mqtt_cfg.broker.address.uri = line;
printf("Broker url: %s\n", line);
} else {
ESP_LOGE(TAG, "Configuration mismatch: wrong broker url");
abort();
}
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(client);
}
/**
@brief Timed printing system information */ static void print_system_info_timercb(TimerHandle_t timer) { uint8_t primary = 0; uint8_t sta_mac[6] = {0}; wifi_ap_record_t ap_info = {0}; wifi_second_chan_t second = 0; wifi_sta_list_t wifi_sta_list = {0x0};
esp_wifi_sta_get_ap_info(&ap_info); esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac); esp_wifi_ap_get_sta_list(&wifi_sta_list); esp_wifi_get_channel(&primary, &second);
ESP_LOGI(TAG, "System information, channel: %d, layer: %d, self mac: " MACSTR ", parent bssid: " MACSTR ", parent rssi: %d, free heap: %"PRIu32"", primary, esp_mesh_lite_get_level(), MAC2STR(sta_mac), MAC2STR(ap_info.bssid), (ap_info.rssi != 0 ? ap_info.rssi : -120), esp_get_free_heap_size());
ESP_LOGI(TAG, "child node number: %d", esp_mesh_lite_get_child_node_number());
for (int i = 0; i < wifi_sta_list.num; i++) { ESP_LOGI(TAG, "Child mac: " MACSTR, MAC2STR(wifi_sta_list.sta[i].mac)); } }
static esp_err_t esp_storage_init(void) { esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
return ret;
}
static void wifi_init(void) { // Station wifi_config_t wifi_config = { .sta = { .ssid = CONFIG_ROUTER_SSID, .password = CONFIG_ROUTER_PASSWORD, }, }; esp_bridge_wifi_set_config(WIFI_IF_STA, &wifi_config);
// Softap
snprintf((char *)wifi_config.ap.ssid, sizeof(wifi_config.ap.ssid), "%s", CONFIG_BRIDGE_SOFTAP_SSID);
strlcpy((char *)wifi_config.ap.password, CONFIG_BRIDGE_SOFTAP_PASSWORD, sizeof(wifi_config.ap.password));
esp_bridge_wifi_set_config(WIFI_IF_AP, &wifi_config);
}
void app_wifi_set_softap_info(void) { char softap_ssid[32]; uint8_t softap_mac[6]; esp_wifi_get_mac(WIFI_IF_AP, softap_mac); memset(softap_ssid, 0x0, sizeof(softap_ssid));
snprintf(softap_ssid, sizeof(softap_ssid), "%.25s_%02x%02x%02x", CONFIG_BRIDGE_SOFTAP_SSID, softap_mac[3], softap_mac[4], softap_mac[5]);
snprintf(softap_ssid, sizeof(softap_ssid), "%.32s", CONFIG_BRIDGE_SOFTAP_SSID);
esp_mesh_lite_set_softap_ssid_to_nvs(softap_ssid);
esp_mesh_lite_set_softap_psw_to_nvs(CONFIG_BRIDGE_SOFTAP_PASSWORD);
esp_mesh_lite_set_softap_info(softap_ssid, CONFIG_BRIDGE_SOFTAP_PASSWORD);
}
void app_main() { /**
@brief Set the log level for serial port printing. / esp_log_level_set("", ESP_LOG_INFO);
esp_storage_init();
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_bridge_create_all_netif();
wifi_init();
esp_mesh_lite_config_t mesh_lite_config = ESP_MESH_LITE_DEFAULT_INIT(); esp_mesh_lite_init(&mesh_lite_config);
app_wifi_set_softap_info();
esp_mesh_lite_start();
/**
@breif Create handler */ mqtt_app_start();
TimerHandle_t timer = xTimerCreate("print_system_info", 10000 / portTICK_PERIOD_MS, true, NULL, print_system_info_timercb); xTimerStart(timer, 0); } `
No details are available for how to utilise MQTT while running ESP MESH LITE. The user guide mentions that MQTT can be used. Would be an excellent improvement if an example was created that showed MQTT usage.