ESP8266 Sends UDP data

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

ESP8266 Sends UDP data

Postby ESP_Faye » Tue May 12, 2015 5:55 pm

:!: :!: :!:

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.

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"

LOCAL os_timer_t test_timer;
LOCAL struct espconn user_udp_espconn;

const char *ESP8266_MSG = "I'm ESP8266 ";
   
/*---------------------------------------------------------------------------*/
LOCAL struct espconn ptrespconn;

 /******************************************************************************
  * FunctionName : user_udp_recv_cb
  * 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_cb(void *arg, char *pusrdata, unsigned short length)
 {
     
     os_printf("recv udp data: %s\n", pusrdata);

 }

 /******************************************************************************
      * FunctionName : user_udp_send
      * Description  : udp send data
      * Parameters  : none
      * Returns      : none
 *******************************************************************************/
 LOCAL void ICACHE_FLASH_ATTR
 user_udp_send(void)
 {
     char DeviceBuffer[40] = {0};
     char hwaddr[6];
     struct ip_info ipconfig;

     const char udp_remote_ip[4] = { 255, 255, 255, 255}; 
     os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4); // ESP8266 udp remote IP need to be set everytime we call espconn_sent
     user_udp_espconn.proto.udp->remote_port = 1112;  // ESP8266 udp remote port need to be set everytime we call espconn_sent

     wifi_get_macaddr(STATION_IF, hwaddr);
 
     os_sprintf(DeviceBuffer, "%s" MACSTR "!" , ESP8266_MSG, MAC2STR(hwaddr));
 
     espconn_sent(&user_udp_espconn, DeviceBuffer, os_strlen(DeviceBuffer));
   
 }
 
 /******************************************************************************
      * FunctionName : user_udp_sent_cb
      * Description  : udp sent successfully
      * Parameters  : arg -- Additional argument to pass to the callback function
      * Returns      : none
 *******************************************************************************/
  LOCAL void ICACHE_FLASH_ATTR
  user_udp_sent_cb(void *arg)
  {
      struct espconn *pespconn = arg;
 
      os_printf("user_udp_send successfully !!!\n");
     
      //disarm timer first
       os_timer_disarm(&test_timer);

      //re-arm timer to check ip
      os_timer_setfn(&test_timer, (os_timer_func_t *)user_udp_send, NULL); // only send next packet after prev packet sent successfully
      os_timer_arm(&test_timer, 1000, 0);
  }


 /******************************************************************************
 * FunctionName : user_check_ip
 * Description  : check whether get ip addr or not
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_check_ip(void)
{
    struct ip_info ipconfig;

   //disarm timer first
    os_timer_disarm(&test_timer);

   //get ip info of ESP8266 station
    wifi_get_ip_info(STATION_IF, &ipconfig);

    if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0)
   {
      os_printf("got ip !!! \r\n");

      wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface

      user_udp_espconn.type = ESPCONN_UDP;
      user_udp_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
      user_udp_espconn.proto.udp->local_port = espconn_port();  // set a available  port
     
      const char udp_remote_ip[4] = {255, 255, 255, 255}; 
     
      os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4); // ESP8266 udp remote IP
     
      user_udp_espconn.proto.udp->remote_port = 1112;  // ESP8266 udp remote port
     
      espconn_regist_recvcb(&user_udp_espconn, user_udp_recv_cb); // register a udp packet receiving callback
      espconn_regist_sentcb(&user_udp_espconn, user_udp_sent_cb); // register a udp packet sent callback
     
      espconn_create(&user_udp_espconn);   // create udp

      user_udp_send();   // send udp data

    }
   else
   {
        if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD ||
                wifi_station_get_connect_status() == STATION_NO_AP_FOUND ||
                wifi_station_get_connect_status() == STATION_CONNECT_FAIL))
        {
         os_printf("connect fail !!! \r\n");
        }
      else
      {
           //re-arm timer to check ip
            os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL);
            os_timer_arm(&test_timer, 100, 0);
        }
    }
}


/******************************************************************************
 * FunctionName : user_set_station_config
 * Description  : set the router info which ESP8266 station will connect to
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
   // Wifi configuration
   char ssid[32] = SSID;
   char password[64] = PASSWORD;
   struct station_config stationConf;

   //need not mac address
   stationConf.bssid_set = 0;
   
   //Set ap settings
   os_memcpy(&stationConf.ssid, ssid, 32);
   os_memcpy(&stationConf.password, password, 64);
   wifi_station_set_config(&stationConf);

   //set a timer to check whether got ip from router succeed or not.
   os_timer_disarm(&test_timer);
    os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL);
    os_timer_arm(&test_timer, 100, 0);

}


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

   //ESP8266 connect to router
   user_set_station_config();

}


If you want to send back a response when received one,

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
 user_udp_recv_cb(void *arg, char *pusrdata, unsigned short length)
 {
     
     os_printf("recv udp data: %s\n", pusrdata);
     struct espconn *pesp_conn = arg;
     
       remot_info *premot = NULL;
       sint8 value = ESPCONN_OK;
       if (espconn_get_connection_info(pesp_conn,&premot,0) == ESPCONN_OK){
             pesp_conn->proto.udp->remote_port = premot->remote_port;
             pesp_conn->proto.udp->remote_ip[0] = premot->remote_ip[0];
             pesp_conn->proto.udp->remote_ip[1] = premot->remote_ip[1];
             pesp_conn->proto.udp->remote_ip[2] = premot->remote_ip[2];
             pesp_conn->proto.udp->remote_ip[3] = premot->remote_ip[3];
             espconn_sent(pesp_conn, pusrdata, os_strlen(pusrdata));
       }
 }

usd18290
Posts: 1
Joined: Mon May 23, 2016 8:31 pm

Re: ESP8266 Sends UDP data

Postby usd18290 » Mon May 23, 2016 8:35 pm

Hi,

I think the response code got issue. The proto should be udp instead of tcp, right ?

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
 user_udp_recv_cb(void *arg, char *pusrdata, unsigned short length)
 {
     
     os_printf("recv udp data: %s\n", pusrdata);
     struct espconn *pesp_conn = arg;
     
       remot_info *premot = NULL;
       sint8 value = ESPCONN_OK;
       if (espconn_get_connection_info(pesp_conn,&premot,0) == ESPCONN_OK){
             pesp_conn->proto.udp->remote_port = premot->remote_port;
             pesp_conn->proto.udp->remote_ip[0] = premot->remote_ip[0];
             pesp_conn->proto.udp->remote_ip[1] = premot->remote_ip[1];
             pesp_conn->proto.udp->remote_ip[2] = premot->remote_ip[2];
             pesp_conn->proto.udp->remote_ip[3] = premot->remote_ip[3];
             espconn_sent(pesp_conn, pusrdata, os_strlen(pusrdata));
       }
 }

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

Re: ESP8266 Sends UDP data

Postby ESP_Faye » Tue May 24, 2016 10:03 am

Hi,

Thanks! We revised it.

semlanik
Posts: 1
Joined: Thu Jan 11, 2018 8:08 pm

Re: ESP8266 Sends UDP data

Postby semlanik » Thu Jan 11, 2018 8:12 pm

Hi all,

I'm trying the similar thing in my network but using station instead of AP. And it doesn't work. I also tried calculate broadcast address accordding IP and mask it doesn't work as well. But send to particular IP works fine.

Any ideas of this behaivor?

I'm using non-os SDK 2.1.x.

Regards,
Alexey.

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: ESP8266 Sends UDP data

Postby Her Mary » Mon Jan 15, 2018 3:14 pm

Try station_only mode when calling wifi_set_opmode. And also call wifi_set_broadcast_if (STATION_MODE); to enable broadcast in the station mode.

coright
Posts: 2
Joined: Wed Jun 13, 2018 4:49 pm

Re: ESP8266 Sends UDP data

Postby coright » Wed Jun 13, 2018 5:09 pm

does ESP8266_RTOS_SDK support UDP broadcast?

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: ESP8266 Sends UDP data

Postby Her Mary » Fri Jun 15, 2018 9:35 am

You can use socket directly on RTOS SDK. I think it should support UDP broadcast.

sonmezroy
Posts: 1
Joined: Thu Feb 10, 2022 8:13 pm
Contact:

Re: ESP8266 Sends UDP data

Postby sonmezroy » Tue Feb 15, 2022 4:51 pm

The WiFi UDP Send block sends data to a UDP host over a wireless network. The block sends data from the port number specified in the Local IP Port parameter. Specify the IP address and the port number of the receiving host in the Remote IP address and the Remote IP Port parameters.

Who is online

Users browsing this forum: No registered users and 3 guests