Web based SSH access for your Raspberry Pi using tty.js

Today I’ll talk about web-based SSH access for your Raspberry Pi using node.js and a little app called tty.js. The idea is to open an SSH session in your web browser. This can be useful when you don’t have putty installed or your school blocks every port.

Installing node.js for tty.js

First we need to install node.js, luckily this now has been ported to ARM and can be installed using apt-get. First what is node.js? Node.js is a software system designed for writing highly scalable Internet applications. It’s written in an extend version of JavaScript using event-driven, asynchronous I/O and to minimize overhead and maximize scalability.

Sending notifications from a web server to the client is problematic. For example, in Gmail you get a notifications when a new email arrived. Traditionally this would require a lot of work to set up web sockets to push the notification. Using node.js this is quite easily.

I’m totally not experienced with node.js, but I’m planning on learning the language. I think it can be really useful, for example porting my ArduinoPi to node.js would be awesome cool! (Instant feed back for example).

So installing node.js, just run apt-get install nodejs npm. This can take some time, the installation will go on (I had a awfull lot of errors, but they didn’t seem relevant at all).

Using the package manager for tty.js

Node.js also has a package manager called npm. You can install packages using npm install tty.js for example. Then I failed on understanding how node.js works. I couldn’t find the location or how to start the application. Ho the tty.js GitHub page.

In your home directory there is a new folder called node_modules and in there a folder tty.js. Not that hard after all. To start the application you first go to the folder containing the file tty.js:

The first line will go to the directory and then we start tty.js using the node command. When you now visit your Raspberry Pi IP address and add port 8080 you get the following result, a web-based ssh client.

tty.js a web based ssh client

Configuring tty.js

I found a way to edit the html, so you can change the title and other parts. Everything is located in the tty.js folder in node_modules. The static folder has the CSS and HTML. You can edit some configuration options apparently but I couldn’t find explanation on what they do. The configuration can be found at ~/.tty.js/config.json check the GitHub page for more information.

A little note, people now have full access to your device, I only use my PI in my home network but remember that people have root access. They don’t need to login when they open the URL in their web browser.

Raspberry Pi Bluetooth connection with BlueSmirf

Today I’ll explorer the Raspberry Pi Bluetooth options. It was a struggle but it finally works! I noticed that there is almost no support or exploration of Bluetooth with the Raspberry Pi. Only some posts about people who like to connect their Bluetooth mouse or keyboard.

Why use a Raspberry Pi Bluetooth connection?

In ArduinoPi the Raspberry Pi always needs to be connect with an Arduino for it to work. Sometimes it’s not possible or desired to embed a Raspberry Pi in your project. Think about robotics, putting my Arduino in the robot sure, but my Raspberry Pi? No thanks I would like to watch a movie tonight.

Of course it would be easier to use WiFi shields for Arduino, but they are quite expensive. I decided to look for a cheaper solution, so I bought a Bluetooth dongle for the Raspberry Pi (a TOPDIGI UA01 USB Bluetooth Dongle Adaptor Plug and Play on eBay), a merely 5 euro.

The idea is that instead of using the UART port on the Raspberry Pi, I now use a USB port with my Bluetooth dongle, on the Arduino side I use a Bluetooth module (BlueSmirf Silver from Sparkfun).

Setting up Bluez for the Raspberry Pi Bluetooth connection

A little rage: during my trail and error I never found one single manual/setup guide/option references for Bluez whatsoever (even the wiki is outdated). They decided to change everything in version 4, so everything you find online is of course for version 3.

First we install bluetooth bluez-utils

Normally this will include a lot of dependencies making sure Bluez can run. The Bluetooth package also installs Bluez an other stuff you may need.

Next I used lsusb to see my device got recognized, luckily my Bluetooth dongle is supported. By now I powered my Arduino and plugged in my BlueSmirf Silver and uploaded the pass through script. Next we can scan for the BlueSmirf Silver using the following command on the Raspberry Pi

Now every blog on the world-wide interwebzzz will say to use the following command to find the channel we want to use:

And it will return nothing, hoo god why. After some research it’s quite normal that the BlueSmirf wont return anything. The sdptool is used for scanning protocols that are supported by the Bluetooth device. The BlueSmirf supports none, it’s a simple serial-over-bluetooth device. Next we edit the rfcomm.conf file.

So, all the options are quite clear I guess, change your address in the device option. For the channel I wild guess 1 and it worked. Oke, maybe not wild guessing, more like trial-error starting from 0. I guess that for all devices this should be one, could be wrong.

I disabled the need for a PIN code on my BlueSmirf (lets not make it complicating). After some research it’s quite easy to add support for it. Just issue the following command.

So lets brake it down, the first part is the remote address of the BlueSmirf dongle, followed by the pin (default its 1234). Then we write this line in a file called pincodes, I used tab so the Bluetooth address of my Raspberry Pi gets filled in automatically. Great now we can restart the Bluetooth service and everything should work…

Pairing is not the same in Linux

So I now have a /dev/rfcomm0 which is my serial port that I can use to write or read data. Lets try it out. I noticed that my BlueSmirf indicated that there was no active Paired device.

And I could see the text appear on my Serial monitor of my Arduino, great it works. I also noticed no green light on the BlueSmirf, still no pairing but I could send data, strange. Time to listen to the rfcomm0 port and send something from my Serial monitor.

Aaa, now it shows a green led, a connection is made. So everything works but this doesn’t work at all. I’m planning on using the Bluetooth with the ArduinoPi, now all my messages send back from the Arduino will be intercepted and not passed to PHP.

