Friday 11 October 2013

My first Raspberry Pi - day 4

Add normal user account

On other computers in my home network I have the user name "jim". For convenient logging into the Pi it's useful to have an account with the same name. This is easily created as follows (when logged in as root):
adduser jim
mkdir /home/jim/.ssh
chmod go-rx /home/jim/.ssh
cp /home/pi/.ssh/authorized_keys /home/jim/.ssh
chown -R jim:jim /home/jim/.ssh
Now I can login from my main computer with a simple ssh firefly command.

It's convenient to allow this account to run commands as root using sudo. This requires editing the /etc/sudoers file, which can only be done via the visudo command. I duplicated the last line in the file (for user "pi") and changed the user name to "jim".

Samba setup

Although most of the machines on my home network are running Linux of one sort or another it's still convenient to use samba to share files across the network. Enabling samba on the Pi is fairly straightforward, although there are too many configuration options, as in all samba setups. Firstly, install the samba packages:
sudo apt-get install samba samba-common-bin
Now edit the /etc/samba/smb.conf file to suit your network. I changed the following lines:
[global]
   workgroup = HOME
   interfaces = 127.0.0.0/8 eth0 wlan0
   security = user
   domain logons = no
   load printers = no
   domain master = no
[homes]
   read only = no
I also commented out the [printers] section.

After editing the configuration, the samba server needs to be restarted and a password for my user name created:
sudo service samba restart
sudo smbpasswd -a jim

Command aliases

There are some variations of standard Linux commands that I expect to find wherever I log in. These are easily added to all users' accounts by editing /etc/profile and adding the following lines:
alias df='df -h'
alias la='ls -ao'
alias ll='ls -l'

Weather station setup

I have a weather station with a USB interface which I've been using with another computer for several years to collect weather data and upload it to various web sites. The Pi is an ideal platform for doing this sort of thing as it uses little enough power that I don't mind it being on all the time. I've written some software called pywws that is well suited to small computers.

The first step is to create a suitable directory and download the pywws software:
mkdir weather
cd weather
git clone https://github.com/jim-easterbrook/pywws.git
Next connect up the weather station and make sure it's detected by running lsusb:
jim@firefly ~/weather $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 1a40:0201 Terminus Technology Inc. FE 2.1 7-port Hub
Bus 001 Device 005: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 006: ID 058f:6390 Alcor Micro Corp. USB 2.0-IDE bridge
Bus 001 Device 007: ID 1941:8021 Dream Link WH1080 Weather Station / USB Missile Launcher
Before we can read anything from the station we need to install a Python USB library. There is a choice of library to use with pywws, but in general "pyusb" is the best one to use. Different Linux distributions use different names for this package, so the safest thing to do is to use apt-cache to search for it:
jim@firefly ~/weather $ apt-cache search usb | grep -i python
python-ftdi - Python module to control and program the FTDI USB controller
python-imobiledevice - Library for communicating with iPhone and iPod Touch devices
python-obexftp - Python binding to the object exchange file transfer library
python-ow - Dallas 1-wire support: Python bindings
python-usb - USB interface for Python
python-usbtc08 - Python wrapper for libusbtc08
This shows that python-usb is the one we want, so install it with sudo apt-get install python-usb.

Now we can test the weather station link:
jim@firefly ~/weather $ cd pywws/
jim@firefly ~/weather/pywws $ sudo python -m pywws.TestWeatherStation -v
12:04:06:pywws.WeatherStation.CUSBDrive:using pywws.device_pyusb
0000 55 aa ff ff ff ff ff ff ff ff ff ff ff ff ff ff 05 20 01 51 11 00 00 00 81 00 00 e5 07 00 d0 f6
0020 17 0c 17 0c 00 00 00 00 00 00 00 13 10 11 12 01 41 23 c8 00 32 80 47 2d 2c 01 2c 81 5e 01 1e 80
0040 a0 00 c8 80 a0 28 80 25 a0 28 80 25 03 36 00 05 6b 00 00 0a 00 f4 01 18 00 00 00 00 00 00 00 00
0060 00 00 55 1c 63 0a 2f 01 59 00 a2 01 59 80 a2 01 59 80 fb 00 a1 80 77 54 00 00 fe ff 00 00 ce 01
0080 0c 02 d0 ff d3 ff 5a 24 d2 24 dc 17 00 12 07 17 23 23 10 03 07 22 18 10 08 11 08 30 11 03 07 12
00a0 36 08 07 24 17 17 13 01 18 23 28 12 07 16 14 21 12 02 11 06 57 12 07 16 14 21 12 02 11 06 57 12
00c0 08 19 08 46 13 10 11 10 55 13 07 14 16 39 11 09 13 17 19 11 08 21 16 53 11 09 13 17 19 12 07 16
00e0 14 21 10 02 22 11 06 11 11 06 13 12 11 11 06 13 12 11 11 10 11 38 11 11 10 11 38 10 02 22 14 43
Note the use of sudo - the normal user "jim" currently doesn't have permission to access the weather station USB device. This requires creating a "udev" rule to give access to the station to a particular group of users, and adding "jim" to that group:
sudo addgroup --system weatherstation
sudo adduser jim weatherstation
sudo vi /etc/udev/rules.d/39-weather-station.rules
The udev rule file should have these three lines:
ACTION!="add|change", GOTO="weatherstation_end"
SUBSYSTEM=="usb", ATTRS{idVendor}=="1941", ATTRS{idProduct}=="8021", GROUP="weatherstation"
LABEL="weatherstation_end"
After rebooting the normal user "jim" can run TestWeatherStation.

The rest of the setup follows the pywws documentation. I installed the following packages:
sudo apt-get install gnuplot
sudo apt-get install python-oauth2
sudo apt-get install gettext python-sphinx
The version of python-tweepy installed by apt-get is out of date (it doesn't work with the current Twitter API) so I installed a more recent version using pip:
sudo apt-get install python-pip
sudo pip install tweepy

No comments: