Pages

Thursday, 15 November 2012

Node.js Raspberry Pi Serial Communication using Socket.io, Express and node-serialport

Okay so today I am gonna experiment with Nodes serial port module https://github.com/voodootikigod/node-serialport

It is already installed but the basics of how to get it running are in the github readme.
So my plan is to eventually get all this running with the Modbus protocol and write my own Modbus class, however for now I just want to make sure I have the basics of node-serialport working.
So first of all this is my setup..


Here you can see the raspberry Pi and also a module I have used to connect the serial comms up.
As I am planning on using Modbus for the communication I have used a 3.3V UART to RS485 converter chip wired to a cat 5 socket which then connects to various Modbus devices.
The chip used is a MAX 3483. This could easily have been a 3.3V UART to USB such as Any of these FTDI chips or simply a MAX232 chip to convert the logic up to RS232 levels. Anyway I have used the 3483 for this project.
The module makes a connection to the RX and TX pins on the GPIO header on the Raspberry Pi.
Which are located on GPIO 14 and GPIO 15 as can be seen from the image below.
For more info check out here..



  

All connected.. lets install!


First thing we are going to do is install express, this will take care of creating all the nice directory structure for us and starts up off with a webserver and our routes etc all written for us.
So create a directory on the Pi and make sure Node.js is installed (I might do a blog post on this in the future but at the minute you figure it out!). I am using Node 0.8.12 and NPM 1.1.63 find out your version by typing “node –v” and “npm –v” also I am using Raspbian Wheezy.
So go to the directory you made and type: “npm install express” –if the next step doesn’t work properly for you, maybe try typing “sudo npm install express –g” this will just install it globally on the computer.
Once installed type “express <your app name here>” so I typed “express serial” this will then generate all the relevant directories.
Once it has created them it will tell you to “cd <directory name” and then type npm install  which will make npm install all the relevant modules etc and finalise the installation.
If you then just typed “node app.js” it would start the web server running, but for now were going to install a few more modules.
We are going to want to use node-serialport so in the working directory type “npm install serialport” this will add it to the project.
Finally I am going to want a way to get these serial messages to and from the browser window. For example I am going to want to maybe have a button send out a serial message and then have a textbox getting the value back.. or anything.. a graph? but to get messages to and from a browser is a bitch so the lovely people at socket.io have created this awesome library  (which I will explain in another blog post) to get these messages in and out in a really easy way.
So as a final install type “npm install socket.io”

All Installed..lets code!

First of all open up the folder in a text editor (I use Sublime Text 2 because it is awesome!) and then open up “app.js” (this could be called server.js or something similar)
At the top of the page there are all the require files for express, we want to add our own modules here so make sure to add this line to the end of the requires. (we are going to add socket.io in a different way later on)
After this it’s time to set up our serial port so add the following code straight after the requires.
Express sets up a server for you but we are going to change it slightly to work with our socket.io module, so find the server setting up code and change it in it’s entirety to the following.
This is creating the server and then binding our socket.io module to listen to that server, whenever someone connects to it a socket session will be started between the client and the server so they can happily send and receive data to each other.

The way socket.io works is that on both the server and the client (the website) there will be something called listeners and emitters. A listener will be listening to the socket connection for certain keywords, when it hears it’s keyword it will fire its code and when it’s done it could emit a keyword to start some other code on the client or it could just do nothing.

So the first thing you need to do before you can add any listeners or emitters is to finish setting up the socket server. This is done through listening for “connection”, once a connection is made you can start having fun with emitters etc.

So this is the code for the server, at the minute it is just listening for the connection, once it is connected it will emit “connected”  and it will also send the number 123 to the client. – This bit is not needed it is just an example of how to emit and listen.

Server Code

Above is the sever code, it is now time to setup the client. The client will be our website which we are going to have to build in html. Express likes to use a module called Jade to do its html templating however I don’t like using that so instead of using the default views I create a file in the “public” directory called “index.html” and then in the routes I change the code to render the index file instead of the jade template. So in /routes/index.js I change the code to this.
So in our index.html file we need to listen to our socket server so the script to do that is shown below (the whole html file will be included at the end so don’t worry about where this all fits in the html structure at the moment)

Client Code

Now lets create a function on the server which will listen for the event called “sendSerial” and take some data in and send that data to the serial port.

So add the following code to the server MAKE SURE IT IS ENCASED WITHIN THE IO.SOCKETS.CONNECTED BIT OF CODE.
We are also going to want to send the data to the client when new data is received by the serial port so we will emit the keyword “receiveSerial” when new data arrives so the client can log it.

So underneath the above code, add the following.
There are a few more serial port functions we need to add to the server for it to work so just below that add this.
This finalises our server so now lets setup our client to respond to the receiveSerial and make it emit the sendSerial keyword with the data needed.

Okay so lets move over to our file “index.html”

This is hopefully all straight forward to any html developer, the file is using some basic jquery so please read up on that if you want to make sense of it.

Basically all that is happening is there is a send button and a textbox when the user clicks the send button it will take the data out of the text box and emit the “sendSerial” event. This will be picked up by the server and sent down the serial line.

There is also a read only textbox which just logs the values received by the serial port.

That's it.. Finished files


So here are the final files which you need to make it work.


APP.JS


INDEX.HTML

This is shown above in full..

INDEX.JS(ROUTER)


To be honest there are probably mistakes in those files somewhere but you should be able to see the basics of how all this works and be able to find and correct the mistakes if they are there


Happy trails 

12 comments:

  1. Hey sam, nice tutorial, really neat, one of the issues is trying to install node.js on Raspian Wheezy (2012-12-16), do you have any recommendations or site links on how to get that running. I am getting "illegal instruction" when i install serialport , "#npm install serialport"

    ReplyDelete
    Replies
    1. Hey man, I am going to do an installing node.js on a blank pi tutorial this week.. my new blog is over at www.badgersblog.co.uk so check it out there.. it will probs be on in next few days.. :) merry christmas!

      Delete
    2. Woo-Hoo! Awesome! Thanks Sam, Merry Christmas to you too !

      Delete
    3. Just letting you know, instructions are now up on the blog for node install :)

      Delete
  2. Sam, I would kill for (ok, maybe not) a tutorial / parts list of your breakout board with MAX 3483. I also want to communicate modbus (RS485) from my Raspberry Pi. Planning to go and buy a MAX 3483 tomorrow but I'll guess there's more parts needed than that? You have a diod for example.

    ReplyDelete
    Replies
    1. I totally only just saw this, it didn't show up on my email.. how are you getting on? still need some help

      Delete
  3. Hi Sam
    Did you ever complete your node.js modbus RTU package?

    ReplyDelete
  4. please provide whole code in zip file.

    ReplyDelete
  5. Hi Sam

    I have found that this works pretty well
    https://github.com/morkai/h5.modbus
    I hope this helps someone

    ReplyDelete
  6. Any chance you can update the project to use express 4 instead of 3? I was getting a lot of errors with the code with express 4 so I installed express 3 but now the socket.io faults as well. Please help.

    ReplyDelete
  7. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Best spanner set brand India

    ReplyDelete