ESP8266 Developer Zone The Official ESP8266 Forum 2016-08-04T23:21:47+08:00 https://bbs.espressif.com:443/feed.php?f=7&t=2495 2016-08-04T23:21:47+08:00 2016-08-04T23:21:47+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2495&p=8289#p8289 <![CDATA[Re: 【请教】ESP8266 接收 UDP 广播丢包]]>
ESP_Rubin wrote:
你可以试一下

试过了,貌似确实有效。谢谢!!!

Statistics: Posted by wsptr — Thu Aug 04, 2016 11:21 pm


]]>
2016-08-02T11:43:41+08:00 2016-08-02T11:43:41+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2495&p=8232#p8232 <![CDATA[Re: 【请教】ESP8266 接收 UDP 广播丢包]]> Statistics: Posted by ESP_Rubin — Tue Aug 02, 2016 11:43 am


]]>
2016-08-01T18:58:19+08:00 2016-08-01T18:58:19+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2495&p=8222#p8222 <![CDATA[Re: 【请教】ESP8266 接收 UDP 广播丢包]]>
ESP_Rubin wrote:
这个log应该是系统自动进入睡眠状态了,此时WIFI不是一直处于工作状态

可是此时系统才刚启动不到5秒啊……
使用wifi_set_sleep_type()可以避免这种情况出现吗?

Statistics: Posted by wsptr — Mon Aug 01, 2016 6:58 pm


]]>
2016-08-01T14:14:10+08:00 2016-08-01T14:14:10+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2495&p=8219#p8219 <![CDATA[Re: 【请教】ESP8266 接收 UDP 广播丢包]]> Statistics: Posted by ESP_Rubin — Mon Aug 01, 2016 2:14 pm


]]>
2016-07-30T15:39:24+08:00 2016-07-30T15:39:24+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2495&p=8209#p8209 <![CDATA[【请教】ESP8266 接收 UDP 广播丢包]]> viewtopic.php?f=31&t=440&p=1682
具体的现象是这样的:当8266刚连接路由器以后,能够接收 UDP 广播,且不丢包;但当出现 pm open,type:2 0 提示(这个提示是什么意思?)后,就开始严重丢包,偶尔能接收到数据包。如果此时改发单播包,也会持续丢包,但过一段时间以后不再丢单播包。若再改为发广播包,则有较大的概率不再丢包。

在如下版本的 SDK 上进行了测试,均出现上述问题:
esp_iot_sdk_v1.3.0_15_08_08
esp_iot_sdk_v1.4.1_15_10_27
esp_iot_sdk_v1.5.0_15_11_27
esp_iot_sdk_v1.5.4_16_05_20
esp_iot_sdk_v1.5.4_16_05_20 with patch

请问这个问题应该如何解决?

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"

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("%s", 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_send(&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)
  {
     return;

      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 = 50000;//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 = 9000;  // 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];
   char password[64];
   struct station_config stationConf;

   os_memset(ssid,0,32);
   os_memset(password,0,64);
   os_sprintf(ssid,"WSN");
   os_sprintf(password,"qazwsxedc");

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

   //wifi_station_connect();
   wifi_station_dhcpc_start();

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

}

// For v1.5.4_16_05_20 with patch
uint32 user_rf_cal_sector_set(void)
{
   enum flash_size_map size_map = system_get_flash_size_map();
   uint32 rf_cal_sec = 0;
   switch (size_map)
   {
      case FLASH_SIZE_4M_MAP_256_256:
         rf_cal_sec = 128 - 5;
         break;
      case FLASH_SIZE_8M_MAP_512_512:
         rf_cal_sec = 256 - 5;
         break;
      case FLASH_SIZE_16M_MAP_512_512:
      case FLASH_SIZE_16M_MAP_1024_1024:
         rf_cal_sec = 512 - 5;
         break;
      case FLASH_SIZE_32M_MAP_512_512:
      case FLASH_SIZE_32M_MAP_1024_1024:
         rf_cal_sec = 512 - 5;
         break;
      default:
         rf_cal_sec = 0;
      break;
   }
   return rf_cal_sec;
}


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

   //ESP8266 connect to router
   user_set_station_config();

}

Statistics: Posted by wsptr — Sat Jul 30, 2016 3:39 pm


]]>