ESP8266 Developer Zone The Official ESP8266 Forum 2022-02-15T16:51:22+08:00 https://bbs.espressif.com:443/feed.php?f=31&t=440 2022-02-15T16:51:22+08:00 2022-02-15T16:51:22+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=100665#p100665 <![CDATA[Re: ESP8266 Sends UDP data]]> Statistics: Posted by sonmezroy — Tue Feb 15, 2022 4:51 pm


]]>
2018-06-15T09:35:04+08:00 2018-06-15T09:35:04+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=20736#p20736 <![CDATA[Re: ESP8266 Sends UDP data]]> Statistics: Posted by Her Mary — Fri Jun 15, 2018 9:35 am


]]>
2018-06-13T17:09:24+08:00 2018-06-13T17:09:24+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=20715#p20715 <![CDATA[Re: ESP8266 Sends UDP data]]> Statistics: Posted by coright — Wed Jun 13, 2018 5:09 pm


]]>
2018-01-15T15:14:35+08:00 2018-01-15T15:14:35+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=19072#p19072 <![CDATA[Re: ESP8266 Sends UDP data]]> Statistics: Posted by Her Mary — Mon Jan 15, 2018 3:14 pm


]]>
2018-01-11T20:12:11+08:00 2018-01-11T20:12:11+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=19026#p19026 <![CDATA[Re: ESP8266 Sends UDP data]]>
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.

Statistics: Posted by semlanik — Thu Jan 11, 2018 8:12 pm


]]>
2016-05-24T10:03:03+08:00 2016-05-24T10:03:03+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=7041#p7041 <![CDATA[Re: ESP8266 Sends UDP data]]>
Thanks! We revised it.

Statistics: Posted by ESP_Faye — Tue May 24, 2016 10:03 am


]]>
2016-05-23T20:35:23+08:00 2016-05-23T20:35:23+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=7035#p7035 <![CDATA[Re: ESP8266 Sends UDP data]]>
I think the response code got issue. The proto should be udp instead of tcp, right ?

Code:

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));
       }
 }

Statistics: Posted by usd18290 — Mon May 23, 2016 8:35 pm


]]>
2015-05-12T17:55:21+08:00 2015-05-12T17:55:21+08:00 https://bbs.espressif.com:443/viewtopic.php?t=440&p=1682#p1682 <![CDATA[ESP8266 Sends UDP data]]> :!: :!:

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:

/******************************************************************************
     * 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:

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));
       }
 }

Statistics: Posted by ESP_Faye — Tue May 12, 2015 5:55 pm


]]>