Skip to content

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

RPI&Zigbee home security project

Student Name: Oleksandr Korniienko Student ID: 20114792

IMPORTANT:

This is not a final product and was created only for the learning porposes.

This project deffinetely has many vulnabilities and probably not safe to use at the moment.

The main purpose of the project was demonstrate ability to interact with IoT devices thought the network.

Description

This is home security project that will include work with RaspberyPi, Raspberry Pico, Zigbee sensors and Mosquitto MQTT brocker.

The main purpose is to create a system that will use various sensors such as movement, presence, open(door/window) and send a screenshot/or short gif from the RaspberryPi Camera to the database and the Bot in the comunication application (Telegram) as well as question what to do: ignore or turn on alarm immediatelly.

The alarm function will be turned on automatically if:

  1. There is noone at home (the users are not connected to the home WiFi)

  2. It's later than 10PM.

Alarm will turn or lights everywhere on the ground floor, send barking sound of a big dog to the speaker and alarm sound in the bedroom.

The alarm can be turned off by pressing the button or sending the request in the Telegram bot.

Tools, Technologies and Equipment

Equipment

RaspberyPi 3b

Picamera

Zigbee Sonoff Dongle-P USB3.0

Zigbee motion sensors

Zigbee door opening sensor

Ziggbe remote button

Technologies and tools

Python

MQTT

Zigbee

Telegram bot

Project Repository

github.com/KorneAlex/HSP_RPIZIG

System workflow

workflow.png

1. Zigbee2MQTT install on RPI3b with Sonoff Zigbee usb dondle

Flash the Sonoff dondle with the latest firmware

https://dongle.sonoff.tech/sonoff-dongle-flasher/

Get project files

sudo git clone https://github.com/KorneAlex/HSP_RPIZIG.git

cd HSP_RPIZIG

sudo install.sh

Instalation (simple)

Run the install.sh file with the administrator rights on your RPI:
sudo git clone https://github.com/KorneAlex/HSP_RPIZIG.git

cd HSP_RPIZIG

sudo install.sh
The following script is taken from the official site:
sudo apt-get install -y curl

sudo curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

sudo apt-get install -y nodejs git make g++ gcc libsystemd-dev

corepack enable
Verify that the correct Node.js version has been installed
node --version # Should output V20.x
Create a directory for zigbee2mqtt and set your user as owner of it
mkdir /opt/zigbee2mqtt

chown -R ${SUDO_USER}: /opt/zigbee2mqtt
Clone Zigbee2MQTT repository
git clone --depth 1 https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
Install dependencies (as user "pi")
cd /opt/zigbee2mqtt

pnpm install --frozen-lockfile
Create a systemctl configuration file for Zigbee2MQTT
sudo nano /etc/systemd/system/zigbee2mqtt.service
Incert into the file:
[Unit]

Description=zigbee2mqtt

After=network.target

  

[Service]

Environment=NODE_ENV=production

Type=notify

ExecStart=/usr/bin/node index.js

WorkingDirectory=/opt/zigbee2mqtt

StandardOutput=inherit

StandardError=inherit

WatchdogSec=10s

Restart=always

RestartSec=10s

User=pi

  

[Install]

WantedBy=multi-user.target
Run this command to reload units.
systemctl daemon-reload
Start Zigbee2MQTT
sudo systemctl start zigbee2mqtt
Show status
systemctl status zigbee2mqtt.service
Enable on boot
sudo systemctl enable zigbee2mqtt.service
View the log of Zigbee2MQTT
sudo journalctl -u zigbee2mqtt.service -f
Update Zigbee2MQTT to the latest version
cd /opt/zigbee2mqtt

./update.sh

2. Configuring the mosquitto service

Install mosquitto on the RPI (server) and client device:

sudo apt install mosquitto mosquitto-clients

