ESP8266 Developer Zone The Official ESP8266 Forum 2016-07-05T13:14:16+08:00 https://bbs.espressif.com:443/feed.php?f=7&t=2378 2016-07-05T13:14:16+08:00 2016-07-05T13:14:16+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2378&p=7731#p7731 <![CDATA[Re: RTOS SDK的ESPNOW功能怎么使用?为什么收不到数据呢?]]> control:
//esp_now send
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_main.c
*
* Description: entry file of user application
*
* Modification history:
* 2015/1/23, v1.0 create this file.
*******************************************************************************/

#include "osapi.h"
#include "at_custom.h"
#include "user_interface.h"
#include "espnow.h"
#include "driver/uart.h"

LOCAL os_timer_t mytimer;
u8 local[6] = {0x5c, 0xcf, 0x7f, 0x0a, 0x13, 0xe4}; // This module in STATION mode
u8 remote[6] = {0x1a, 0xfe, 0x34, 0xed, 0x86, 0x99}; // Another modules in SOFTAP mode

void esp_now_recv_cb(u8 *mac_addr, u8 *data, u8 len)
{
os_printf("esp_now_recv_cb_t %d\n",len);
os_printf("esp_now_recv_cb_date: %s\n",data);
}

void esp_now_send_cb(u8 *mac_addr, u8 status)
{
os_printf("esp_now_send_cb_t\n");
}


LOCAL void ICACHE_FLASH_ATTR timer_cb(void *arg)
{
u8 all_cnt, encrypt_cnt;
os_printf("enter timer_cb\n");

if (esp_now_get_cnt_info(&all_cnt, &encrypt_cnt))
os_printf("get_cnt_info failed\r\n");
os_printf("client:%d, encrypted client:%d\r\n", all_cnt, encrypt_cnt);
if (esp_now_send(remote, "Hello", 6))
os_printf("fail\r\n");
else
os_printf("ok\r\n");
}


void user_init(void)
{
char hwaddr[6] = {0};
char Device_mac_buffer[60] = {0};

uint8 ch = 1;

u8 key[16]= {0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44};
//u8 local[6] = {0x5c, 0xcf, 0x7f, 0x0a, 0x13, 0xe4}; // This module in STATION mode
// u8 remote[6] = {0x1a, 0xfe, 0x34, 0xed, 0x86, 0x99}; // Another modules in SOFTAP mode

wifi_set_opmode(STATION_MODE);

if(wifi_get_macaddr(STATION_IF, hwaddr)){
os_sprintf(Device_mac_buffer, "%s " MACSTR ," ", MAC2STR(hwaddr));
os_printf("station mac: %s\n", Device_mac_buffer);
}

if(esp_now_init() == 0)
{
os_printf("esp_now_init ok\n");
esp_now_register_recv_cb(esp_now_recv_cb);
esp_now_register_send_cb(esp_now_send_cb);

wifi_set_channel(ch);
ch = wifi_get_channel();
os_printf("wifi channel is %d\n",ch);

if(esp_now_set_self_role(1) == 0)
os_printf("set myself as control\n");

if(esp_now_add_peer(local, ESP_NOW_ROLE_CONTROLLER, 1, key, 16) == 0)
os_printf("add local peer addr success\n");

if(esp_now_add_peer(remote, ESP_NOW_ROLE_SLAVE, 1, key, 16) == 0)
os_printf("add remote peer addr success\n");

os_printf("esp config success\n");
}


os_timer_disarm(&mytimer);
os_timer_setfn(&mytimer, (os_timer_func_t *)timer_cb, (void *)0);
os_timer_arm(&mytimer, 3000, 1);
}

slave:
//esp_now recv
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_main.c
*
* Description: entry file of user application
*
* Modification history:
* 2015/1/23, v1.0 create this file.
*******************************************************************************/

#include "osapi.h"
#include "at_custom.h"
#include "user_interface.h"
#include "espnow.h"
#include "driver/uart.h"

//LOCAL os_timer_t mytimer;
u8 local[6] = {0x5c, 0xcf, 0x7f, 0x0a, 0x13, 0xe4}; // This module in STATION mode
u8 remote[6] = {0x1a, 0xfe, 0x34, 0xed, 0x86, 0x99}; // Another modules in SOFTAP mode

void esp_now_recv_cb(u8 *mac_addr, u8 *data, u8 len)
{
os_printf("esp_now_recv_cb_t %d\n",len);
os_printf("esp_now_recv_cb_date: %s\n",data);
}

