Wednesday 12 September 2012

Arduino: Establish a socket between 3G and NodeJS

Introduction
This article is about the modem GPRS with Arduino and NodeJS, everything connected with a socket.
I am going to show how can you create your little client on Arduino that is connected with a NodeJS server.

Create Arduino client
Arduino has a great GPRS Shield that is a little bit expensive but I found it very powerful, although a few months ago Cooking Hacks has released an awesome GPRS Shield with a Camera, SD, etc..

The shield communicate via Serial port and you have to use AT Commands to setup a internet connection, no worries I have done all the hardest job for you and these are commands:

//Basic setup
AT+CFUN=1
AT+CMGF=1
AT+CREG=1
AT+COPS=0
//Remove comment if you have SIM CARD Pin
//AT+CPIN=xxxx

//Setup APN T-mobile UK
AT+CGDCONT=1,"IP","general.t-mobile.uk"
AT+CGACT=1,1

//Get GPRS status, when is 1 means you have internet connection
AT+CGATT?

//Connect to your server on the port 66
AT+SDATACONF=1,"TCP","IP",66
//Start connection
AT+SDATASTART=1,1

//Read message from socket
AT+SDATATREAD=1

//Send message to socket
AT+SSTRSEND=1,"test"

I have made a simple script that is not stable 100% but you can try it and connect to your server.

Download script

Please note that after you have started the socket, the modem is taking a few seconds to establish a connection.

Create NodeJS server
The server is a simple script that is receiving only information through a socket, you can also send information but that is up to you.
My example is only to check that everything is working and you can make a communication even if you don't have a Ethernet cable or a wifi connection.


var net = require('net');
var netServer = net.createServer(function (stream) {
        stream.setTimeout(0);
        stream.setEncoding("utf8");
        //Arduino is connected
 stream.addListener("connect", function () {
  console.log("Arduino connected");
        });
 //Arduino has sent an information
        stream.addListener("data", function (data) {
  console.log("Get data: "+data);         
 });
 //Arduino has terminated connection
        stream.addListener("end", function() {
  console.log("Arduino disconnected");
                stream.end();
        });
});
netServer.listen(66);

Result
  1. Upload and run the client
  2. Wait from the green signal by serial port
  3. Check on your server that Arduino is connected and sending messages


Tuesday 11 September 2012

How connect ROS to ROSBridge with service

Introduction
ROS is extendable and it can be controllable from website over a websocket, what you need is to install ROSBridge that makes your environmental accessible from a socket.
In the next steps I am going to show how to call a service from a website through websocket.

Install ROSBridge
This step is very simple, you have to make sure to have installed ROS correctly and then continue with ROSBridge installation on this link.
This operation will take only a few minutes and then when you have finished it, try to create a simple html page with this javascript example:

The ros.js is available from the repository that you have cloned.
You have to change  IP with your server's ip, and then try to run it, you should read "Connected to ROS" on your console, please use Google Chrome that makes your debugging easier.
 
Create a server service
You can use the example from ROS to create a simple service, just look on this link and try to make it works on your server but make sure that you are following this guide as well.

If you have problem to create the file AddTwoInts.srv, just create it and copy and paste the next rows:

int64 a
int64 b
---
int64 sum

When everything is working, try to sum two numbers from your client and then you are ready for the last step.

Call service from website
This is the last step to finish your little project.
You have to make sure that you have started you ROSBridge, the connection is working and your service is running. After these checks you can use the call "callService" on your website.

Modify your code like that:


When you run your webpage, you should see the result of the sum on your console.
You can change it and make a little calculator that uses a ROS service to do the calculation.