Danny Benedettelli recently provided me with a NXT2WIFI prototype sensor to start development of a leJOS driver for it. I’ve been working on the code in my (limited) spare time and have a basic client and server interface developed in Java. This is available in the latest SVN snapshot of leJOS.
I’ve been working on a new driver class in leJOS for the NXT2WIFI prototype sensor. The NXT2WIFI connects to the NXT using the high-speed RS485 port 4. Commands are sent from the NXT as ASCII strings and replies are received from the NXT2WIFI in string format. The NXT2WIFI has a chip on board that computes the wifi key so it can join secure wifi networks; I’ve tested it with my home WPA2 wifi and it works perfectly. The NXT2WIFI also supports ad-hoc networks, so you can create a local wifi network and join devices to it (such as your phone or tablet). The really interesting part of this sensor is that it has a built-in webserver that allows you to control the NXT motors directly – very handy for remote control from your phone!
The NXT2WIFI has an on-board rechargeable battery so it won’t drain your NXT’s batteries. The battery is charged using a mini-USB plug that connect to the board. The mini-USB plug also allows you to talk directly to the sensor over a console serial link – this is an incredibly useful feature for debugging and testing!
The video above shows the sensor allowing you to control a robot using an iPhone connected via a wifi to the on-board webserver. Yes, it really is that easy to drive a robot from your phone. But doing anything more complicated requires a bit more programming experience.
In my first version of the leJOS driver I focused on getting the basics working; talking to the device, connecting to a local wifi network and sending and receiving data using TCP sockets. I implemented the Java standard InputStream and OutputStream classes in my driver to provide a high-level abstraction for reading and writing data to and from a TCP socket. The advantage of a Stream approach is that it greatly simplifies your code; you don’t have to worry about sending/receiving low-level data blocks and instead can focus on what your code needs to do.
Some sample code I’ve developed so far:
- Class to talk to the NXT2WIFI: NXT2WIFI.java
- Test if the sensor is working: PingNXT2WIFI.java
- Connect to a WPA2-PSK wifi: ConnectWPA2.java
- TCP Server test: TCPServer.java
To use this code, first open the ConnectWPA2.java file in an editor and enter your wifi SSID and passphrase in the variable definitions at the start. Then compile and download to the NXT. Press enter and it will start to connect to your wifi network – this can take up to 30 seconds. If you open the leJOS Console Viewer GUI then you’ll be able to watch the progress. You can also open the serial link to the NXT2WIFI in a terminal program and see the messages flow from the sensor as it computes the key and connects onto your wifi network. The screenshot below illustrates sample output from the terminal…
Next compile and download the TCP server class to the NXT. It listens on port 88 for connections and then replies back with a message for every line you type. Press Enter to start the TCP server. Then open a terminal window and telnet to port 88. Type “quit” to exit:
I’ll be updating this driver with new code over the next few weeks – enjoy!