void esp_now_send_cb(u8 *mac_addr, u8 status)
{
os_printf("esp_now_send_cb_t\n");
}

#if 0
LOCAL void ICACHE_FLASH_ATTR timer_cb(void *arg)
{
u8 all_cnt, encrypt_cnt;
os_printf("enter timer_cb\n");

if (esp_now_get_cnt_info(&all_cnt, &encrypt_cnt))
os_printf("get_cnt_info failed\r\n");
os_printf("client:%d, encrypted client:%d\r\n", all_cnt, encrypt_cnt);
if (esp_now_send(local, "Hello", 6))
os_printf("fail\r\n");
else
os_printf("ok\r\n");
}
#endif


void user_init(void)
{
char hwaddr[6] = {0};
char Device_mac_buffer[60] = {0};

uint8 ch = 1;

u8 key[16]= {0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44, 0x33, 0x44};
//u8 local[6] = {0x5c, 0xcf, 0x7f, 0x0a, 0x13, 0xe4}; // This module in STATION mode
// u8 remote[6] = {0x1a, 0xfe, 0x34, 0xed, 0x86, 0x99}; // Another modules in SOFTAP mode

wifi_set_opmode(SOFTAP_MODE);

if(wifi_get_macaddr(SOFTAP_IF, hwaddr)){
os_sprintf(Device_mac_buffer, "%s " MACSTR ," ", MAC2STR(hwaddr));
os_printf("softap mac: %s\n", Device_mac_buffer);
}

if(esp_now_init() == 0)
{
os_printf("esp_now_init ok\n");
esp_now_register_recv_cb(esp_now_recv_cb);
esp_now_register_send_cb(esp_now_send_cb);

wifi_set_channel(ch);
ch = wifi_get_channel();
os_printf("wifi channel is %d\n",ch);

if(esp_now_set_self_role(2) == 0)
os_printf("set myself as slave\n");

if(esp_now_add_peer(local, ESP_NOW_ROLE_CONTROLLER, 1, key, 16) == 0)
os_printf("add local peer addr success\n");

if(esp_now_add_peer(remote, ESP_NOW_ROLE_SLAVE, 1, key, 16) == 0)
os_printf("add remote peer addr success\n");

os_printf("esp config success\n");
}

#if 0
os_timer_disarm(&mytimer);
os_timer_setfn(&mytimer, (os_timer_func_t *)timer_cb, (void *)0);
os_timer_arm(&mytimer, 3000, 1);
#endif

}

Statistics: Posted by ESP_Rubin — Tue Jul 05, 2016 1:14 pm


]]>
2016-07-05T10:36:23+08:00 2016-07-05T10:36:23+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2378&p=7725#p7725 <![CDATA[Re: RTOS SDK的ESPNOW功能怎么使用?为什么收不到数据呢?]]>
esp_rubin wrote:
hi,
不好意思,没有给您及时反馈。
我们这边需要先验证一下,RTOS下可能会花些时间
您那边能否先帮忙debug一下,看看问题出在哪一步?这样我们分析起来会比较有重点,效率也会提高,谢谢!!

首先看一下使用的流程对吗?前边已描述,就是2片都esp_now_init后,添加peer,调整到同一wifi channel,然后发送。
问题是发送方回调中总是提示失败,而接收方总是收不到。

还可以有什么更详细的debug信息可以打印吗?

Statistics: Posted by tobewinner — Tue Jul 05, 2016 10:36 am


]]>
2016-07-05T09:39:45+08:00 2016-07-05T09:39:45+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2378&p=7724#p7724 <![CDATA[Re: RTOS SDK的ESPNOW功能怎么使用?为什么收不到数据呢?]]> 不好意思,没有给您及时反馈。
我们这边需要先验证一下,RTOS下可能会花些时间
您那边能否先帮忙debug一下,看看问题出在哪一步?这样我们分析起来会比较有重点,效率也会提高,谢谢!!

Statistics: Posted by ESP_Rubin — Tue Jul 05, 2016 9:39 am


]]>
2016-07-04T10:31:21+08:00 2016-07-04T10:31:21+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2378&p=7697#p7697 <![CDATA[Re: RTOS SDK的ESPNOW功能怎么使用?为什么收不到数据呢?]]> Statistics: Posted by tobewinner — Mon Jul 04, 2016 10:31 am


]]>
2016-07-02T15:21:22+08:00 2016-07-02T15:21:22+08:00 https://bbs.espressif.com:443/viewtopic.php?t=2378&p=7666#p7666 <![CDATA[【Feedback】RTOS SDK的ESPNOW功能怎么使用?为什么收不到数据呢?]]>

