How to use the NVS with the ESP32 #243
Replies: 1 comment 3 replies
-
Hi @wduraes, thanks for detailed explanation. I am having issues when implmenting this. I hope you can give me some guidance. I am getting this error when building the project: Are you familiar with this error? Attached you can find the files that I edited, following these intructions. Thanks in advanced. |
Beta Was this translation helpful? Give feedback.
-
Pre-requirement: before changing the ESP32 sample to use NVS, please follow the sample in the official repo to ensure the basic environment, toolchain and your code are all correct.
Using NVS with the eSP32
This file shows the changes needed to separate the device credentials from the sample binaries in 2 steps:
STEP 1
To store data into the ESP32 NVS, use the code from this gist: https://gist.github.com/wduraes/584d5d517ea59aaf2f4cc6835cd491d6
The best way to do it is to duplicate the sample called nvs_rw_value (under you ESP-IDF directory /examples/storage) and replace its nvs_value_example.c code for the gist above.
Add your own credentials (lines 12 to 16) and flash the device.
At this point, your device will have the credentials stored to the NVS.
Repeat this step for each device you wish to connect.
STEP 2
Open file demos\projects\ESPRESSIF\adu\main\azure_iot_freertos_esp32_main.c
Add the function below to read data from NVS
Add a call for that function under app_main, right before the call to connect network:
Add the NVS and ESP headers to deal with NVS:
Also add the variables to be populated by NVS read (note I'm creating char arrays that are 32 characters long. If your device credentials are longer, adjust accordingly)
Finally, change where the code get its wifi and password from:
comment out:
then add the below:
Function to read NVS and populate Wifi and password data are fine now, however we still need to populate for hub FQDN, device id and key in a different file.
Open the file demos\sample_azure_iot_adu\sample_azure_iot_pnp_data_if.h
Add the extern variables below to allow for its use across the sample:
Finally, open the file demos\sample_azure_iot_adu\sample_azure_iot_adu.c
Add local variables for use:
Under the function prvAzureDemoTask replace the reading from current variables:
Also under #ifdef democonfigDEVICE_SYMMETRIC_KEY replace these 3 lines:
Done!
Now, your sample will read from NVS, and correctly populate wifi credentials and Azure credentials.
Compile and build once again and it should be working.
Beta Was this translation helpful? Give feedback.
All reactions