Reader small image

You're reading from  Developing IoT Projects with ESP32 - Second Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803237688
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Vedat Ozan Oner
Vedat Ozan Oner
author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner

Right arrow

Employing Third-Party Libraries in ESP32 Projects

In the previous chapters, we mostly stayed in the ESP-IDF environment and used the components and libraries that come with it. However, developing an IoT product usually means that you need help from third parties for practical reasons, such as cost, time, and market needs. It is obvious that every development that we decide to do in-house means more time and more money to burn in order to have a final, working product. However, we can cut some of the costs by using third-party libraries where possible – no need to reinvent the wheel. Market needs can also drive your development decisions. Let’s say your product has to support a specific type of communication layer – for example, Matter, a popular smart home connectivity protocol. Then, it would make sense to use an SDK for it to ensure a smooth certification process for your product. In this chapter, we are not going to talk about Matter, but some other popular...

Technical requirements

We will use Visual Studio Code and ESP-IDF command-line tools to create, develop, flash, and monitor the applications during this chapter.

For hardware, only ESP32-S3 Box Lite will be employed. The sensors and other hardware components of this chapter are:

  • A light-dependent resistor (LDR)
  • A jumper wire (both ends are male)
  • A pull-up resistor (10K)

The source code in the examples is located in the repository found at this link: https://github.com/PacktPublishing/Developing-IoT-Projects-with-ESP32-2nd-edition/tree/main/ch4

LittleFS

The design goal of LittleFS is to provide fail-safe filesystem access for MCUs even in case of power loss. All POSIX operations are atomic, which means that when a function call returns successfully, LittleFS makes sure the result is persistent on the filesystem. If you have hard requirements on fail-safety features of the underlying filesystem in a project, then LittleFS can be a good option. In this example, we are going to use a port of LittleFS for ESP-IDF.

You can find the library on GitHub here: https://github.com/joltwallet/esp_littlefs. It comes with an MIT license.

The goal in this example is to develop a door event logger. It will simply log when the door is opened and closed on the LittleFS filesystem. We will use ESP32-S3 Box Lite as the development kit and simulate a door sensor by clicking on a button of the devkit.

There are different methods to include a third-party library in an ESP-IDF project. One of them is the IDF Component Manager. Espressif...

Nlohmann-JSON

JavaScript Object Notation (JSON) is a common data exchange format that uses human-readable text, and nlohmann/json is a popular library that implements the JSON functionality in C++. It is released under the MIT license, which allows us to use the library in our projects without any limitations.

The IDF Component Registry doesn’t have nlohmann/json; therefore, we can use it in a project by directly downloading the header file from its repository here: https://github.com/nlohmann/json

Nlohmann-JSON provides a single header file as the library; therefore, the only thing we need to do is simply to download this header file from its repository and include it in the project.

The goal of the example is to develop a touch logger. We will use ESP32-S3 Box Lite and a simple jumper wire to expose the GPIO9 pin of the devkit on Pmod header 2 so that we can use it as the touch sensor of the application. When we touch the pin of the jumper wire, it will generate...

Miniz

Miniz is a lossless data compression library that implements RFC 1950 and RFC 1951 for compression/decompression. The library port in ESP-IDF is licensed under the MIT license. ESP-IDF has already imported it for its own purposes, but we can also use it freely. There is no need for library management. Miniz can be especially helpful when you need to transfer a large amount of data. After compressing and sending the data, the receiving side can easily decompress it with any library that implements the same RFCs.

Unfortunately, the documentation is very poor (in fact, there is almost none) for Miniz. You can see the examples in this repository: https://github.com/richgel999/miniz

In this example, we will simply compress and decompress a sample text by pressing the buttons on ESP32-S3 Box Lite. There is no other hardware required in this example. We can create a new project to see how to use this library in an application next.

Creating a project

Let’s create...

FlatBuffers

FlatBuffers is a cross-platform serialization library from Google. The library supports many different programming languages, so it is possible to use it on any platform. Another interesting feature of FlatBuffers is that it can directly map binary data to its representation on the platform it runs without parsing or using extra buffers. As a result, the library is quite efficient in terms of both memory usage and processing power.

The library has great documentation here: https://google.github.io/flatbuffers/

In the FlatBuffers example, we will collect analog data from an LDR and convert the data to binary format (serialization) by using the FlatBuffers library. Moreover, we will again employ the library to revert the binary data to the programming structures (deserialization).

The hardware components of the project are:

  • ESP32-S3 Box Lite
  • An LDR
  • A pull-up resistor (10K)

The following is a Fritzing sketch that shows the connections...

