Hi everyone, I recently got the "official" WiFi shield, and I'm running into some trouble.
Basically, I would like to run a WiFiClient interaction with an external server in the setup() and then run a WiFiServer in the loop(). It sounds pretty simple, but I'm getting some baffling results. Here's a simple sketch for the task (95% copied from the WiFi library examples):
#include "SPI.h"
#include "WiFi.h"
char ssid[] = "belkin.3c2"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
WiFiServer server(80);
void setup() {
Serial.begin(9600);
int status = WL_IDLE_STATUS;
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
WiFiClient client;
char gserver[] = "google.com";
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(gserver, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET / HTTP/1.1");
client.print("Host:");
client.println(gserver);
client.println("Connection: close");
client.println();
}
delay(10000);
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.stop();
server.begin();
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("Hello");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
When you run the above code, the setup() WiFiClient successfully retrieves google.com, but the Server doesn't work when you try to visit the arduino from your browser. However, if you comment out the setup() WiFIClient part, then the arduino is accessible from the web.
I changed the WiFiServer to port 81, and left the setup() on port 80, but I had the same problem. I changed the setup() to port 81, and then nothing works (even when the WiFiServer part is entirely commented out). It just hangs at "Connecting to server."
Any ideas?
Edit: added code tags