Code:

OS SDK ver: 1.4.0(c599790) compiled @ Feb 26 2016 11:08:14

测试过程是这样的,2片8266:
1片MAC地址为5c:cf:7f:8b:d0:64
wifi_opmode设置为STATION_MODE,实际并未连接WIFI,esp_now_role设置为ESP_NOW_ROLE_CONTROLLER
注册了RecvCb和SendCb,添加的peer地址分别为(前两个为ESP_NOW_ROLE_SLAVE,后两个为ESP_NOW_ROLE_CONTROLLER)

Code:

static u8 DevMacs[][6] = {
   {0x5C, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5E, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5C, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
   {0x5E, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
};

wifi_channel设置为1
1片MAC地址为5c:cf:7f:8b:2b:87
wifi_opmode设置为SOFTAP_MODE,esp_now_role设置为ESP_NOW_ROLE_SLAVE
注册了RecvCb和SendCb,添加的peer地址分别为(前两个为ESP_NOW_ROLE_SLAVE,后两个为ESP_NOW_ROLE_CONTROLLER)

Code:

static u8 DevMacs[][6] = {
   {0x5C, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5E, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5C, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
   {0x5E, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
};


实验过程如下,先给slave的板子上电,然后给controller的板子上电,观察日志,发现一个发送总是失败,接收总收不到,相关日志如下:
5c:cf:7f:8b:2b:87的日志:
OS SDK ver: 1.4.0(c599790) compiled @ Feb 26 2016 11:08:14
SDK version:1.4.0(c599790)
chipid=008B2B87:5CCF7F8B2B87
system_rtc_clock_cali_proc=0x65C8, rst:6,0
user_init over[rtc u=26071]
mode : softAP(5e:cf:7f:8b:2b:87)
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
add if1
bcn 100
esp_now_register_recv_cb ret:0.
esp_now_register_send_cb ret:0.
esp_now_set_self_role ret:0.
esp_now_add_peer 0 ret:0.
esp_now_add_peer 1 ret:0.
esp_now_add_peer 2 ret:0.
esp_now_add_peer 3 ret:0.
wifi_set_channel ret:1.
esp_now_is_peer_exist ret:1.
wifi_get_channel ret:1.
esp_now_is_peer_exist ret:1.
wifi_get_channel ret:1.
esp_now_is_peer_exist ret:1.
wifi_get_channel ret:1.
esp_now_is_peer_exist ret:1.
wifi_get_channel ret:1.
esp_now_is_peer_exist ret:1.



5c:cf:7f:8b:d0:64的日志:
OS SDK ver: 1.4.0(c599790) compiled @ Feb 26 2016 11:08:14
SDK version:1.4.0(c599790)
chipid=008BD064:5CCF7F8BD064
system_rtc_clock_cali_proc=0x5D64, rst:6,0
Awake Time:99217.17973.
user_init over
mode : sta(5c:cf:7f:8b:d0:64)
add if0
esp_now_register_recv_cb ret:0.
esp_now_register_send_cb ret:0.
esp_now_set_self_role ret:0.
esp_now_add_peer 0 ret:0.
esp_now_add_peer 1 ret:0.
esp_now_add_peer 2 ret:0.
esp_now_add_peer 3 ret:0.
wifi_set_channel ret:1.
esp_now_send[@122889] ret:0.
EspNowSendCb[@384121] 2 [5CCF7F8B2B87], s=1.
EspNowSendCb[@384211] 2 [5ECF7F8B2B87], s=1.
EspNowSendCb[@384265] 2 [5CCF7F8BD064], s=1.
EspNowSendCb[@384825] 2 [5ECF7F8BD064], s=1.
esp_now_send[@623407] ret:0.
EspNowSendCb[@884820] 2 [5CCF7F8B2B87], s=1.
EspNowSendCb[@884881] 2 [5ECF7F8B2B87], s=1.
EspNowSendCb[@884936] 2 [5CCF7F8BD064], s=1.
EspNowSendCb[@885523] 2 [5ECF7F8BD064], s=1.
esp_now_send[@1123403] ret:0.
EspNowSendCb[@1385564] 2 [5CCF7F8B2B87], s=1.
EspNowSendCb[@1385626] 2 [5ECF7F8B2B87], s=1.
EspNowSendCb[@1385682] 2 [5CCF7F8BD064], s=1.
EspNowSendCb[@1386526] 2 [5ECF7F8BD064], s=1.

5c:cf:7f:8b:2b:87的源码:

Code:

#include "esp_common.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"

#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "espnow.h"

#include "user_config.h"
#include "gpio.h"
#include "uart.h"

static u8 DevMacs[][6] = {
   {0x5C, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5E, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
   {0x5C, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
   {0x5E, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
};

static u8 Key[16] = {
   0x12, 0x21, 0x34, 0x43, 0x53, 0x87, 0x74, 0x95, 0x12, 0x21, 0x34, 0x43, 0x53, 0x87, 0x74, 0x95
};

ICACHE_FLASH_ATTR
void EspNowRecvCb(uint8 *mac_addr, uint8 *data, uint8 len)
{
   printf("EspNowRecvCb[@%u] %uB from [%02X%02X%02X%02X%02X%02X]:%02X.\n", system_get_time(), len,
         mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], data?data[0]:256);
}

ICACHE_FLASH_ATTR
void EspNowSendCb(uint8 *mac_addr, uint8 status)
{
   printf("EspNowSendCb[@%u] 2 [%02X%02X%02X%02X%02X%02X], s=%u.\n", system_get_time(),
         mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], status);
}

ICACHE_FLASH_ATTR
void Init(void* pArg)
{
   int Ret;
   u8 i=0;

   Ret = esp_now_init();
   if (Ret==0) {
      Ret = esp_now_register_recv_cb(EspNowRecvCb);
      printf("esp_now_register_recv_cb ret:%d.\n", Ret);
      Ret = esp_now_register_send_cb(EspNowSendCb);
      printf("esp_now_register_send_cb ret:%d.\n", Ret);
      Ret = esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
      printf("esp_now_set_self_role ret:%d.\n", Ret);
      Ret = esp_now_add_peer(DevMacs[0], ESP_NOW_ROLE_SLAVE, 14, Key, 16);
      printf("esp_now_add_peer 0 ret:%d.\n", Ret);
      Ret = esp_now_add_peer(DevMacs[1], ESP_NOW_ROLE_SLAVE, 14, Key, 16);
      printf("esp_now_add_peer 1 ret:%d.\n", Ret);
      Ret = esp_now_add_peer(DevMacs[2], ESP_NOW_ROLE_CONTROLLER, 14, Key, 16);
      printf("esp_now_add_peer 2 ret:%d.\n", Ret);
      Ret = esp_now_add_peer(DevMacs[3], ESP_NOW_ROLE_CONTROLLER, 14, Key, 16);
      printf("esp_now_add_peer 3 ret:%d.\n", Ret);
      Ret = wifi_set_channel(1);
      printf("wifi_set_channel ret:%d.\n", Ret);
      while (1) {
         Ret = esp_now_send(NULL, "Hello EspNow!", 13);
         printf("esp_now_send[@%u] ret:%d.\n", system_get_time(), Ret);
         vTaskDelay(1000/portTICK_RATE_MS);
      }
   } else {
      printf("Failed[%d] to esp_now_init.\n", Ret);
   }
   vTaskDelay(30000/portTICK_RATE_MS);
   printf("Thread Over.\n");
}

ICACHE_FLASH_ATTR
void user_init(void)
{
   TCP_WND = 2*TCP_MSS;
   struct rst_info *pInfo = system_get_rst_info();
   u8 Mac[6] = {0};

   PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_UART1_TXD_BK);
   UART_SetBaudrate(UART0, BIT_RATE_115200);
   UART_SetPrintPort(UART0);
   wifi_get_macaddr(STATION_IF, Mac);
   printf("SDK version:%s\nchipid=%08X:%02X%02X%02X%02X%02X%02X\nsystem_rtc_clock_cali_proc=0x%X, rst:%u,%u\n",
   system_get_sdk_version(),
        system_get_chip_id(), Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5],
   system_rtc_clock_cali_proc(),
   pInfo->reason, pInfo->exccause);
   system_update_cpu_freq(SYS_CPU_160MHZ);
   printf("Awake Time:%u.%u.\n", system_get_time(), system_get_rtc_time());
   wifi_set_opmode(STATION_MODE);
   xTaskCreate(Init, (signed char*)"Init", 512, NULL, 9, NULL);

   printf("user_init over\n");
}


5c:cf:7f:8b:d0:64的源码:
[code

#include "esp_common.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"

#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "espnow.h"

#include "user_config.h"
#include "gpio.h"
#include "uart.h"

static u8 DevMacs[][6] = {
{0x5C, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
{0x5E, 0xCF, 0x7F, 0x8B, 0x2B, 0x87},
{0x5C, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
{0x5E, 0xCF, 0x7F, 0x8B, 0xD0, 0x64},
};

static u8 Key[16] = {
0x12, 0x21, 0x34, 0x43, 0x53, 0x87, 0x74, 0x95, 0x12, 0x21, 0x34, 0x43, 0x53, 0x87, 0x74, 0x95
};

ICACHE_FLASH_ATTR
void EspNowRecvCb(uint8 *mac_addr, uint8 *data, uint8 len)
{
printf("EspNowRecvCb[@%u] %uB from [%02X%02X%02X%02X%02X%02X]:%02X.\n", system_get_time(), len,
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], data?data[0]:256);
}

ICACHE_FLASH_ATTR
void EspNowSendCb(uint8 *mac_addr, uint8 status)
{
printf("EspNowSendCb[@%u] 2 [%02X%02X%02X%02X%02X%02X], s=%u.\n", system_get_time(),
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], status);
}

ICACHE_FLASH_ATTR
void Init(void* pArg)
{
int Ret;
u8 i=0;

Ret = esp_now_init();
if (Ret==0) {
Ret = esp_now_register_recv_cb(EspNowRecvCb);
printf("esp_now_register_recv_cb ret:%d.\n", Ret);
Ret = esp_now_register_send_cb(EspNowSendCb);
printf("esp_now_register_send_cb ret:%d.\n", Ret);
Ret = esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
printf("esp_now_set_self_role ret:%d.\n", Ret);
Ret = esp_now_add_peer(DevMacs[0], ESP_NOW_ROLE_SLAVE, 14, Key, 16);
printf("esp_now_add_peer 0 ret:%d.\n", Ret);
Ret = esp_now_add_peer(DevMacs[1], ESP_NOW_ROLE_SLAVE, 14, Key, 16);
printf("esp_now_add_peer 1 ret:%d.\n", Ret);
Ret = esp_now_add_peer(DevMacs[2], ESP_NOW_ROLE_CONTROLLER, 14, Key, 16);
printf("esp_now_add_peer 2 ret:%d.\n", Ret);
Ret = esp_now_add_peer(DevMacs[3], ESP_NOW_ROLE_CONTROLLER, 14, Key, 16);
printf("esp_now_add_peer 3 ret:%d.\n", Ret);
Ret = wifi_set_channel(1);
printf("wifi_set_channel ret:%d.\n", Ret);
while (1) {
Ret = esp_now_is_peer_exist(DevMacs[2]);
printf("esp_now_is_peer_exist ret:%d.\n", Ret);
Ret = wifi_get_channel();
printf("wifi_get_channel ret:%d.\n", Ret);
vTaskDelay(3000/portTICK_RATE_MS);
}
} else {
printf("Failed[%d] to esp_now_init.\n", Ret);
}
printf("Thread Over.\n");
}

void ICACHE_FLASH_ATTR
user_init(void)
{
TCP_WND = 2*TCP_MSS;
struct rst_info *pInfo = system_get_rst_info();
u8 Mac[6] = {0};

PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_UART1_TXD_BK);
UART_SetBaudrate(UART1, BIT_RATE_115200);
UART_SetPrintPort(UART1);
wifi_get_macaddr(STATION_IF, Mac);
printf("SDK version:%s\nchipid=%08X:%02X%02X%02X%02X%02X%02X\nsystem_rtc_clock_cali_proc=0x%X, rst:%u,%u\n",
system_get_sdk_version(),
system_get_chip_id(), Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5],
system_rtc_clock_cali_proc(),
pInfo->reason, pInfo->exccause);
system_update_cpu_freq(SYS_CPU_160MHZ);
wifi_set_opmode(SOFTAP_MODE);

xTaskCreate(Init, (signed char*)"Init", 512, NULL, 9, NULL);
printf("user_init over[rtc u=%u]\n", system_rtc_clock_cali_proc());
}

[/code]

Statistics: Posted by tobewinner — Sat Jul 02, 2016 3:21 pm


]]>