Loewion
Loewion

Reputation: 21

ESP mesh send package from root node to a computer

I want to send packages from a node in a esp mesh to my computer using c with the esp-idf. the esp mesh documentation says, that you have to call the esp_mesh_send function and set the to parameter to null, and the address parameter to a ip4 address port peer.

Then you have to call esp_mesh_recv_toDS on the root node to get the package according to the documentation. Next you have to send the package to your final address. This also works for me up to the point of sending the data from the root node to the PC

I try to use a TCP socket for the problem. without the using the mesh my TCP socket works great. but. with the mesh it doesn't work. I tested the connection to my access point , the WLAN from my esp in successfully with the access point from my network connected

Here is my socket code:

Without the mesh it works, but with the mesh it doesn't work, and in both cases the WLAN is successfully connected to the esp and my computer is in the same network

void wifiTest(void){
    wifi_ap_record_t ap_info;
    esp_err_t err = esp_wifi_sta_get_ap_info(&ap_info);
    if (err == ESP_OK) {
        
        ESP_LOGI("wifiTest", "succesfullx conected with SSID: %s\n", ap_info.ssid);
    } else {

        ESP_LOGE("wifiTest", "not connected\n");
    }
}


void root_task(void ) {
    vTaskDelay(10000 / portTICK_PERIOD_MS);
    wifiTest();
    // initialization of the basic variables 
    char rx_buffer[128];
    char host_ip[] = CONFIG_SERVER_IP;

    // initialize thr server variables
    struct sockaddr_in dest_addr;
    inet_pton(AF_INET, host_ip, &dest_addr.sin_addr);
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(CONFIG_SERVER_PORT);
    int addr_family = AF_INET;
    int ip_protocol = IPPROTO_IP;
    int sock = -1;

    // initialize error variables
    int sendError = 0;
    int receiveError = 0;
    int buildError = 0;
    
    // initialization finished
    ESP_LOGI(ROOT, "Socket basic variables initialized");

    while(1) {
        // create socket
        sock =  socket(addr_family, SOCK_STREAM, ip_protocol);
        if (sock < 0) {
            ESP_LOGE(ROOT, "Unable to create socket: errno %d", errno);
            buildError++;
        }
        ESP_LOGI(ROOT, "Socket created, connecting to %s:%d", host_ip, CONFIG_SERVER_PORT);

        //connect socket to server
        int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
        if (err != 0) {
            ESP_LOGE(ROOT, "Socket unable to connect: errno %d", errno);
            buildError++;
        }
        ESP_LOGI(ROOT, "Successfully connected");
        buildError = 0;

        while (1) {
            //send message
            int err = send(sock, "payload", strlen("payload"), 0);
            if (err < 0) {
                ESP_LOGE(ROOT, "Error occurred during sending: errno %d", errno);
                sendError++;
                break;
            }
            sendError = 0;

            // receive message
            int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
            if (len < 0) {
                ESP_LOGE(ROOT, "recv failed: errno %d", errno);
                receiveError++;
                break;
            }else {
                rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
                ESP_LOGI(ROOT, "Received %d bytes from %s:", len, host_ip);
                ESP_LOGI(ROOT, "%s", rx_buffer);
                receiveError = 0;
            }

            // wait 1 second
            vTaskDelay(1000 / portTICK_PERIOD_MS);
        }    

        //error handling
        if(sendError>MAXERROR || receiveError>MAXERROR || buildError>MAXERROR){
            ESP_LOGE(ROOT, "root task stopped  (socket build errors: %d) (socket send errors: %d) (socket receive Errors: %d)"
            ,buildError, sendError, receiveError);
            break;
        }else{
            ESP_LOGE(ROOT, "socket build errors: %d  socket send errors: %d  socket receive Errors: %d" ,buildError, sendError, receiveError);
            ESP_LOGE(ROOT, "In 2 secunde socket buildig will tryed again");
            vTaskDelay(2000 / portTICK_PERIOD_MS);
        }

        ESP_LOGE(ROOT, "Shutting down socket and restarting...");
        shutdown(sock, 0);
        close(sock);
    }
    #
    // close socket and release resources
    if (sock != -1) {
        ESP_LOGE(ROOT, "Shutting down socket and restarting...");
        shutdown(sock, 0);
        close(sock);
    }
    
}

Upvotes: 2

Views: 28

Answers (0)

Related Questions