
Controlling the WiFly module I: Command mode
So you’ve got a WiFly module and you want to integrate it with your Arduino project to lift it to the next level. But were do you start?
The last couple of months I’ve had a lot of hands on experience working with Roving Networks WiFly and I know it can have a mind of it’s own sometimes. I decided to write this starting guide to share the knowledge I’ve acquired. Because my hardware setup is a bit unique I will focus primarily on controlling the WiFly through Arduino and not how to connect the components to each other.
There are three ways to connect a WiFly module to Arduino: Hardware Serial, Software Serial and SPI. In these examples the WiFly is connceted to Arduino Leonardo’s digital pins 0 (RX, receive) and 1 (TX, transmit); hardware serial.
Basic communication
Let’s begin with some basic communication to try to understand how the WiFly works. In this example we will communicate to the WiFly directly. (Remember these examples are based on controlling with hardware serial).
Upload the following sketch to the Arduino to directly send and receive data from the WiFly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void setup() { Serial1.begin(9600); // UART, RX (D0), TX (D1) are connected to WiFly Serial.begin(9600); // USB } void loop() { if(Serial1.available() > 0) //if RX pin (digital 0) received a message { Serial.write(Serial1.read()); } if(Serial.available() > 0) //if USB has received a message { Serial1.write(Serial.read()); } } |
Open the serial monitor, select no line ending and type $$$.
The WiFly module can’t receive any data for 250 milliseconds before and after $$$ is send to it or else it won’t go into command mode. When the WiFly sends CMD back then the command mode is initialized and you will have total control over the WiFly.
Get configuration settings
The next step is to ask the WiFly for some configuration settings. This is a great way to debug when working on a project because it’s fast and the values in the WiFly are shown directly. When getting or setting a specific configuration you’ll need to end the command with a carriage return.
When in command mode send get wlan to the WiFly.
The WiFly will send back the current wireless configuration values ended by the firmware version (2.38.3 in my case).
Note that a command reference and other applications/examples for WiFly can be found in the WiFly User Manual, which can be downloaded on this page.
Set configuration values
The next step is to configure values on the WiFly. Lets say we want to change the current SSID and password so the WiFly can connect to our wireless network. Go into command mode and type set wlan ssid mynetworkname and send it with a carriage return. If all goes well then the WiFly will return AOK followed by the firmware version.

Set password (WPA and WPA 2). After CMD is initiated and SSID is set.
Some useful commands when configurating your WiFly to connect to the wireless network. Check the user manual for an extensive description of the commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
set wlan ssid <string> set wlan phrase <string> // for WPA and WPA2 set wlan key <value> // for WEP set wlan join <value> // set join mode // 0 = Manual // 1 = Try to associate to stored SSID // 2 = Associate with any network // more options in user guide set wlan channel <value> // set wlan channel // 0 = scan all channels // 1 - 13 = scan this channel // more options in user guide set ip dhcp <value> // set DHCP mode // 0 = DHCP off (manual set IP adress) // 1 = DHCP on // more options in user guide set ip adress <adress> // manual set IP adress |
When you are happy with your configuration you will have to save it so that it can be loaded as default when the system reboots. To save the configuration send save to the WiFly, it will respond with Storing in config followed by the firware version.
Join network and open connection
Now that we know how to view and store settings on the WiFly it is time to make a connection with a wireless network and a webserver. Type join in CMD to manually connect to a wireless network. The WiFly will use the stored SSID and password/key to make the connection. If the connection is successful then the WiFly will send some data (IP adress, gateway etc.) back.
Now that you are connected to a wireless network your WiFly can connect to a local server or a server on the internet. To open a connection type open <ip address> <port number> in CMD. When a connection is succesfully openend the WiFly will send *OPEN* and CMD will stop. I’ve made a (really simple) server in Node.js that displays the IP address of a WiFly module when it connects, shows the data that is send to the server and gives us a notice if a WiFly disconnects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var net = require('net'); var socket; var server = net.createServer(function(socket) { //'connection' listener socket.setEncoding('utf8'); socket.on('connect', function() { console.log('WiFly connected with ip adress: ' + socket.remoteAddress); }); socket.on('end', function() { console.log('WiFly disconnected'); }); socket.on('data', function(data) { console.log(socket.remoteAddress + " sends: " + data); }); }); server.listen(1337, function() { //'listening' listener console.log('start WiFly test server...'); }); |
Run the server with node.js, start CMD and type (in my case) open 192.168.1.107 1337. When the WiFly sends back *OPEN* you can send data to the server. Note that if you want to connect to a HTTP server you can also type the URL address; open www.google.com 80.

Connection to the server is open, everything that will be send will go to the server.

The server sees that a WiFly is connected and the data that it sends.
Conclusion
Controlling the WiFly with command mode is in my opinion a valuable (debug) tool. It can be used to configure the WiFly module and to connect with a server, however without the use of libraries it is a very tedious way of doing things because everything must be entered manually or we have to write code that checks if the WiFly received the command correctly; did it send back CMD or the Firware version? Luckily there are libraries that can communicate with the WiFly module (via the same way as is done in this tutorial). The following post will go into details of some of these libraries.
4 Comments
After $$$, i get the message CMD. But when ik ask get wlan, i get the message:
java.io.IOException: Device not configured in writeArray
I think there is a problem with the IDE or Java. Which OS are you working on? Try to re-install the IDE and/or Java.
Intranet or External IP access with Wifly in 3G or WiFi: https://play.google.com/store/apps/details?id=appinventor.ai_sandro_juca.Wifi_SanUSB&hl
The microcontroller firmware link with circuit and programs is on APK. You can send with this Free APK any character typed into the text box next to the button.
The IP address entered by the user is stored permanently in application memory, ie when you restart the application the new IP will be shown.
Great initiative, very very useful. Keep up the good work.
I only have to check node.js never heard of.
Recent Posts
Sep 30, 2013
Jan 10, 2013
Dec 25, 2012
Dec 06, 2012
Recent Comments
Archives
Categories