No information whatsoever wasted hours

I now went on looking how I can force a paired connection. Everyone calls this a paired connection, no it’s not. it’s configured but not active. I spend four hours researching and messing up my Raspberry Pi and finally it came to this:

This will first release the connection (it’s needed for some reason), and then starts a new connection on channel 1. Now the device is paired and I can send and receive using my ArduinoPi. From this moment the device is PAIRED, before there was just some configuration.

Now I’m planning on changing the ArduinoPi to support this new feature. Controlling your robot from a web page and Bluetooth? No problem.

ArduinoPi Web based Controller for Arduino

UPDATE: New version found here: http://www.fritz-hut.com/arduinopi-v1-5-bluetooth-dongle-support/

The ArduinoPi is a web-based controller for the Arduino using the Raspberry Pi. The advantage of using a Raspberry Pi (€28) is that its cheaper than an Ethernet shield (€35) and actually more useful.

I talked about connecting the Raspberry Pi and Arduino over a serial connection and how to install and configure PHP serial class so PHP can talk with the Arduino.

The principal behind ArduinoPi

First a little clarification, the ArduinoPi isn’t really a library or a command set or an out of the box controller, its more a proof of concept using already know programming languages. If you want to use it be ready for some PHP, CSS, HTML, C++ and jQuery!

The principal is as follows: The Arduino is connected as a slave device, meaning it waits for a command, execute it and maybe return a value or something. Everything must be initiated from the web browser. A true slave device.

It’s also possible for the Arduino to execute its own program and the ArduinoPi can then be used as a controller to switch variables. For example I have an automatic light switching system but using the ArduinoPi controller and my web browser I’m able to manually override it.

The command set of the ArduinoPi

The Arduino will check for a valid command. Every command starts with @ and ends with :, variables can be separated with a comma. Of course other commands can be added yourself, I’ve found that these are the basics and cover enough to make some interesting interfaces.

Basic switch-port-high command

This command is sent to the Arduino, for example this will switch port 6 high (255 = HIGH). The first value in this command must be between 0-99, which corresponds to the port number. Note that there is no checking so always see the port is set in OUTPUT mode and you are addressing the right port.

RGB command

I’ve also added a RGB command for LEDs, this is a special command and is constructed as follows:

The 101 indicates the special command mode and the following 3 values are just the RGB values. In my example setup I’ve added 3 RGB LEDs and they all get turned on to the right value using the RGB command.

Read Sensor Command

The last command is the sensor command. It will read an analog sensor value and return it to the PHP script.

The 102 indicate a special command and the following value indicates the port that should be read using analogRead().

My test setup for the ArduinoPi

I’ve used the following components for testing various functions: 3 RGB LEDs + resistors and a light sensor. The example will work with the following configuration. The image does not contain the connection setup with level converter circuit from last time (to cut some space).

The connection schematic for the ArduinoPi test setup

The Arduino code for ArduinoPi

SerialEvent1 gets called every time data is available on the Serial1 interface, the function will check for a valid command, if it found a valid command (meaning it starts with @ and ends with : ) it will switch the Boolean value cmdRec to true, in the loop function we always call handleCmd(); now the cmdRec is set so we process the command. The while loop will split out the values and save them in the data array. Because we also need the last command we repeat the same stuff out of the while loop once more, I should fix that btw.

If the first value has a number lower than 100 we turn the port on with the second value as second parameter, else we process it like a special command. I’ve used a switch so you can add your own special commands as needed.

The web browser interface for the ArduinoPi

As interface I decided to use 4 pages each showcasing the commands. I made use of the following scripts/plugins/blogs/code and I would like to thank the original authors: Bootstrap, jQuery, Farbtastic Color Picker, jQuery color picker using Farbtastic and jQuery UI and flot. The complete source is on Github, I’ll give a short overview of the different pages.

Hover example

The hover example for the ArduinoPi

The Hover example works as follows, we add a jQuery hover handler over all the buttons. If the user moves over the button we request the value of the button, this value corresponds with the port we want to switch. We then send an ajax call to a PHP script that will handle the communication with Arduino and sending the right command. For a value we use 255. When the mouse leaves the button we do the same but use the value 0 to turn it off.

Color picker

The color picker for the ArduinoPi

The color picker works as follows. The user selects the desired color the RGB LEDs should produce and then click on the big square. An AJAX request is then made to a PHP page that will send the special command 101 with the right values for red, green and blue.

 Basic Sensor Display

Sensor display for the ArduinoPi

This has nothing to do with the Arduino, but remember my light measurements? I always had trouble displaying them and I’ve included an example how you can display graphs really easy using this tool. It’s really useful for offline measurements that are saved on a SD card and then need to be represented in a browser.

Live Sensor Data

Live sensor Data for the ArduinoPi

This will execute live measurements. A PHP cron job needs to be setup to run every minute as follows:

The PHP script will execute a measurement and save the value in a JSON file. On the client side we refresh the graph every minute with an Ajax call. I’ve also include two buttons to clear the log or request your own value.

Conclusion

I gave a bare bone, alpha release of my ArduinoPi controller. This example can be expanded all the way and check the Github source, I’ve added a lot of comments to get people started.

There is one big limitation, the server (Raspberry Pi) always has to request data from the Arduino. It’s not possible to send data from the Arduino to the Raspberry Pi and then magically update the browser. When using for example Processing, it is possible to have a more live connection and build an interface in JAVA. But this web interface is accessible for any smartphone or laptop (assuming you don’t run IE6).