ESP-MDF multicasting by group

meloon
Posts: 1
Joined: Thu Dec 13, 2018 11:05 am

ESP-MDF multicasting by group

Postby meloon » Thu Dec 13, 2018 11:38 am

Hi,

I'm trying to establish mesh network with 10 ESP32-LyraT boards. At this moment I prepared simply transmiter code running on ROOT device and receiver running on NODE. If I send message with predefined MAC address everything works fine, but when I defined mesh group NODE doesn't receive any message. Unfortunately it's really hard to find any example code related to mesh group. Maybe someone have some expirience with multicast groups and could help me?

Thanks in advance
David

ROOT device code

Code: Select all

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/ringbuf.h"
#include "esp_log.h"
#include "sdkconfig.h"

#include "mdf_common.h"
#include "mwifi.h"

#include "driver/gpio.h"


// #define MEMORY_DEBUG

static const char *TAG = "ROOT";

static void transmiter_task(void *arg)
{
    mdf_err_t ret                  = MDF_OK;
    mwifi_data_type_t data_type    = {0};
    data_type.communicate = MWIFI_COMMUNICATE_BROADCAST;
    const uint8_t group_id[MWIFI_ADDR_LEN] = {0x01, 0x00, 0x5e, 0xde, 0xde, 0xde};
    //const uint8_t group_id[MWIFI_ADDR_LEN] = {0x24, 0x0a, 0xc4, 0x9d, 0x06, 0x50};
    char *data                     = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
    size_t size;

    MDF_LOGI("Transmiter task is running");

    //Wait for node connection
    vTaskDelay(5000 / portTICK_RATE_MS);
    for (int i = 0;; ++i) 
    {
        size = sprintf(data, "Sending message: %d", i);
        MDF_LOGI("Sending message: %d", i);
        ret = mwifi_write(group_id, &data_type, data, size, true);
        MDF_ERROR_CONTINUE(ret != MDF_OK, "<%s> mwifi_write", mdf_err_to_name(ret));
        vTaskDelay(1000 / portTICK_RATE_MS);
    }
    MDF_LOGW("Transmiter task is exit");

    MDF_FREE(data);
    vTaskDelete(NULL);    
}

