Server Pi on the side, Node.js, Python or PHP?

I’m currently in two minds wether to make use of Node.js, Python or even PHP to control my GPIO on my Raspberry Pi.

How to install Node.js on the Raspberry Pi

This actually vary simple, since precompiled binaries are now available. Previously it would take around 90min to compile Node.js from source on the pi.

sudo apt-get install nodejs npm

If everything went according to plan, you should be able to run:

$ node --version
v0.8.19
$ npm --version
1.2.2

Backup Raspberry Pi SD Card on Mac OSX

On Mac, you can also use the standard dd tool. Use df -h to determine which disk device your SD card is allocated. Where /dev/rdiskx is your SD card.

dd if=/dev/rdiskx of=/path/to/image bs=1m

Once your SD image is created, which will reflect the exact size of your SD card, it is advisable to compress (zip it) to save on space.

To restore the image back to the SD card at a later stage, similarly the dd tool can be used like this:

dd bs=1m if=/path/to/image of=/dev/rdiskx

Please note: These commands will need sudo access. You also need to unmount any mounted SD card partitions before proceeding with the restore. This can be done by making use of the umount command.

Also, you can check on progress at any time by pressing Ctrl+T. This will provide output that looks like this:

load: 0.87 cmd: dd 21591 uninterruptible 0.00u 0.16s
176+0 records in
176+0 records out
184549376 bytes transferred in 4.826822 secs (38234137 bytes/sec)

 

Multiple instances of omxplayer as background processes

I originally bought myself a raspberry pi for all sorts of automation needs, and as a possible means to replace the arduinos that I have running.

My first project was to get XBMC running and make use of its player to playback 720p rtsp streams from my IP cameras. This worked well, but was excessive and only allowed the playback of a single stream at a time. I was in need of a solution that allowed me to present multiple streams simultaneously and making use of as GPU acceleration as much as possible.

Finally, I reverted back to running the pure Rasbian OS for the Raspberry Pi and additionally installing the latest OMXPlayer builds. These are available from here.

Initially, this worked very well as I was able to get the OMXPlayer to present the live stream over HDMI with relative ease. It was also possible to scale and present this video in a specific area on screen.

omxplayer.bin --win x1 y1 x2 y2 rtsp://ip_address/live

However, OMXPlayer only allows a single instance be run from a session as it requires a interactive console to be alive. This was solved easily by making use of the screen function. The example below illustrates how to create a 2×2 matrix (four rtsp streams) for display on a 1920×1080 capable display. My raspberry pi averages around 15% CPU utilisation in this configuration.

screen -dmS camera1 sh -c 'omxplayer --win "0 0 960 540" rtsp://ip_address/live; exec bash'
screen -dmS camera2 sh -c 'omxplayer --win "960 0 1920 540" rtsp://ip_address/live; exec bash'
screen -dmS camera3 sh -c 'omxplayer --win "0 540 960 1080" rtsp://ip_address/live; exec bash'
screen -dmS camera4 sh -c 'omxplayer --win "960 540 1920 1080" rtsp://ip_address/live; exec bash'

From a bash scripting perspective, it is then easy to control, create and kill.

screen -X -S camera3 kill

An alternative approach would be

nohup omxplayer --win "0 0 960 540" rtsp://ip_address/live &
...

and to stop

killall omxplayer.bin

In a future post, I will explore cycling of feeds, dynamic allocation for any size grid, rendering of text and capturing motion.