IoT-Based Baby Monitoring System

As parents, it is very frightening to leave your baby while doing something, isn’t it? We often want to check on them but at the same time, we need to focus on our tasks. With this simple project, we can monitor if there is movement from our baby using a motion sensor. Once the motion sensor detects a movement, the user will receive a notification. 👶🍼 

Video Tutorial

Components Required

  • NodeMCU-32S
  • Breadboard
  • PIR Sensor
  • Jumpers

You can get all these components from Cytron IoT kit here

Circuit Diagram

circuit diagram

Workflow Code

# Start your code below, you can access parameter values as normal list starting from index 1 e.g. i = parameter[1], you can write to output value as normal list starting from index 1 e.g. output[1]= 1+1

obj = parameter[1]["data"]
arrlgt = parameter[1]["count"]["total"] - 1
motion = obj[arrlgt]["Motion"]
threshold = 0

if int(motion) > threshold:
    msgbody='<p>Your baby awake. Please go check on him.</p><br>'
    output[1]="[Alert]  Motion Detected! "
    output[2]=msgbody
    output[3]=2

# end your code here #

Arduino Code

/*
  ESP32 publish telemetry data to VOne Cloud (Baby Monitoring)
*/

#include "VOneMqttClient.h"

//define device id
const char* PIRsensor = "bda7cc3c-84fb-4731-b333-8c3c32c4a6b6";  //Replace with YOUR deviceID for the PIR sensor

//Used Pins
const int motionSensor = 22;

#define timeSeconds 3

//input sensor
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
  startTimer = true;
  lastTrigger = millis();
}

//Create an instance of VOneMqttClient
VOneMqttClient voneClient;

//last message time
unsigned long lastMsgTime = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {

  setup_wifi();
  voneClient.setup();

  //sensor
  pinMode(motionSensor, INPUT);
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
}

void loop() {

  if (!voneClient.connected()) {
    voneClient.reconnect();
    String errorMsg = "PIRsensor Fail";
    voneClient.publishDeviceStatusEvent(PIRsensor, true);
  }
  voneClient.loop();

  unsigned long cur = millis();
  if (cur - lastMsgTime > INTERVAL) {
    lastMsgTime = cur;

    now = millis();
    // Turn off the LED after the number of seconds defined in the timeSeconds variable
    if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
      startTimer = false;
    }

    voneClient.publishTelemetryData(PIRsensor, "Motion", startTimer);

  }
}

References

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Earth’s clock seems to be stopped for Marty as he speeds past while he sees his clock as shifting normally.
A Research-based Strategy To Relationships
A Research-based Strategy To Relationships
A Research-based Strategy To Relationships
A Research-based Strategy To Relationships
Tutorials of Cytron Technologies Scroll to Top