ESP8266 Developer Zone The Official ESP8266 Forum 2016-11-09T17:21:28+08:00 https://bbs.espressif.com:443/feed.php?f=31&t=230 2016-11-09T17:21:28+08:00 2016-11-09T17:21:28+08:00 https://bbs.espressif.com:443/viewtopic.php?t=230&p=10478#p10478 <![CDATA[Re: ESP8266 creates a UDP listening]]>
esp8266ac wrote:
能否提供基于RTOS SDK上的sample code?

在RTOS上我遇到的情况是espconn_init()调用后,不停的重启。不如直接用include/lwip下的lwip来直接实现,就跟socket编程一样。 :D

Statistics: Posted by mike.wu — Wed Nov 09, 2016 5:21 pm


]]>
2016-09-27T11:28:26+08:00 2016-09-27T11:28:26+08:00 https://bbs.espressif.com:443/viewtopic.php?t=230&p=9954#p9954 <![CDATA[Re: ESP8266 creates a UDP listening]]> espconn_get_connection_info should be invoked in user_udp_recv before ip_addr_netcmp
and reset the remote_ip & remote_port

Statistics: Posted by beyondhong — Tue Sep 27, 2016 11:28 am


]]>
2016-02-19T00:49:58+08:00 2016-02-19T00:49:58+08:00 https://bbs.espressif.com:443/viewtopic.php?t=230&p=5782#p5782 <![CDATA[Re: ESP8266 creates a UDP listening]]> Statistics: Posted by esp8266ac — Fri Feb 19, 2016 12:49 am


]]>
2015-02-28T14:45:56+08:00 2015-02-28T14:45:56+08:00 https://bbs.espressif.com:443/viewtopic.php?t=230&p=840#p840 <![CDATA[ESP8266 creates a UDP listening]]> :idea: :idea:

Sample code below is based on ESP8266_NONOS_SDK.
If using espconn in ESP8266_RTOS_SDK, please call espconn_init in user_init as initialization.

Create a UDP listening on ESP8266,if ESP8266 got UDP message "Are You ESP8266 Device?",it will response "Yes,I'm ESP8266!" with its MAC address and IP address
UDP_Test.png


Code:

/******************************************************************************
 * Copyright 2013-2014 Espressif Systems
 *
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"

#include "espconn.h"
#include "user_json.h"
#include "user_devicefind.h"

const char *device_find_request = "Are You ESP8266 Device?";

const char *device_find_response_ok = "Yes,I'm ESP8266!";

/*---------------------------------------------------------------------------*/
LOCAL struct espconn ptrespconn;

/******************************************************************************
 * FunctionName : user_devicefind_recv
 * Description  : Processing the received udp packet
 * 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
user_udp_recv(void *arg, char *pusrdata, unsigned short length)
{
    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 *)ptrespconn.proto.udp->remote_ip, &ipconfig.ip, &ipconfig.netmask))
      {
         //udp packet is received from ESP8266 station
            wifi_get_ip_info(STATION_IF, &ipconfig);
            wifi_get_macaddr(STATION_IF, hwaddr);
        }
      else
      {
         //udp packet is received from ESP8266 softAP
        }
      
    }
   else
   {
      //udp packet is received from ESP8266 station
        wifi_get_ip_info(STATION_IF, &ipconfig);
        wifi_get_macaddr(STATION_IF, hwaddr);
    }

    if (pusrdata == NULL)
        return;
   

    if (length == os_strlen(device_find_request) &&
            os_strncmp(pusrdata, device_find_request, os_strlen(device_find_request)) == 0)
    {
        //received device find message
       
        os_sprintf(DeviceBuffer, "%s" MACSTR " " IPSTR, device_find_response_ok,
                   MAC2STR(hwaddr), IP2STR(&ipconfig.ip));

        os_printf("%s\n", DeviceBuffer);
        length = os_strlen(DeviceBuffer);
      
      //if received "Are You ESP8266 ?" , response "Yes,I'm ESP8266!" + ESP8266 mac + ESP8266 ip
        espconn_sent(&ptrespconn, DeviceBuffer, length);
      
    }
   else
   {
      //received some other data
    }
   
}

/******************************************************************************
 * FunctionName : user_devicefind_init
 * Description  : create a udp listening
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_udp_init(void)
{
    ptrespconn.type = ESPCONN_UDP;
    ptrespconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
    ptrespconn.proto.udp->local_port = 1025;  // ESP8266 udp port
    espconn_regist_recvcb(&ptrespconn, user_udp_recv); // register a udp packet receiving callback
    espconn_create(&ptrespconn);   // create udp
}

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{
       os_printf("SDK version:%s\n", system_get_sdk_version());
   
   //Set softAP + station mode
   wifi_set_opmode(STATIONAP_MODE);

   // Create udp listening.
   user_udp_init();

}

Statistics: Posted by ESP_Faye — Sat Feb 28, 2015 2:45 pm


]]>