Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Friday, 4 October 2013

Lightwave RF Arduino


Intro:
I had some spare time ( and money ) and I decided to make my little flat a beginning of automate house.
I did some research how to control my lights from an Arduino, even knowing that I could replace the normal switch with a relay. This wasnt enough because I wanted to keep the manual system and to be controlled from an Arduino, so after few hours I came up with LightWaveRF system that has been "hacked" and it is possible to be controlled from an Arduino.


LightWaveRF
Their products are working with a simple RF protocol at 434 mhz, and they give you the possibility to control it from your smartphone, remote controller and a webpage.
Some people understood how the protocol works and they just write a simple library to interact with Arduino.
There are various adapter: Light switch, Power socket, Light, Sensor PIR, etc.. and if you get a remote controller and a RF receiver, you can reproduce the same signal from your code.

Sniffing:
I was using this library to find out what ID and what kind of signals the remote controller was sending to the light switch. After a few minutes on the result and few website, I understood how to reproduce the same behaviour with a single click.
I suggest to use the Decode example to get your ID and to understand what kind of signal your are sending. If you have a multi channels remote, you have to be careful on which command and which channel.

Here an example to turn on my light:

Here a little description what message it sends:



Send:
When you got the ID, Channel and which command to send, you re ready to send your first message to your device.
Use the SEND example as based and create your custom message, replace it on the turnOn function and then compile. When the terminal is ready, you should be able to send "1" and whatever you decided to do, it will do it.
Be careful! If something is not working, just try with simple command like ON and OFF, after that move on dimming and mood ones.

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


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;
}