Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino libraries not found #116

Closed
bernch1nv opened this issue Dec 12, 2022 · 18 comments
Closed

Arduino libraries not found #116

bernch1nv opened this issue Dec 12, 2022 · 18 comments

Comments

@bernch1nv
Copy link

bernch1nv commented Dec 12, 2022

I'm trying to use Arduino based libraries within the light example.
I followed the install guide here: https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html , but installed the library globally under /esp-idf/components, and it seems to work as I can compile the sample_project in esp-idf/examples/get-started folder.

My project is located in esp-matter/examples, and when I try to include Arduino.h it says "fatal error: Arduino.h: No such file or directory"

I also set "CONFIG_FREERTOS_HZ" in sdkconfig to 1000 (as required by arduino-esp32)
I can find the Arduino Component now under menuconfig (and also tried ticking the box within compatibility options to "Include headers across components as before IDF v4.0" -> made no difference)

Still getting the error that Arduino.h can't be found.

Using ESP-IDF: v4.4.2

Any ideas how Arduino can be correctly added to an esp-matter project?

@sanketwadekar
Copy link
Contributor

Hi @bernch1nv,
I tried the steps you mentioned above and was able to compile the example given here by creating a sample project in esp-matter/examples.
Using IDF Branch: release/v4.4

Is there any other library that you are using with Arduino ?

@bernch1nv
Copy link
Author

Hi @sanketwadekar ,
all I'm using is a copy of the light example of esp-matter, where I so far only added #include <Arduino.h> in app_main.cpp . This already causes the error.
Strangely while the idf.py build command is running, it does print that its building some objects which are Arduino-related.
I also did not change anything in the menuconfig regarding the use of setup & loop functions etc., just the include of Arduino.h in app_main.cpp.

@sanketwadekar
Copy link
Contributor

@SuGlider maybe you can provide some help.

@SuGlider
Copy link

@bernch1nv -

I followed the install guide here: https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html , but installed the library globally under /esp-idf/components, and it seems to work as I can compile the sample_project in esp-idf/examples/get-started folder.

The libraries shall not go under /esp-idf/components.
An example of folder strucuture is https://github.com/SuGlider/RainMaker_as_Arduino_Component
It is a RainMaker Arduino Project as IDF component.

You should clone Matter in the /yourProject/components
Then you shall add its components to the CMakeList.txt of the project:

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

set(EXTRA_COMPONENT_DIRS components/esp-matter/components)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(arduino_matter_test)

@SuGlider
Copy link

SuGlider commented Dec 15, 2022

I was reading the code from https://github.com/espressif/esp-matter/blob/main/examples/light/main/app_main.cpp and it does have already an app_main() function.

Is that code that you want to use and add to it some Arduino API calls?

@bernch1nv
Copy link
Author

My goal is to have the device operating (based on a hw-switch) in either Wifi AP-mode for configuration purposes, or in matter-"mode". As I have the AP-mode part already written using libraries like ESPAsyncWebserver, AsyncTCP etc. which are based on arduino-esp32 I somehow need the Arduino.h file in there

@SuGlider
Copy link

How is the project structured in the folders?
Is Arduino cloned to the /components folder of your project?

@RoCorbera
Copy link

Matter has its own commissioning methods.
WiFi credentials shall be passed to the Matter device using the BLE commissioning secure protocol.

@bernch1nv - What is AP-mode used for in your project?
Matter devices will use WiFi for IPv6 communications over the Matter network.

@bernch1nv
Copy link
Author

bernch1nv commented Dec 16, 2022

When I opened this issue, as described I installed arduino globally under {$home}/esp-idf/components (with the intention to being able to use it by multiple projects)
esp-matter was installed under {$home}/esp-matter (so everything should be like the development guide says)
Project was located under {$home}/esp-matter/examples/my_project (which was just a copy of the light example)
This is where i tried to include Arduino.h in my_project/main/app_main.cpp and got that error.

Right now I just removed my existing esp-matter installation, and moved my project to {$home}/esp-projects/my_project

There I now cloned the esp-matter repo into my_project/components/esp-matter, as suggested by @SuGlider
And also cloned arduino in there.

I did not adapt the CMakeLists.txt file though, because i think the current set EXTRA_COMPONENTS_DIRS looks fine for me, as it uses the ESP_MATTER_PATH variable.

With this setup, I now get the Error "Failed to resolve component 'app_reset'", because esp-matter does not contain a CMakeLists.txt file

