ESP-MDF multicasting by group
Posted: 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
NODE device code
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);
}
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);
}