Monthly Archives: November 2013

Raspberry Pi and USB weather station hints and tips

In a previous post I described construction of a internet weather station using an off-the-shelf Maplins weather station and a Raspberry Pi. In this post I will take you through some of the lessons learned in the first couple of weeks operations.

Issues

1. Network

I very quickly ditched the WiFi dongles completely and went back to a Ethernet cable via a homeplug socket. Firstly, this seems much more stable and secondly it overcomes any power/overheating issues. I’ve got some of these, others brands and speeds are available: TP-Link TL-PA211KIT AV200 Nano 200Mbps Powerline Adapter – Twin Pack.

2. Backups

There are three levels of backup that you probably want:
a. Daily backups of the data collected.
b. Regular backups of code etc. that you have been tinkering with.
c. Irregular comes of your complete SD card.

3. Where to store your data?

The default place to store your whether station data is of course on your SD card mount in your Raspberry Pi. There are three good reasons not to do this:

  1. SD cards are a generally one of the more flaky storage options;
  2. SD cards can be written to a limited number of times. If you are saving data to it every few minutes I think that the SD card lifetime will be measured in months.
  3. If there is a power outage when the SD card is being written to then you are likely to end up with a corrupted card, i.e. one you can’t boot from without either fixing the corruption (usually seems to be impossible) or doing a complete reinstall (tedious, but perhaps easier if you have the backups mentioned above).

The conclusion is that it is better to store your weather data elsewhere, the obvious places being a USB stick or a network drive. Note that connecting a USB device will draw current, so you need to limit this, a stick should be OK.

Solutions

How to add a USB stick

1. Insert the stick.2. See what it is called, almost certainly /dev/sda1, the following command will also tell you what the file system type is:

sudo fdisk -l

3. To mount and unmount the stick:

sudo mkdir /mnt/usbdrive
sudo mount /dev/sda1 /mnt/usbdrive

4. You need to make sure that you always unmount the drive before you remove it, or before you format it (assuming you need to):

sudo umount /dev/sda1

5. The best format for the stick is going to be EXT4 which is the native format for the Raspberry Pi, with the stick unmounted do:

sudo mkfs.ext4 /dev/sda1 -L volumenamewhateveryoulike

6. Now, rather than having to mount the stick every time that you reboot you can edit the fstab file:

sudo nano -Bw /etc/fstab

7. Do not edit anything that is there already, but add the following line:

/dev/sda1 /mnt/usbdrive auto defaults,user 0 1

8. Now the USB stick will mount at /mnt/usbdrive whenever the Pi reboots, to mount it now without rebooting use the mount command with the auto option:

sudo mount -a

9. If you are using this for your weather station data like me then copy the whole of your weather data directory to the stick and remember to specify the new location!

mkdir /mnt/usbdrive/weather
cp -r data /mnt/usbdrive/weather

How to add a network drive

This is similar to adding the usb stick above:

cd /mnt
sudo mkdir MyBookLive
sudo mkdir MyBookLive/RaspberryPi
sudo nano /etc/fstab

Then add the following line:

//192.168.1.64/Backups /mnt/Mybooklive/Backups cifs username=your_username,password=your_password 0 0

Please note, no spaces around the comma between username and password!
Now just do:

sudo mount -a

and away you go.

Automating a backup script

You’ll need a script to do the job and you will need to schedule a cron job. I’ll use the three backup examples above to illustrate.

a. Daily backups of the data collected.

Move old backup:

sudo mv /mnt/Mybooklive/Backups/RaspberryPi/weatherdatabackup.tar.gz /mnt/Mybooklive/Backups/RaspberryPi/previousweatherdatabackup.tar.gz

Create a tar file:

sudo tar -czvf /mnt/Mybooklive/Backups/RaspberryPi/weatherdatabackup.tar.gz /mnt/usbdrive/weather

Done!
Now, add those two lines to a script (e.g. /home/pi/scripts/backupweatherdata.sh) and make the script executable:

chmod oug+x /home/pi/scripts/backupweatherdata.sh

Add a line to your cron file:

crontab -e

To execute the backup at 6 am every day add the line:

0 6 * * * sudo sh /home/pi/scripts/backupweatherdata.sh

b. Regular backups of code etc. that you have been tinkering with.

Having written the backup for a. it should now be easy for you to do b. with the same principles.

c. Irregular comes of your complete SD card.

OK, you may want a complete copy of your SD card just in case it every gets corrupted. Elsewhere on you may find third party software running under Windows/Linux or even Mac to clone the SD card, but assuming that you’ve got your network drive mounted you can do it straight from the Pi itself:

sudo dd if=/dev/mmcblk0p6 of=/mnt/Mybooklive/Backups/RaspberryPi/RPiSDCardBackup.img bs=1M

Please don’t get the if= and of= the wrong way round as doing this may make you cry. To restore, you’d just swap the if and of.