Static IP Address on Raspberry Pi 2

Using Raspian, howto set a static IP Address on the Raspberry Pi 2.

cat /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

To set the Pi to a static IP address, you need to know the following:

Now you edit /etc/network/interfaces and replace iface eth0 inet dhcp with iface eth0 inet static and add the information as above.  Example below, replace with information to suit the network settings for your Pi.

sudo nano /etc/network/interfaces
pi@pi2 ~ $ cat /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet static
address 192.168.0.XXX

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Now reboot

sudo reboot

Installing Emoncms on Raspberrry Pi 2

Emoncms can do some great input processing and visual dashboards, along with many other things. Below is the process I used to install it on a Raspberry Pi 2 using Raspian.

Install Dependencies

Install all the dependencies.

sudo apt-get install apache2 mysql-server mysql-client php5 libapache2-mod-php5 php5-mysql php5-curl php-pear php5-dev php5-mcrypt git-core redis-server build-essential python-serial python-configobj python-pip python-dev

During this install, it will prompt you for further information. Install pecl dependencies (redis and swift mailer)

sudo pecl install redis
sudo pear channel-discover
sudo pear install log4php/Apache_log4php

Add pecl modules to php5 config

sudo sh -c 'echo "" > /etc/php5/apache2/conf.d/20-redis.ini'
sudo sh -c 'echo "" > /etc/php5/cli/conf.d/20-redis.ini'

Configure dependencies


Configure redis to run without logging or data persistence.

sudo nano /etc/redis/redis.conf

Comment out redis log file and all the redis saving.

# logfile /var/log/redis/redis-server.log
# save 900 1
# save 300 10
# save 60 10000

Then restart the Redis service

sudo /etc/init.d/redis-server start


Emoncms uses Apache to route requests, so modrewrite needs to be enabled.

sudo a2enmod rewrite

Modify the Apache2 config to allow rewrite.

sudo nano /etc/apache2/sites-enabled/000-default

Change (line 7 and line 11), AllowOverride None to AllowOverride All. Comment out line # CustomLog ${APACHE_LOG_DIR}/access.log combined Comment the access log to other-vhosts (add #)

sudo nano /etc/apache2/conf.d/other-vhosts-access-log

Now Reboot the Pi

sudo reboot

Install Emoncms

For the install Emoncms, it will be installed from the git repository for Emoncms. First, lets setup the web directory and permissions.

cd /var
sudo chown pi www

Download Emoncms from the git repository. For this we’re using a cloning a specialised version of Emoncms which has been designed to write less to the SD Card.

cd www
git clone -b bufferedwrite

Configure Emoncms


Lets setup the mysql database for Emoncms. If you would like to move the mysql database to another location (for easier backup,etc), now is the time to do it. For this, I’ve chosen to move the Mysql database to /home/pi/data/mysql. I first create the directory and then copy the mysql data accross.

mkdir /home/pi/data/mysql
sudo cp -rp /var/lib/mysql/. /home/pi/data/mysql

Now update the mysql configuration file to use the new location.

sudo nano /etc/mysql/my.cnf

change line datadir to /home/pi/data/mysql Create the Emoncms database.

mysql -u root -p

This will prompt you for a password and the following SQL will create the database.

mysql> CREATE DATABASE emoncms;

Now create a user and assign permissions that Emoncms will use to access the mysql databse. In the commands below, update username and password to suit your choice.

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON username . * TO 'username'@'localhost';

Data directory

Now we create some directories were the Emoncms’ feed engines (phpfina and phptimeseries) will store data. I chose /home/pi/data/.

mkdir /home/pi/data/phpfina
mkdir /home/pi/data/phptimeseries
sudo chown www-data:root /home/pi/data/phpfina
sudo chown www-data:root /home/pi/data/phptimeseries

Emoncms Configuration settings

Lets setup the configuration file for Emoncms. We copy the default.settings.php to settings.php.

cd /var/www/emoncms
cp default.settings.php settings.php

Now we update the settings to suit this installation of Emoncms.

nano settings.php

Update the following things: Database Settings. Update these to the username and password you created previously when setting up Mysql.

$username = "USERNAME";
$password = "PASSWORD";

If you chose a different location to store your data directory earlier, update these lines to suit your data directories.



Setup the feedwriter script as a service.  Copy the service script to /etc/init.d/, update permissions and enable for service.

