Upgrading Nodejs on Raspberry Pi 2

I read about using the n helper for node to perform an upgrade, however this did not work for me.  Instead, I added another source and upgraded with apt-get.

If you see another way which consistently works, please let me know.  This worked for me, I had version 0.10 and this upgraded me to 0.12.

NOTE: I ran this as root, as sudo did not work.

You may want to perform an upgrade of packages before you do this.

sudo bash
 curl -sL https://deb.nodesource.com/setup_0.12 | bash -
 exit

The process should look something like this.

pi@central:~ $ sudo bash
root@central:/home/pi# curl -sL https://deb.nodesource.com/setup_0.12 | bash -

## Installing the NodeSource Node.js 0.12 repo...

## Populating apt-get cache...

+ apt-get update
 Hit http://archive.raspberrypi.org jessie InRelease
 Get:1 http://mirrordirector.raspbian.org jessie InRelease [15.0 kB]
 Hit https://deb.nodesource.com jessie InRelease
 Hit https://deb.nodesource.com jessie/main Sources
 Hit http://archive.raspberrypi.org jessie/main armhf Packages
 Hit https://deb.nodesource.com jessie/main armhf Packages
 Hit http://archive.raspberrypi.org jessie/ui armhf Packages
 Get:2 http://mirrordirector.raspbian.org jessie/main armhf Packages [8,963 kB]
 Get:3 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Hit http://archive.raspberrypi.org jessie/staging armhf Packages
 Get:4 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:5 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Get:6 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:7 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Get:8 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:9 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Get:10 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:11 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Ign https://deb.nodesource.com jessie/main Translation-en_AU
 Get:12 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Ign https://deb.nodesource.com jessie/main Translation-en
 Ign http://archive.raspberrypi.org jessie/main Translation-en_AU
 Ign http://archive.raspberrypi.org jessie/main Translation-en
 Ign http://archive.raspberrypi.org jessie/staging Translation-en_AU
 Ign http://archive.raspberrypi.org jessie/staging Translation-en
 Ign http://archive.raspberrypi.org jessie/ui Translation-en_AU
 Get:13 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [37.5 kB]
 Ign http://archive.raspberrypi.org jessie/ui Translation-en
 Get:14 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [70.3 kB]
 Get:15 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1,356 B]
 Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
 Ign http://mirrordirector.raspbian.org jessie/main Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/main Translation-en
 Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
 Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
 Fetched 9,088 kB in 31s (285 kB/s)
 Reading package lists... Done

## Confirming "jessie" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_0.12/dists/jessie/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
 OK

## Creating apt sources list file for the NodeSource Node.js 0.12 repo...

+ echo 'deb https://deb.nodesource.com/node_0.12 jessie main' > /etc/apt/sources.list.d/nodesource.list
 + echo 'deb-src https://deb.nodesource.com/node_0.12 jessie main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
 Hit http://mirrordirector.raspbian.org jessie InRelease
 Hit http://archive.raspberrypi.org jessie InRelease
 Get:1 https://deb.nodesource.com jessie InRelease [3,913 B]
 Hit http://mirrordirector.raspbian.org jessie/main armhf Packages
 Get:2 https://deb.nodesource.com jessie/main Sources [777 B]
 Hit http://archive.raspberrypi.org jessie/main armhf Packages
 Get:3 https://deb.nodesource.com jessie/main armhf Packages [976 B]
 Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages
 Hit http://archive.raspberrypi.org jessie/ui armhf Packages
 Get:4 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages
 Get:5 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Hit http://archive.raspberrypi.org jessie/staging armhf Packages
 Get:6 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages
 Get:7 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:8 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Get:9 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:10 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Get:11 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Get:12 https://deb.nodesource.com jessie/main Translation-en_AU [162 B]
 Ign https://deb.nodesource.com jessie/main Translation-en_AU
 Get:13 https://deb.nodesource.com jessie/main Translation-en [162 B]
 Ign https://deb.nodesource.com jessie/main Translation-en
 Ign http://archive.raspberrypi.org jessie/main Translation-en_AU
 Ign http://archive.raspberrypi.org jessie/main Translation-en
 Ign http://archive.raspberrypi.org jessie/staging Translation-en_AU
 Ign http://archive.raspberrypi.org jessie/staging Translation-en
 Ign http://archive.raspberrypi.org jessie/ui Translation-en_AU
 Ign http://archive.raspberrypi.org jessie/ui Translation-en
 Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
 Ign http://mirrordirector.raspbian.org jessie/main Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/main Translation-en
 Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
 Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_AU
 Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
 Fetched 5,666 B in 17s (319 B/s)
 Reading package lists... Done

## Run `apt-get install nodejs` (as root) to install Node.js 0.12 and npm

Then use apt-get to perform the upgrade.

sudo apt-get upgrade

The upgrade should look similar to this.

