Build a room monitor for a baby or a pet with Blynk

A sound sensor and a DHT11 keep an eye on a sleeping room. Blynk shows the noise level, temperature and humidity, sends a notification when it gets loud or too warm, and dims a night light.
  • Level:Advanced
  • Duration: 1 h
  • Ages 12+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a room monitor for a baby or a pet with Blynk – EEMENTOR STEM™ lesson

This monitor sits in a bedroom and keeps track of three things: how loud it is, the temperature and the humidity. The Blynk app shows all three, sends a notification when the room stays loud for a few seconds (someone is awake!) or gets too warm or cold, and lets you switch on a gentle night light from your phone. It works for a baby's room, a pet's room or your own. It is an extra, not a safety device: a young baby always needs a grown-up and a proper baby monitor.

What you'll learn

  • How to tell a real noise from a short bang
  • How to watch a value against a lower and an upper limit
  • How to dim an LED from a slider in the app
  • How to avoid sending the same notification again and again

What you need

  • ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
  • High-sensitivity sound sensor module (45-in-1 Sensor Kit)
  • DHT11 temperature and humidity module (45-in-1 Sensor Kit)
  • White LED and 1 resistor, 220 Ω (ESP32 IoT Starter Kit)
  • A free Blynk account and the Blynk IoT app on a phone
  • A 2.4 GHz Wi-Fi network
  • The printed parts from the 3D printing box below (monitor case), or the cardboard version described there
  • Breadboard and jumper wires (ESP32 IoT Starter Kit)

Wiring

Breadboard wiring diagram: Build a room monitor for a baby or a pet with Blynk
Breadboard view. Check each connection against the wiring table below.
From To
Sound sensor AO ESP32 GPIO 34
Sound sensor + and G 3V3 and GND
Sound sensor DO Not used
DHT11 module S ESP32 GPIO 27
DHT11 module + and − 3V3 and GND
ESP32 GPIO 25 220 Ω resistor, then the white LED's long leg
LED short leg GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Set up Blynk

In the Blynk console, create a new template called Room Monitor (hardware ESP32, connection type Wi-Fi) and add these datastreams. Lesson 1 of this path shows each step in detail.

Virtual pin Name Data type Range Phone widget
V0 Noise Integer 0–100 Gauge or Superchart
V1 Temperature Double 0–50 Value display
V2 Humidity Integer 0–100 Value display
V3 Night light Integer 0–100 Slider

Then open the template's Events & Notifications tab, add an event with the code room_awake (name: Room is noisy) and turn on push notifications for it. The event code must match the sketch exactly.

Save the template, create a new device from it and copy its three BLYNK_… lines. In the phone app, open the device, tap the wrench icon and add a Gauge (V0), two Value displays (V1, V2) and a Slider (V3). Link each widget to its datastream.

Add a second event with the code room_temp (name: Room temperature) and turn on notifications for it too.

Example screen for: Build a room monitor for a baby or a pet with Blynk
Example screen with sample values. Yours may look different.

The sketch

Replace the three BLYNK_… lines at the top with the ones from your device's Device info page, and add your Wi-Fi name and password (2.4 GHz only). The BLYNK_… lines must stay above #include <BlynkSimpleEsp32.h>.

In the Arduino IDE, open Library Manager and install the DHT sensor library by Adafruit.

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Room Monitor"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial          // show connection messages in the Serial Monitor

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

const int SOUND_PIN = 34;
const int DHT_PIN = 27;
const int LIGHT_PIN = 25;
const int LOUD = 45;               // noise level (0-100) that counts as loud
const int LOUD_SECONDS = 5;        // loud for this many of the last 10 seconds
const float TOO_COLD = 16.0, TOO_WARM = 24.0;

DHT dht(DHT_PIN, DHT11);
BlynkTimer timer;
int loudHistory[10];               // 1 = this second was loud
int historyPos = 0;
bool awakeSent = false;
bool tempSent = false;