sudo cp /var/www/emoncms/run/feedwriter /etc/init.d/
sudo chmod 755 /etc/init.d/feedwriter
sudo update-rc.d feedwriter defaults

Now go to a web browser and go to the emoncms website. Update the IP-Address below to your host.


Click Register and create yourself a new Admin user. After you have successfully logged in and do not want to create any other users for this Emoncms, it is best to disable the register functionality. To do this, we edit the settings again.

nano settings.php

And change $allowusersregister to FALSE.

$allowusersregister = FALSE;

As you have now gone through the initial setup for Emoncms, you can also disable the dbtest.

$dbtest = FALSE;


The following log entries should be created and help with this.

tail -f /var/log/feedwriter.log
tail -f /var/log/emoncms.log


Installation of Pywws on Raspberry Pi 2 for Weather Station

This is the process I followed to install pywws on a Raspberry Pi 2 on Raspian.  I have a 3080 compatible weather station connected to the Raspberry Pi 2 via USB.

Install Dependencies

As pywws is written in Python, it has a few dependencies which need to be installed on the Raspberry Pi 2.  I like to make sure that before this I update to the latest updates.

sudo apt-get update
sudo apt-get upgrade

Now install the python, libusb and gnuplot.  Libusb as we need to access the weather station via USB and gnuplot in order for draw graphs with pywws.

sudo apt-get install python-pip python-dev python-usb python-daemon gnuplot libusb-1.0.0

There are other dependencies if you want to use other features or pywws (eg. twitter), but these will give you the basics to get pywws up and running.  Other dependencies can be found here.

Install Pywws and permissions

After all the required software is installed, you can install pywws.

cd ~
sudo pip install pywws

After it has installed, plug in the internal weather station unit to the Raspberry Pi 2 via USB. Now you can test to ensure it can talk to the weather station.

sudo pywws-testweatherstation

This command should produce some output that looks like this.

pi@pi2 ~ $ sudo pywws-testweatherstation
22:09:41:pywws.Logger:pywws version 15.01.0
0000 55 aa df f9 00 27 15 01 28 09 36 15 01 28 11 37 05 20 02 53 11 00 00 00 10 00 00 c0 0c 00 d8 a0
0020 c8 27 f4 27 00 00 00 00 00 00 00 15 04 17 22 09 41 23 c8 00 00 00 45 2d 2c 01 64 81 c8 00 00 00
0040 64 40 64 80 a0 28 80 25 a0 28 80 25 03 c3 00 05 81 01 00 0a 00 f4 01 12 00 b9 c5 2d 0a 0f bf 63
0060 19 00 58 18 63 13 69 01 a0 00 76 01 e0 80 76 01 e0 80 09 01 fd 80 35 28 28 27 fa 27 ed 26 3d 00
0080 63 00 10 02 b0 04 98 0a 04 0e 42 0f 00 15 02 21 09 06 14 08 03 13 07 15 02 21 14 22 14 08 03 12
00a0 58 15 01 16 16 28 14 08 01 07 10 15 01 01 06 35 14 08 29 02 10 15 01 01 06 35 14 08 29 02 10 1d
00c0 01 27 11 14 15 02 08 01 23 14 08 06 20 28 15 01 16 16 23 14 08 06 20 28 15 01 16 16 23 14 11 11
00e0 05 08 15 02 07 13 23 15 02 08 02 00 15 02 21 00 00 15 02 22 00 00 15 03 01 00 00 15 02 27 09 33
pi@pi2 ~ $

If this does not work, further information is available at which may help. You have to run pywws in python using sudo. It is much better to run it as the user Pi and the give the Pi user access to the USB port. First create a group to grant the access and add the Pi user to the group

sudo addgroup --system weather
sudo adduser pi weather

You need to identify the idVendor and idProduct for your WeatherStation that is plugged into the USB port. Look into the kernal log file.

cat /var/log/kern.log | grep hid

Look for idVendor and idProduct, mine were idVendor=1941 and idProduct=8021. My kern.log look like this.

[254954.344198] hid-generic 0003:1941:8021.0003: hiddev0,hidraw0: USB HID v1.00 Device [HID 1941:8021] on usb-bcm2708_usb-1.3/input0

To modify the USB, create a rule that sets the usb port to the group weather

sudo nano /etc/udev/rules.d/39-weather-station.rules

