项目作者: kurucu

项目描述 :
Python and Laravel based aquarium controller for the Raspberry Pi, controlling lights and more according to daylight hours in your location.
高级语言: PHP
项目地址: git://github.com/kurucu/fishtankserver.git
创建时间: 2017-09-10T12:53:07Z
项目社区:https://github.com/kurucu/fishtankserver

开源协议:MIT License

下载


Fish Tank Server

This project helps you set up a controller for your aquarium lights based on a Raspberry Pi. The project currently has the following features:

  • Automatically switches the lights between day and night mode at sunrise and sunset, accordingly (Auto mode).
  • Allows you to visit a web application on your phone to turn the lights manually to Day/Night/Off or back to Auto.
  • Uses Avahi (Bonjour to Apple users) to allow auto-discovery

There are more ideas in the pipeline, and the code is easily extended. I welcome pull requests to add/modify features and/or the documentation.

Technical Overview

The project is based on the following technologies and designs:

  • A Raspberry Pi (any should do, I use a B+) with a USB WiFi dongle
  • A Pimeroni AutomationHAT (with some tweaking of the python script, any relay or control board should be fine)
  • Raspbian Stretch Lite (CLI Only, the Raspberry Pi hides in the aquarium and controls the lighting relays, so why not)
  • A python script which runs as service, and controls the GPIO
  • Laravel 5.5 for the web application, which lets the python script know what to do
  • Avahi to enable auto-discovery

The project runs a basic service, written in python, that checks a mode file (default is /etc/fishtank/mode.txt) every 10 seconds. The mode file can contain either “day”, “night” or “off”, and the python script will alter the tank lighting accordingly.

The laravel web framwork does two things. Firstly, using its scheduler (run from the CLI, via Cron) it will check to see whether there is daylight (i.e. we are between sunrise and sunset). If the user has selected Auto, it updates the mode file with day/night accordingly; otherwise it leaves the file alone.

Secondly, it offers a web interface, accessible from any computer on the local network which provides:

  • An HTML page with control buttons and feedback elements
  • An API to find out information about the tank and to change modes (all used by the web page itself)

Installation Method

Set Up the Pi

You need to enable SSH on your Pi, either by connecting a monitor and keyboard and running “sudo raspi-config”; or by saving an empty file called “ssh” in the /boot partition before putting the SD card in the Pi.

Then, plug in the EdiMax Wifi dongle, and configure your Wifi as detailed here. Make sure the connection is working, and then disconnect the ethernet/keyboard and monitor. Perhaps connect via SSH one last time (via wifi) to make sure.

While you might like to make the IP address static, you can also rely on Avahi (included with Raspbian) and then find your Pi automatically at raspberrypi.local.; or a hostname of your choosing.

Electronics and Wiring

Follow the guide here to wire up your Pi, or get ideas as to how you might do so.

Install Dependencies

If you’re using the AutomationHAT from Pimeroni, then follow the install guide here.

Install ngingx (a lightweight web server) and PHP.

  1. sudo nginx apt-get install php7.0 php7.0-dom php7.0-mbstring php7.0-gd php7.0-zip php7.0-sqlite3

Then install composer, by following the instructions here.
At the time of writing, this would be done as follows:

  1. cd ~
  2. wget https://getcomposer.org/installer
  3. mv composer.phar /usr/local/bin/composer

You will then need to add the composer path to your $PATH, but editing ~./bashrc, using nano ~/.bashrc, and adding the following line:

  1. export PATH="/path/to/dir:$PATH"

Then type source ~/.bashrc to effect the changes.

Set up the web server

Go to the default web location, set up the folder structure and pull in this repo. Ideally, instead of pulling in the repo with sudo,
you would change your /var/www to be owned by www-data, and then pull it in normally. But the below works if you don’t know how to
do that. It’s an aquarium, so unless you’re putting vital data in your fish tank, this is ok.

  1. cd /var/www
  2. sudo mv html html.back
  3. sudo git clone git@github.com:kurucu/fishtankserver.git
  4. sudo chmod 777 -R fishtankserver/storage
  5. sudo chmod 777 -R fishtankserver/logs
  6. # Create a database file
  7. sudo touch database/database.sqlite
  8. # Update the composer dependencies - this may take a while on a pi
  9. composer update
  10. # Migrate the database
  11. php artisan migrate
  12. php artisan migrate --path=vendor/anlutro/l4-settings/src/migrations

You will now need to reconfigure ngingx to use the /var/www/fistankserver/public directory, and to use PHP, as follows:

  1. sudo nano /etc/nginx/sites-available/default

Replace the root directory:

  1. # root /var/www/html
  2. root /var/www/fishtankserver/public;

Add index.php to the allowed index documents:

  1. # Add index.php to the list if you are using PHP
  2. index index.php index.html index.htm index.nginx-debian.html;

And make a few changes in the location area:

  1. location / {
  2. # First attempt to serve request as file, then
  3. # as directory, then fall back to displaying a 404.
  4. # try_files $uri $uri/ =404;
  5. try_files $uri $uri/ /index.php?$query_string;
  6. }
  7. # pass PHP scripts to FastCGI server
  8. #
  9. location ~ \.php$ {
  10. include snippets/fastcgi-php.conf;
  11. # With php-fpm (or other unix sockets):
  12. fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  13. }

Create service

Create a file to watch for the desired state (/etc/fishtank/mode.txt), allow it to be edited by anyone and set a starting value.
Copy the python fishtank service to the same place, and allow it to be executed. This file runs as a service to watch
the /etc/fishtank/mode.txt file, and keep the lights turned to the appropriate settings.

  1. sudo mkdir /etc/fishtank
  2. sudo touch /etc/fishtank/mode.txt
  3. sudo chmod 777 /etc/fisktank/mode.txt
  4. echo "night" > /etc/fishtank/mode.txt
  5. sudo cp /path/to/cloned/repo/supporting_files/fishtank.py /etc/fishtank/fishtank.py
  6. sudo chmod +x /etc/fishtank/fisktank.py

Go and create a service definition file:

  1. cd /lib/systemd/system/
  2. sudo nano fishtank.service

And paste in the following contents.

  1. [Unit]
  2. Description=Fishtank Service
  3. After=multi-user.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/bin/python /etc/fishtank/fishtank.py
  7. Restart=on-abort
  8. [Install]
  9. WantedBy=multi-user.target

Press ctrl-o (save), enter (confirm) and ctrl-x (exit).

Now enable the service:

  1. sudo chmod 644 /lib/systemd/system/fishtank.service
  2. sudo systemctl daemon-reload
  3. sudo systemctl enable fishtank.service
  4. sudo systemctl start fishtank.service

In general:

  1. # Check status
  2. sudo systemctl status fishtank.service
  3. # Start service
  4. sudo systemctl start hello.service
  5. # Stop service
  6. sudo systemctl stop hello.service
  7. # Check service's log
  8. sudo journalctl -f -u hello.service

You might have guessed that this part came from HOW TO RUN A SCRIPT AS A SERVICE IN RASPBERRY PI - RASPBIAN JESSIE.