<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Storing settings in the memory of the Core]]></title><description><![CDATA[<h1>Storing settings in the memory of the Core</h1>
<p dir="auto">Hello! If you are fortunate enough to hold the <strong>ESP32</strong> microcontroller in your hands (I was more fortunate and have <strong>M5Stack</strong> in my hands) from the Chinese company <strong>ESPRESSIF</strong>, then this post may be useful.</p>
<p dir="auto"><img src="https://sun9-65.userapi.com/c858232/v858232374/fc72e/ppkebAPjiCo.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">There is a situation when it is necessary to save some parameters in non-volatile memory (for example: count the number of times the device is turned on for the entire time or save the Wi-Fi settings). This can be done with ease using the <strong>Preferences</strong> library.</p>
<p dir="auto">We declare an instance of the <strong>Preferences</strong> class, and there we will see ...</p>
<p dir="auto">The first thing we need to do is <strong>create</strong> a keychain by calling the <strong>begin</strong> method with a pair of arguments (but only with the first one): the name of the keychain and the read-only flag.</p>
<p dir="auto">To <strong>save</strong> a string value in memory, you need to pass the key and the value itself to a method whose name consists of two parts: the first is <strong>put</strong> and the second is the type name, for example: <strong>String</strong>. Everything is clear and understandable. True, there are still "raw" bytes <s>without roasting</s>, to which no one wants to assign a type. In this case, the method also takes the third argument with the number of these bytes. With this procedure, everything seems to be.</p>
<p dir="auto">After the value has been written, you can <strong>read</strong> using the method (whose name is similar to the previous one), where the first part will be <strong>get</strong>. This method returns the value for the key of the corresponding type. Remember the byte case? If you don’t know (or don’t remember) how many bytes are on the key, then pull the <strong>getBytesLength</strong> method with a single argument - the key, it will calculate everything and return the amount to <strong>size_t</strong>.</p>
<p dir="auto">If you want to <strong>remove</strong> a certain key from the keychain, then give it the only argument to the <strong>remove</strong> method.</p>
<p dir="auto">Do you want to bring a real marafet and <strong>clear the whole bunch</strong>? Call the <strong>clear</strong> method without any arguments!</p>
<p dir="auto">When you wish to <strong>complete</strong> the work with the bundle, call the <strong>end</strong> method without any arguments.</p>
<p dir="auto">In general, the following types are supported: <strong>Char</strong>, <strong>UChar</strong>, <strong>Short</strong>, <strong>UShort</strong>, <strong>Int</strong>, <strong>UInt</strong>, <strong>Long</strong>, <strong>ULong</strong>, <strong>Long64</strong>, <strong>ULong64</strong>, <strong>Float</strong>, <strong>Double</strong>, <strong>Bool</strong>, <strong>String</strong> and <strong>Bytes</strong>.</p>
<p dir="auto">I understand that I want <s>something</s> code, so here is a sketch. A sketch counts the number of turns on the device and displays it on the display:</p>
<pre><code class="language-cpp">#include &lt;M5Stack.h&gt;
#include &lt;Preferences.h&gt;

Preferences preferences;
const char* key = "OnOff";
uint32_t count;

void setup() {
  m5.begin();
  preferences.begin("MyKeyChain");
  count = preferences.getUInt(key);
  preferences.putUInt(key, count + 1);
  M5.Lcd.setTextSize(3);
  M5.Lcd.setTextColor(TFT_WHITE);
  M5.Lcd.println("Hello, Habr!");
  M5.Lcd.setTextSize(2);
  M5.Lcd.println("M5Stack Turned On:");
  M5.Lcd.setTextSize(3);
  M5.Lcd.setTextColor(TFT_RED);
  M5.Lcd.println(count);
  M5.Lcd.setTextColor(TFT_WHITE);
  M5.Lcd.setTextSize(2);
  M5.Lcd.println("times");
}

void loop() { }
</code></pre>
<p dir="auto">Library <a href="https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences" target="_blank" rel="noopener noreferrer nofollow ugc">link</a>.</p>
]]></description><link>https://community.m5stack.com/topic/1496/storing-settings-in-the-memory-of-the-core</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 02:29:32 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/1496.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Nov 2019 08:51:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Storing settings in the memory of the Core on Sat, 09 Jan 2021 16:43:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dimi" aria-label="Profile: dimi">@<bdi>dimi</bdi></a><br />
Congratulations ! Thank you very much.</p>
]]></description><link>https://community.m5stack.com/post/11627</link><guid isPermaLink="true">https://community.m5stack.com/post/11627</guid><dc:creator><![CDATA[cacb]]></dc:creator><pubDate>Sat, 09 Jan 2021 16:43:21 GMT</pubDate></item></channel></rss>