sudo systemctl enable --now mosquitto
check if the port is open on the
nc -vz <rpi_ip> 1883
if port is closed, set the config file:
sudo mosquitto -c /etc/mosquitto/mosquitto.conf
add these lines to the config:
listener 1883 0.0.0.0

allow_anonymous true

log_type all
restart the mosquitto service:
sudo systemctl restart mosquitto
Test subscribe to the topic
mosquitto_sub -h <rpi_ip> -t "zigbee2mqtt/#"

sudo nano /etc/systemd/system/my_python_app.service

Preparing python packages for the service

Install tools

sudo apt update

apt install -y python3-venv python3-pip python3-dev python3-setuptools meson pipx

git clone https://git.libcamera.org/libcamera/libcamera.git

cd libcamera

meson setup build

cd ..

sudo rm -rf libcamera

  

sudo apt install python3-paho-mqtt python3-telegram python3-picamera2 sudo apt install ffmpeg

python3 -m pip install --break-system-packages --force-reinstall --upgrade python-telegram-bot

Copy project files to /opt/hsp_rpizig

TODO: copy only needed files
echo "Copying files to /opt/hsp_rpizig..."

mkdir -p /opt/hsp_rpizig

cp -a ./* /opt/hsp_rpizig/

cd /opt/hsp_rpizig/

Create the hsp service file

touch /etc/systemd/system/hsp.service
Inscert this inside:
[Unit]

Description=HomeSecurityProgect_RaspberryPiZigbee

After=network.target mosquitto.service zigbee2mqtt.service

;Requires=mosquitto.service zigbee2mqtt.service

  

[Service]

Type=simple

User=<USER_NAME>

WorkingDirectory=/opt/hsp_rpizig/

ExecStart=/opt/hsp_rpizig/main.py

Restart=always

RestartSec=5

  

[Install]

WantedBy=multi-user.target

Create the hsp_bot service file

touch /etc/systemd/system/hsp_bot.service
Inscert this inside:
[Unit]  

Description=HomeSecurityProgect_RaspberryPiZigbee-bot

After=network.target mosquitto.service

  

[Service]

Type=simple

User=<USER_NAME>

WorkingDirectory=/opt/hsp_rpizig

ExecStart=/usr/bin/python3 /opt/hsp_rpizig/tg.py

Restart=always

RestartSec=5

  

[Install]

WantedBy=multi-user.target" > /etc/systemd/system/hsp_bot.service

Run this command to give access to user

chown -R ${SUDO_USER}:${SUDO_USER} /opt/hsp_rpizig/*

Enable and start services:

systemctl daemon-reload

systemctl enable mosquitto.service zigbee2mqtt.service hsp.service hsp_bot.service

systemctl start mosquitto.service zigbee2mqtt.service hsp.service hsp_bot.service

Create the config file and edit it:

cp /opt/hsp_rpizig/modules/config_example.py /opt/hsp_rpizig/modules/config.py

sudo chown -R ${SUDO_USER}:${SUDO_USER} /opt/hsp_rpizig/modules/config.py

Check if it works/Troubleshoot

go to your <RPI_IP>:8080 and chose your Sonoff dondle with Ember setting for E version (for other versions check the Sonoff oficial site https://sonoff.tech/)

in order for zigbee2mqtt to work it needs the port 1883 to be open, the dondle to be latest version (flashed) and mosquitto setvice up and running.

if it doesn't connect, check the mosquitto service for errors.

when connected, you can open the secound terminal and try to listen the broadcast with mosquitto:

mosquitto_sub -h <RPI_IP> -p 1883 -t "zigbee2mqtt/#"

if you see a lot of data then we are good. if not check if you can ping the <RPI_IP> and if the base_topick is correct in the /opt/zigbee2mqtt/data/configuration.yaml

To setup bot please use @BotFather

Add the required information to your /opt/hsp_rpizig/modules/config.py

When everything is fine before the last check retart the RPI.

Try to send commands to the bot.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages