B4R Library rArduinoNvs NVS library for esp32

it is a wrapper for NVS library from : https://github.com/rpolitex/ArduinoNvs

NVS is a port for a non-volatile storage (NVS, flash) library for ESP32 to the Arduino Platform and B4R now.

The ESP32 NVS stored data in the form of key-value. Keys are ASCII strings, up to 15 characters. Values can have one of the following types:
  • integer types in B4R: byte, uint, int, ulong, long, ArrayByte, String
  • read of String can be made by return or in a buffer
  • variable length binary data (blob) are used for ArrayByte only and read is done in a buffer
  • and with B4RSerializator you can create ArrayByte with multiple variable, to save it with NVS, and to recover it when needed
thanks peacemaker for his highlight of this library
 

Attachments

  • rArduinoNvs_V1.1.zip
    7.7 KB · Views: 135
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Thanks for the library !
Especially for "erase" methods: i have found that after first flashing if to try to Read something from NVS - MCU hangs, i think, due to cannot find correct records.
 

candide

Active Member
Licensed User
i did a test of read after full erase and i have result with "default value" when key was not created.

No problem seen in my case.
 

peacemaker

Expert
Licensed User
Longtime User
Yes, i meant that the trouble is if without pre-erasing initially.
 

candide

Active Member
Licensed User
in my case i cannot reproduce if it is only in first read of data after creation of "storage"
 

Mostez

Well-Known Member
Licensed User
Longtime User
Thanks for the library, storing parameters like password, IP address, and Wi-Fi settings..., is much easier now.
 

Mostez

Well-Known Member
Licensed User
Longtime User
Any your code example ?
I've replaced my old code with NVS, it works OK on ES32 device, here what I've done:

I declared NVS keys constants and config Type
B4R:
Private Const MEM_DEVICE_ID As String = "DEVICE_ID"
    Private Const MEM_DEVICE_IP As String = "DEVICE_IP"
    Private const MEM_GATEWAY_IP As String = "GATEWAY_IP"
    Private const MEM_SUBNET_MASK As String = "SUBNET_MASK"
    Private Const MEM_IS_STATIC_IP As String = "IS_STATIC_IP"
    Private Const MEM_SERVER_IP As String = "SERVER_IP"
    Private Const MEM_SERVER_PORT As String = "SERVER_PORT"
    Private Const MEM_NETWORK_SSID As String = "NETWORK_SSID"
    Private Const MEM_NETWORK_PW As String = "NETWORK_PW"
    Private Const MEM_LCD_CONTRAST As String = "LCD_CONTRAST"
    Private Const MEM_LCD_BL_LEVEL As String = "LCD_BL_LEVEL"

    Type DeviceConfig(DeviceID As String, DeviceIP As String , GateWayIP As String, SubnetMask As String , ServerIP As String, _
            ServerPort As UInt, IsStatic As Boolean,NetworkSSID As String, NetworkPassword As String,LCDcontrast As Byte, LCDbacklightLevel As Byte)

Using this sub to store values:
B4R:
Private Sub SaveConfigs(Config As DeviceConfig)   
    NVS.setstring(MEM_SERVER_IP,Config.ServerIP,False)
    NVS.setUInt(MEM_SERVER_PORT,Config.ServerPort,False)   
    NVS.setString(MEM_DEVICE_ID, Config.DeviceID ,False)   
    NVS.setstring(MEM_DEVICE_IP,Config.DeviceIP,False)
    NVS.setstring(MEM_GATEWAY_IP,Config.GateWayIP,False)
    NVS.setstring(MEM_SUBNET_MASK,Config.SubnetMask,False)
    NVS.setByte(MEM_IS_STATIC_IP, SysTools.BooleanToByte(Config.IsStatic) ,False)
    NVS.setString(MEM_NETWORK_SSID, Config.NetworkSSID,False)
    NVS.setString(MEM_NETWORK_PW, Config.NetworkPassword,False)   
    NVS.setByte(MEM_LCD_CONTRAST,Config.LCDcontrast,False)
    NVS.setByte(MEM_LCD_BL_LEVEL,Config.LCDbacklightLevel,False)
End Sub

to read value, I just call function, I know it would be easier to read all of values one time and return them in DeviceConfig type, just wanted to call a specific function only where I need it, not to read the whole thing every time, which method is best?

B4R:
private Sub GetDeviceID () As String
    Return NVS.getString1(MEM_DEVICE_ID)
End Sub

private Sub GetDeviceIP() As String
    Return NVS.getString1(MEM_DEVICE_IP)
End Sub
 
to read value, I just call function, I know it would be easier to read all of values one time and return them in DeviceConfig type, just wanted to call a specific function only where I need it, not to read the whole thing every time, which method is best?
Hi, Thanks for nice sharing. At power up of ESP it is better to read all values in one go and after that if we change any value and save it and then to read any single value is also good way. So please share method to read all values one time at startup. Thanks.
 
Last edited:

Mostez

Well-Known Member
Licensed User
Longtime User
I tried to return DeviceConfig type from sub ,like:
B4R:
Private Sub ReadAll() As DeviceConfig
but, this error occurred:
Objects cannot be returned from subs (only primitives, strings and arrays of primitives).
 
I tried to return DeviceConfig type from sub ,like:
B4R:
Private Sub ReadAll() As DeviceConfig
but, this error occurred:
Objects cannot be returned from subs (only primitives, strings and arrays of primitives).

Just waiting for the answer...

To get only a sensor data, it is perfect to use ESP8266 so is there chance to have rArduinoNvs NVS library for esp8266. Thanks.
 
Top