ESP32-带有本地LwIP库的UDP广播器/接收器 [英] ESP32 - UDP broadcaster/ receiver with native LwIP library

查看:1063
本文介绍了ESP32-带有本地LwIP库的UDP广播器/接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ESP32(一个不错的平台)构建一个分布式应用程序,其中所有参与者都应以最简单的形式通过UDP进行通信:通过广播发送消息并收听所有浮动消息.每个参与者自己过滤相关的消息.

I'm building a distributed application with the ESP32 (a great platform btw) where all participants should communicate over UDP in it's simplest form: sending messages via broadcast and listening to all messages floating around. Each participant filters the relevant messages by itself.

到目前为止,我有以下初始化例程:

So far, I have the following initialization routine:

int lavor_wifi_openUDPsocket(){
    // Create a socket
    int sckt = socket(AF_INET, SOCK_DGRAM, 0);
    if ( sckt < 0 ){
        printf("socket call failed");
        exit(0);
    }

    // Prepare binding to port
    struct sockaddr_in sLocalAddr;
    // Initialize the address
    memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));

    sLocalAddr.sin_family = AF_INET;
    sLocalAddr.sin_len = sizeof(sLocalAddr);
    sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    sLocalAddr.sin_port = UDP_SOCKET_PORT;

    bind(sckt, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));

    return sckt;
}

然后,将使用以下消息发送消息:

Then, a message would be send with:

void lavor_wifi_sendUDPmsg(int sckt, char* msg, int len){
    // Prepare the address to sent to via BROADCAST
    struct sockaddr_in sDestAddr;
    // Initialize the address
    // memset((char *)&sDestAddr, 0, sizeof(sDestAddr));

    sDestAddr.sin_family = AF_INET;
    sDestAddr.sin_len = sizeof(sDestAddr);
    sDestAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    sDestAddr.sin_port = UDP_SOCKET_PORT;

    if(sendto(sckt, msg, len, 0, (struct sockaddr *)&sDestAddr, sizeof(sDestAddr)) < len){
         printf("UDP message couldn't be sent.");
    }
}

最后,接收消息将像这样:

And finally, receiving messages would work like this:

void lavor_wifi_processor(void* sckt){
    int nbytes;
    char buffer[UDP_BUFF_LEN];
    // Listen for incoming messages as long as the socket is open
    while(1){   // TO DO: Test if socket open
        // Try to read new data arrived at the socket
        nbytes = recv(*((int *)sckt), buffer, sizeof(buffer), 0);
    ...

但是,即使我只是尝试调用上面的初始化函数,ESP也会疯狂运行,并引发一个Guru Meditation错误.

But even if I just try to call the above initialization function, the ESP goes wild and throws one Guru Meditation error after another.

有人用所述的方式进行UDP通信吗?

Does anyone has experience with UDP communication in the descripted way?

推荐答案

我获得了带有ESP32/UDP/LWIP的UDP来使用此示例:

I got UDP with ESP32/UDP/LWIP to work with this example:

https://github.com/Ebiroll/qemu_esp32/blob/master/examples/19_udp

请注意,只有在我们收到IP地址后,send_thread()才会启动.

Note that send_thread() is not started until we have received an ip address.

但是,您也可能需要esp-idf v2.0.我也得到了最新版本的Guru Meditation错误.

However it also might also be that you need v2.0 of esp-idf. I also got Guru Meditation errors with latest version.

这篇关于ESP32-带有本地LwIP库的UDP广播器/接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