@RoCorbera AP-mode is hosting a webapp for configuration purposes, for variables that cannot be known in production yet.
But like I said, I want to make it either operating in AP-mode OR matter-mode. The only thing the both modes share would be some variables in NVS.

@SuGlider
Copy link

With this setup, I now get the Error "Failed to resolve component 'app_reset'", because esp-matter does not contain a CMakeLists.txt file

app_reset is in Examples/Common, not as a component.
https://github.com/espressif/esp-matter/tree/5ff7d0d7fa9e93d248b557f0c4eee2a7df5410d8/examples/common/app_reset

In the other Matter examples, the CMakeLists.txt adds the common folder to the sources and includes.
https://github.com/espressif/esp-matter/blob/5ff7d0d7fa9e93d248b557f0c4eee2a7df5410d8/examples/light_switch/CMakeLists.txt

You may need to add this folder to your project and fix your project's CMakeLists.txt to add it.

@SuGlider
Copy link

SuGlider commented Dec 18, 2022

@bernch1nv - I've built the esp-matter/examples/light example with an extra line #include <Arduino.h> as requested.

In order to get it done, I followed these steps:

# Fixes Tick in Arduino
CONFIG_FREERTOS_HZ=1000

To fix this error there are a couple possibilitites:

  1. Change the name of INADDR_NONE variable from esp-matter/components/arduino/cores/esp32 and in the WiFi Library (esp-matter/components/arduino/libraries/WiFi/src)
    https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/IPAddress.cpp#L125
    https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/IPAddress.h#L95
    https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiAP.cpp#L50
    https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiSTA.cpp#L54
    https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiGeneric.cpp#L86

  2. OR Change the esp-matter file esp-matter/connectedhomeip/connectedhomeip/src/inet/IPAddress.h
    https://github.com/espressif/connectedhomeip/blob/87bee4de25886ba00f1a177cd0f8791a48528a1f/src/inet/IPAddress.h#L51
    it shouldn't include lwip/inet.h and instead just define two symbols that will be enough to compile it.
    Change the line 51 of esp-matter/connectedhomeip/connectedhomeip/src/inet/IPAddress.h to:

//#include <lwip/inet.h>
#define INET6_ADDRSTRLEN  46
#define INET_ADDRSTRLEN   16

Finally, just run idf.py build in the esp-matter/examples/light example and it will get it done fine.

@SuGlider
Copy link

@bernch1nv

/home/chbe/esp-idf/components/arduino-esp32/cores/esp32/esp32-hal-cpu.c:47:16: note: in expansion of macro 'MHZ'
const uint32_t MHZ = 1000000;

It seems that the folders are not setup as I instructed.
You have Arduino cloned in the IDF folder instead.
Please delete the whole esp-idf/components/arduino-esp32/ from your IDF envionment.

You should have something like this:

 /myProj
     |
     +-- esp-idf-v4.4.2/                  ==> cloned from esp-idf.git + checkout v4.4.2 + install.sh + export.sh
     |
     +-- esp-matter/                      ==> cloned from esp-matter.git
     +-- esp-matter/components/arduino    ==> cloned from arduino-esp32.git and modified as instructed
     +-- esp-matter/examples/light        ==> add "Arduino.h" and necessary sdkconfig/CMakeLisit.txt changes

@SuGlider
Copy link

@bernch1nv - Please let me know if this issue is solved. Thanks,

@bernch1nv
Copy link
Author

bernch1nv commented Dec 20, 2022

So the first time I tried this I didn't remove arduino under esp-idf/components, you're right.
Now the build runs as described when including Arduino.h, thanks for that!

However now I just tried to establish a wifi in AP mode, to see if this is really working:

const char* ssid = "examplewifissid";
const char* password = "tenonezero";
WiFi.mode(WIFI_AP);                //Configure Access Point mode
WiFi.softAP(ssid, password);    //Password length minimum 8 char

But I get this error:

