Skip to content

Getting started with the RN-XV WiFi Module & Node.js

2011 December 7

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

    I’m curious what Arduino you are using? I am using an Uno which is a 5 volt Arduino and my understanding is that any 3.3 volt module will not be able to talk over the serial connection because it’s actually a 0-5 volt on/off communication and the 5 volts is too much for the RN-XV and as such it will require some form of level shifting.

    So I’m guessing you are using a 3 volt Arduino but if not I’m wondering how you got the two devices to talk.

    Would love to hear back from you.

    Oh and great projects!

    • http://www.liminastudio.com Tedb0t

      I’m using an Arduino Diecimila. The RN-XV module is powered by the Arduino’s 3.3v output. No level-shifting required.

      • http://twitter.com/stefan_crain stefan crain

        I can understand Eric’s confusion, The data-sheet for the RN-XV says the UART_TX & UART_RX are only 3.3V tolerant.

        http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-XV-DS.pdf

        • http://log.liminastudio.com/ T3db0t

          Oh hm, interesting.  Well, I don’t appear to have damaged the device with my current setup… I’ll have to look more into this.  Thanks!

  • Thomas Pujolle

    Hello there!

    I have an arduino Uno, and I didn’t managed to get this working properly :(
    I just cannot join any network, and at the best I get some “$$$” logged out.

    Anyone ?

  • Ib

    Seems WiFly.setUart(&Serial); does not work anymore.

    Getting error: ‘class WiFlyDevice’ has no member named ‘setUart’

    • Ted Hayes

      Hm, that’s strange, it’s right here: https://github.com/jcrouchley/WiFly-Shield/blob/master/src/WiFly/WiFlyDevice.cpp#L210

      • Ib

        Thank you, my bad – used original SFE lib instead of the forked. However now Client is renamed in the lib to WiFlyClient:
        WiFlyClient client(serverAddress, serverPort);