OpenSprinkler Pi

From Rayshobby Wiki
Revision as of 05:25, 12 August 2013 by Ray (talk | contribs)
Jump to navigationJump to search

Download Location

It is recommended to use Raspbian “wheezy”. There is unlikely to be any reason to use the slower soft-float version.


Please refer to the instructions on


Login: Username: pi
Password: raspberry
IP address: via DHCP

Initial Setup

If you are using your Raspberry Pi without a monitor you will need to run the initial setup manually from the command line when you first log in to it via ssh:

sudo raspi-config

DS1307 RTC

The following is collected from various sources:

Edit /etc/modules

sudo editor /etc/modules

And add the following:


Add the modules to the kernel (they will automatically be added on subsequent boots from /etc/modules):

modprobe i2c-bcm2708 
modprobe i2c-dev
modprobe rtc-ds1307

Comment out both spi-bcm2708 & i2c-bcm2708 from /etc/modprobe.d/raspi-blacklist.conf
Install i2c-tools

sudo apt-get install i2c-tools

Look for ID #68 with i2cdetect
On a 256MB Raspberry Pi Model B:

sudo i2cdetect -y 0

On a 512MB Raspberry Pi Model B:

sudo i2cdetect -y 1

The result should look like:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

As root:
On a 256MB Raspberry Pi Model B:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

On a 512MB Raspberry Pi Model B:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

Check the kernel logs for messages like

rtc core: registered ds1307 as rtc0

Then check for time from the clock (which will show Sat 01 Jan 2000 if it is the first time it is used):

sudo hwclock -r

And then write the current system time to the clock:

sudo hwclock -w

The edit /etc/rc.local:

sudo editor /etc/rc.local

and add the following before exit 0:
On a 256MB Raspberry Pi Model B:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
hwclock -s

On a 512MB Raspberry Pi Model B:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s

Hardware watchdog

The following is collected from:

Install watchdog

sudo apt-get install watchdog

Uncomment the following line in /etc/watchdog.conf

watchdog-device = /dev/watchdog

Edit /etc/modules

sudo editor /etc/modules

And add the following:

# hardware watchdog

Run the following command to activate the module and service:

sudo modprobe bcm2708_wdog ; sudo service watchdog restart

Wifi setup with a static address

You are generally going to want a static IP address on your Raspberry Pi for OpenSprinkler.

There are two methods of connecting to a WPA network. wpa_supplicant is slightly more complicated but has the advantage that it should automatically reconnect if the network goes away temporarily.

wpa_supplicant method

Edit /etc/network/interfaces

sudo editor /etc/network/interfaces

Add a new entry with the interface name the same as the IDString used in the next step with wpa_supplicant.

iface <IDString> inet static
	address <ip address>
	netmask <netmask>
	gateway <gateway address>

Connect to the wifi network through the GUI or wpa_cli or directly edit /etc/wpa_supplicant/wpa_supplicant.conf and then restart the networking service. In all cases make sure you set the IDString to the same as in /etc/network/interfaces or you will not get a static address. If editing the conf file it will look similar to this for a WPA PSK network:


/etc/network/interfaces method

Edit /etc/network/interfaces

sudo editor /etc/network/interfaces

Change the wlan0 interface type from manual to static and entering the following:

iface wlan0 inet static
	address <ip address>
	netmask <netmask>
	gateway <gateway address>
	wpa-ssid "<SSID>"
	wpa-psk	"<presharedkey>"

Restart networking with the following command or reboot.

sudo service networking restart

Remote syslog

If you have another server setup to act as a syslog server you can send the systems logs to it to reduce the writes to the Raspberry Pi's SD card.

Edit /etc/rsyslog.conf

sudo editor /etc/rsyslog.conf

Comment out all the lines not starting with a $ sign (i.e. all the rules).

Then add the following line:

*.* @

The restart the syslog service:

sudo service rsyslog restart

Further steps

Further installation and configuration is done as per a standard Debian or Ubuntu setup.
E.g. Network configuration can be done through /etc/network/interfaces

Data Logging on the Raspberry Pi

As with all flash memory, the SD card that is used on the Pi in place of a hard drive to hold the operating system and programs has a limited number of write cycles. Programs and data on the card can be read any number of times but new data can only be written to each memory location on the card about ten thousand times. That may seem like a lot and for general programming and updating the OS it is plenty.

A problem arises when you want to have the Pi or something connected to it make frequent changes to one or more files such as for data logging. This can eventually ”wear out” the SD card.

One solution is to attach another storage device to the Pi and use that to hold frequently updated files. You could use a

  • Flash drive
  • USB hard drive
  • Network Attached Storage (NAS)
  • Probably even “cloud” storage.

Flash Drive

Using a flash drive to store log files will keep power consumption to a minimum. A flash drive has the same write cycle limitation as the SD card but flash drives are relatively inexpensive and can be swapped out without having to transfer the operating system or programs.

The steps needed to make an attached storage device work reliably with the Pi include:

  1. Create a mount point for the storage device.This is a location in the file system that gives you a “path” to the attached storage.
  2. Set up the Pi to automatically mount the device at boot time.
  3. Move the log file to the attached storage and create a symbolic link from the the file's original location to the actual file on the attached storage.
  4. If using a flash drive, have a procedure to safely swap out the drive from time to time.

Create a Mount point:

To make a mount point named “flashdrive” at /media/flashdrive . Issue the command:

sudo mkdir /media/flashdrive

You can name the new mount point anything you like but be sure to substitute the name you give it in the following steps.

Mount a Storage device at Boot:

Find the ID of the storage device you will be using. Before you plug in the device, type on the Pi’s command line “df –h” (without the quotes). This will give you a listing of the memory usage on the Pi.

Plug in your storage device, wait a couple of seconds for the device to mount, then issue the “df –h” command again. You should now see the added device with its ID as something like “/dev/sda1”.

add the flash drive to the file system table (fstab) with the following commands:

sudo editor /etc/fstab

This opens the file in the editor. Move to the end of the file and add:

/dev/sda1 /media/flashdrive auto auto,user,rw,exec 0 0

Where “sda1” is the ID of the storage device and “flashdrive” is the name of the mount point you created.

Save the file and exit the editor.

Mount the drive (it will happen automatically from /etc/fstab on subsequent boots):

mount -a

The next time your Pi is booted up with the storage device plugged in you should see it in the “df –h” listing and you can use it just like any other directory on the system.

Create a Symbolic Link:

The instructions in this step are specific to the Python interval program running on an OpenSprinkler Pi but should be general enough to be used in other situations.

With your attached storage device mounted, move the log file from it’s original location to the attached storage device. In the case of the program, use the command:

sudo mv ./OSPi/static/log/water_log.csv /media/flashdrive/water_log.csv

Create a symbolic link from the file’s original location to the file on the attached storage with the command ln -s [/path/to/file] [/path/to/symlink] In this case:

ln -s /media/flashdrive/water_log.csv ./OSPi/static/log/water_log.csv

The logging program will now operate as if the file is still in its original location but will actually be writing to the attached storage device.

Swap Out the Flash Drive:

Unplugging a flash drive from a rev 2 Pi will cause the Pi to reboot, you can’t just unmount the attached flash drive and unplug it. It is recommended to shutdown the Pi and swap out the flash drive while the Pi is powered off. The safest way to do this is to issue the command:

sudo shutdown –h now

Once the Py has shutdown, turn off/disconnect the power and change the flash drive then restart the Pi.

USB Hard drive

A hard drive does not have the write cycle limitation but requires more power to operate than a solid state device.