ESP8266 creates a UDP listening

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

ESP8266 creates a UDP listening

Postby ESP_Faye » Sat Feb 28, 2015 2:45 pm

:idea: :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
UDP_Test.png (12.31 KiB) Viewed 26434 times


Code: Select all

/******************************************************************************
 * 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();

}

esp8266ac
Posts: 7
Joined: Sun Oct 04, 2015 12:39 am

Re: ESP8266 creates a UDP listening

Postby esp8266ac » Fri Feb 19, 2016 12:49 am

能否提供基于RTOS SDK上的sample code?

beyondhong
Posts: 2
Joined: Sun Sep 25, 2016 9:08 am

Re: ESP8266 creates a UDP listening

Postby beyondhong » Tue Sep 27, 2016 11:28 am

there's more jobs to be done in the sample code
espconn_get_connection_info should be invoked in user_udp_recv before ip_addr_netcmp
and reset the remote_ip & remote_port

User avatar
mike.wu
Posts: 2
Joined: Tue Nov 08, 2016 8:27 am
Contact:

Re: ESP8266 creates a UDP listening

Postby mike.wu » Wed Nov 09, 2016 5:21 pm

esp8266ac wrote:能否提供基于RTOS SDK上的sample code?

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

Who is online

Users browsing this forum: No registered users and 3 guests