ESP32: Getting Started with Deta Base (Unlimited and Free Database for Developers)

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’s perfect for your hobbyist projects and prototyping. You’ll learn how to perform create, read, update, delete and query operations in a Deta Base instance using an ESP32.

ESP32 Getting Started with Deta Base Unlimited and Free Database for Developers

To interact with Deta base using the ESP32, we’ll use the detaBaseArduinoESP32 library. The present tutorial was based on the guides created by the library developer. You can find the guides on the following link:

Introducing Deta Base

The best way to describe Deta Base:

Deta Base is a fully-managed, fast, scalable and secure NoSQL database with a focus on end-user simplicity. It offers a UI through which you can easily see, query, update and delete records in the database.

https://docs.deta.sh/docs/base/about

And the best part is that Deta Base is free to use!

If you’re wondering where your data is saved and if it is secured, here’s the answer:

Your data is encrypted and stored safely on AWS. Encryption keys are managed by AWS; AWS manages Exabytes of the world’s most sensitive data.

https://docs.deta.sh/docs/base/about#is-my-data-secure

We recommend taking a look at the docs to get more familiar with Deta Base:

Deta Base is still in the beta version, so you may expect improvements in the future.

Creating a Deta Base Account

To get started, you need to create a Deta Base account. Go to deta.sh, and click on Join Deta to create a new account.

deta base website

Enter a username, password, and email to create a new account.

deta base create account

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:

Creating a Deta Base Account

By default, it creates a new project called “default”. As mentioned, projects are accessed via ids and keys. When you click on the See My Key button, you’ll get your project id and key. Make sure you save it somewhere because you’ll need those laterthe key will only be shown once.

Creating a Deta Base Account api key

When you’re done. Click Close.

The library we’ll use with the ESP32 automatically creates a Base (database table) instance for your project. So, you don’t need to manually create it on the Deta Base interface.

Installing the Deta Base Library for ESP32

To interact with Deta Base using the ESP32, we’ll use the detaBaseArduinoESP32 library. You can install the library in the Arduino IDE. Go to Sketch > Include Library > Manage Libraries. Search for detabasearduinoesp32 and install the library by Kushagra Goel.

Install Deta Base Library for ESP32 Arduino IDE

Deta Base with ESP32: CRUD Operations

In this section, you’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.

In the Arduino IDE, make sure you have an ESP32 board selected in Tools > Board. Then, go to File > Examples > detabaseAduinoESP32 and select the detaLibTest example.

The following code should load.

# Original Source: https://github.com/A223D/detaBaseArduinoESP32/blob/main/examples/detaLibTest/detaLibTest.ino

#include <detaBaseArduinoESP32.h>
#include <WiFiClientSecure.h>
#define LED 2

char* apiKey = "MY_KEY";
char* detaID = "MY_ID";
char* detaBaseName = "MY_BASE";

WiFiClientSecure client;
DetaBaseObject detaObj(client, detaID, detaBaseName, apiKey, true);


