Monday 3 December 2012

[Supervisor] How to Monitor your program

Description
Supervisor is a great program that takes care about the healthy of your application.
The main feature is to monitor your application and then if something happens e.g. crash, it will restart the program.
There is a web interface where you can check the healthy of your programs and check uptime and logs, also you can stop/restart the process.

Installing

The follow command is to install supervisor on your Ubuntu/Debian machine:

sudo apt-get install supervisor

Configuration
The configuration file is located on your etc folder, I found it into /etc/supervisor/supervisor.conf on my Ubuntu.

You can easier add your applications to the file, just following the guide at this link, but I can give you some advice to configure it.
Each application that you want to monitor have to start with this header:
[program:name_your_application]

Add your path where the application is:
command=/bin/cat

and the process name

process_name=%(program_name)s


after that you are ready to restart your supervisor and it should start the process, just check it with "ps aux".
If everything is fine, continue to add more features, otherwise go back and check what is wrong.

These are helpful settings that you should have on your process that makes it alive even if it crashes
autostart=true
autorestart=true
startsecs=10 
startretries=3 

Also add some settings to save all logs from your application:
stdout_logfile=/a/path
stderr_logfile=/a/path

After that, restart your supervisor and everything should be fine.

Enable Web Interface
Usually the web interface is not enabled by default but you can do it , just adding these 3 lines on your configuration file

[inet_http_server]
port = 127.0.0.1:9001
username = user
password = 123

Wednesday 7 November 2012

[Python] HUAWEI E3131 library

Description
The modem Huawei E3131 is used to connect any pc or laptop to internet through a 3G network, you can also think about it and create a program that  needs a 3G connection.
This model is very useful because you don't need to install any drivers and it works strength away on each operate system, due the clever idea to show this device as a normal Ethernet card.
I used this great product on my Raspberry project and I can say that I am very happy with it!


API
I found a website that talks about how to use the API with the modem Huawei and which commands you can send.
Check this out to understand what can you do with this little device, on the website is not described all functions but just the basic one.
You can even looking for a specific action when you are browsing the web interface of the modem, it's very simple and clear! Just go to http://192.168.1.1 when the dongle is attached on your computer.

Library
I had to create a library to use this dongle with my project, you may not like Python but you can even port my helper class to any other languages!
Just remember that each call have to be sent as XML content

Link of the library


How to use it
The library is very easy to use, just import it on your own project and then create the object:
a  = Huawei()
after that you can easily check if your dongle is already connected, just calling:
a.is_connected()

Issues
Please send me an email if you are going to find any issue with this library or if you want to add some other things.

Implementation with Raspberry 

Friday 12 October 2012

[NodeJS] Write bytes to a file

Description
The idea is to get a buffer of bytes from a socket or a file, and then convert them into a file, you can thinking about to transfer an image through a socket, that is my case.

Buffer library
NodeJS has a library buffer that takes care to convert decimal to hex, and then you can use it to write into a file. In my case, I have to convert decimal to hex but you can even populate the buffer with only hex values.

This is a piece of code to write into a file:

var Buffer = require('buffer').Buffer;
var fs = require('fs');


var myBuffer= new Buffer(3);
myBuffer[0] = 71;
myBuffer[1] = 73;
myBuffer[2] = 70;

console.log(test);


fs.writeFile("test.txt", test, function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});

Your output should be something like that:

<Buffer 47 49 46>

means that your values has been converted into bytes, from decimal to hex, you can check it with a Calculator.
Also you should get the message "The file was saved!" and then if you check what there is inside, you have to read "GIF".

That is all you need to convert your buffer of bytes into a file, image, text, etc...

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.

Wednesday 22 August 2012

Arduino: Detect when two wires are connected

Arduino can detect if you cut the connection of a wire or you want implement a piezo sensor, basically the theory is the same.
One application, that I have tried at home, is to detect when the door is open and when is close.
You have to put 2 wires on the door that when it is closed, the two wires must make a bridge and break it when it is open.

The piezo works with the same concept , when you touch it, your finger does a bridge and then you can catch it with your Arduino.

This is a piezo sensor

CODE

void setup() {
pinMode(A0,INPUT);
}
void loop() {
   if (analogRead(A0) < 50) {
      //The two wires are doing a bridge
  } else {
     //The two wires are disconnected
  }
  delay(100);
}


You should try it and find out a good value but normally when the two wires are connected the value is zero.

This is an example of to connect the piezo on your Arduino


WIRING
Connect one wire on the ground pin and the other one in the A0 pin.


Monday 18 June 2012

Android: Motorola Issue with Listview

Motorola Droid X and maybe other models, have an issue with ListView that you will not see the background when you don't have an item inside.

On this link you can follow the discussion and the solution, if you don't want to read all the thread and understand how to fix the problem, you can read below how fix it.

Solution:
On your ListView you need to add four parameters that makes your ListView transparent when you dont have any items

Layout

android:background="@color/transparent"
android:cacheColorHint="@color/transparent"
android:descendantFocusability="afterDescendants"           
android:overScrollFooter="@null"

Color reference
<color name="transparent">#00000000</color>

Thursday 24 May 2012

[Git] Clone the local repositor

You can clone your local repositor somewhere without any authentication, just find out what is your path of the repo.

git clone --no-hardlinks <repo path>

[Git] Restore file or directory

Git has a great system to restore a file that you maybe deleted, you can check what is the last commit about the file/directory:

git rev-list -n 1 HEAD -- <file_path>
If you want to restore it you can use this simple command and in a few seconds your file/directory will be back

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Friday 11 May 2012

Android: Force gallery refresh

This is a simple broadcast to force the gallery refresh instead of wait that you plug a usb cable or remount the sdcard:


 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+
                     Environment.getExternalStorageDirectory())));
Basically  the code does a fake action and the gallery will update the pictures.

Friday 4 May 2012

Convert a String to an Integer

I think that a lot of people is looking for a function to convert a string to an integer because the function ATOI works only with char. 

This is the solution for you project with Arduino:
int stringToNumber(String thisString) {
int i, value, length;
length = thisString.length();
char blah[(length+1)];
for(i=0; i<length; i++) {
blah[i] = thisString.charAt(i);
}
blah[i]=0;
value = atoi(blah);
return value;
}