static mdf_err_t wifi_init()
{
    mdf_err_t ret          = nvs_flash_init();
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        MDF_ERROR_ASSERT(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    MDF_ERROR_ASSERT(ret);

    tcpip_adapter_init();
    MDF_ERROR_ASSERT(esp_event_loop_init(NULL, NULL));
    MDF_ERROR_ASSERT(esp_wifi_init(&cfg));
    MDF_ERROR_ASSERT(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    MDF_ERROR_ASSERT(esp_wifi_set_mode(WIFI_MODE_STA));
    MDF_ERROR_ASSERT(esp_wifi_set_ps(WIFI_PS_NONE));
    MDF_ERROR_ASSERT(esp_mesh_set_6m_rate(false));
    MDF_ERROR_ASSERT(esp_wifi_start());

    return MDF_OK;
}

/**
 * @brief All module events will be sent to this task in esp-mdf
 *
 * @Note:
 *     1. Do not block or lengthy operations in the callback function.
 *     2. Do not consume a lot of memory in the callback function.
 *        The task memory of the callback function is only 4KB.
 */
static mdf_err_t event_loop_cb(mdf_event_loop_t event, void *ctx)
{
    MDF_LOGI("event_loop_cb, event: %d", event);

    switch (event) {
        case MDF_EVENT_MWIFI_STARTED:
            MDF_LOGI("MESH is started");
            break;

        case MDF_EVENT_MWIFI_PARENT_CONNECTED:
            MDF_LOGI("Parent is connected on station interface");
            break;

        case MDF_EVENT_MWIFI_PARENT_DISCONNECTED:
            MDF_LOGI("Parent is disconnected on station interface");
            break;

        default:
            break;
    }

    return MDF_OK;
}

void app_main()
{
    mwifi_init_config_t cfg   = MWIFI_INIT_CONFIG_DEFAULT();
    mwifi_config_t config = {
        .channel   = CONFIG_MESH_CHANNEL,
        .mesh_id   = CONFIG_MESH_ID,
        .mesh_type = CONFIG_DEVICE_TYPE,
    };
    /**
     * @brief Set the log level for serial port printing.
     */
    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_DEBUG);

    /**
     * @brief Initialize wifi mesh.
     */
    MDF_ERROR_ASSERT(mdf_event_loop_init(event_loop_cb));
    MDF_ERROR_ASSERT(wifi_init());
    MDF_ERROR_ASSERT(mwifi_init(&cfg));
    MDF_ERROR_ASSERT(mwifi_set_config(&config));
    MDF_ERROR_ASSERT(mwifi_start());

    mesh_addr_t group_id = { .addr = {0x01, 0x00, 0x5e, 0xde, 0xde, 0xde}};
    ESP_ERROR_CHECK(esp_mesh_set_group_id(&group_id,1));


    /*** Transmiter start ***/
    xTaskCreate(transmiter_task, "transmiter_task", 16 * 1024, NULL, CONFIG_MDF_TASK_DEFAULT_PRIOTY, NULL);
}
NODE device code

Code: Select all

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/ringbuf.h"
#include "esp_log.h"
#include "sdkconfig.h"

#include "mdf_common.h"
#include "mwifi.h"

#include "driver/gpio.h"

// #define MEMORY_DEBUG

static const char *TAG = "NODE";

static void receiver_task(void *arg)
{
    mdf_err_t ret                  = MDF_OK;
    char *data                     = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
    size_t size                    = MWIFI_PAYLOAD_LEN;
    uint8_t src_addr[MWIFI_ADDR_LEN] = {0x0};
    mwifi_data_type_t data_type    = {0};

    MDF_LOGI("Receiver task is running");

#if CONFIG_DEVICE_TYPE != MESH_ROOT
    while(mwifi_is_connected() == false) {
        vTaskDelay(500 / portTICK_RATE_MS);
        ESP_LOGI(TAG, "[Wait for root connected");
        continue;
    }  
#endif
    for (int i = 0;; ++i) 
    {
        size = MWIFI_PAYLOAD_LEN;
        memset(data, 0, MWIFI_PAYLOAD_LEN);
        ESP_LOGI(TAG, "Receive...");
        ret = mwifi_read(src_addr, &data_type, data, &size, portMAX_DELAY);
        ESP_LOGI(TAG, "Received data:  %s", data);
        MDF_ERROR_CONTINUE(ret != MDF_OK, "<%s> mwifi_read", mdf_err_to_name(ret));
        vTaskDelay(100 / portTICK_RATE_MS);
    }

    MDF_LOGW("Receiver task is exit");
    MDF_FREE(data);
    vTaskDelete(NULL);
}

static mdf_err_t wifi_init()
{
    mdf_err_t ret          = nvs_flash_init();
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        MDF_ERROR_ASSERT(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    MDF_ERROR_ASSERT(ret);

    tcpip_adapter_init();
    MDF_ERROR_ASSERT(esp_event_loop_init(NULL, NULL));
    MDF_ERROR_ASSERT(esp_wifi_init(&cfg));
    MDF_ERROR_ASSERT(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    MDF_ERROR_ASSERT(esp_wifi_set_mode(WIFI_MODE_STA));
    MDF_ERROR_ASSERT(esp_wifi_set_ps(WIFI_PS_NONE));
    MDF_ERROR_ASSERT(esp_mesh_set_6m_rate(false));
    MDF_ERROR_ASSERT(esp_wifi_start());

    return MDF_OK;
}

/**
 * @brief All module events will be sent to this task in esp-mdf
 *
 * @Note:
 *     1. Do not block or lengthy operations in the callback function.
 *     2. Do not consume a lot of memory in the callback function.
 *        The task memory of the callback function is only 4KB.
 */
static mdf_err_t event_loop_cb(mdf_event_loop_t event, void *ctx)
{
    MDF_LOGI("event_loop_cb, event: %d", event);

    switch (event) {
        case MDF_EVENT_MWIFI_STARTED:
            MDF_LOGI("MESH is started");
            break;

        case MDF_EVENT_MWIFI_PARENT_CONNECTED:
            MDF_LOGI("Parent is connected on station interface");
            break;

        case MDF_EVENT_MWIFI_PARENT_DISCONNECTED:
            MDF_LOGI("Parent is disconnected on station interface");
            break;

        default:
            break;
    }

    return MDF_OK;
}

void app_main()
{
    mwifi_init_config_t cfg   = MWIFI_INIT_CONFIG_DEFAULT();
    mwifi_config_t config = {
        .channel   = CONFIG_MESH_CHANNEL,
        .mesh_id   = CONFIG_MESH_ID,
        .mesh_type = CONFIG_DEVICE_TYPE,
    };    

    /**
     * @brief Set the log level for serial port printing.
     */
    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_DEBUG);

    /**
     * @brief Initialize wifi mesh.
     */
    MDF_ERROR_ASSERT(mdf_event_loop_init(event_loop_cb));
    MDF_ERROR_ASSERT(wifi_init());
    MDF_ERROR_ASSERT(mwifi_init(&cfg));
    MDF_ERROR_ASSERT(mwifi_set_config(&config));
    MDF_ERROR_ASSERT(mwifi_start());

    mesh_addr_t group_id = { .addr = {0x01, 0x00, 0x5e, 0xde, 0xde, 0xde}};
    ESP_ERROR_CHECK(esp_mesh_set_group_id(&group_id,1));

    /*** Receiver start ***/
    xTaskCreate(receiver_task, "receiver_task", 16 * 1024, NULL, CONFIG_MDF_TASK_DEFAULT_PRIOTY, NULL);
}

ESP_LBB
Posts: 108
Joined: Fri May 18, 2018 3:41 am

Re: ESP-MDF multicasting by group

Postby ESP_LBB » Mon Dec 17, 2018 9:40 am

Hello meloon,

Yes we haven't developed this group function in MDF yet. We thought that the broadcast is not a reliable way, so we lowed its priority.

The current situation, if we want to control several devices defined as a "group", we just forward the message to them by unicast with their mac address directly. This can be done by the app.

The next demo that will be released soon(by this week), will use multicast. This can increase the speed if we want to control all devices.

The next step will be to add group id into this function, so that we can control certain devices by group id. I think whis can meet your requirements.

Then we will try with broadcast by increasing its success rate. But we don't reconmand this.

Also, if it's not classified, could you share us the scenario that you want to apply this group function ? Maybe we can come up with a suitable sugestion ?

DutchOrange
Posts: 74
Joined: Fri Dec 04, 2020 4:23 pm

Re: ESP-MDF multicasting by group

Postby DutchOrange » Fri Jan 08, 2021 3:13 pm

Are there any Examples up yet for the Multicast :?:
Thanks.

XiotSamuel
Posts: 53
Joined: Sun Aug 29, 2021 1:50 pm

Re: ESP-MDF multicasting by group

Postby XiotSamuel » Wed Sep 29, 2021 8:45 am

Any one have example for the multicast and device grouping.

https://docs.espressif.com/projects/esp ... mwifi.html

I can only find same code in no-router-example in function demo. anyone have better coding example?

Who is online

Users browsing this forum: No registered users and 14 guests