void setup() {
  Serial.begin(115200);
  Serial.println("Let's begin initialization");
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.println("Reached before WiFi init");
  WiFi.begin("0xCAFE", "0xC0FFEE");
  Serial.println("Waiting to connect to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  digitalWrite(LED, HIGH);

}

// PUT "{\"items\":[{\"age\":4}]}"

//INSERT "{\"item\":{\"key\":\"cba\",\"age\":4}}"

//INSERT "{\"item\":{\"key\":\"abc\",\"age\":4}}"

//UPDATE "{\"increment\":{\"age\":1}}", key:abc

//UPDATE "{\"increment\":{\"age\":1}}", key:cba

//QUERY "{\"query\":[{\"age?lt\": 10}]}"

void loop() {
  printResult(detaObj.putObject("{\"items\":[{\"age\":4}]}"));
  Serial.println();
  printResult(detaObj.getObject("cba"));
  Serial.println();
  printResult(detaObj.deleteObject("abc"));
  Serial.println();
  printResult(detaObj.insertObject("{\"item\":{\"key\":\"cba\",\"age\":4}}"));
  Serial.println();
  printResult(detaObj.insertObject("{\"item\":{\"key\":\"abc\",\"age\":4}}"));
  Serial.println();
  printResult(detaObj.updateObject("{\"increment\":{\"age\":1}}", "abc"));
  Serial.println();
  printResult(detaObj.updateObject("{\"increment\":{\"age\":1}}", "bcs"));
  Serial.println();
  printResult(detaObj.query("{\"query\":[{\"age?lt\": 10}]}"));
  Serial.println();

  while (true);
}

View raw code

You need to insert your project API KEY and ID. You also need to inserte a name for the database—it can be whatever you want. I called it Test.

char* apiKey = "REPLACE_WITH_YOUR_PROJECT_API_KEY";
char* detaID = "REPLACE_WITH_YOUR_PROJECT_ID";
char* detaBaseName = "Test";

In the setup(), you need to include your network credentials, SSID and password so that your ESP32 can connect to the internet.

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

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.

How it Works

Read this section to learn how to interact with Deta Base using the ESP32.

Include Libraries

First, include the detaBaseArduinoESP32 library. You also need to include the WiFiClientSecure library—this automatically includes the WiFi.h library, also needed in this example.

#include <detaBaseArduinoESP32.h>
#include <WiFiClientSecure.h>

Deta Base Key, ID, and Name

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 Test. If you’ve already created a Base instance manually on Deta Base, you can use it’s name here.

char* apiKey = "REPLACE_WITH_YOUR_PROJECT_API_KEY";
char* detaID = "REPLACE_WITH_YOUR_PROJECT_ID";
char* detaBaseName = "Test";

Creating a DetaBaseObject

Then, you need to create a WiFiClientSecure and a DetaBaseObject objects. The DetaBaseObject accepts as arguments the WiFi client, the project ID, base name, API key, lastly a boolean variable. This last boolean variable when set to true enables debugging statement.

WiFiClientSecure client;
//choose this:
DetaBaseObject detaObj(client, detaID, detaBaseName, apiKey, true);
//or this:
//DetaBaseObject detaObj(client, detaID, detaBaseName, apiKey);

The client is passed to the DetaBaseObject as is, without any modification. This is done because a root CA certificate is set in the DetaBaseObject constructor. This is required since we are making requests over HTTPS.

setup()

In the setup(), connect the ESP32 to Wi-Fi using your Wi-Fi credentials: SSID and password.

void setup() {
  Serial.begin(115200);
  Serial.println("Let's begin initialization");
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.println("Reached before WiFi init");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.println("Waiting to connect to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  digitalWrite(LED, HIGH);
}

Insert

To insert items into the database, you can use the putObject() function. As described in the Deta Base documentation, a put operation expects a JSON object in the following format:

{
  // array of items to put
  "items": [
    {
      "key": {key}, // optional, a random key is generated if not provided
      "field1": "value1",
      // rest of item
    },
    // rest of items
  ]
}

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.

The following line of code:

printResult(detaObj.putObject("{\"items\":[{\"age\":4}]}"));

That contains the following JSON:

{
  "items":[
    {
      "age":"4"
    }
  ]
}

Adds the following to the database:

{
  "age": 4
}

A backslash character (\) is added before each to indicate an escape character, since we require in the JSON input.

Note: Keys have to be strings. 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.)

With PUT you can insert multiple items in a single request. For example:

printResult(detaObj.putObject("{\"items\":[{\"age\":4,\"weight\":28}]}"));

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).

If you go to your Deta Base project, you should see a new base instance and the item we’ve just inserted.

Retrieve an Object

You can retrieve an object by its key using the getObject() function. The function expects a key as argument. It can be an existing key or a non-existing key.

The following line of code tries to retrieve an object with cba key.

printResult(detaObj.getObject("cba"));

You should get an error message because there isn’t any object with that key on the database yet.

However, if you manually create an object with the cba key and run the code again, it will retrieve the object with success.

To create an object manually, you can click on the +Add button on the deta base interface. It will automatically create a new item with a predefined key. You can change it to cba.

Delete

To delete an object on the database, use the deleteObject() 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’s no object with that key. In our case, it tries to delete an object with the abc key.

printResult(detaObj.deleteObject("abc"));

However, if there was an object with the abc key it would be deleted.

Insert

