Web application for controlling remote-controlled power sockets at home via smartphone using a Raspberry Pi and a 433Mhz transmitter. Supports timed events - even based on current sunrise/sunset time.
Hardware and 433Mhz library from Ninjablocks set up as described here.
git clone https://github.com/mbarde/sloth-tools.git
cd sloth-tools
pip3 install -r requirements.txt
Edit config.json
:
codesend-bin-path
: (string) Path to the compiled codesend-binary (see prerequisites)longitude
andlatitude
: (float) Longitute and latitude of your location - needed for sunrise/sunset computation
export FLASK_APP=server.py
flask init-db
Database then lives in database.sqlite
. To reset database, delete this file and run init-db
again.
(see https://github.com/mbarde/sloth-tools/releases/tag/v1.0.0)
export FLASK_APP=server.py
flask migrate-to-v1
env FLASK_APP=server.py flask run --host=0.0.0.0
This application is based on Flask, so you should take a look at its official deployment guidelines.
In order to ensure timed events to work as expected, your server has to use the same timezone as the application users. For Ubuntu server see: https://help.ubuntu.com/community/UbuntuTime#Using_the_Command_Line_.28terminal.29
To ensure the application is always running as soon as the machine is up you should create a service as described here: https://wiki.debian.org/systemd
In combination with waitress your service configuration could look like following:
homecontrol.service
[Unit]
Description=Sloth-Tools
After=network.target
[Service]
User=pi
WorkingDirectory=/home/pi/server
ExecStart=/home/pi/.local/bin/waitress-serve --port=5000 --call "server:create_app"
Restart=always
[Install]
WantedBy=multi-user.target
Enable:
sudo systemctl enable homecontrol.service
Once running you can access Sloth Tools via the provided web application. But you can also directly interact with the backend by calling certain endpoints:
/on?id={ID}
: Put node with specified ID on/off?id={ID}
: Put node with specified ID off/toggle?id={ID}
: Toggle state of node with specified ID (aka. blink)
For example you can use the motion detection library Motion to execute scripts which call these endpoints on certain motion events (see: https://motion-project.github.io/motion_config.html#OptTopic_Scripts).
For example you could use following script to blink node 42 everytime a motion is detected (assuming Sloth Tools is running on the machine with the IP 192.168.0.13
on port 5000
in your network):
#!/bin/sh
wget -O/dev/null 192.168.0.13:5000/toggle?id=42 -q