Set Up RPi, RTC, WiFi, Data Log

= Download Location =

http://www.raspberrypi.org/downloads

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

= Installation = Please refer to the instructions on http://www.raspberrypi.org

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:
 * http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
 * http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/set-rtc-time

Edit sudo editor /etc/modules And add the following: i2c-bcm2708 i2c-dev rtc-ds1307 Add the modules to the kernel (they will automatically be added on subsequent boots from ): modprobe i2c-bcm2708 modprobe i2c-dev modprobe rtc-ds1307 Comment out both  &amp;   from Install sudo apt-get install i2c-tools Look for ID #68 with 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 &gt; /sys/class/i2c-adapter/i2c-0/new_device On a 512MB Raspberry Pi Model B: echo ds1307 0x68 &gt; /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  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 : sudo editor /etc/rc.local and add the following before : On a 256MB Raspberry Pi Model B:  echo ds1307 0x68 &gt; /sys/class/i2c-adapter/i2c-0/new_device hwclock -s On a 512MB Raspberry Pi Model B: echo ds1307 0x68 &gt; /sys/class/i2c-adapter/i2c-1/new_device hwclock -s

= Hardware watchdog = The following is collected from:
 * http://pi.gadgetoid.com/post/001-who-watches-the-watcher

Install sudo apt-get install watchdog

Uncomment the following line in  watchdog-device = /dev/watchdog

Edit sudo editor /etc/modules And add the following: bcm2708_wdog
 * 1) 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 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 &lt;IDString&gt; inet static address &lt;ip address&gt; netmask &lt;netmask&gt; gateway &lt;gateway address&gt;

Connect to the wifi network through the GUI or wpa_cli or directly edit  and then restart the networking service. In all cases make sure you set the  to the same as in   or you will not get a static address. If editing the conf file it will look similar to this for a WPA PSK network: network={ ssid="&lt;SSID&gt;" psk="&lt;presharedkey&gt;" proto=RSN key_mgmt=WPA-PSK pairwise=CCMP auth_alg=OPEN id_str="&lt;IDString&gt;" }

/etc/network/interfaces method
Edit sudo editor /etc/network/interfaces Change the wlan0 interface type from manual to static and entering the following: iface wlan0 inet static address &lt;ip address&gt; netmask &lt;netmask&gt; gateway &lt;gateway address&gt; wpa-ssid "&lt;SSID&gt;" wpa-psk	"&lt;presharedkey&gt;" 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 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: *.* @192.168.26.6:514

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

= 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 “ ” (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 “ ” 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  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 “ ” 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 ospi.py 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 ospi.py 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  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.