Sunday, May 15, 2011

A beginner’s guide to connecting and operating the WiFly to Arduino Serially

We were working on a home automation project , we used the wifly gsx in our project .
When we bought it, we thought it will be easy to communicate it like a serial modem between the arduino and PC ,,, of course we are not noobs :) but every inch in the datasheet made us feel like one :)
And when we searched for a solution for even one of our countless problems we faced.... the result can be summarized in three words ... WIFLY NOT WORKING or we are noobs :D

Of course i am kidding :) ,,we are not noobs and wifly is WORKING :o , thanks to sparkfun wifly library and

We decided to start our new virtual hackerspace with this easy step by step tutorial for wifly :

Please post any problem you faced with wifly or arduino .

  1. First of all download the WiFly Serial Library from credit goes Twaldock for making this library and examples, for more information go to ,also download NewSoftSerial, Streaming, Time and PString libraries from
  1. Add the WiFly and the other libraries to your Arduino library folder and restart your Arduino IDE, you now should see the library and examples.

  1. To test the WiFly and library open one of the examples that came with the library, for this tutorial I will use the WebTime example, this one send html of the current time to your browser.

  1. Now connect the WiFly board to the Arduino, connect the Tx of the WiFly to the Rx of the Arduino and the Rx of the WiFly to the Tx of the Arduino (don’t use Pins 0 and 1 or any reserved Pins) for the sake of this example connect the Tx to the Pin 2 and Rx to Pin 3 on the Arduino. Connect the VDD-BATT on the WiFly to the 3.3V on the Arduino and the GND to GND.

  1. Back to the WebTime sketch, modify the first couple of lines of code to your SSID and Passphrase if there’s any, and modify the TX and Rx pins according to your setup.

  1. now add this function to example, this function adjust the configuration of the WiFly in order to make it connect to your Access Point or Ad-Hoc connection

boolean initSettings()
WiFly.SendCommand("set wlan auth <value>","AOK");
//WiFly.SendCommand("set wlan key <value>","AOK");
WiFly.SendCommand("set wlan channel <value>","AOK");
WiFly.SendCommand("set wlan join <value>","AOK");
WiFly.SendCommand("set dhcp <value>","AOK");

  1. instead of <value> a real value should be entered
    1. for the first line of code WiFly.SendCommand("set wlan auth <value>","AOK") this is for the authentication type replace <value> with
      1. 0, Open
      2. 1, WEP-128
      3. 2, WPA1
      4. 3, Mixed WPA1&WPA2-PSK
      5. 4, WPA2-PSK
      6. 5, Not Used
      7. 6, Adhoc, Join any Adhoc network

    1. You don’t need to do this step unless you chose WEP-128 in the authentication type then you must enter this line WiFly.SendCommand("set wlan key <value>","AOK") where the value is the HEX key.

    1. WiFly.SendCommand("set wlan channel <value>","AOK"), 1-13 is the valid range for a fixed channel. If 0 is set, then scan is performed, using the ssid.

    1. The WiFly.SendCommand("set wlan join <value>","AOK") sets the policy for automatically joining/associating with network access points. This policy is used when the module powers up, including wake up from the sleep timer.
      1. 0,, Manual, do not try to join automatically.
      2. 1, Try to join the access point that matches the stored SSID, passkey and channel. Channel can be set to 0 for scanning.
      3. 2, Join ANY access point with security matching the stored authentication mode. This ignores the stored SSID and searches for the access point with the strongest signal.
      4. 3, Reserved – Not used.
      5. 4, Create an Adhoc network, using stored SSID, IP address and netmask. Channel MUST be set. DHCP should be 0 (static IP) or set to Auto-IP with this policy. (unless another Adhoc device can act as DHCP server) add the ip and netmask
        1. WiFly.SendCommand("set ip address","AOK");
        2. WiFly.SendCommand("set netmask","AOK");
        3. Also set channel to 1, WiFly.SendCommand("set wlan chan 1","AOK");

    1. Last line in the configuration process WiFly.SendCommand("set dhcp <value>","AOK"); which set the DHCP
      1. 0, DHCP OFF, use stored static IP address
      2. 1, DHCP ON, get IP address and gateway from AP
      3. 2, Auto-IP, generally used with Adhoc networks
      4. 3, DHCP cache mode, Uses previous IP address if lease is not expired (lease survives reboot)

    1. You need to save and reboot each time you configure the WiFly Wlan options thus the WiFly.SendCommand("save","AOK") and WiFly.SendCommand("reboot","AOK");

  1. Now add initSettings() as the first line of a function called Reconnect();

  1. That’s it, upload the WebTime Sketch to your Arduino and see if it works, if you connected the green, yellow, red LEDs to the PIO4, PIO5, PIO6 respectively of the WiFly you should see its status
    1. Red LED
      1. Fast blinking, not connected to wireless network
      2. OFF, Connected
    2. Yellow LED
      1. With each blink it means its either sending or receiving data from Tx or Rx
    3. Green LED
      1. ON Solid, connected over TCP
      2. Fast Blink, no IP address
      3. Slow Blink, IP address OK

  1. After uploading the Sketch open the serial monitor to see the Arduino activity, it should display “Starting WebTime - Please wait.” and then “Free memory= <value>” and then stops for about 10 sec or more and the continue, if it doesn’t continue the means you have something wrong in your connection between the Arduino and WiFly and it’s not receiving data

  1. After it continues you should see quite a lot of lines coming up on your serial monitor if it connected successfully you should see your IP address that has been assigned to the WiFly and the Green LED is slowly Blinking while the RED LED is OFF, if you find your IP address to be then the WiFly failed to connect to your wireless network and that you may have configured it wrong.

  1. When you see ready for HTTP in the serial monitor, go to your browser and enter the IP address found in the serial monitor and add port 2000 to it (

  1. You should see some activity in the serial monitor and the browser should show you some data about current time

  1. If all works right for you, you can remove the initSettings() function as this is only need to be run once to configure the WiFly and you won’t be needing it anymore

  1. For more information about the commands that can be sent to the WiFly check the WiFly Command Reference Manual

  1. Good Luck and Have Fun