Getting started with the RN-XV WiFi Module & Node.js
The RN-XV WiFi module is a nifty little WiFi module designed to fit the same pinout as an XBee, so it’s intended to be a drop-in replacement.
Tonight I whipped up a little test of the module to get a joystick to talk to a Node.js server over WiFi. I attached +3V power and ground to the module (pins 1 and 10, respectively), pin 2 (TX) to Arduino digital pin 0 (RX), and pin 1 (RX) to Arduino digital pin 1 (TX). That’s all the hardware setup you need.
I used this WiFly library to handle the connection. All it does is talk to the WiFly module over serial and send control commands, so the library abstracts that a bit. Here’s the Arduino sketch I built:
#include "WiFly.h"
#define PIN_VERT 0 // analog
#define PIN_HOR 1 // analog
#define PIN_PUSH 2
#define PIN_LED_RED 3
#define PIN_LED_GRN 4
int vert = 0;
int hor = 0;
bool push;
char* ssid = "yourNetwork";
char* pass = "yourPassword";
char* serverAddress = "yourServer";
int serverPort = 1337;
Client client(serverAddress, serverPort);
void setup(){
pinMode(PIN_PUSH, INPUT);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_GRN, OUTPUT);
digitalWrite(PIN_PUSH, HIGH); // set pull-up resistor
digitalWrite(PIN_LED_RED, LOW); // start off
digitalWrite(PIN_LED_GRN, LOW);
Serial.begin(9600);
WiFly.setUart(&Serial);
WiFly.begin();
if (!WiFly.join(ssid, pass, true)) {
digitalWrite(PIN_LED_RED, HIGH);
while (1) {
// Hang on failure.
}
}
digitalWrite(PIN_LED_GRN, HIGH);
if (client.connect()) {
client.println("ohai!");
client.println();
} else {
// do nothing
}
}
void loop(){
vert = analogRead(PIN_VERT);
hor = analogRead(PIN_HOR);
push = digitalRead(PIN_PUSH);
digitalWrite(PIN_LED_RED, !push);
client.print(vert);
client.print('\t');
client.print(hor);
client.print('\t');
client.print(push);
client.println();
delay(10);
}
And here’s the very basic Node.js server that just prints out the values it receives:
var net = require('net');
var server = net.createServer(function(socket) { //'connection' listener
console.log('server connected');
socket.setEncoding('ascii');
socket.on('end', function() {
console.log('server disconnected');
});
socket.on('data', function(data){
console.log(data);
});
});
server.listen(1337, function() { //'listening' listener
console.log('server bound');
});
Related Posts:
-
Eric Viele
-
http://twitter.com/stefan_crain stefan crain
-
http://log.liminastudio.com/ T3db0t
-
Thomas Pujolle
-
Ib
-
Ted Hayes
-
Ib