Reader small image

You're reading from  Hands-on ESP32 with Arduino IDE

Product typeBook
Published inJan 2024
PublisherPackt
ISBN-139781837638031
Edition1st Edition
Right arrow
Author (1)
Asim Zulfiqar
Asim Zulfiqar
author image
Asim Zulfiqar

Asim Zulfiqar is a blogger and tech content creator who has been writing tutorials on embedded systems and IoT on his blog and YouTube channel, High Voltages. Currently, he is working as a scientific programmer for IoT research projects. He completed his bachelor's degree in electronic engineering at Sir Syed University of Engineering and Technology, Pakistan. After that, he completed his Erasmus Mundus joint master's degree program in Photonics Integrated Circuits, Sensors, and Networks at Scuola Superiore Sant'Anna (Italy), Aston University (U.K), and Osaka University (Japan).
Read more about Asim Zulfiqar

Right arrow

Project 3 – Logging, Monitoring, and Controlling using ESP32

In this chapter, we will start another exciting journey to explore the capabilities of the ESP32 microcontroller in the realm of home automation and monitoring. The mission of this chapter is to demonstrate how ESP32 can be harnessed to enhance the convenience, security, and efficiency of your daily life.

Our focus will be on logging, monitoring, and controlling various aspects of your home using the versatile ESP32 platform. We’ll deploy sensors in key areas such as the kitchen, bathroom, bedroom, and living room to collect valuable data. This data will be efficiently stored in an InfluxDB database, providing a robust foundation for analysis and monitoring.

The heart of our monitoring system lies in Grafana, a powerful tool that allows us to visualize and gain insights from the data collected. We’ll delve into the technical aspects of setting up Grafana, creating informative dashboards, and setting...

Technical requirements

For this project, we will need the following:

  • An ESP32 board
  • Servo motor
  • PIR motion sensor
  • Light-detecting resistor module
  • DHT22 temperature and humidity sensor
  • InfluxDB Cloud account (free account)
  • Grafana cloud account (free account)
  • HiveMQ public MQTT broker

All the code files used in this chapter will be available at https://github.com/PacktPublishing/Programming-ESP32-with-Arduino-IDE/tree/main/chapter%208

Interfacing sensors and actuators with ESP32

In this section, we’ll dive into the hardware components of our home monitoring and control project. Here, we will explore the seamless integration of a wide range of sensors and actuators with the ESP32 microcontroller, allowing us to transform our ordinary home into a smart and efficiently managed living space.

Our project hinges on the precise coordination of various elements to log data and monitor different rooms in our home, offering us valuable insights and control. To achieve this, we will connect the ESP32 microcontroller to a set of essential components, including DHT22 sensors, motion sensors, Light Dependent Resistor (LDR) modules, and an additional servo motor for the living room.

Each room in our home as depicted in Figure 8.1, will be equipped with a set of sensors. The DHT11 sensors will provide temperature and humidity data, the motion sensors will detect human presence, and the LDR modules will measure light...

Setting up InfluxDB Cloud and logging the data

InfluxDB is a high-performance, open source time series database designed for efficiently storing and querying timestamped data. It is particularly well suited for applications that collect and analyze data that changes over time, such as sensor readings, application metrics, and system monitoring data.

Time series data is a type of data where each data point is associated with a specific timestamp. It is used to record changes or measurements over time, making it ideal for tracking trends, patterns, and historical data. In time series data, time is a critical dimension, and the data points are typically sorted in chronological order.

For example, let’s consider a DHT sensor that measures temperature and humidity. The sensor records readings at regular intervals and stores them with timestamps. Here’s a simplified representation of time series data from a DHT sensor:

...

Monitoring and visualization using the Grafana cloud

Grafana is an open source platform for data visualization and monitoring. It allows users to create interactive and customizable dashboards that display real-time or historical data from various sources, including databases, time series databases such as InfluxDB, and more. Grafana is widely used for monitoring system performance, IoT devices, application metrics, and other data sources, making it a valuable tool for gaining insights from complex datasets through visually appealing and customizable graphs, charts, and panels.

First, we will set up the Grafana cloud:

  1. Go to https://grafana.com/auth/sign-in. Sign in if you already have an account or register if you are using Grafana for the first time.
  2. Once you’ve logged in, you will have to click on Add Stack, as shown in the following figure:
Figure 8.21 – Grafana Cloud Portal

Figure 8.21 – Grafana Cloud Portal

  1. Give your instance a name and click on...

Controlling the main entrance gate using the MQTT protocol

In this section, we will learn how we can control the appliances in IoT projects using the MQTT protocol. We will use the HiveMQ public MQTT broker, whose credentials are available at https://www.hivemq.com/mqtt/public-mqtt-broker/.

For the living room ESP32 microcontroller, we will upload the following code so that we can send the data to InfluxDB as well as receive the MQTT message to control the door lock. The connection diagram for the living room is provided in Figure 8.4. The code is available on GitHub at https://github.com/PacktPublishing/Programming-ESP32-with-Arduino-IDE/blob/main/chapter%208/livingroom/livingroom.ino:

#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <WiFiMulti.h>
#include <InfluxDbClient.h> //https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git
#include <InfluxDbCloud.h>
#include <PubSubClient.h>
#include <ESP32Servo.h>
// WiFi configuration...

Summary

In this chapter, we delved into the world of data logging and visualization for IoT applications. Our focus shifted toward utilizing InfluxDB Cloud for data logging, harnessing the power of Grafana to create insightful data visualizations, and implementing MQTT with the HiveMQ public broker to remotely control a door lock. This chapter extended our practical IoT skills, enabling us to log, analyze, and visualize data while also providing the means for remote device control.

In this chapter, our focus revolved around leveraging cloud-based solutions for data storage and visualization, specifically utilizing InfluxDB Cloud and the Grafana cloud. These platforms, built on the robust infrastructure of Amazon Web Services (AWS), offer comprehensive application development services. However, alternative deployment options are also available to provide flexibility to users.

One viable option is to harness AWS directly, utilizing services such as AWS EC2, which functions as a...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-on ESP32 with Arduino IDE
Published in: Jan 2024Publisher: PacktISBN-13: 9781837638031
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 $15.99/month. Cancel anytime

Author (1)

author image
Asim Zulfiqar

Asim Zulfiqar is a blogger and tech content creator who has been writing tutorials on embedded systems and IoT on his blog and YouTube channel, High Voltages. Currently, he is working as a scientific programmer for IoT research projects. He completed his bachelor's degree in electronic engineering at Sir Syed University of Engineering and Technology, Pakistan. After that, he completed his Erasmus Mundus joint master's degree program in Photonics Integrated Circuits, Sensors, and Networks at Scuola Superiore Sant'Anna (Italy), Aston University (U.K), and Osaka University (Japan).
Read more about Asim Zulfiqar