Cant Convert Unicast to MultiCast.

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

Cant Convert Unicast to MultiCast.

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

So I have this code that was from a unicast example that I found and worked very well but now I would like to make it into Multicast mesh. You will see there are many attempts of where I have tried to make it into Multicast. This is the current code that works for unicast:
Thanks for any help.

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"

// sense_main.c
// Light_example.c
// aliyun_mdf.c
// ret = mwifi_root_write(dest_addrs, dest_addrs_num, &data_type, data, size, true);  //THIS ONE ? ???? ?
//mwifi.c    IS WHAT U NEED

// #define MEMORY_DEBUG

static const char *TAG = "ROOT";

static void transmiter_task(void *arg) {    // 1 / 3 Is executed at bottom  //This is the transmission function 

    mdf_err_t ret                  = MDF_OK; 
    mwifi_data_type_t data_type    = {0};
    //mwifi_data_type_t data_type    = {MWIFI_COMMUNICATE_MULTICAST};
    data_type.communicate = MWIFI_COMMUNICATE_MULTICAST;    //Type of Transmission 
    // const uint8_t group_id[MWIFI_ADDR_LEN] = {0xa8, 0x03, 0x2a, 0xc0, 0x46, 0x44};   //MWIFI_ADDR_LEN Is the Mac Adress
     const uint8_t group_id[MWIFI_ADDR_LEN] = {0x24, 0x0a, 0xc4, 0x58, 0x7e, 0xd8};
    char *data                     = MDF_MALLOC(MWIFI_PAYLOAD_LEN);   //MCOMMON Helps with memmory leak ?  //MWIFI_PAYLOAD_LEN Max Payload size 
    size_t size;

    MDF_LOGI("Transmiter task is running");    // MDF_LOGI(Format...) ??

    //Wait for node connection
    vTaskDelay(5000 / portTICK_RATE_MS);
    for (int i = 0;; i=i+1)                                             //Data What is sent 
    {
        size = sprintf(data, "Sending message: %d", i);     //sprintf() Fancy printf(); 
        MDF_LOGI("Sending message: %d", i);                                      //MDF_LOGI to log data
  //     ret = mwifi_write(group_id, &data_type, data, size, true);     //Original
//mwifi_write(const uint8_t *dest_addrs, const mwifi_data_type_t *data_type,               //Writes here
//                      const void *data, size_t size, bool block)
//ret = mwifi_root_write(send_to_everyone,1,&data_type,sleep_msg,size,true);
 //ret = mwifi_write(NULL, &data_type, data, size, ​true​);
 //ret = mwifi_write(dest_addr, &data_type, send_data, strlen(send_data), true);
 ret = mwifi_write(group_id, &data_type, data, size, true);
    // return ret;
    vTaskDelay(5000);
    }
    MDF_LOGW("Transmiter task is exit");

    MDF_FREE(data);
    vTaskDelete(NULL);    
}

static mdf_err_t wifi_init()    //Function 
{
    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)   //Function 
{
    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()    //I put my data in here, SUch as running the LEDs or Buzzer
{
    mwifi_init_config_t cfg   = MWIFI_INIT_CONFIG_DEFAULT();
    mwifi_config_t config = {
        .channel   = CONFIG_MESH_CHANNEL,
        .mesh_id   = CONFIG_MESH_ID,
        .mesh_type =  MESH_ROOT,
    //  .mesh_id   = 13,
        //.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));  //Used for conecting to external network
    MDF_ERROR_ASSERT(wifi_init());                          //Used for conecting to external network
    MDF_ERROR_ASSERT(mwifi_init(&cfg));
    MDF_ERROR_ASSERT(mwifi_set_config(&config));
    MDF_ERROR_ASSERT(mwifi_start());
/*
    mesh_addr_t group_id = { .addr = {0xa8, 0x03, 0x2a, 0xc0, 0x46, 0x44}};
    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);    //one of "transmiter_task" is at the top where it is set up  
    
}

Who is online

Users browsing this forum: Google [Bot], netfox and 162 guests