IoT-Based Pet Feeder Using NodeMCU-32S

When you go to work don’t forget about your lovely pet! They must be waiting for their food. Even if you are working in the office, you still can take care of your pet. But how is that even possible?

This project is mainly about how to feed your pet remotely. Using an infrared sensor, you can know the presence of your pet near the food container. When the infrared sensor detects the pet, you will get a notification which means that your pet is hungry! You can directly open the food container remotely by using the V-One dashboard. Then, change to another compartment of the container for the next meal with the help of a servo. 🐈

Video Tutorial

Components Required

  • NodeMCU-32S
  • Breadboard
  • Infrared Sensor
  • Servo
  • 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
people = obj[arrlgt]["Obstacle"]
threshold = 1

if int(people) < threshold:
    msgbody='<p>Food container is empty. Please refill the cat food.</p><br>'
    output[1]="[Notification] Refill Cat Food "
    output[2]=msgbody
    output[3]=2

# end your code here #

Arduino Code

/*
  ESP32 publish telemetry data to VOne Cloud (Pet Feeder)
*/

#include "VOneMqttClient.h"
#include <ESP32Servo.h>

//define device id
const char* ServoMotor = "7b9ad8f7-dc0f-4979-9e0d-87a6cab8ff83";      //Replace with YOUR deviceID for the servo
const char* InfraredSensor = "6d81b821-59b4-4e88-8096-47bc40eb8f4d";  //Replace with YOUR deviceID for the infrared sensor

//Used Pins
const int servoPin = 32;
const int InfraredPin = 35;

//Output
Servo Myservo;

//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 triggerActuator_callback(const char* actuatorDeviceId, const char* actuatorCommand)
{
  //actuatorCommand format {"servo":90}
  Serial.print("Main received callback : ");
  Serial.print(actuatorDeviceId);
  Serial.print(" : ");
  Serial.println(actuatorCommand);

  String errorMsg = "";

  JSONVar commandObjct = JSON.parse(actuatorCommand);
  JSONVar keys = commandObjct.keys();

  if (String(actuatorDeviceId) == ServoMotor)
  {
    //{"servo":90}
    String key = "";
    JSONVar commandValue = "";
    for (int i = 0; i < keys.length(); i++) {
      key = (const char* )keys[i];
      commandValue = commandObjct[keys[i]];

    }
    Serial.print("Key : ");
    Serial.println(key.c_str());
    Serial.print("value : ");
    Serial.println(commandValue);

    int angle = (int)commandValue;
    Myservo.write(angle);
    voneClient.publishActuatorStatusEvent(actuatorDeviceId, actuatorCommand, errorMsg.c_str(), true);//publish actuator status
  }
  else
  {
    Serial.print(" No actuator found : ");
    Serial.println(actuatorDeviceId);
    errorMsg = "No actuator found";
    voneClient.publishActuatorStatusEvent(actuatorDeviceId, actuatorCommand, errorMsg.c_str(), false);//publish actuator status
  }
}

void setup() {

  setup_wifi();
  voneClient.setup();
  voneClient.registerActuatorCallback(triggerActuator_callback);

  //sensor
  pinMode(InfraredPin, INPUT);
  //actuator
  Myservo.attach(servoPin);
  Myservo.write(0);
}

void loop() {

  if (!voneClient.connected()) {
    voneClient.reconnect();
    voneClient.publishDeviceStatusEvent(InfraredSensor, true);
  }
  voneClient.loop();

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

    //Publish telemetry data
    int InfraredVal = !digitalRead(InfraredPin);
    voneClient.publishTelemetryData(InfraredSensor, "Obstacle", InfraredVal);
  }
}

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