data transmission over wifi using esp32 on gr lychee.

Hi, 

Can you please provide example for sending recorded data over wifi using GR LYchee module with esp32? 

 

Thanks.

 

Regards

Anusha

  • Hi,
    I am assuming that you are processing data into GR Lychee and want to transfer that data over wifi using ESP32, if that is the case then you can use TCP Socket communication,

    if you are new to socket, it has Client Server model in which Client basically connects to the Socket Server running into your system with a IP and Port defined and then it sends the data over Wifi,but for this you need a Socket Server which is in listen state for your Port written in any language

    Below example does the same, it connects to the Socket Server and then it transfer your data from Client to Server using Send method, and on the Server Side you can Receive this data for further processing,

    This is Client side example,but you can create Socket Server also using ESP32,

    #include <Arduino.h>
    #include "TCPSocket.h"
    #include "ESP32Interface.h"

    #define MBED_CONF_APP_WIFI_SSID "Test" //change details here
    #define MBED_CONF_APP_WIFI_PASSWORD "oooooooo" // "
    ESP32Interface wifi;

    const char *sec2str(nsapi_security_t sec)
    {
    switch (sec) {
    case NSAPI_SECURITY_NONE:
    return "None";
    case NSAPI_SECURITY_WEP:
    return "WEP";
    case NSAPI_SECURITY_WPA:
    return "WPA";
    case NSAPI_SECURITY_WPA2:
    return "WPA2";
    case NSAPI_SECURITY_WPA_WPA2:
    return "WPA/WPA2";
    case NSAPI_SECURITY_UNKNOWN:
    default:
    return "Unknown";
    }
    }

    int scan_demo(WiFiInterface *wifi)
    {
    WiFiAccessPoint *ap;
    int count = 15; /* Limit number of network arbitrary to 15 */

    printf("Scan:\r\n");

    ap = new WiFiAccessPoint[count];
    count = wifi->scan(ap, count);
    for (int i = 0; i < count; i++)
    {
    printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
    sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
    ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
    }
    printf("%d networks available.\r\n", count);

    delete[] ap;

    return count;
    }

    void setup() {
    printf("WiFi example\r\n\r\n");

    while (scan_demo(&wifi) == 0);

    printf("\r\nConnecting...\r\n");
    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    if (ret != 0) {
    printf("\r\nConnection error\r\n");
    while(1);
    }

    printf("IP: %s\r\n", wifi.get_ip_address());

    TCPSocket socket;

    // Open a socket on the network interface, and create a TCP connection
    socket.open(&wifi);
    socket.connect(wifi.get_ip_address(), 9090); // add your local IP on which Server is listening

    // Send a simple request
    char sbuffer[] = "Your Recorded Data";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory and bring down the network interface
    socket.close();

    wifi.disconnect();

    printf("\r\nDone\r\n");
    }

    void loop() {

    }

    I hope this works for you :)
  • In reply to RiteshGohil:

    Hi, Thank you for this.

    I tried compiling the above code but it is throwing error in IDE for GR as well as the mbed compiler... please check and let me know thank you again !
  • In reply to anuramiyer:

    Okay!
    mbed compiler will give you error, as online will not compile this code because it uses Arduino style code so that is fine,
    but it should work on IDE for GR! I need to find this
    I have used Online compiler to compile this code from,
    tool-cloud2.renesas.com/index.php
    you can also use this compiler to build this code, it should work .
    Thanks
  • In reply to RiteshGohil:

    Ok I will check and get back. Thanks
  • In reply to anuramiyer:

    Hey,
    I have checked the code, it is compiling in IDE for GR without any error