Raspberry PI Yard Lights

The Raspberry Pi is a small computer. It about size of a business card (1/4 inch taller, but the same width). It it runs a quad core 32 bit processor at up to 3 GH. Since it so small, it makes sense to use it in many DIY projects around the house.

You probably would not choose to dedicate a laptop or tower to being your lawn sprinkler system controller or yard lights controller. But the Raspberry pi has the ability to control the sprinkler system, your yard lights and many more areas.

This page documents my yard lights controller system. About half way through the project you will be able to remove the keyboard, mouse and HDMI monitor and the Pi will become “headless”. The information that follows documents how I made it work. The yard lights system controls the 37 led lights in my yard.

I have a USB keyboard, USB mouse, HDMI monitor which I can use on the first part of each of my Raspberry Pi projects. The second part of the project does not need these components.
1. Components Needed
Raspberry Pi 3+
Case
Power supply cord
16 GB SD card with noobs preloaded
WIFI USB Dongel
Jumpers (2), female / male
Powertail II
200 W transformer (110 to 12V)

USB keyboard, USB mouse, HDMI monitor (needed for the first few steps.)

Put Raspberry Pi in the case (but do not put on the cover), hookup USB keyboard and mouse, hookup   HDMI monitor. Plugin to power.

2. Install OS and Set your locale.   (menu / Raspberry PI/Configuration)

Set up keyboard configuration. (US/EN)

Note: (default root password is raspberry)

Setup Wifi.(going to need this feature). Make sure that you connect to a WIFI router that will be available when you put the box into “production”.

(Check log files every few minutes. See /var/log/messages for error messages)

Users and Passwords.

Change the name of your pi. Edit the file /etc/hostname and change it to yardlights.

This is a good time to start your documentation. Record all user names and password in your documentation. Title of documentation is “yardlights” – the name of this server.

Set password for root. (see wlperry.biz for more info about passwords.)Create a user “house”, and set a password.
Test  by click on shutdown, then “command line”, Make sure that you can sign on to root and house. If you can not sign on do not proceed.
Remove user “pi”.
Do orderly shutdown. Un-plug.

3. Wiring
Pins are numbered left to right, then top to bottom. Top is the end away from the USB ports.
Pin 1 is upper left, pin to 2 is to its right, pin 3 is just below pin 1.

Ground is pin 1. There are several options for which GPIO pins that you can use for power. I chose GPIO pin 7.

Take a jumper, insert the female end over pin 1. I used purple. Take the male end of that jumper and insert it in left opening on the side of the Powertail (Powertail 1). In the top of the Power tail there are 3  holes. Insert a small screwdriver in the left hole and tighten down the screw inside.

Take another jumper, insert the female end over pin 7. I used grey. Take the male end of that jumper and insert it in center opening on the side of the Powertail (Powertail 1). Insert a small screwdriver in the center hole on the top of the Powertail and tighten down the screw inside.

Document which color wires go where. Put the cover on the Raspberry PI.

4. Software

Power up Raspberrry PI and power tail II.

crontab and how it works
Cron is a standard system utility on Linux and Unix systems. This program runs for each user that has a cron table. Each user has their own table. This tool allows you to run a program at a certain time even if you are not there to activate the program.

use
crontab -e
to edit crontab

use
crontab -l
to list your crontab

crontab arguments are separated by spaces.
The arguments are  m h dom mon dow com:
m      Minutes
h      Hour (use 24 hr notation)
dom    Day of month (1-31)
mon    Month (1-12)
dow    Day of week (0-6)
com    Command

Note:
*12 * * * mycommand
will cause mycommand to be executed once a minute from 12:00 to 12:59

For example:
*/5  12 * * * mycommand
to run this program every 5 minutes from 12:00 to 12:59

For example, to run a program every Friday at 3 pm:

0  15 *  *  5 mycommand

sunwait and how to use it
sign on as house
Get sunwait from sourceforge.net

download to /home/house/Downloads
unzip
type:
cd /home/house/Downloads/sunwait
make

test:
(Use your exact Lat & Long here. Use Google to get your location, note that 38,-120, means 38N and 120W. There is no such thing as neg. degrees.) There must be 7 digits to the right of the decimal. Record this information. You will need it later. Test your setup by entering your Lat and Long in the following command:

sunwait -p   38.0000000N  120.0000000W

You should see some info here.

Sign off house and restart your pi.
sign on as root
copy the file to its executable location. Do
cp /house/Downloads/sunwait  /usr/local/bin/sunwait

While we are logged on as root, lets make sure that we are up to date.

do:
apt-get -y update
apt-get -y upgrade

5. Programming the lights

5A. setup file
The setup file is a file which contains the instructions which must be executed every time the Raspberry pi is restarted.

5A1. Sign on as root
Use your favorite editor or nano to create the file.
nano /usr/local/bin/setup.sh

 

#!/bin/bash

echo “4” > /sys/class/gpio/export
echo “out” > /sys/class/gpio/gpio4/direction
echo “1” > /sys/class/gpio/gpio4/value

 

save and exit
make it executable by:
chmod +x /sys/local/bin/setup.sh

