I spent a while going through lots of different WiFi libraries before I found one that made things easy. Eventually I’ve settled on dpslwk‘s version of the WiFly library here and there’s one big reason why:
The WiFlyClient class inherits from Client meaning it can be dropped in as a replacement for the standard Ethernet library.
That said, there’s still a little bit of work to do if you don’t want to use the shield as that uses SPI communication and without the shield, you’re stuck with normal uart. Here’s a modified version of the WiFly_WebClient that uses SoftwareSerial.
// (Based on WiFly's WebClient Example)
#include <SPI.h>
#include <WiFly.h>
#include <SoftwareSerial.h>
char passphrase[] = "yourpassphrase";
char ssid[] = "yourssid";
WiFlyClient client;
SoftwareSerial pin89Serial(8,9);
void setup() {
Serial.begin(9600);
pin89Serial.begin(9600);
WiFly.setUart(&pin89Serial);
WiFly.begin();
if (!WiFly.join(ssid, passphrase)) {
Serial.println("Association failed.");
while (1) {
// Hang on failure.
}
}
Serial.println("connecting...");
if (client.connect("reddit.com", 80)) {
Serial.println("connected");
client.println("GET / HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
} |
Upload that, open the serial console and you should see the HTTP output after a few seconds.
