ESP NOW WiFi initialization function:-
Code: Select all
void init_espnow_master(void)
{
const wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_create_default() );
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
ESP_ERROR_CHECK( esp_wifi_start() );
#if MY_ESPNOW_ENABLE_LONG_RANGE
ESP_ERROR_CHECK( esp_wifi_set_protocol(MY_ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR) );
#endif
ESP_ERROR_CHECK( esp_now_init() );
ESP_ERROR_CHECK( esp_now_register_recv_cb(recv_cb) );
ESP_ERROR_CHECK( esp_now_set_pmk((const uint8_t *)MY_ESPNOW_PMK) );
ESP_ERROR_CHECK( esp_now_register_send_cb(packet_sent_cb) );
// Alter this if you want to specify the gateway mac, enable encyption, etc
const esp_now_peer_info_t broadcast_destination = {
.peer_addr = MY_RECEIVER_MAC,
.channel = MY_ESPNOW_CHANNEL,
.ifidx = ESP_IF_WIFI_AP
};
ESP_ERROR_CHECK( esp_now_add_peer(&broadcast_destination) );
}
WiFi in AP mode initialize functions in the existing code:-
Code: Select all
// INET - TCP-IP Adapter initialization
void INET_Configuration(void)
{
tcpip_adapter_ip_info_t ipInfo;
tcpip_adapter_dns_info_t dns_info;
ip_addr_t dnsip;
dnsip.type = IPADDR_TYPE_V4;
IP4_ADDR(&dnsip.u_addr.ip4, 192,168,1,1);
IP4_ADDR(&ipInfo.ip, 192,168,1,1); // Access Point IP Address
IP4_ADDR(&ipInfo.ip, 192,168,1,1); // Access Point IP Address
IP4_ADDR(&ipInfo.gw, 192,168,1,1); // Access Point Gateway
IP4_ADDR(&ipInfo.netmask, 255,255,255,0); // Access Point Subnet
IP4_ADDR(&dns_info.ip.u_addr.ip4, 192,168,1,1); // DNS Server address
tcpip_adapter_init();
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo);
tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info) ;
dns_init();
tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
}
// WiFi - WiFi Access Point Initialization
void WiFi_Configuration(void)
{
uint8_t WiFi_MAC[6];
wifi_init_config_t wconf = WIFI_INIT_CONFIG_DEFAULT();
wifi_config_t sconf;
esp_efuse_mac_get_default(WiFi_MAC);
esp_event_loop_init(WiFi_EventHandler, NULL);
esp_wifi_init(&wconf);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_AP);
sprintf((char *)sconf.ap.ssid,"ESP32");
sprintf((char *)sconf.ap.password,"alohomora");
sconf.ap.ssid_len = 0;
sconf.ap.channel = 10;
sconf.ap.authmode = WIFI_AUTH_WPA2_PSK;
sconf.ap.ssid_hidden = 0;
sconf.ap.max_connection = 3;
sconf.ap.beacon_interval = 2000;
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP,&sconf));
ESP_ERROR_CHECK(esp_wifi_start());
}
// WEBS - HTTP Server Initialization
void WEBS_Configuration(void)
{
httpd_config_t hconf = HTTPD_DEFAULT_CONFIG();
hconf.max_uri_handlers = 16;
httpd_start(&server,&hconf);
httpd_register_uri_handler(server,&price_change_conf);
httpd_register_uri_handler(server,&adv_set_login_conf);
httpd_register_uri_handler(server,&advanced_settings_conf);
httpd_register_uri_handler(server,&otapage_conf);
}
// NVSS - NON Volatile Storage Initialization
void NVSS_Configuration(void)
{
esp_err_t ret;
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
nvs_open("nvs",NVS_READWRITE,&hnvs);
ret = nvs_flash_init_partition("nvsuser");
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase_partition("nvsuser"));
ret = nvs_flash_init_partition("nvsuser");
}
nvs_open("nvsuser",NVS_READWRITE,&hnvsuser);
}
How should I merge these functions so that both esp now and wifi in AP mode works together?