test it by typing:

/usr/local/bin/setup.sh

5A2. Still signed on as root,

fix the scripts to execute at startup:
Use nano to edit  /etc/rc.local and add one line to execute  your new script.

nano /etc/rc.local

go to the bottom and type:
/usr/local/bin/setup.sh

save and exit
make sure that it is executeable:
chmod +x /etc/rc.local

5B. start_lights script
Sign on as user house.
1. Get Lat and Long you used earlier
If you are signed on as root, exit and shut down and restart the pi.
Sign on as house
mkdir bin
cd bin

create your lights on script. I want to have my lights come on 15 min. before sundown. Sunwait needs to start in the afternoon, well before sundown. Sunwait will pause and wait until 15 minutes before sundown then allow the script to continue with next instruction. (which is the gpio instruction. – see below.)

nano /home/house/bin/start_lights
#!/bin/bash
echo “start_lights script =============”
/usr/local/bin/sunwait   sun down -0:15:00 38.0000000N  120.0000000W
gpio write 7 0
echo “end start_lights ==============”

Save and exit

chmod +x /home/house/bin/start_lights

5C. stop_lights script
create your stop_lights script. By doing:

nano /home/house/bin/stop_lights
#!/bin/bash
echo =”stop lights===================”
gpio write 7 1
echo=”lights stopped==============”

Save and exit

chmod +x /home/house/bin/stop_lights

5D. crontab setup

use the command

crontab -e

I have chosen to have sunwait start at 3pm so it will start looking for sundown at that time.

Enter the following in your crontab.

1  17 * * * /home/house/bin/start_lights >> /home/house/local.log
1  21 * * * /home/house/bin/stop_lights >> /home/house/local.log

5E. Find out what ip address has been assigned to your pi.

Type ifconfig

Your pi’s ip addresses will be displayed. Write it down, you need it for future work.

6  Set up for long term

Mount your pi in its permanent location. At this point you no longer need the keyboard, mouse or monitor. (put them away for use in your next pi project.) Your pi is now “headless” and can be accessed from your smart phone or tablet. Just install Juicessh and log in to root@<ipaddress> where <ipaddress> is the ip address from step 5E.

imag0131
Sign on as root.

Daily update
Create a script to do needed daily tasks.
daily.sh
#!/bin/bash
apt-get -y update
apt-get -y upgrade

Add it to root’s crontab
1  1  *  *  * /root/daily.sh  >> /home/house/local.logs

Report to you:
add the needed utilities to your pi
Sign on as root:
apt-get -y ssmtp mailutils

Configure
You really should have a gmail account. In this example my Gmail email is account bill@gmail.com, my password is Complex1t4

At this point you have several files installed which are templates and need to be configured for your use.
Edit /etc/ssmtp/ssmtp.conf

root =postmaster
mailhub=smtp.gmail.com:587
hostname=yardlights.mydomain.com
AuthUser=bill@gmail.com
AuthPass= Complex1t4
UseSTARTTLS=YES
rewriteDoman=mydomain.com
FromLineOverride=YES

 

Edit /etc/ssmtp/revaliases
root:root@mydomain.com:smtp.gmail.com:587

chmod 774 /etc/ssmtp/ssmtp.conf

test:
echo “My Test”| mail -s “Test Message” bill@gmail.com

If all goes well, Google will send you a message that someone is trying to send email through your email account and the message is coming from a less secure app. You will need to approve this and try again. Once you get the message to go through, you have mail configured.

If you have trouble, check:
tail /var/log/mail.err
tail /var/log/messages

Add a log file reporting script:

create script /root/monthly.sh

#!/bin/bash
echo =========== Monthly script================
mail -A /home/house/local.log -s “Report from yardlights”  bill@gmail.com
rm /home/house/local.log
touch /home/house/local.log
chown house.house /home/house/local.log
echo ============= end Monthly script ==========

save and exit.

chmod  +x /root/monthly.sh

To test:

/root/monthly.sh

Make sure that you receive the email

crontab to run the monthly script once a month only on day 1.

1   1  1  * *  monthly.sh > monthly_logs

7. How to monitor

Suppose that you know that sunset is 6:15 and you want to check the yard lights server, you don’t want to sit in the garge watching the powertail to be sure that the red led comes on at 6:15.  Using your tablet, bring up juicessh and login to house@<ip>. Then type:

tail -f local.log

At 6:15 you should see the info from “start_lights” scroll by on your screen. Use control-c  to kill the tail command.

8. Harden your pi

8.a  Install fail2ban

sudo apt-get -y install fail2ban

in /etc/fail2ban/jail.conf

   set the following values:

        destemail= (reports)

        sender=root@(machine name.Your Domain)

    (Otherwise defaults should be ok)

8.b Install tripwire

Finish your tripwire install:

tripwire –init

cd /etc/tripwire

cp twpol.txt twpol.txt.BKP

tripwire -m c | grep Filename > /var/tmp/firstrun.txt

./confirure_twpol.pl

twadmin -m P /etc/tripwire/twpol.txt

tripwire -m i

tripwire -m c

 

 

Up in the country.