Copy and paste the following into this file. Make sure you update the idVendor and idProduct to suit your USB.

ACTION!="add|change", GOTO="station_end"
SUBSYSTEM=="usb", ATTRS{idVendor}=="1941", ATTRS{idProduct}=="8021", GROUP="weather"

After you’ve saved and exit, reboot the Pi. Once the Pi has started again, test connecting to the weatherstation as user Pi, without sudo.

python -m pywws.TestWeatherStation

You should get the same results as you did above when you tested using the command with sudo. As we’ll be running this as user Pi, ensure that all the weather data is owned by user Pi.

sudo chown pi.pi -R /home/pi/data/weather

Configuration of Pywws

I wanted readings every 5 minutes from the weather station and as pywws can easily read and log the weather station data every 5 minutes (recommended setting is 5min too), this was ideal. So, I set the weather station to log every 5 minutes.

sudo pywws-setweatherstation -r 5

It’s worth noting that the memory in the internal unit is not very large and although it will buffer some data, it cannot hold very much weather data. Now to setup where to log the data.  I chose /home/pi/data/weather, but it could be anywhere.  If a different directory is used, update the url through the rest of this page to the select directory.

mkdir /home/pi/data
mkdir /home/pi/data/weather
mkdir /home/pi/data/weather/data

All the weather station’s logged data should go into this directory. As per the getting started doco above for pywws, we now need to setup the timezone and weather station type. We will need to run the following command and it will create the configuration files the first time it is run.

sudo python -m pywws.LogData -vvv /home/pi/data/weather/data

The configuration files are found in the data directory /home/pi/data/weather/data. I adjusted the following in the weather.ini configuration file.

ws type = 3080
logdata sync = 1
day end hour = 0
asynchronous = False

There is a full page in the doco about the weather.ini configuration settings here. I also update the configuration paths in the weather.ini file.

work = /tmp/weather
templates = /root/weather/templates/
graph_templates = /root/weather/graph_templates/
local_files = /root/weather/results/

My full weather.ini looks like this.

ws type = 3080
usb activity margin = 3.0
pressure offset = -4.4
logdata sync = 1
asynchronous = False
frequent writes = True
template encoding = iso-8859-1
gnuplot version = 4.2
gnuplot encoding = iso_8859_1
day end hour = 0
rain day threshold = 0.2

work = /tmp/weather
templates = /home/pi/data/weather/templates/
graph_templates = /home/pi/data/weather/graph_templates/
local_files = /home/pi/data/weather/results/

local site = False
site =
user = username
password = secret
directory = public_html/weather/data/
secure = False
port = 21

services = []
plot = []
text = []

services = []
text = []
plot = []

services = []
text = []
plot = []

[12 hourly]
services = []
text = []
plot = []

services = []
text = []
plot = []

Now create these directories and copy the pywws examples into them.

pi@pi2 ~/data/weather $ pywws-version -v
commit: 06fb57d
Python: 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3]
USB:    pywws.device_pyusb
pi@pi2 ~/data/weather $ cp -r /usr/local/lib/python2.7/dist-packages/pywws/examples/templates /home/pi/data/weather/templates
pi@pi2 ~/data/weather $ cp -r /usr/local/lib/python2.7/dist-packages/pywws/examples/graph_templates /home/pi/data/weather/graph_templates
pi@pi2 ~/data/weather $ mkdir /home/pi/data/weather/results
pi@pi2 ~/data/weather $ ls -al
total 24
drwxr-xr-x 6 pi pi 4096 Apr 19 23:22 .
drwxrwxrwx 6 pi pi 4096 Apr 16 22:01 ..
drwxr-xr-x 7 pi pi 4096 Apr 19 22:44 data
drwxr-xr-x 2 pi pi 4096 Apr 19 23:20 graph_templates
drwxr-xr-x 2 pi pi 4096 Apr 19 23:22 results
drwxr-xr-x 3 pi pi 4096 Apr 19 23:19 templates

Then we run the command again.

python -m pywws.LogData -vvv /home/pi/data/weather/data

This will probably need to run for a while to download the data. It should have some output similar to this.

