Hao
Published

A pet monitoring and alarm device based on Yolov8

With Seeed XIAO ESP32S3 Sense, Raspberry Pi 4, Homeassistant, ESPhome, Mosquitto and YOLOV8

BeginnerFull instructions provided1 hour1,012
A pet monitoring and alarm device based on Yolov8

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Seeed Studio XIAO ESP32S3 Sense
Seeed Studio XIAO ESP32S3 Sense
×1

Software apps and online services

Home Assistant
Home Assistant
MQTT
MQTT

Story

Read more

Code

Yolo code

Python
from ultralytics import YOLO
from ha_mqtt_discoverable import Settings
from ha_mqtt_discoverable.sensors import BinarySensor, BinarySensorInfo

CAM_URL = "http://xiaos3_ip:8080"
MQTT_USERNAME = "admin"
MQTT_PASSWORD = "admin"

mqtt_settings = Settings.MQTT(
    host="localhost", username=MQTT_USERNAME, password=MQTT_PASSWORD
)
sensor_info = BinarySensorInfo(name="dog", off_delay=3)
mysensor = BinarySensor(Settings(mqtt=mqtt_settings, entity=sensor_info))

model = YOLO("yolov8n.pt")
results = model.predict(CAM_URL, stream=True, show=False, conf=0.5)

# loop
for result in results:
    for box in result.boxes:
        class_id = result.names[box.cls[0].item()]
        cords = box.xyxy[0].tolist()
        cords = [round(x) for x in cords]
        confi = round(box.conf[0].item(), 2)
        print("Object type:", class_id)
        print("Coordinates:", cords)
        print("Probability:", confi)
        print("---")
        if class_id == "dog":
            mysensor.on()

ESPHome

YAML
esphome:
 name: xiaos3-cam
 friendly_name: xiaos3_cam

 # PlatformIO build options
 platformio_options:
  build_flags: -DBOARD_HAS_PSRAM
  board_build.arduino.memory_type: qio_opi
  board_build.f_flash: 80000000L
  board_build.flash_mode: qio 

esp32:
 board: seeed_xiao_esp32s3
 variant: esp32s3
 framework:
  type: arduino
  version: latest
  platform_version: 6.5.0



# Enable logging
logger:

# Enable Home Assistant API
api:
 encryption:
  key: "xxxxxxxxxxxxxxxxxxx"

ota:
 password: !secret password

wifi:
 ssid: !secret wifi_ssid
 password: !secret wifi_password

 # Enable fallback hotspot (captive portal) in case wifi connection fails
 ap:
  ssid: "Xiaos3-Cam Fallback Hotspot"
  password: !secret password

captive_portal:

# Configuration for the status LED light
light:
 - platform: status_led
  id: light0
  name: "Xiao S3 State"
  pin:
   number: GPIO21
   inverted: true

# Configuration for the binary sensor (Boot Switch)
binary_sensor:  
 - platform: gpio
  pin: 
   number: GPIO2
   mode:
    input: true
    pullup: true
  name: Boot Switch
  internal: true
  on_press:
   # - voice_assistant.start:
   - light.turn_off: light0
  on_release:
   # - voice_assistant.stop:
   - light.turn_on: light0

# Configuration for the ESP32 Camera
esp32_camera:
 id: espcam
 name: My Camera
 external_clock:
  pin: GPIO10
  frequency: 20MHz
 i2c_pins:
  sda: GPIO40
  scl: GPIO39
 data_pins: [GPIO15, GPIO17, GPIO18, GPIO16, GPIO14, GPIO12, GPIO11, GPIO48]
 vsync_pin: GPIO38
 href_pin: GPIO47
 pixel_clock_pin: GPIO13
 resolution: 1280x1024
 
# Configuration for the ESP32 Camera Web Server
esp32_camera_web_server:
 - port: 8080
  mode: stream
 - port: 8081
  mode: snapshot

Docker compose

YAML
version: '3'

services:
  homeassistant:
    image: ghcr.io/home-assistant/home-assistant:stable
    container_name: homeassistant
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /run/dbus:/run/dbus:ro
      - /home/hao/docker/homeassistant/config:/config
    privileged: true
    network_mode: host
    restart: unless-stopped
    
  esphome:
    image: ghcr.io/esphome/esphome
    container_name: esphome
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /home/hao/docker/esphome/config:/config
    environment:
      - USERNAME=admin
      - PASSWORD=admin
    privileged: true
    network_mode: host
    restart: unless-stopped

  mosquitto:
    image: eclipse-mosquitto
    container_name: mosquitto
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - /home/hao/docker/mosquitto/mosquitto/config:/mosquitto/config
      - /home/hao/docker/mosquitto/mosquitto/data:/mosquitto/data
      - /home/hao/docker/mosquitto/mosquitto/log:/mosquitto/log
    restart: unless-stopped

Credits

Hao

Hao

1 project • 1 follower

Comments