The use of the ESP8266 in the world of IoT

User avatar
By ficeto
#11404
It seems that the espconn takes the last incoming UDP packet's source IP address and uses that as the RemoteIP for subsequent transmissions.


which espconn object are you using when sending? Can you not edit the remote_ip of the object before sending data? You did not post much source to see where the problem might be ;)
User avatar
By Demolitron
#11412
ficeto wrote:which espconn object are you using when sending? Can you not edit the remote_ip of the object before sending data? You did not post much source to see where the problem might be ;)


Fair enough. :)

*Error handling has been removed for clarity.*
This is my initialization function: uPnP_Con is a global struct holding the espconn struct. This function allocates it and sets up the connection for UDP on the correct port and IP, Opens the connection, and joins the Multicast Group.
Code: Select all    uPnP_Con.pCon = (struct espconn *) os_zalloc(sizeof (struct espconn));
    uPnP_Con.pCon->type = ESPCONN_UDP;
    uPnP_Con.pCon->state = ESPCONN_NONE;
    uPnP_Con.pCon->proto.udp = (esp_udp *) os_zalloc(sizeof (esp_udp));
    uPnP_Con.pCon->proto.udp->local_port = 1900;
    uPnP_Con.pCon->proto.udp->remote_port = 1900;
    uPnP_Con.pCon->proto.udp->remote_ip[0] = 239;
    uPnP_Con.pCon->proto.udp->remote_ip[1] = 255;
    uPnP_Con.pCon->proto.udp->remote_ip[2] = 255;
    uPnP_Con.pCon->proto.udp->remote_ip[3] = 250;

    uPnP_Con.pCon->reverse = NULL;
    res = espconn_regist_recvcb(uPnP_Con.pCon, slip_uPnP_OnRecieve);
    res = espconn_regist_sentcb(uPnP_Con.pCon, slip_uPnP_OnSendComplete);
    res = espconn_create(uPnP_Con.pCon);
    IP4_ADDR(&uPnP_MulticastIP.ip, 239, 255, 255, 250);
    res = espconn_igmp_join(&LocalIP.ip, &uPnP_MulticastIP.ip);
    }


Here is the On Receive Callback:
Code: Select allvoid ICACHE_FLASH_ATTR slip_uPnP_OnRecieve(void *arg, char *pdata, unsigned short len) {
    SendMessage(ESP_UPNP_RECIEVE, 0, 0, 0, 0, 0, 0, (uint8_t *) pdata, len);
}


And the On Sent Complete Callback:
Code: Select allvoid ICACHE_FLASH_ATTR slip_uPnP_OnSendComplete(void *arg) {
    if (uPnP_Con.isSending == false) return;
    uPnP_Con.isSending = false;
    SendMessage(ESP_UPNP_SEND_RESULT, 0, SendOK, 0, 0, 0, 0, NULL, 0);
}


And finally here is the call I would make to send data to the Multicast Group:
Code: Select allvoid ICACHE_FLASH_ATTR cmd_MCU_uPnP_ASYNCSEND(uint8_t *buffer, uint16_t len) {   
    uPnP_Con.isSending = true;
    sint8_t res = espconn_sent(uPnP_Con.pCon, buffer, len);
}


This code will receive packets as I expect. When I send data a UDP packet is sent with the correct (1900) local and remote port numbers, the local IP corresponds with the ESP's IP address, and the remote IP is a unicast address equal to my desktop PC which is the only thing TX'ing on the network. I need it to send to the MultiCast address instead.

In an effort to force it to send on the multicast IP address I have tried the following modification to the md_MCU_uPnP_ASYNCSEND function:
Code: Select allvoid ICACHE_FLASH_ATTR cmd_MCU_uPnP_ASYNCSEND(uint8_t *buffer, uint16_t len) {
    uPnP_Con.isSending = true;

    uPnP_Con.pCon.proto.udp->local_port = 1900;
    uPnP_Con.pCon.proto.udp->remote_port = 1900;
    uPnP_Con.pCon.proto.udp->remote_ip[0] = 239;
    uPnP_Con.pCon.proto.udp->remote_ip[1] = 255;
    uPnP_Con.pCon.proto.udp->remote_ip[2] = 255;
    uPnP_Con.pCon.proto.udp->remote_ip[3] = 250;

    sint8_t res = espconn_sent(uPnP_Con.pCon, buffer, len);
}


Using this code I cannot see ANY packets on the network coming from the ESP. *Note: I use wireshark to sniff the network.* The ESP still receives the uPnP multicast packets being sent by my PC.


Thanks for your help.
User avatar
By ehizogie
#13425 Hi everyone,
a quick question on device discovery.
The first challenge is getting the ioT device on a specific wifi networks without hardcoding the SSID into the firmware.

does anyone have any ideas on how this could be done ?

Ehi