{"id":114323,"date":"2022-08-25T11:54:08","date_gmt":"2022-08-25T11:54:08","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=114323"},"modified":"2022-08-25T11:54:11","modified_gmt":"2022-08-25T11:54:11","slug":"esp32-deta-base-database-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-deta-base-database-arduino\/","title":{"rendered":"ESP32: Getting Started with Deta Base (Unlimited and Free Database for Developers)"},"content":{"rendered":"\n<p>Get started with Deta Base using the ESP32 board. Deta Base is a NoSQL database. It is unlimited, free, and easy to use. Additionally, it requires minimal setup. So, it&#8217;s perfect for your hobbyist projects and prototyping. You&#8217;ll learn how to perform create, read, update, delete and query operations in a Deta Base instance using an ESP32.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Getting Started with Deta Base Unlimited and Free Database for Developers\" class=\"wp-image-114353\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>To interact with Deta base using the ESP32, we&#8217;ll use the <a href=\"https:\/\/github.com\/A223D\/detaBaseArduinoESP32\" target=\"_blank\" rel=\"noreferrer noopener\">detaBaseArduinoESP32 library<\/a>. The present tutorial was based on the guides created by the library developer. You can find the guides on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/A223D\/ESP32DetaGuides\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Deta Base Guides by Kushagra Goel<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Deta Base<\/h2>\n\n\n\n<p>The best way to describe <a href=\"https:\/\/www.deta.sh\/\" target=\"_blank\" rel=\"noreferrer noopener\">Deta Base<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Deta Base is a fully-managed, fast, scalable and secure NoSQL database with a focus on end-user simplicity. It offers a&nbsp;<a href=\"https:\/\/docs.deta.sh\/docs\/base\/base_ui\" target=\"_blank\" rel=\"noreferrer noopener\">UI<\/a>&nbsp;through which you can easily see, query, update and delete records in the database.<\/p><cite>https:\/\/docs.deta.sh\/docs\/base\/about<\/cite><\/blockquote>\n\n\n\n<p>And the best part is that Deta Base is <strong>free <\/strong>to use!<\/p>\n\n\n\n<p>If you&#8217;re wondering where your data is saved and if it is secured, here&#8217;s the answer:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Your data is encrypted and stored safely on AWS. Encryption keys are managed by AWS; AWS manages Exabytes of the world&#8217;s most sensitive data.<\/p><cite>https:\/\/docs.deta.sh\/docs\/base\/about#is-my-data-secure<\/cite><\/blockquote>\n\n\n\n<p>We recommend taking a look at the docs to get more familiar with Deta Base:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.deta.sh\/docs\/base\/about\" target=\"_blank\" rel=\"noreferrer noopener\">Deta Base documentation<\/a><\/li><\/ul>\n\n\n\n<p>Deta Base is still in the beta version, so you may expect improvements in the future.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Deta Base Account<\/h2>\n\n\n\n<p>To get started, you need to create a Deta Base account. Go to <a href=\"http:\/\/deta.sh\" target=\"_blank\" rel=\"noreferrer noopener\">deta.sh<\/a>, and click on <strong>Join Deta<\/strong> to create a new account.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"364\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-website.jpg?resize=750%2C364&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"deta base website\" class=\"wp-image-114332\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-website.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-website.jpg?resize=300%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Enter a username, password, and email to create a new account.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"456\" height=\"498\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/Deta-base-sign-up.jpg?resize=456%2C498&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"deta base create account\" class=\"wp-image-114333\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/Deta-base-sign-up.jpg?w=456&amp;quality=100&amp;strip=all&amp;ssl=1 456w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/Deta-base-sign-up.jpg?resize=275%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 275w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/figure><\/div>\n\n\n\n<p>Deta base will send you a verification email. Verify your account, and you should be redirected to your Deta dashboard. The following window pops up:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"283\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/see-my-key-deta-base.png?resize=593%2C283&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Creating a Deta Base Account\" class=\"wp-image-114334\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/see-my-key-deta-base.png?w=593&amp;quality=100&amp;strip=all&amp;ssl=1 593w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/see-my-key-deta-base.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure><\/div>\n\n\n\n<p>By default, it creates a new project called &#8220;default&#8221;. As mentioned, projects are accessed via ids and keys. When you click on the <strong>See My Key button<\/strong>, you&#8217;ll get your project id and key. Make sure you <strong>save it somewhere because you&#8217;ll need those later<\/strong>\u2014<strong>the key will only be shown once<\/strong>. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"347\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-project-confidentials.jpg?resize=590%2C347&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Creating a Deta Base Account api key\" class=\"wp-image-114335\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-project-confidentials.jpg?w=590&amp;quality=100&amp;strip=all&amp;ssl=1 590w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-project-confidentials.jpg?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure><\/div>\n\n\n\n<p>When you&#8217;re done. Click <strong>Close<\/strong>.<\/p>\n\n\n\n<p>The library we&#8217;ll use with the ESP32 automatically creates a Base (database table) instance for your project. So, you don&#8217;t need to manually create it on the Deta Base interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the Deta Base Library for ESP32<\/h2>\n\n\n\n<p>To interact with Deta Base using the ESP32, we&#8217;ll use the <a href=\"https:\/\/github.com\/A223D\/detaBaseArduinoESP32\" target=\"_blank\" rel=\"noreferrer noopener\">detaBaseArduinoESP32 library<\/a>. You can install the library in the Arduino IDE. Go to <strong>Sketch<\/strong> &gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. Search for <strong>detabasearduinoesp32 <\/strong>and install the library by Kushagra Goel.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/install-detabasearduinoesp32-library-Arduino-IDE.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Deta Base Library for ESP32 Arduino IDE\" class=\"wp-image-114338\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/install-detabasearduinoesp32-library-Arduino-IDE.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/install-detabasearduinoesp32-library-Arduino-IDE.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/install-detabasearduinoesp32-library-Arduino-IDE.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Deta Base with ESP32: CRUD Operations<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to program your ESP32 to perform CRUD (create, read, update, delete) operations and queries on Deta Base. The library provides a simple example showing how to do that.<\/p>\n\n\n\n<p>In the Arduino IDE, make sure you have an ESP32 board selected in <strong>Tools <\/strong>&gt; <strong>Board<\/strong>. Then, go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>detabaseAduinoESP32 <\/strong>and select the <strong>detaLibTest <\/strong>example.<\/p>\n\n\n\n<p>The following code should load.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Original Source: https:\/\/github.com\/A223D\/detaBaseArduinoESP32\/blob\/main\/examples\/detaLibTest\/detaLibTest.ino\n\n#include &lt;detaBaseArduinoESP32.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#define LED 2\n\nchar* apiKey = &quot;MY_KEY&quot;;\nchar* detaID = &quot;MY_ID&quot;;\nchar* detaBaseName = &quot;MY_BASE&quot;;\n\nWiFiClientSecure client;\nDetaBaseObject detaObj(client, detaID, detaBaseName, apiKey, true);\n\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println(&quot;Let's begin initialization&quot;);\n  pinMode(LED, OUTPUT);\n  digitalWrite(LED, LOW);\n  Serial.println(&quot;Reached before WiFi init&quot;);\n  WiFi.begin(&quot;0xCAFE&quot;, &quot;0xC0FFEE&quot;);\n  Serial.println(&quot;Waiting to connect to WiFi&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println();\n  digitalWrite(LED, HIGH);\n\n}\n\n\/\/ PUT &quot;{\\&quot;items\\&quot;:[{\\&quot;age\\&quot;:4}]}&quot;\n\n\/\/INSERT &quot;{\\&quot;item\\&quot;:{\\&quot;key\\&quot;:\\&quot;cba\\&quot;,\\&quot;age\\&quot;:4}}&quot;\n\n\/\/INSERT &quot;{\\&quot;item\\&quot;:{\\&quot;key\\&quot;:\\&quot;abc\\&quot;,\\&quot;age\\&quot;:4}}&quot;\n\n\/\/UPDATE &quot;{\\&quot;increment\\&quot;:{\\&quot;age\\&quot;:1}}&quot;, key:abc\n\n\/\/UPDATE &quot;{\\&quot;increment\\&quot;:{\\&quot;age\\&quot;:1}}&quot;, key:cba\n\n\/\/QUERY &quot;{\\&quot;query\\&quot;:[{\\&quot;age?lt\\&quot;: 10}]}&quot;\n\nvoid loop() {\n  printResult(detaObj.putObject(&quot;{\\&quot;items\\&quot;:[{\\&quot;age\\&quot;:4}]}&quot;));\n  Serial.println();\n  printResult(detaObj.getObject(&quot;cba&quot;));\n  Serial.println();\n  printResult(detaObj.deleteObject(&quot;abc&quot;));\n  Serial.println();\n  printResult(detaObj.insertObject(&quot;{\\&quot;item\\&quot;:{\\&quot;key\\&quot;:\\&quot;cba\\&quot;,\\&quot;age\\&quot;:4}}&quot;));\n  Serial.println();\n  printResult(detaObj.insertObject(&quot;{\\&quot;item\\&quot;:{\\&quot;key\\&quot;:\\&quot;abc\\&quot;,\\&quot;age\\&quot;:4}}&quot;));\n  Serial.println();\n  printResult(detaObj.updateObject(&quot;{\\&quot;increment\\&quot;:{\\&quot;age\\&quot;:1}}&quot;, &quot;abc&quot;));\n  Serial.println();\n  printResult(detaObj.updateObject(&quot;{\\&quot;increment\\&quot;:{\\&quot;age\\&quot;:1}}&quot;, &quot;bcs&quot;));\n  Serial.println();\n  printResult(detaObj.query(&quot;{\\&quot;query\\&quot;:[{\\&quot;age?lt\\&quot;: 10}]}&quot;));\n  Serial.println();\n\n  while (true);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/detaLibTest.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You need to insert your project API KEY and ID. You also need to inserte a name for the database\u2014it can be whatever you want. I called it <span class=\"rnthl rntliteral\">Test<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>char* apiKey = \"REPLACE_WITH_YOUR_PROJECT_API_KEY\";\nchar* detaID = \"REPLACE_WITH_YOUR_PROJECT_ID\";\nchar* detaBaseName = \"Test\";<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, you need to include your network credentials, SSID and password so that your ESP32 can connect to the internet.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.begin(WIFI_SSID, WIFI_PASSWORD);<\/code><\/pre>\n\n\n\n<p>You can upload the code now and it will work straight away. We recommend reading through the following section to understand how things actually work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it Works<\/h2>\n\n\n\n<p>Read this section to learn how to interact with Deta Base using the ESP32.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Include Libraries<\/h3>\n\n\n\n<p>First, include the <span class=\"rnthl rntliteral\">detaBaseArduinoESP32<\/span> library. You also need to include the <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> library\u2014this automatically includes the <span class=\"rnthl rntliteral\">WiFi.h<\/span> library, also needed in this example.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;detaBaseArduinoESP32.h&gt;\n#include &lt;WiFiClientSecure.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Deta Base Key, ID, and Name<\/h3>\n\n\n\n<p>Insert the project key, ID, and a name for the Base. As mentioned previously, if not created yet, the library will automatically create a Base instance for you on Deta Base. The name for the database can be whatever best describes it. In this case, I called it <span class=\"rnthl rntliteral\">Test<\/span>. If you&#8217;ve already created a Base instance manually on Deta Base, you can use it&#8217;s name here.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>char* apiKey = \"REPLACE_WITH_YOUR_PROJECT_API_KEY\";\nchar* detaID = \"REPLACE_WITH_YOUR_PROJECT_ID\";\nchar* detaBaseName = \"Test\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a DetaBaseObject<\/h3>\n\n\n\n<p>Then, you need to create a <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> and a <span class=\"rnthl rntliteral\">DetaBaseObject<\/span> objects. The <span class=\"rnthl rntliteral\">DetaBaseObject<\/span> accepts as arguments the WiFi client, the project ID, base name, API key, lastly a boolean variable. This last boolean variable when set to <span class=\"rnthl rntliteral\">true<\/span> enables debugging statement.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiClientSecure client;\n\/\/choose this:\nDetaBaseObject detaObj(client, detaID, detaBaseName, apiKey, true);\n\/\/or this:\n\/\/DetaBaseObject detaObj(client, detaID, detaBaseName, apiKey);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">client<\/span> is passed to the <span class=\"rnthl rntliteral\">DetaBaseObject<\/span> as is, without any modification. This is done because a root CA certificate is set in the <span class=\"rnthl rntliteral\">DetaBaseObject<\/span> constructor. This is required since we are making requests over HTTPS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, connect the ESP32 to Wi-Fi using your Wi-Fi credentials: SSID and password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup() {\n  Serial.begin(115200);\n  Serial.println(\"Let's begin initialization\");\n  pinMode(LED, OUTPUT);\n  digitalWrite(LED, LOW);\n  Serial.println(\"Reached before WiFi init\");\n<strong>  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n<\/strong>  Serial.println(\"Waiting to connect to WiFi\");\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(\".\");\n  }\n  Serial.println();\n  digitalWrite(LED, HIGH);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Insert<\/h3>\n\n\n\n<p>To insert items into the database, you can use the <span class=\"rnthl rntliteral\">putObject()<\/span> function. As described in the <a href=\"https:\/\/docs.deta.sh\/docs\/base\/http\/#put-items\" target=\"_blank\" rel=\"noreferrer noopener\">Deta Base documentation<\/a>, a put operation expects a JSON object in the following format:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \/\/ array of items to put\n  \"items\": &#091;\n    {\n      \"key\": {key}, \/\/ optional, a random key is generated if not provided\n      \"field1\": \"value1\",\n      \/\/ rest of item\n    },\n    \/\/ rest of items\n  ]\n}<\/code><\/pre>\n\n\n\n<p>The key is optional, and will be assigned by Deta Base if not provided. If a key is provided, and an entry already exists with that key, it is overwritten. <\/p>\n\n\n\n<p>The following line of code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.putObject(\"{\\\"items\\\":&#091;{\\\"age\\\":4}]}\"));<\/code><\/pre>\n\n\n\n<p>That contains the following JSON:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"items\":&#091;\n    {\n      \"age\":\"4\"\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<p>Adds the following to the database:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"age\": 4\n}<\/code><\/pre>\n\n\n\n<p>A backslash character (<span class=\"rnthl rntliteral\">\\<\/span>) is added before each <span class=\"rnthl rntliteral\">&#8220;<\/span> to indicate an escape character, since we require <span class=\"rnthl rntliteral\">&#8220;<\/span> in the JSON input. <\/p>\n\n\n\n<p><strong>Note: <\/strong> <strong>Keys have to be strings<\/strong>. If you want to use a number as a key, make sure it is interpreted as a string by enclosing it in double-quotes. (Double quotes with back-slashes.)<\/p>\n\n\n\n<p>With PUT you can insert multiple items in a single request. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.putObject(\"{\\\"items\\\":&#091;{\\\"age\\\":4,\\\"weight\\\":28}]}\"));<\/code><\/pre>\n\n\n\n<p>If the request succeeds, we will see a 200-level status code in the Serial monitor, as well as the entire object(s) with its key(s).<\/p>\n\n\n\n<p>If you go to your Deta Base project, you should see a new base instance and the item we&#8217;ve just inserted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieve an Object<\/h3>\n\n\n\n<p>You can retrieve an object by its key using the <span class=\"rnthl rntliteral\">getObject()<\/span> function. The function expects a key as argument. It can be an existing key or a non-existing key.<\/p>\n\n\n\n<p>The following line of code tries to retrieve an object with <span class=\"rnthl rntliteral\">cba<\/span> key. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.getObject(\"cba\"));<\/code><\/pre>\n\n\n\n<p>You should get an error message because there isn&#8217;t any object with that key on the database yet.<\/p>\n\n\n\n<p>However, if you manually create an object with the <span class=\"rnthl rntliteral\">cba<\/span> key and run the code again, it will retrieve the object with success.<\/p>\n\n\n\n<p>To create an object manually, you can click on the <strong>+Add <\/strong>button on the deta base interface. It will automatically create a new item with a predefined key. You can change it to <span class=\"rnthl rntliteral\">cba<\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Delete<\/h3>\n\n\n\n<p>To delete an object on the database, use the <span class=\"rnthl rntliteral\">deleteObject()<\/span> function. This function accepts as an argument the key of the object we want to delete. The output of this function returns a 200-level code that indicates success even though there&#8217;s no object with that key. In our case, it tries to delete an object with the <span class=\"rnthl rntliteral\">abc<\/span> key. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.deleteObject(\"abc\"));<\/code><\/pre>\n\n\n\n<p>However, if there was an object with the <span class=\"rnthl rntliteral\">abc<\/span> key it would be deleted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Insert<\/h3>\n\n\n\n<p>To insert a new item in the database, you can use the <span class=\"rnthl rntliteral\">insertObject()<\/span> function that will make a <a href=\"https:\/\/docs.deta.sh\/docs\/base\/http\/#insert-item\" target=\"_blank\" rel=\"noreferrer noopener\">POST request<\/a>. This will create a new item only if no item with the same key exists. It expects a JSON in the following format:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"item\": {\n    \"key\": {key}, \/\/ optional\n    \/\/ rest of item\n  }\n}<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t provide a key, Deta Base will automatically do that for you.<\/p>\n\n\n\n<p>In the example, the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.insertObject(\"{\\\"item\\\":{\\\"key\\\":\\\"cba\\\",\\\"age\\\":4}}\"));<\/code><\/pre>\n\n\n\n<p>Adds the following object to the database:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"key\": \"cba\",\n  \"age\": 4\n}<\/code><\/pre>\n\n\n\n<p>If you&#8217;re already have an object with that key you&#8217;ll get a 400-level code and an error in the payload message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">updateObject()<\/span> method updates existing entries. It accepts as arguments the key for an existing objkect and a JSON object in the following format:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>{\n  \"set\"  :  {\n    \/\/set some attribute to some value like\n    \/\/age: 10\n  },\n  \"increment\"  :{\n    \/\/increment some attribute by some value like\n    \/\/age: 1\n  },\n  \"append\":  {\n    \/\/append some value to some list attribute like\n    \/\/names: &#091;\"John\"]\n  },\n  \"prepend\": {\n    \/\/append some value to some list attribute like\n    \/\/names: &#091;\"Sam\"]\n  },\n  \"delete\":  &#091;\/\/attributes to be deleted]\n}<\/code><\/pre>\n\n\n\n<p>All of those JSON sub-objects (<span class=\"rnthl rntliteral\">set<\/span>, <span class=\"rnthl rntliteral\">increment<\/span>, <span class=\"rnthl rntliteral\">append<\/span>, <span class=\"rnthl rntliteral\">prepend<\/span>, and <span class=\"rnthl rntliteral\">delete<\/span>) are optional.<\/p>\n\n\n\n<p>You can learn more about this type of request on the <a href=\"https:\/\/docs.deta.sh\/docs\/base\/http\/#update-item\" target=\"_blank\" rel=\"noreferrer noopener\">documentation<\/a>.<\/p>\n\n\n\n<p>In our example, the following line will increment the age by 1 in the entry with the <span class=\"rnthl rntliteral\">abc<\/span> key.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code> printResult(detaObj.updateObject(\"{\\\"increment\\\":{\\\"age\\\":1}}\", \"abc\"));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Query Data<\/h3>\n\n\n\n<p>Deta Base also supports queries for fetching data that match certain conditions. You can learn more about Deta Base Queries on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.deta.sh\/docs\/base\/queries\/\" target=\"_blank\" rel=\"noreferrer noopener\">Deta Base Queries Documentation<\/a><\/li><\/ul>\n\n\n\n<p>To make a query you can use the <span class=\"rnthl rntliteral\">query()<\/span> function that accepts as argument the query itself in JSON format (see the link to the documentation above).<\/p>\n\n\n\n<p>The following query will return all the objects whose value of age is less than 10, which in our case corresponds to all objects in the database.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printResult(detaObj.query(\"{\\\"query\\\":&#091;{\\\"age?lt\\\": 10}]}\"));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After running the example, you should get the following on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"774\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-1.png?resize=601%2C774&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Deta Base Serial Monitor Demonstration\" class=\"wp-image-114378\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-1.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-1.png?resize=233%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 233w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"774\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-2.png?resize=601%2C774&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Deta Base Serial Monitor Demonstration\" class=\"wp-image-114379\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-2.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-with-deta-base-demonstration-serial-monitor-2.png?resize=233%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 233w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<p>And your database should look as follows.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"398\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-demonstration.png?resize=750%2C398&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"deta base with ESP32 UI demonstration\" class=\"wp-image-114377\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-demonstration.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/deta-base-demonstration.png?resize=300%2C159&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to interact with Deta Base using the ESP32. Deta Base is a NoSQL database, it&#8217;s free and unlimited. This means you won&#8217;t have to pay anything to use it, and you can add as many entries as needed. The database requires minimal setup and you can use it right away. Thanks to the <a href=\"https:\/\/github.com\/A223D\/detaBaseArduinoESP32\" target=\"_blank\" rel=\"noreferrer noopener\">detaBaseArduinoESP32 library<\/a>, it&#8217;s even easier to make HTTP requests to the database and handle the responses.<\/p>\n\n\n\n<p>In this example, you learned how to insert sample values. You can easily modify the examples to save sensor readings, for example, or GPIO states. <\/p>\n\n\n\n<p>The present tutorial was based on the <a href=\"https:\/\/github.com\/A223D\/ESP32DetaGuides\" target=\"_blank\" rel=\"noreferrer noopener\">tutorial created by Kushagra Goel<\/a>.<\/p>\n\n\n\n<p>We hope you found this article useful.<\/p>\n\n\n\n<p>We have guides for other popular databases with the ESP32:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">ESP32: Getting Started with <strong>Firebase (Realtime Database)<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-influxdb\/\">ESP32: Getting Started with <strong>InfluxDB<\/strong><\/a> <strong>(Time Series Database)<\/strong><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-mysql-database-php\/\">ESP32\/ESP8266 Insert Data into <strong>MySQL Database<\/strong> using PHP and Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get started with Deta Base using the ESP32 board. Deta Base is a NoSQL database. It is unlimited, free, and easy to use. Additionally, it requires minimal setup. So, it&#8217;s &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32: Getting Started with Deta Base (Unlimited and Free Database for Developers)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-deta-base-database-arduino\/#more-114323\" aria-label=\"Read more about ESP32: Getting Started with Deta Base (Unlimited and Free Database for Developers)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":114353,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-114323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/08\/ESP32-Get-Started-Deta-Base.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/114323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=114323"}],"version-history":[{"count":19,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/114323\/revisions"}],"predecessor-version":[{"id":115004,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/114323\/revisions\/115004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/114353"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=114323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=114323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=114323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}