/home/chbe/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/arduino/libarduino.a(WiFiGeneric.cpp.obj):(.literal._Z16wifiLowLevelInitb+0x24): undefined reference to `esp_netif_create_default_wifi_ap'
/home/chbe/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/arduino/libarduino.a(WiFiGeneric.cpp.obj): in function `wifiLowLevelInit(bool)':
/home/chbe/esp-matter/components/arduino/libraries/WiFi/src/WiFiGeneric.cpp:624: undefined reference to `esp_netif_create_default_wifi_ap'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

@SuGlider
Copy link

SuGlider commented Dec 20, 2022

@bernch1nv

OK, now it is another issue.

This issue is related to the linker that can't find the function in the object files.
It may happen quite often along your project development and it will demand that you compare your Matter MyProj sdkconfig with the Arduino default sdkconfig and then find what you need to add to your project sdkconfig in order to build it.
The Arduino default sdkconfig file is in
https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/esp32/sdkconfig
You can change it to the target SoC of your project in the sdk/esp32xxxx/ folder.

For this specific issue with softAp() you must add CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y to your sdkconfig file.
You may want to add it to the sdkconfig.defaults in order to make it global for any SoC, but then, please don't forget to delete the previous sdkconfig file in the project folder in order to force rebuilding it again.

I added this line to my sdkconfig and it builds fine.

You shall have in mind that mixing Matter Code and Framework with Arduino Code and Framework won't be easy.
Each one initializes NVS, PSRAM, LwIP, BLE, etc. in a different way.
Compiling doesn't mean that it will run...

@SuGlider
Copy link

SuGlider commented Dec 20, 2022

Arduino has its own app_main() function that initializes everything necessary to make Arduino work.
It depends on a set of global #define and it may conflict with the way Matter wants to initialize its own data and peripherals.

You can check the Arduino initialization in
https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/main.cpp
https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-misc.c#L217-L268

Plus all other system bring-up functions....

@SuGlider
Copy link

@bernch1nv - I made it work and start SoftAP with this code:

extern "C" void app_main()
{


    esp_err_t ard_err = nvs_flash_init();
    if(ard_err == ESP_ERR_NVS_NO_FREE_PAGES || ard_err == ESP_ERR_NVS_NEW_VERSION_FOUND){
        const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
        if (partition != NULL) {
            ard_err = esp_partition_erase_range(partition, 0, partition->size);
            if(!ard_err){
                ard_err = nvs_flash_init();
            } else {
                printf("ARDUINO :: Failed to format the broken NVS partition!");
            }
        } else {
            printf("ARDUINO :: Could not find NVS partition");
        }
    }
    if(ard_err) {
        printf("ARDUINO :: Failed to initialize NVS! Error: %u", ard_err);
    }

    Serial.begin(115200);
    Serial.println();

    const char* ssid = "examplewifissid";
    const char* password = "tenonezero";
    Serial.print("Setting soft-AP ... ");
    WiFi.mode(WIFI_AP);                //Configure Access Point mode
    Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");    //Password length minimum 8 char

    Serial.print("Soft-AP IP address = ");
    Serial.println(WiFi.softAPIP());
    
    Serial.println("======================================");
}

OUTPUT:

Setting soft-AP ... I (918) wifi:wifi driver task: 3ffdb8fc, prio:23, stack:6656, core=0
I (922) wifi:wifi firmware version: eeaa27d
I (922) wifi:wifi certification version: v7.0
I (922) wifi:config NVS flash: enabled
I (925) wifi:config nano formating: disabled
I (929) wifi:Init data frame dynamic rx buffer num: 32
I (933) wifi:Init management frame dynamic rx buffer num: 32
I (939) wifi:Init management short buffer num: 32
I (943) wifi:Init dynamic tx buffer num: 32
I (947) wifi:Init static rx buffer size: 1600
I (951) wifi:Init static rx buffer num: 4
I (955) wifi:Init dynamic rx buffer num: 32
I (960) wifi_init: rx ba win: 6
I (963) wifi_init: tcpip mbox: 32
I (967) wifi_init: udp mbox: 6
I (970) wifi_init: tcp mbox: 6
I (974) wifi_init: tcp tx win: 5744
I (978) wifi_init: tcp rx win: 5744
I (982) wifi_init: tcp mss: 1440
I (986) wifi_init: WiFi IRAM OP enabled
I (991) wifi_init: WiFi RX IRAM OP enabled
I (997) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1100) wifi:mode : softAP (84:0d:8e:e3:b2:19)
I (1102) wifi:Total power save buffer number: 16
I (1102) wifi:Init max length of beacon: 752/752
I (1104) wifi:Init max length of beacon: 752/752
I (3028) wifi:Total power save buffer number: 16
Ready
Soft-AP IP address = 192.168.4.1
======================================

@bernch1nv
Copy link
Author

@SuGlider now this works, guess I can close this issue now.
Hope I don't run too much into conflicts as I plan to not have matter code run simultaneously with the Arduino/SoftAP code, but we will see.
Thanks for your support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants