Sunday, April 5, 2015

Wednesday, March 4, 2015

Raspy Http Server running on startup

Challenge

So we want to run a simple http server that will start running when the Raspberry Pi starts up?

Install node.js on Pi


Follow these simple instructions to install node.js on pi

wget http://node-arm.herokuapp.com/node_latest_armhf.deb 
sudo dpkg -i node_latest_armhf.deb

Test:

node --version

On your Pi create a folder called ~/nodejs.

Create a file called https.js on Mac with the following:

var http = require('http');
var number = 0;

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  var num = number.toString();
  res.end('Hello World ' + num + '\n' );
}).listen(1337, 'xxx.x.x.x');

var interval = setInterval(function(){
  number++; 
},2000);

console.log('Server running at http://xxx.x.x.x:1337/');

where xxx.x.x.x is the IP address of your Raspberry Pi. This server will increment a number every 2 seconds indefinitely and publish it.

Copy the file https.js to Pi's ~/nodejs folder as follows:

scp ./https.js pi@raspberrypi:~/nodejs
            ..or..
scp ./https.js pi@xxx.x.x.x:~/nodejs

On your pi edit the file /etc/rc.local. Add the following line:

su pi -c 'node /home/pi/nodejs/https.js'

This will run the node command at startup as the user pi (who installed node.js).

This way your http server will start running as soon as your Pi restarts.

To test on a browser

http://xxx.x.x.x:1337/

This will show a page as follows. The number will increment by one at each refresh at 2 second interval.

Hello World 905

Thanks to Pieter Beulque's tips here.

Monday, March 2, 2015

Quick and dirty Http client/server code with node.js

Http Server (sampler)

This node.js Http server samples a metric and serves it at regular intervals.

var http = require('http');
var number = 0;

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  var num = number.toString();
  res.end('Hello World ' + num + '\n' );
}).listen(1337, '127.0.0.1');

var interval = setInterval(function(){
  number++; 
},2000);

console.log('Server running at http://127.0.0.1:1337/');

Http Client (poller)

This node.js Http client polls (requests) data at regular intervals.

To test start server on a terminal, then client on a different terminal.

var http = require('http');

var interval = setInterval(function(){

  http.get("http://127.0.0.1:1337", function(res) {
    console.log("Got response: " + res.statusCode);
    res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
    });
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });
},2000);

Http Client (agent)

This node.js Http client, when requested redirects GET request to the server on port 1337 and returns the response on port 1338.

To test start server on a terminal, then agent on a different terminal. On a browser request/refresh "http://127.0.0.1/1338".

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});

  http.get("http://127.0.0.1:1337", function(_res) {
    console.log("Got response: " + _res.statusCode);
    _res.on('data', function (chunk) {
      res.end('BODY: ' + chunk);
      console.log('BODY: ' + chunk);
    });
   }).on('error', function(e) {
      console.log("Got error: " + e.message);
  });

}).listen(1338, '127.0.0.1');

Saturday, February 28, 2015

A snappy adventure

Project

My long term objective is to play with networking apps, particularly node.js in the Pi environment.  In order to achieve that I need some sort of Linux OS installed on Pi.

Raspberry Pi comes with various OSs.

The ones caught my eye are Raspbian, and Snappy Ubuntu Core.

In this article I will demonstrate a "hello world" app built on an Ubuntu VM and deployed into a Snappy Ubuntu VM.


For this exercise we won't even need a Raspberry Pi board. We will be simulating app deployment to Snappy Ubuntu Core.

Setup

Note: this is my setup You don't need to match it 100%. Equivalents will do. 
  • A Mac Book Pro.
  • A virtualisation app, I use VMware Fusion. ($AU 61.)
  • Download Ubuntu (free) and create an Ubuntu VM using your virtualisation app.
  • Download an OVA image (free) and create a Snappy Ubuntu Core VM using your virtualisation app.
  • An account in Ask Ubuntu portal (free).

Ubuntu VM


Download

This is where you will develop the snappy app. Installing Ubuntu VM using VMware Fusion is straightforward.

I allocated 30 GB, physical space, and 4 Gb RAM, probably an over-allocation. I just want to make sure swift development performance.

I think defaults would just do fine too.

Take a record of the static IP address of the VM allocated by VMware:
ergun@ubuntu:~$ ip adr show


Snappy Ubuntu Core VM



The above disk image is provided in OVA format. To create the VM using VMware follow these instructions.

After creating the Snappy Ubuntu Core VM, it will boot up. The booting process is slow (ironic if you consider the OS is called 'snappy'). The first message it displays: is "Error: malformed file". Simply ignore it. Later on it will also look like stuck with a message "Started Journal Service". But it is not. Don't panic, just leave it running, wait until it finishes, this may take several minutes.

After the boot process finishes it will ask you user name and password, just type "ubuntu" and "ubuntu" respectively until you see you are on:

ubuntu@localhost:~$

The first thing we need to do is to change the default ssh port from 22 to 8022. For this we need to edit the file /etc/ssh/sshd_config:

This file is read-only. You need to make it writeable first:
ubuntu@localhost:~$ cd /etc/ssh
ubuntu@localhost:/etc/ssh$ sudo chmod 777 sshd_config
ubuntu@localhost:/etc/ssh$ vi sshd_config
Find the line "Port 22" and change it as:
Port 8022
Save and exit. Put back the file into read-only:
ubuntu@localhost:/etc/ssh$ sudo chmod 644 sshd_config
One last bit before we leave the Snappy. Current version of Snappy Ubuntu Core image was shipped with cloud ssh setup. This introduced nasty "connection refused" errors when I attempted to deploy my snappy app. I reported the issue on here at Ask Ubuntu portal.

To fix the ssh setup you need to run these commands:
cd
sudo mv /etc/ssh/sshd_not_to_be_run .
sudo service ssh start

Verify that the ssh service started with:
ubuntu@localhost:~$ sudo service --status-all | grep ssh
 [ + ] ssh

Power-off your snappy than restart afterwards:
ubuntu@localhost:~$ sudo poweroff

Remember, starting snappy will take minutes to complete, so be patient.

Take a record of the static IP address of the Snappy VM allocated by VMware
ubuntu@localhost:~$ ip adr show

Build snappy app, deploy and test


As the last step, we are going to build a "hello world" snappy app on the Ubuntu VM, and deploy it to Snappy VM.

Go to your Ubuntu VM.

Make sure you fixed the ssh setup issue on your Snappy Ubuntu Core VM (See the section above.)

Just follow the instructions given here at Building Snappy Apps for Ubuntu Core.

If all goes well this is what you would see:
ergun@ubuntu:~/snappy-examples/hello-world$ snappy-remote 
        --url=ssh://172.16.187.129:8022 install 
        ./hello-world_1.0.5_all.snap
=======================================================

Installing ./hello-world_1.0.5_all.snap from local environment
ubuntu@172.16.187.129's password: 

ubuntu@172.16.187.129's password: 
WARNING:root:Signature check failed, but installing anyway 
        as requested

Reboot to use the new ubuntu-core.

=======================================================

ubuntu@172.16.187.129's password: 
ergun@ubuntu:~/snappy-examples/hello-world$ 

Go back to your Snappy Ubuntu Core VM and test:
ubuntu@localhost:~$ hello-world.echo
Hello World!
Note that as soon as you run your app for the first time it is auto deployed and added to apps folder:
ubuntu@localhost:~$ ls
apps snappy-bin sshd_not_to_be_run


Raspy Tools

Why Raspberry Pi

It's a lot of fun.

What you need to know

What you need

  • A Raspberry Pi board
  • A computer. A laptop is recommended if you want to move things around. Operating System can be Windows, Mac OS X or flavour of Linux.
  • Raspberry Pi Accessories (*)
  • Patience

(*) Raspberry Pi Accessories


As a minimum you'd need:
  • A multi-comp case
  • A pre-loaded 8GB micro-SD card in an SD-adapter
  • A micro-SD card Reader
  • A power supply designed for your Raspberry Pi board
  • An ethernet network cable

Optionally you may need:
  • An HDMI cable
  • An external HDMI display
  • A USB keyboard
  • A USB mouse
  • A USB power hub

Welcome to Raspy Pi

Hi Stranger

I am a guy addicted to programming. I do programming in my daytime job, as well as during some nights and on most weekends.

This blog is about my latest pursuit, Raspberry Pi projects. I ordered my first Raspberry Pi board yesterday.

On this blog I'll share stuff that I don't want to forget and may be useful to you as well.

Enjoy the ride..

Learning something new could be challenging. You'll need time, patience, and endurance. Be prepared for long nights with no results. Try to see it as an adventure game. Not everything will go according to plan, but in the end persistence pays off. See failures opportunity to learn and have fun.