Still no luck with this this is the mesh code
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <inttypes.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/event_groups.h"
#include "esp_event.h"
#include "esp_system.h"
#include "esp_netif.h"
#include "esp_smartconfig.h"
//#include "esp_event_loop.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#include "esp_mac.h"
#endif
#include "esp_bridge.h"
#include "esp_mesh_lite.h"
#define PAYLOAD_LEN (1456) /**< Max payload size(in bytes) */
static EventGroupHandle_t s_wifi_event_group;
const int WIFI_CONNECTED_BIT = BIT0;
static const int ESPTOUCH_DONE_BIT = BIT1;
static const char *TAG2 = "smartconfig_example";
static void check_status()
{
EventBits_t uxBits;
uxBits = xEventGroupGetBits(s_wifi_event_group);
if (uxBits & WIFI_CONNECTED_BIT)
{
printf("wifi connected\n");
}
else if (!(uxBits & WIFI_CONNECTED_BIT))
{
printf("wifi disconnected\n");
}
}
uint8_t ssid[33] = {0};
uint8_t password[65] = {0};
uint8_t rvd_data[65] = {0};
char *ssid_str = NULL;
char *password_str = NULL;
extern bool wifi_connected;
extern bool start_config;
#define SMARTCONFIG_START_CONFIG() { \
.enable_log = false, \
.esp_touch_v2_enable_crypt = true, \
.esp_touch_v2_key = "qwBIf6GbZZ5jj7Kc"};
static void smartconfig_example_task(void *parm);
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
check_status();
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
check_status();
}
else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE)
{
ESP_LOGI(TAG2, "Scan done");
}
else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL)
{
ESP_LOGI(TAG2, "Found channel");
}
else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD)
{
ESP_LOGI(TAG2, "Got SSID and password");
smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
wifi_config_t wifi_config;
bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
wifi_config.sta.bssid_set = evt->bssid_set;
if (wifi_config.sta.bssid_set == true)
{
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
}
memcpy(ssid, evt->ssid, sizeof(evt->ssid));
memcpy(password, evt->password, sizeof(evt->password));
ESP_LOGI(TAG2, "SSID:%s", ssid);
ESP_LOGI(TAG2, "PASSWORD:%s", password);
ESP_ERROR_CHECK(esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)));
ESP_LOGI(TAG2, "RVD_DATA:%s", rvd_data);
ESP_ERROR_CHECK(esp_wifi_disconnect());
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
esp_wifi_connect();
}
else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE)
{
nvs_handle_t my_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK)
{
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
}
else
{
err = nvs_set_str(my_handle, "ssid", (char *)ssid);
printf((err != ESP_OK) ? "Failed to write ssid!\n" : "Wrote ssid\n");
err = nvs_set_str(my_handle, "password", (char *)password);
printf((err != ESP_OK) ? "Failed to write password!\n" : "Wrote password\n");
err = nvs_set_str(my_handle, "rvd_data", (char *)rvd_data);
printf((err != ESP_OK) ? "Failed to write rvd_data!\n" : "Wrote rvd_data\n");
err = nvs_commit(my_handle);
printf((err != ESP_OK) ? "Failed to commit changes!\n" : "Committed changes\n");
nvs_close(my_handle);
}
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
}
}
static esp_err_t esp_storage_init(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
return ret;
}
void initialise_wifi(void)
{
// nvs_flash_erase();
nvs_handle_t my_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle); // Changed from NVS_READONLY to NVS_READWRITE
if (err == ESP_OK)
{
size_t required_size;
err = nvs_get_str(my_handle, "ssid", NULL, &required_size);
if (err == ESP_OK)
{
ssid_str = malloc(required_size);
err = nvs_get_str(my_handle, "ssid", ssid_str, &required_size);
}
err = nvs_get_str(my_handle, "password", NULL, &required_size);
if (err == ESP_OK)
{
password_str = malloc(required_size);
err = nvs_get_str(my_handle, "password", password_str, &required_size);
}
if (ssid_str && password_str)
{
ESP_LOGI(TAG2, "Found NVS WiFi credentials. Starting connecting to wifi...");
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_bridge_create_all_netif();
s_wifi_event_group = xEventGroupCreate();
wifi_config_t wifi_config = {
.sta = {
.ssid = "",
.password = "",
},
};
strncpy((char *)wifi_config.sta.ssid, ssid_str, sizeof(wifi_config.sta.ssid));
strncpy((char *)wifi_config.sta.password, password_str, sizeof(wifi_config.sta.password));
esp_bridge_wifi_set(WIFI_MODE_STA, (char *)wifi_config.sta.ssid, (char *)wifi_config.sta.password, NULL);
// Softap
memset(&wifi_config, 0x0, sizeof(wifi_config_t));
size_t softap_ssid_len = sizeof(wifi_config.ap.ssid);
if (esp_mesh_lite_get_softap_ssid_from_nvs((char *)wifi_config.ap.ssid, &softap_ssid_len) != ESP_OK)
{
snprintf((char *)wifi_config.ap.ssid, sizeof(wifi_config.ap.ssid), "%s", CONFIG_BRIDGE_SOFTAP_SSID);
}
size_t softap_psw_len = sizeof(wifi_config.ap.password);
if (esp_mesh_lite_get_softap_psw_from_nvs((char *)wifi_config.ap.password, &softap_psw_len) != ESP_OK)
{
strlcpy((char *)wifi_config.ap.password, CONFIG_BRIDGE_SOFTAP_PASSWORD, sizeof(wifi_config.ap.password));
}
esp_bridge_wifi_set(WIFI_MODE_AP, (char *)wifi_config.ap.ssid, (char *)wifi_config.ap.password, NULL);
}
else
{
ESP_ERROR_CHECK(esp_netif_init());
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG2, "Could not open NVS to read WiFi credentials. Starting SmartConfig...");
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
// free(ssid_str);
// free(password_str);
nvs_close(my_handle);
}
}
static void smartconfig_example_task(void *parm)
{
EventBits_t uxBits;
ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_V2));
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG();
ESP_ERROR_CHECK(esp_smartconfig_start(&cfg));
while (1)
{
uxBits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if (uxBits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG2, "WiFi Connected to ap");
}
if (uxBits & ESPTOUCH_DONE_BIT)
{
ESP_LOGI(TAG2, "smartconfig over");
esp_smartconfig_stop();
esp_restart();
vTaskDelete(NULL);
}
}
}
void app_main_mesh()
{
/**
* @brief Set the log level for serial port printing.
*/
esp_log_level_set("*", ESP_LOG_INFO);
esp_storage_init();
initialise_wifi();
if (WIFI_CONNECTED_BIT && ssid_str && password_str)
{
wifi_connected = true;
}
if (wifi_connected)
{
esp_mesh_lite_config_t mesh_lite_config = ESP_MESH_LITE_DEFAULT_INIT();
esp_mesh_lite_init(&mesh_lite_config);
esp_mesh_lite_start();
start_config = true;
}
}
and this is the code from the cpp
#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <EEPROM.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
#include <Preferences.h>
#include <WiFiClient.h>
extern "C" void app_main_mesh();
#define API_KEY ""
#define DATABASE_URL ""
#define USER_EMAIL ""
#define USER_PASSWORD ""
FirebaseData fbdo;
FirebaseData stream;
FirebaseAuth auth;
FirebaseConfig config;
Preferences preferences;
String parentPath = "/";
String childPath[2] = {"/brightness", "/color"};
bool start_config = false;
bool wifi_connected = false;
int count = 0;
volatile bool dataChanged = false;
unsigned long sendDataPrevMillis = 0;
void receivedCallback(uint32_t from, String &msg);
void streamCallback(FirebaseStream data)
{
printf("sream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\nvalue, %s\n\n",
data.streamPath().c_str(),
data.dataPath().c_str(),
data.dataType().c_str(),
data.eventType().c_str(),
data.stringData().c_str());
printResult(data);
printf("Received stream payload size: %d (Max. %d)\n\n", data.payloadLength(), data.maxPayloadLength());
dataChanged = true;
}
void streamTimeoutCallback(bool timeout)
{
if (timeout)
Serial.printf("stream timed out, resuming...\n");
if (!stream.httpConnected())
Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str());
}
void setup()
{
disableCore0WDT();
disableCore1WDT();
app_main_mesh();
//while (WiFi.status() != WL_CONNECTED)
//{
// printf(".");
//}
printf("startting\n\n");
printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
printf("startting2\n\n");
Firebase.reconnectNetwork(false);
printf("startting3\n\n");
fbdo.setBSSLBufferSize(16384 /* Rx buffer size in bytes from 512 - 16384 */, 16384 /* Tx buffer size in bytes from 512 - 16384 */);
printf("startting4\n\n");
stream.setBSSLBufferSize(16384 /* Rx buffer size in bytes from 512 - 16384 */, 16384 /* Tx buffer size in bytes from 512 - 16384 */);
printf("startting5\n\n");
Firebase.begin(&config, &auth);
if (fbdo.httpCode() < 0)
{
printf("Failed to initialize Firebase connection. Error: %s\n", fbdo.errorReason().c_str());
}
printf("startting6\n\n");
stream.keepAlive(5, 5, 1);
printf("startting7\n\n");
if (!Firebase.RTDB.beginStream(&stream, "/WACDEtKtNgat7RRJIZelTa5WgpH2/rooms/yeet"))
printf("sream begin error, %s\n\n", stream.errorReason().c_str());
Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);
}
void loop()
{
if (dataChanged)
{
dataChanged = false;
}
}
void receivedCallback(uint32_t from, String &msg)
{
printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
}
this is the log output
I (29) boot: ESP-IDF 4.4.5 2nd stage bootloader
I (29) boot: compile time 20:18:37
I (29) boot: chip revision: v3.0
I (32) boot.esp32: SPI Speed : 40MHz
I (37) boot.esp32: SPI Mode : DIO
I (41) boot.esp32: SPI Flash Size : 4MB
I (46) boot: Enabling RNG early entropy source...
I (51) boot: Partition Table:
I (55) boot: ## Label Usage Type ST Offset Length
I (62) boot: 0 nvs WiFi data 01 02 00009000 00004000
I (69) boot: 1 otadata OTA data 01 00 0000d000 00002000
I (77) boot: 2 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 3 ota_0 OTA app 00 10 00010000 001e0000
I (92) boot: 4 ota_1 OTA app 00 11 001f0000 001e0000
I (99) boot: 5 coredump Unknown data 01 03 003d0000 00010000
I (107) boot: 6 reserved Unknown data 01 fe 003e0000 00020000
I (114) boot: End of partition table
I (119) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=20640h (132672) map
I (175) esp_image: segment 1: paddr=00030668 vaddr=3ffb0000 size=03204h ( 12804) load
I (180) esp_image: segment 2: paddr=00033874 vaddr=40080000 size=0c7a4h ( 51108) load
I (202) esp_image: segment 3: paddr=00040020 vaddr=400d0020 size=c8f94h (823188) map
I (500) esp_image: segment 4: paddr=00108fbc vaddr=4008c7a4 size=089b0h ( 35248) load
I (525) boot: Loaded app from partition at offset 0x10000
I (525) boot: Disabling RNG early entropy source...
I (537) cpu_start: Pro cpu up.
I (537) cpu_start: Starting app cpu, entry point is 0x40081ddc
I (523) cpu_start: App cpu up.
I (553) cpu_start: Pro cpu start user code
I (553) cpu_start: cpu freq: 160000000
I (554) cpu_start: Application information:
I (558) cpu_start: Project name: espidf-arduino-wifiscan
I (564) cpu_start: App version: 1
I (569) cpu_start: Compile time: Nov 11 2023 21:40:28
I (575) cpu_start: ELF file SHA256: 8031330a96b0b205...
I (581) cpu_start: ESP-IDF: 4.4.5
I (586) cpu_start: Min chip rev: v0.0
I (590) cpu_start: Max chip rev: v3.99
I (595) cpu_start: Chip rev: v3.0
I (600) heap_init: Initializing. RAM available for dynamic allocation:
I (607) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (613) heap_init: At 3FFB8B18 len 000274E8 (157 KiB): DRAM
I (619) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (626) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (632) heap_init: At 40095154 len 0000AEAC (43 KiB): IRAM
I (640) spi_flash: detected chip: generic
I (643) spi_flash: flash io: dio
I (650) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (74) smartconfig_example: Found NVS WiFi credentials. Starting connecting to wifi...
I (76) bridge_common: esp-iot-bridge version: 0.9.0
I (811) wifi:wifi driver task: 3ffbb178, prio:23, stack:6656, core=0
I (816) system_api: Base MAC address is not set
I (821) system_api: read default base MAC address from EFUSE
I (849) wifi:wifi firmware version: 0f80fa0
I (849) wifi:wifi certification version: v7.0
I (849) wifi:config NVS flash: enabled
I (849) wifi:config nano formating: disabled
I (853) wifi:Init data frame dynamic rx buffer num: 32
I (858) wifi:Init management frame dynamic rx buffer num: 32
I (863) wifi:Init management short buffer num: 32
I (868) wifi:Init dynamic tx buffer num: 32
I (872) wifi:Init static rx buffer size: 1600
I (876) wifi:Init static rx buffer num: 10
I (880) wifi:Init dynamic rx buffer num: 32
I (885) wifi_init: rx ba win: 6
I (887) wifi_init: tcpip mbox: 32
I (891) wifi_init: udp mbox: 6
I (895) wifi_init: tcp mbox: 6
I (899) wifi_init: tcp tx win: 5744
I (903) wifi_init: tcp rx win: 5744
I (907) wifi_init: tcp mss: 1440
I (911) wifi_init: WiFi IRAM OP enabled
I (916) wifi_init: WiFi RX IRAM OP enabled
I (921) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1023) wifi:mode : null
I (1025) ip select: IP Address:192.168.4.1
I (1025) ip select: GW Address:192.168.4.1
I (1025) ip select: NM Address:255.255.255.0
I (1029) bridge_wifi: IP Address:192.168.4.1
Add netif ap with (commit id)
I (1037) bridge_common: netif list add success
I (1043) wifi:mode : softAP (c0:49:ef:e0:b8:fd)
I (1049) wifi:Total power save buffer number: 16
I (1051) wifi:Init max length of beacon: 752/752
I (1055) wifi:Init max length of beacon: 752/752
Add netif sta with (commit id)
I (1062) bridge_common: netif list add success
I (1068) wifi:mode : sta (c0:49:ef:e0:b8:fc) + softAP (c0:49:ef:e0:b8:fd)
I (1074) wifi:enable tsf
I (1076) bridge_wifi: Found ssid EE-Hub-wKt8
I (1081) bridge_wifi: Found password VAN-amble-late
I (1087) wifi:Disabled PMF config for STA
I (1090) bridge_wifi: [esp_bridge_wifi_set] sta ssid: EE-Hub-wKt8 password: VAN-amble-late
W (1100) vendor_ie: Error Get[4354]
W (1103) vendor_ie: Error Get[4354]
I (1863) wifi:Total power save buffer number: 16
I (1864) bridge_wifi: [esp_bridge_wifi_set] softap ssid: ESP_Bridge password: 12345678
I (1865) Mesh-Lite: esp-mesh-lite component version: 0.9.0
Mesh-Lite commit id: 57bc05c
I (1875) vendor_ie: Mesh ID: 77
W (1878) vendor_ie: Error Get[4354]
W (1882) vendor_ie: Error Get[4354]
I (1888) ESP_Mesh_Lite_Comm: msg action add success
I (1892) ESP_Mesh_Lite_Comm: msg action add success
I (1894) ESP_Mesh_Lite_Comm: Bind Socket 54, port 6364
W (1898) vendor_ie: [Unexpected restart]: the rtc data: level:1, ssid:EE-Hub-wKt8, bssid 00:00:00:00:00:00
I (1904) ESP_Mesh_Lite_Comm: Bind Socket 55, port 6363
W (1914) vendor_ie: Try to connect the original parent node
I (1920) ESP_Mesh_Lite_Comm: Bind Socket 56, port 6366
startting
I (1932) ESP_Mesh_Lite_Comm: Bind Socket 57, port 6365
I (1936) wifi:ap channel adjust o:1,1 n:11,2
Firebase Client v4.4.8
I (1942) wifi:new:<11,0>, old:<1,1>, ap:<11,2>, sta:<11,0>, prof:1
startting2
startting3
startting4
startting5
I (2741) wifi:state: init -> auth (b0)
I (2745) wifi:state: auth -> assoc (0)
I (2750) wifi:state: assoc -> run (10)
I (2778) wifi:connected with EE-Hub-wKt8, aid = 9, channel 11, BW20, bssid = c4:e5:32:85:a7:e9
I (2779) wifi:security: WPA2-PSK, phy: bgn, rssi: -34
I (2781) wifi:pm start, type: 1
I (2795) wifi:<ba-add>idx:0 (ifx:0, c4:e5:32:85:a7:e9), tid:6, ssn:0, winSize:64
I (2866) wifi:AP's beacon interval = 102400 us, DTIM period = 3
I (3789) esp_netif_handlers: sta ip: 192.168.1.249, mask: 255.255.255.0, gw: 192.168.1.254
I (3790) bridge_wifi: Connected with IP Address:192.168.1.249
I (3794) vendor_ie: RTC store: ssid:EE-Hub-wKt8; bssid:00:00:00:00:00:00 crc:2871029244
I (18340) wifi:<ba-add>idx:1 (ifx:0, c4:e5:32:85:a7:e9), tid:0, ssn:2, winSize:64
if p put the classic arduino while WL_CONNECTED it just stays in the while forever is the issue the firebase / arduino cant connect tot the wifi ? or the connected bit the arduino tries to get is different from the idf ?
also i know it looks like the wifi is established after the firebase begin call but even if i do it as before with the waiting for the time flag the same result… again if i start the wifi arduino side it all runs fine just the mesh needs to be commented out