espressif / esp-mesh-lite

A lite version Wi-Fi Mesh, each node can access the network over the IP layer.
135 stars 22 forks source link

Example usage for MQTT while using ESP MESH LITE. (AEGHB-567) #66

Open jorgie0 opened 8 months ago

jorgie0 commented 8 months ago

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.

xcguang commented 8 months ago

You can use MQTT like a general Wi-Fi device. MQTT connects to the MQTT broker directly.

jorgie0 commented 8 months ago

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

ThatBigPrint commented 8 months ago

@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);

if CONFIG_MESH_LITE_ENABLE

            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();

else

            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();

endif / CONFIG_MESH_LITE_ENABLE /

            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

jorgie0 commented 8 months ago

@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.

jorgie0 commented 8 months ago

@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:

`

include

include "esp_log.h"

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "freertos/timers.h"

include "esp_wifi.h"

include "nvs_flash.h"

include <sys/socket.h>

include "protocol_examples_common.h"

include "freertos/semphr.h"

include "freertos/queue.h"

include "lwip/sockets.h"

include "lwip/dns.h"

include "lwip/netdb.h"

include "esp_log.h"

include "mqtt_client.h"

define CONFIG_BROKER_URL "mqtt://mqtt.eclipseprojects.io"

if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)

include "esp_mac.h"

endif

include "esp_bridge.h"

include "esp_mesh_lite.h"

define PAYLOAD_LEN (1456) /*< Max payload size(in bytes) /

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); } }

/*

static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = CONFIG_BROKER_URL, };

if CONFIG_BROKER_URL_FROM_STDIN

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();
}

endif / CONFIG_BROKER_URL_FROM_STDIN /

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);

}

/**

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));

ifdef CONFIG_BRIDGE_SOFTAP_SSID_END_WITH_THE_MAC

snprintf(softap_ssid, sizeof(softap_ssid), "%.25s_%02x%02x%02x", CONFIG_BRIDGE_SOFTAP_SSID, softap_mac[3], softap_mac[4], softap_mac[5]);

else

snprintf(softap_ssid, sizeof(softap_ssid), "%.32s", CONFIG_BRIDGE_SOFTAP_SSID);

endif

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() { /**