To insert a new item in the database, you can use the insertObject() function that will make a POST request. This will create a new item only if no item with the same key exists. It expects a JSON in the following format:

{
  "item": {
    "key": {key}, // optional
    // rest of item
  }
}

If you don’t provide a key, Deta Base will automatically do that for you.

In the example, the following line:

printResult(detaObj.insertObject("{\"item\":{\"key\":\"cba\",\"age\":4}}"));

Adds the following object to the database:

{
  "key": "cba",
  "age": 4
}

If you’re already have an object with that key you’ll get a 400-level code and an error in the payload message.

Update

The updateObject() method updates existing entries. It accepts as arguments the key for an existing objkect and a JSON object in the following format:

{
  "set"  :  {
    //set some attribute to some value like
    //age: 10
  },
  "increment"  :{
    //increment some attribute by some value like
    //age: 1
  },
  "append":  {
    //append some value to some list attribute like
    //names: ["John"]
  },
  "prepend": {
    //append some value to some list attribute like
    //names: ["Sam"]
  },
  "delete":  [//attributes to be deleted]
}

All of those JSON sub-objects (set, increment, append, prepend, and delete) are optional.

You can learn more about this type of request on the documentation.

In our example, the following line will increment the age by 1 in the entry with the abc key.

 printResult(detaObj.updateObject("{\"increment\":{\"age\":1}}", "abc"));

Query Data

Deta Base also supports queries for fetching data that match certain conditions. You can learn more about Deta Base Queries on the following link:

To make a query you can use the query() function that accepts as argument the query itself in JSON format (see the link to the documentation above).

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.

printResult(detaObj.query("{\"query\":[{\"age?lt\": 10}]}"));

Demonstration

After running the example, you should get the following on the Serial Monitor.

ESP32 with Deta Base Serial Monitor Demonstration
ESP32 with Deta Base Serial Monitor Demonstration

And your database should look as follows.

deta base with ESP32 UI demonstration

Wrapping Up

In this tutorial, you learned how to interact with Deta Base using the ESP32. Deta Base is a NoSQL database, it’s free and unlimited. This means you won’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 detaBaseArduinoESP32 library, it’s even easier to make HTTP requests to the database and handle the responses.

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.

The present tutorial was based on the tutorial created by Kushagra Goel.

We hope you found this article useful.

We have guides for other popular databases with the ESP32:

Learn more about the ESP32 with our resources:

Thanks for reading.



Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

33 thoughts on “ESP32: Getting Started with Deta Base (Unlimited and Free Database for Developers)”

  1. Hello, what can I do if the steps to load the Deta Base Library for ESP32 fail? This particular library does not show up when I search for it. I am really looking forward to working through this post. 🙂

    Reply
    • It should show up for esp32 and esp8266 architectures. Which architecture are you using? Try following the steps that Sara has mentioned to load it manually. Let me know if something doesn’t seem to work.

      Kushagra (library developer)

      Reply
  2. I am learning as I go here so I would like the pose the Question waht would be the most practical application of this ESP32 Detabase? How could I use it to make my life easier?

    Any feedback appreciated, Thanks

    Reply
    • Hi James,
      You could connect sensors to ESP32 and write python code to store the data from those sensors into the Deta Base for analysis. For example temp data changes with date/time.
      Or humidity or light levels etc…
      Good luck.

      Reply
    • The main advantage of Deta Base is that it is virtually unlimited, and you can have a (technically) infinite number of Base instances. This alone makes it viable alternative to other limited online NoSQL databases.

      Reply
  3. I have been working with ESP32/Firebase for a few days now and while it’s working, it works great, but I am having connectivity to the database (not my wifi connection) issues. The connection to the database will drop and not be able to reconnect.

    I find that others are having the same problem… ESP.restart() is the only reliable solution…

    I’m ready to try something new… But are there similar issues with Detabase? Does it stay connected any better?

    Reply
    • Hi Mike.
      I have to test my Firebase examples again and see if I can reproduce that issue and how to solve it…
      When I tested the examples I didn’t have connectivity issues, but that might have changed. I have to check that.

      I haven’t experimented with Deta Base enough time to find connectivity issues.
      Regards,
      Sara

      Reply
    • I have done a lot of testing with this library, and have not found any connection issues which stem from the library itself. However, if you do encounter any issues, I’d appreciate if you can open an issue on GitHub and describe it in as much detail as possible.

      Kushagra (library developer)

      Reply
  4. According to the documentation: “A WiFi connection must be established using the inbuilt WiFi library like so:”

    Does this mean the library will not work with an Ethernet connection? I am designing for a RF noisy environment, which demands reliability through a wired internet connection.

    Reply
  5. Hello.
    I’ve enjoyed your past articles on creating a server using Firebase. I have a data-acquisition system based on your tutorial for using Firebase with the 8266. It works flawlessly and I use it to monitor temperatures and magnetic fields in my remote “lab” (an air-conditioned/heated backroom in my garage). It’s pretty cool!

    My question: Can this arduino library be used for the not only the ESP32, but also the NodeMCU 8266?

    Thanks,
    James

    Reply
    • Hi.
      Yes. I think it is also compatible with the ESP8266 as is mentioned in the original tutorial, but I haven’t tested it.
      Regards,
      Sara

      Reply
  6. please show me come to insert name is Superman:
    I’d tried this:
    detaObj.item(“{\”items\”:[{\”name\”: \”Superman\”}]}”);

    but it’s not work.

    Reply
    • Whenever I had this issue, it was because the value of the “Content-Length” header was wrong. Check that and try again.

      Kushagra Goel (library developer)

      Reply
  7. Hi Sara,
    how to substite i.e in field “age\”:4, the number 4 with a variable.?
    I try “int x” instead of “4” but doesn’t run.
    Thank

    Reply
  8. Hello, I followed the steps but I find this error and it does not let me move forward, please, I would like help as soon as possible, thank you very much.
    I am using an esp32 and Arduino IDE, and it gives me the following error.
    #include <WiFiClientSecure.h>
    ^~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    exit status 1
    Error compiling for ESP32 Dev Module card.

    Reply
  9. Again a superb tutorial!!
    (and amazing to see the original library developer here as well)

    I’ve put your tutorial to use in my latest project, an odometer for my (borrowed) cats and their hamsterwheel.

    I’m pretty new to this kind of databases, but currently my cats are walking in their hamster wheel and I’m counting the meters they walk during the day.
    Is there a way to summarize all these short walks they take from this database and do some calculations in the Arduino software?

    Kind regards,
    Me-Chiel

    Reply
    • Hi.
      Yes.
      After learning how to get data from the database, you can do all the needed calculations on the Arduino sketch.
      Regards,
      Sara

      Reply
      • I hope there will be a more clear example of how to get the data out again:) Having some difficulties there. Or mostly misunderstandings;)

        I tried running a webserver together with the detabase’s securewifi and ESPAsyncWebServer + AsyncElegantOTA etc but those two aren’t friends. is there a way around this?

        Reply
          • I hope someone here does, and knows how to fix it.
            And another question (if you don’t mind)
            I tried adding another Database, but that gave a lot of errors to my ESP32-C3

            DetaBaseObject detaObj(client, detaID, detaBaseName, apiKey, true);
            DetaBaseObject detaObj2(client, detaID, detaBaseName2, apiKey, true);

            does this mean I can’t make two objects? of did I forgot to change something in the naming?

            The idea is to have one database for all the short burst
            and one for the daily totals.

          • the problem was with the microcontroller. I was experimenting with an ESP32-C3. With an “old” ESP32 there is no problem when I use these libraries together (sofar)

  10. Hi,
    I get the following warning with all 3 char* variables –
    warning: ISO C++ forbids converting a string constant to ‘char‘ [-Wwrite-strings]
    char
    apiKey = “[email protected]_Tbi8xxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
    So when I run the program, unsurprisingly I get ‘unauthorised’ displayed.
    Where am I going wrong please ?
    I have tried with Arduino 2.0 and Arduino 1.81.16 – same problem.

    Many thanks

    Terry

    Reply
  11. Hi, me again !
    I managed to get the prog to compile by changing
    char* detaBaseName = “TR_BASE”;
    to
    char* detaBaseName = (char*)”TR_BASE”;

    but I still get
    Reply: {“errors”:[“Unauthorized”]}

    on every operation !

    Reply

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.