I'm testing ESP-NOW for lighting project but seems stuck.
The situation being:
The light under control is SLAVE. Button is the CONTROLLER.
I'm sending packet from controller to slave's SOFT_AP interface.
It works fine when the light (slave) is not connected to any WiFi AP.
However if the light connects to WiFi, no packet is received. The sender's callback gives statue=1, means failure.
Below is slave side testing code. From controller side whether I send to slave's station or soft_ap interface all failure.
The "app_light" program in ESP8266_LIGHT_WITH_MESH doesn't seems to have this restriction but I'm not able to test it.
Any help will be appreciated.
Code: Select all
#include <user_interface.h>
#include <osapi.h>
#include <mem.h>
#include <os_type.h>
#include <gpio.h>
#include <espnow.h>
#include "espmissingincludes.h"
#include "uart.h"
void ICACHE_FLASH_ATTR user_rf_pre_init(void)
{
}
void printMacAddress(uint8_t* macaddr)
{
int i;
os_printf("{");
for (i = 0; i < 6; i++)
{
os_printf("0x");
os_printf("%x", macaddr[i]);
if (i < 5) os_printf(",");
}
os_printf("}\n");
}
void ICACHE_FLASH_ATTR rcv_cb(u8 *macaddr, u8 *data, u8 len)
{
size_t i;
os_printf("Reveive ");
for (i = 0; i < len; i++)
{
os_printf("0x%x ", data[i]);
}
os_printf("from ");
printMacAddress(macaddr);
}
void ICACHE_FLASH_ATTR main()
{
os_printf("System init done.\n");
if (esp_now_init()==0) {
os_printf("direct link init ok\n");
esp_now_register_recv_cb(rcv_cb);
} else {
os_printf("esp-now already init\n");
}
//It is designed to send data from station interface of switch/controller to softap interface of light
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); //role 1: switch ; role 2 : light;
}
#define STA_SSID "AAA"
#define STA_PASSWORD "BBB"
void ICACHE_FLASH_ATTR wifi_init(void)
{
struct station_config sta_conf;
// connect to our station
os_bzero(&sta_conf, sizeof(struct station_config));
wifi_station_get_config(&sta_conf);
os_strncpy(sta_conf.ssid, STA_SSID, sizeof(sta_conf.ssid));
os_strncpy(sta_conf.password, STA_PASSWORD, sizeof(sta_conf.password));
wifi_station_disconnect();
ETS_UART_INTR_DISABLE();
wifi_station_set_config(&sta_conf);
ETS_UART_INTR_ENABLE();
wifi_station_connect();
}
void ICACHE_FLASH_ATTR user_init(void)
{
// Configure the UART
uart_init(BIT_RATE_74880, BIT_RATE_74880);
os_printf("SDK version:%s\n", system_get_sdk_version());
wifi_set_opmode(STATIONAP_MODE);
wifi_station_set_auto_connect(0); // Change to 1 doesn't work
//wifi_init();
system_init_done_cb(main);
}