ESP8266 Developer Zone The Official ESP8266 Forum 2015-08-08T00:25:59+08:00 https://bbs.espressif.com:443/feed.php?f=7&t=916 2015-08-08T00:25:59+08:00 2015-08-08T00:25:59+08:00 https://bbs.espressif.com:443/viewtopic.php?t=916&p=3082#p3082 <![CDATA[Re: Retrieve IP Address from UDP Packet]]> This is a duplicate question and answers are starting to appear here:

http://www.esp8266.com/viewtopic.php?f=6&t=4414

Statistics: Posted by kolban — Sat Aug 08, 2015 12:25 am


]]>
2015-08-08T00:03:49+08:00 2015-08-08T00:03:49+08:00 https://bbs.espressif.com:443/viewtopic.php?t=916&p=3081#p3081 <![CDATA[Retrieve IP Address from UDP Packet]]>

Code:

#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?

Statistics: Posted by Guest — Sat Aug 08, 2015 12:03 am


]]>