pi@central:/home/pi# sudo apt-get upgrade
 Reading package lists... Done
 Building dependency tree
 Reading state information... Done
 Calculating upgrade... The following packages were automatically installed and are no longer required:
 libasn1-8-heimdal libc-ares2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal
 Use 'apt-get autoremove' to remove them.
 Done
 The following packages have been kept back:
 pypy-upstream raspberrypi-ui-mods
 The following packages will be upgraded:
 libimlib2 nodejs
 2 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
 Need to get 6,757 kB of archives.
 After this operation, 7,867 kB of additional disk space will be used.
 Do you want to continue? [Y/n] y
 Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libimlib2 armhf 1.4.6-2+deb8u2 [150 kB]
 Get:2 https://deb.nodesource.com/node_0.12/ jessie/main nodejs armhf 0.12.13-1nodesource1~jessie1 [6,608 kB]
 Fetched 6,757 kB in 9s (677 kB/s)
 (Reading database ... 135296 files and directories currently installed.)
 Preparing to unpack .../libimlib2_1.4.6-2+deb8u2_armhf.deb ...
 Unpacking libimlib2 (1.4.6-2+deb8u2) over (1.4.6-2+deb8u1) ...
 Preparing to unpack .../nodejs_0.12.13-1nodesource1~jessie1_armhf.deb ...
 Detected old npm client, removing...
 Unpacking nodejs (0.12.13-1nodesource1~jessie1) over (0.10.44-1nodesource1~jessie1) ...
 Processing triggers for man-db (2.7.0.2-5) ...
 Setting up libimlib2 (1.4.6-2+deb8u2) ...
 Setting up nodejs (0.12.13-1nodesource1~jessie1) ...
 Processing triggers for libc-bin (2.19-18+deb8u4) ...

 

As I use node for node_red, I restarted node_red after the upgrade.  I experienced no issues with node_red after the upgrade to 0.12.

 

UPDATE 29 April 2016: Upgrading to other newer versions of node.js works this way too.  I recently just upgraded to v6 this same way, just use a different url.  You can find the urls for different versions here.  Although make sure that you check dependencies for other programs which depend on it, like node_red.

 

 

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:
address
netmask
network
broadcast
gateway

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
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1

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 pear.apache.org/log4php
sudo pear install log4php/Apache_log4php

Add pecl modules to php5 config

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

Configure dependencies

Redis

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

Apache2

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 https://github.com/emoncms/emoncms.git

Configure Emoncms

Mysql

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.

        'phpfina'=>array(
            'datadir'=>'/home/pi/data/phpfina/'
        ),

        'phptimeseries'=>array(
            'datadir'=>'/home/pi/data/phptimeseries/'

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.

http://IP-Address/emoncms/

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;

Monitoring/Debugging/Troubleshooting

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

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

References

https://github.com/emoncms/emoncms/blob/master/readme.md
https://github.com/emoncms/emoncms/blob/bufferedwrite/docs/install.md
http://emoncms.org/

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 http://jim-easterbrook.github.io/pywws/doc/en/html/guides/getstarted.html 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"
LABEL="station_end"

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.

[config]
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

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

[ftp]
local site = False
site = ftp.username.your_isp.co.uk
user = username
password = secret
directory = public_html/weather/data/
secure = False
port = 21

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

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

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

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

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

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

pi@pi2 ~/data/weather $ pywws-version -v
15.01.0
commit: 06fb57d
Python: 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3]
USB:    pywws.device_pyusb
examples:
   /usr/local/lib/python2.7/dist-packages/pywws/examples
docs:
   http://jim-easterbrook.github.com/pywws/
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.

#!/bin/sh

### BEGIN INIT INFO
# 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
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

DAEMON=/usr/local/bin/pywws-livelog-daemon
DAEMON_NAME=pywws
DATADIR=/home/pi/data/weather/data
LOGFILE=$DATADIR/livelogger.log

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

. /lib/lsb/init-functions

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

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

References

http://www.weather-watch.com/smf/index.php?topic=39257.0
https://jim-easterbrook.github.io/pywws/doc/en/html/index.html
https://groups.google.com/forum/#!topic/pywws/r9KfWM_AgKY
https://github.com/borpin/pywws-scripts/blob/master/pywws-livelog

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

Installation

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 https://deb.nodesource.com/setup | 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
v0.10.38
pi@pi2 ~ $
pi@pi2 ~ $ npm -v
1.4.28
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 http://127.0.0.1:1880/
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
### BEGIN INIT INFO
# 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
### END INIT INFO
# Note: this runs as the user called pi

PIDFILE=/var/run/nodered.pid

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

start)
        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
;;

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

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

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 https://www.npmjs.com/

Troubleshooting

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

https://learn.adafruit.com/raspberry-pi-hosting-node-red/setting-up-node-dot-js
Adafruit has a very nice guide for setting up node-red on a Raspberry Pi (version1). Very useful.

https://www.npmjs.com/
Has many npm packages which can be installed.

http://nodered.org/
The Node-Red site.

https://www.youtube.com/watch?v=f5o4tIz2Zzc
Interesting demo. Twitter, Mongo REST API and sentiment Analytics in 3 minute demo.

https://github.com/node-red/node-red
The Git Hub repository for Node-Red.