// Loudness 0-100 from the swing of the sound wave over 50 ms
int level() {
  int lowest = 4095, highest = 0;
  unsigned long start = millis();
  while (millis() - start < 50) {
    int v = analogRead(SOUND_PIN);
    lowest = min(lowest, v);
    highest = max(highest, v);
  }
  return constrain((highest - lowest) / 20, 0, 100);
}

void everySecond() {
  int noise = 0;
  for (int i = 0; i < 5; i++) noise = max(noise, level());   // loudest moment of the second
  Blynk.virtualWrite(V0, noise);

  loudHistory[historyPos] = noise >= LOUD;
  historyPos = (historyPos + 1) % 10;
  int loudCount = 0;
  for (int i = 0; i < 10; i++) loudCount += loudHistory[i];

  if (loudCount >= LOUD_SECONDS && !awakeSent) {
    Blynk.logEvent("room_awake", "It has been loud in the room for a few seconds.");
    awakeSent = true;
  }
  if (loudCount == 0) awakeSent = false;     // quiet again: ready for the next alert
}

void climate() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) return;
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  bool outside = t < TOO_COLD || t > TOO_WARM;
  if (outside && !tempSent) {
    Blynk.logEvent("room_temp", String("The room is at ") + String(t, 1) + " C.");
    tempSent = true;
  }
  if (t > TOO_COLD + 0.5 && t < TOO_WARM - 0.5) tempSent = false;
}

BLYNK_WRITE(V3) {                    // night light slider
  analogWrite(LIGHT_PIN, map(param.asInt(), 0, 100, 0, 255));
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V3);             // restore the night light after a restart
}

void setup() {
  Serial.begin(115200);
  pinMode(LIGHT_PIN, OUTPUT);
  dht.begin();
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L, everySecond);
  timer.setInterval(30000L, climate);
}

void loop() {
  Blynk.run();
  timer.run();
}

Tune it

  1. Watch V0 in the app while the room is quiet. It should stay well below LOUD. If not, turn the sensor's screw a little, or raise LOUD.
  2. Talk or clap near the monitor for a few seconds: the notification should arrive.
  3. The comfortable limits of 16–24 °C suit most bedrooms. Change TOO_COLD and TOO_WARM to suit yours.

How it works

A door slamming or a cough is loud, but only for a moment. The sketch keeps a history of the last ten seconds in an array: one if the second was loud, zero if it was quiet. historyPos walks round the array and overwrites the oldest second, a trick called a ring buffer. Only when at least five of the last ten seconds were loud does the notification go out.

Both alerts are sent once and then wait. The noise alert resets after ten quiet seconds. The temperature alert resets only when the room is half a degree inside the comfortable range, so a temperature wobbling around 24.0 °C can't send a message every 30 seconds. That gap is called hysteresis.

BLYNK_WRITE(V3) runs whenever you move the slider, and analogWrite() dims the LED by switching it on and off very fast.

Try this next

  • Make the night light come on by itself when it gets loud at night, and fade out after a minute.
  • Add a Superchart to see how the night went: when was it loudest?
  • Play a soft lullaby on a passive buzzer from a button in the app.

Troubleshooting

  • The device shows "Offline" in the app: check the Serial Monitor at 115200 baud. "Invalid auth token" means the BLYNK_AUTH_TOKEN line is wrong; if it never connects, check the Wi-Fi name and password, and that your network is 2.4 GHz.
  • A notification comes every few minutes: the room is never quiet enough. Raise LOUD or lower the sensor's sensitivity.
  • The temperature is too high: the DHT11 is too close to the ESP32. Move it to the other side of the case.
  • The slider does nothing: it must be linked to V3, and the LED must be on GPIO 25 with its long leg to the resistor.
  • A printed part is too tight or too loose: printers differ by a few tenths of a millimetre. Sand or file a tight hole, and fix a loose part with a drop of hot glue or a strip of tape.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

ESP32 IoT Starter Kit | Arduino IDE Compatible | Free Tutorials $57.00

Shop the kit