Chat freely about anything...

User avatar
By Bontor Humala
#25287 Hello, I am trying to broadcast an UDP message in a network. All corresponding devices should respond to my message "DiscoverDevice". I successfully receive UDP packets from these devices, but I need their IP address. Does ESP8266 SDK allows me to retrieve IP address from UDP packet? I attach example code:

Code: Select all#include "discover.h"

LOCAL struct espconn Conn;
static ETSTimer WiFiLinker;

uint8 udpServerIP[] = { 255, 255, 255, 255 };

/******************************************************************************
 * FunctionName : user_devicefind_recv
 * Description  : Processing the received data from the host
 * Parameters   : arg -- Additional argument to pass to the callback function
 *                pusrdata -- The received data (or NULL when the connection has been closed!)
 *                length -- The length of received data
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
recvdata(void *arg, char *pusrdata, unsigned short length)
{
   os_printf("Recv data callback\n");
    char DeviceBuffer[40] = {0};
    char Device_mac_buffer[60] = {0};
    char hwaddr[6];

    struct ip_info ipconfig;

    if (wifi_get_opmode() != STATION_MODE) {
        wifi_get_ip_info(SOFTAP_IF, &ipconfig);
        wifi_get_macaddr(SOFTAP_IF, hwaddr);

        if (!ip_addr_netcmp((struct ip_addr *)Conn.proto.udp->remote_ip, &ipconfig.ip, &ipconfig.netmask)) {
            wifi_get_ip_info(STATION_IF, &ipconfig);
            wifi_get_macaddr(STATION_IF, hwaddr);
        }
    } else {
        wifi_get_ip_info(STATION_IF, &ipconfig);
        wifi_get_macaddr(STATION_IF, hwaddr);
    }

    if (pusrdata == NULL) {
        return;
    }
   else {
      os_printf("recv data: %s\n", pusrdata); // only contains received UDP messages, I need the IP address
   }
}

void DISCOVER_Init(void) {
   os_printf("Entering DISCOVER mode\n");

      Conn.type = ESPCONN_UDP;
       Conn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
      Conn.proto.udp->local_port = 15002;
       Conn.proto.udp->remote_port = 15002;   
   os_memcpy(Conn.proto.udp->remote_ip, udpServerIP, 4);
       espconn_regist_recvcb(&Conn, recvdata);
       espconn_create(&Conn);

   os_timer_disarm(&WiFiLinker);
   os_timer_setfn(&WiFiLinker, (os_timer_func_t *)senddata, NULL);
   os_timer_arm(&WiFiLinker, 15000, 0);
}

static void ICACHE_FLASH_ATTR senddata()
{
   unsigned short length;
   os_timer_disarm(&WiFiLinker);
   os_printf("Broadcasting UDP DeviceDiscover...\n");

   espconn_sent(&Conn, "DeviceDiscover", 14);
}


Anyone have suggestion to get the source IP from these UDP packets?
User avatar
By kolban
#25288 Howdy Bontor,

The "args" parameter in the receive data callback is a pointer to a "struct espconn". If you cast and examine, you will find that it contains a field called "esp_udp" that contains both the localIP/localPort and remoteIP/remotePort.

Neil
User avatar
By vivek yuvan
#39926 Hi
I need help to write socket code for ESP8266. I already tried to compile some examples in the ESP8266 SDK through the Eclipse Mars MinGW compiler but it shows error like this "semantic error" .please help to resolve this problem.
Actually I am trying to do Android WiFi to ESP8266 WiFi communication. I need to receive data which initiated from Android WiFi . I am Very new to work with ESP 8266 help me out to resolve my problem. give some Examples socket code for android WiFi to ESP826 WiFi .

Thanks in Advance....
User avatar
By ivee
#42242
kolban wrote:Howdy Bontor,

The "args" parameter in the receive data callback is a pointer to a "struct espconn". If you cast and examine, you will find that it contains a field called "esp_udp" that contains both the localIP/localPort and remoteIP/remotePort.

Neil


This doesn't work.
esp_udp.remoteIP always contains 0.0.0.0

Haven't anyone solved the problem?