pi@pi2 ~/data/weather/data $ sudo python -m pywws.LogData -vvv /home/pi/data/weather/data
23:32:54:pywws.Logger:pywws version 15.01.0
23:32:54:pywws.Logger:Python version 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3]
23:32:54:pywws.WeatherStation.CUSBDrive:using pywws.device_pyusb
23:32:56:pywws.DataLogger:weather station type appears to be incorrect
23:32:57:pywws.DataLogger:weather station type appears to be incorrect
23:32:57:pywws.DataLogger:Synchronising to weather station
23:32:57:pywws.weather_station:delay 3, pause 17.6943
23:33:16:pywws.weather_station:avoid 5.72013122047
23:33:22:pywws.weather_station:live_data new data
23:33:22:pywws.DataLogger:Reading time 12:33:19
23:33:22:pywws.DataLogger:log time 12:29:37
23:33:22:pywws.DataLogger:Fetching data

So now that the weather station data can be downloaded, I want to setup a service that will run to download the weather data. There are two options, an hourly update or a live logger. I opted for the live logger as I wanted data updated as close to every 5min as possible. Pywws comes with a livelogger than can run as a daemon.  It can be run like this.

pywws-livelog-daemon -v /home/pi/data/weather/data /home/pi/data/weather/data/livelogger.log start

While this works well, it will not start automatically. A service init script is much easier to manage for me. Create a new init.d file by running the following.

sudo nano /etc/init.d/pywws

Now copy and paste the following into this file. Update the DAEMON, DATADIR and LOGFILE to suit your installation.


# Provides:        pywws-livelog-daemon
# Required-Start:  $all
# Required-Stop:   $all
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start pywws daemon


. /lib/lsb/init-functions


UGID=$(getent passwd $DAEMONUSER | cut -f 3,4 -d:) || true

. /lib/lsb/init-functions

case $1 in
        log_daemon_msg "Starting pywws service" "pywws"
        if [ -z "$UGID" ]; then
                log_failure_msg "user \"$DAEMONUSER\" does not exist"
                exit 1
        sudo -u $DAEMONUSER $DAEMON -v $DATADIR $LOGFILE start
        log_end_msg $status
        log_daemon_msg "Stopping pywws service" "pywws"
        sudo -u $DAEMONUSER $DAEMON -v $DATADIR $LOGFILE stop
        log_end_msg $?
        $DAEMON -v $DATADIR $LOGFILE restart
        if $0 status >/dev/null; then
                $0 restart
                exit 0
        exit 3
        status_of_proc $DAEMON "pywws service"
        echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
        exit 2

Once the init script has been saved, it needs to be made executable and added to rc.d so it will start and stop at startup and shutdown.

sudo chmod +x /etc/init.d/pywws
sudo update-rc.d pywws defaults

You can also start/stop pywws as a service.

sudo service pywws start
sudo service pywws stop


Weather Station

I recently purchased a weather station from ebay, model WS-3081.  The weather station has an internal and external unit and transmits via wireless between them – 433Mhz.  The internal unit runs on batteries and although unverified, it appears like it will also run off power supplied via USB.  The external unit has rechargeable batteries and a solar panel to recharge the batteries.

WH3080 Weather StationThe weather station can measure wind speed, wind direction, rainfall, temperature, humidity, solar UV and lux from the external unit.  The internal unit can measure temperature, humidity and pressure.

This is great, but I want to a way to get that information into the computer so that I can more interesting things with it.  The weather station comes with Windows software, which will let you download the data over USB to the internal unit.  I had a look at this and although it works, it doesn’t give me the flexibility I want to download the data quickly, easily and export in an automated way.

Further posts coming on the weather station as I work on getting it automated.

Raspberry Pi 2 Node-Red setup

In order to setup node-red, node.js is required.  Node.js runs on javascript and is one of the building blocks of node-red.  The install processes for Raspberry Pi and Raspberry Pi 2 are different and have different instructions.  Below are instructions for Raspberry Pi 2 and I have tested this process on Raspian wheezy 2015-20-16.  These are my own notes for my own purposes, but hopefully somebody else will find them useful too.

Before running these, update Raspian first.

sudo apt-get update
sudo apt-get upgrade


To install node-red for the Raspberry Pi 2, the node-red guys have made it much easier.

Run the following command. This will update the raspian package cache and the source list.

curl -sL | sudo bash -

Now to install all the packages.

sudo apt-get install git-core screen build-essential python-dev python-rpi.gpio nodejs

Verify that node and npm have installed correctly. Run the following.

node -v
npm -v

You should get results similar to the following.