LVGL

Light and Versatile Graphics Library, or LVGL for short, is one of the most popular graphics libraries for embedded systems. There are many factors that I can count here as the reasons for its popularity:

  • First and foremost, it is really lightweight compared to the functionality and widgets that come with it.
  • It has fully configurable, modern widgets.
  • It has a simple API with plain C structures and callbacks.
  • There is extensive documentation available with examples.
  • It has great support from the LVGL team.
  • It is free and open source with an MIT license (the GitHub repository is at https://github.com/lvgl/lvgl).

We have already developed an example with LVGL in the previous chapter while talking about displays and GUI development. In this example, we will have more chances to discuss LVGL and its capabilities in detail. We will develop different screens that we can navigate by using the devkit buttons and also interact with the...

ESP-IDF Components library

It is impossible not to mention the ESP-IDF Components library by UncleRus in this chapter. It is one of the most famous libraries in the community. We have already used a port of this library in Chapter 3, Using ESP32 Peripherals, where we discussed I2C communication and integrated different sensor breakout boards. The library especially focuses on I2C devices and maintains many drivers for sensors from different vendors in a single repository. The driver licenses are all categorized under the FOSS classification but please check for the specific license type before including a sensor driver in your project.

The GitHub repository of the ESP-IDF Components library is here: https://github.com/UncleRus/esp-idf-lib

The library officially supports ESP32, ESP32-S2, ESP32-C3, and ESP8266. However, as of the time of writing this book, it failed to run I2C devices on ESP32-S3 because of some kind of timing issue. The book repository contains an updated version...

Espressif frameworks and libraries

Besides all these third-party libraries, Espressif Systems empowers developers with many other frameworks and libraries. As a quick overview, here is a short list of those frameworks from Espressif:

  • ESP-IoT-Solution: This framework brings different hardware drivers together as a working solution to minimize compatibility issues with ESP-IDF. It contains sensor drivers, display controller drivers and LVGL, input devices and buttons, audio output utilities, and more functionality for hassle-free development (https://github.com/espressif/esp-iot-solution).
  • Audio Development Framework (ESP-ADF): This is the core framework for audio input/output and processing. It collects all the necessary components under the same roof to develop audio applications, such as music players/recorders, speech recognition applications, smart speakers, etc. (https://github.com/espressif/esp-adf).
  • Image Processing Framework (ESP-WHO): This provides image...

Summary

In this chapter, we covered a selection of third-party libraries that we can use in our ESP32 projects. We learned that we can use LittleFS as an alternative to SPIFFS, nlohmann/json as a modern JSON library, Miniz for data compression, and FlatBuffers to share data between different platforms and architectures. LVGL has a special place among them, such that it is the framework to use if you want to create an amazing graphical interface for the users of your product. The ESP-IDF Components library is another popular library for ESP32 developers and it provides many device drivers that we can employ in our projects. Finally, we talked about some important frameworks by Espressif.

They provide a head-start and make life much easier when commencing a new ESP32 project. The examples in this chapter also showed us different methods of importing third-party libraries in our projects so that we can apply them when we need other libraries in the next project.

The next chapter...

Questions

You can answer the following questions as a review of the topics in this chapter:

  1. Which of the following would be a good use case for FlatBuffers?
    1. Serializing data on an external flash
    2. Sending data to a mobile application
    3. Querying an I2C sensor
    4. Formatting log data into JSON
  2. There is a good amount of data to be transferred over WiFi with repeating information. Which method would help most to reduce the data size?
    1. Formatting into JSON by using the nlohmann/json library
    2. Binary serialization with FlatBuffers
    3. Using Miniz to compress data
    4. Ignoring repeated information
  3. Which of the following statements is NOT correct about the use of LVGL in a project?
    1. It is a GUI library so the device should have a display.
    2. LVGL provides an API for input devices, such as keypads or touchscreens.
    3. There is no need for a GUI designer...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Developing IoT Projects with ESP32 - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781803237688
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Vedat Ozan Oner

Vedat Ozan Oner is an IoT product developer and software architect, with an excellent blend of technical knowledge and experience. During his career, he has contributed to several IoT projects in different roles, which allowed him to discover all key aspects of developing successful IoT products in highly competitive markets. Vedat has a bachelor's degree in METU/computer engineering and holds several industry-recognized credentials and qualifications, including PMP®, ITIL®, and AWS Certified Developer. Vedat started his limited company, Mevoo Ltd, in London in 2018 to provide consultancy services to his clients as well as develop his own IoT products. He still lives in London with his family.
Read more about Vedat Ozan Oner