pi@pi2 ~ $ node -v
pi@pi2 ~ $
pi@pi2 ~ $ npm -v
pi@pi2 ~ $

Once these are installed, run the following command to install node-red.

sudo npm install -g --unsafe-perm node-red

This takes quite some time.

I got a few warnings and the build of the node-icu-charset-detector node failed completely, however node-red still built successfully.

Now to test node-red. Run the following to start node-red from the command line.

node-red-pi --max-old-space-size=64 -v

Node-Red should start and should look something like this.

pi@pi2 ~ $ node-red-pi --max-old-space-size=64 -v

Welcome to Node-RED

16 Apr 20:12:47 - [info] Node-RED version: v0.10.6
16 Apr 20:12:47 - [info] Node.js  version: v0.10.38
16 Apr 20:12:47 - [info] Loading palette nodes
16 Apr 20:12:52 - [warn] ------------------------------------------
16 Apr 20:12:52 - [warn] [arduino] Error: Cannot find module 'arduino-firmata'
16 Apr 20:12:52 - [warn] [redisout] Error: Cannot find module 'redis'
16 Apr 20:12:52 - [warn] [mongodb] Error: Cannot find module 'mongodb'
16 Apr 20:12:52 - [warn] ------------------------------------------
16 Apr 20:12:52 - [info] User Directory : /home/pi/.node-red
16 Apr 20:12:52 - [info] Flows file     : /home/pi/.node-red/flows_pi2.json
16 Apr 20:12:52 - [info] Server now running at
16 Apr 20:12:52 - [info] Starting flows
16 Apr 20:12:52 - [info] Started flows

You should get the node-red web interface by going to the following page in your web browser.

http://<Pi 2 IP Address>:1880

Now to install an initialisation script so that node-red can run as a service and start/stop automatically on startup/shutdown.  Create a new init.d file by running the following.

sudo nano /etc/init.d/node_red

Then copy and paste the script below into this file and save.  This script will run node-red as the user pi.  It’s very simliiar to the Adafruit (link below) except updated for the new installs for Raspberry Pi 2.

#! /bin/sh
# Starts and stops Node-RED
# /etc/init.d/node_red
# Provides:     node_red
# Required-Start:       $syslog
# Required-Stop:        $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Node-RED initialisation
# Note: this runs as the user called pi


#Load up node red when called
case "$1" in

        echo "Starting Node-Red.."
        su -l pi -c "screen -dmS red node-red-pi --max-old-space-size=64 -v"
        echo `screen -ls red | sed -n '2p' | cut -f1 -d.` > $PIDFILE

        echo "Stopping Node-Red.."
        su -l pi -c "screen -S red -X quit"

        echo "Restarting Node-Red.."
        $0 stop
        $0 start
        echo "Usage: $0 {start|stop|restart}"
        exit 1

Once the init script has been saved, it needs to be made executable and added to rc.d so it will start and stop at startup and shutdown.

sudo chmod +x /etc/init.d/node_red
sudo update-rc.d node_red defaults

You can also start/stop/restart the node-red service using this script.

sudo service node_red start
sudo service node_red stop
sudo service node_red restart

node-red addons

There are many addons for node-red.  Some of my favorites are ping, wol, emoncms, prowl, mysql and pushbullet – but there is many others. To install them, use npm from the node-red directory.  The following will install my favorite addons.

sudo service node_red stop
cd /usr/lib/node_modules/node-red
sudo npm install node-red-node-pushbullet
sudo npm install node-red-node-wol
sudo npm install node-red-node-ping
sudo npm install node-red-node-prowl
sudo npm install node-red-node-emoncms
sudo npm install node-red-node-mysql
sudo service node_red start

There are many other addons and you can find other packages at


I found that the install didn’t always successfully compile all addons. If there is an addon you expect to see in the node-red GUI that is not there, you can enable more verbose logging.  It should tell you if it is getting any errors or if it cannot load a specific module.  The below will run node-red manually from the command line.  Ctrl-C to break out.

sudo service node_red stop
node-red-pi --max-old-space-size=64 -v

Useful node-red sites & references
Adafruit has a very nice guide for setting up node-red on a Raspberry Pi (version1). Very useful.
Has many npm packages which can be installed.
The Node-Red site.
Interesting demo. Twitter, Mongo REST API and sentiment Analytics in 3 minute demo.
The Git Hub repository for Node-Red.