Go Down

Topic: WiFi shield help please (Read 1 time) previous topic - next topic

wdb22

Oct 11, 2012, 09:08 am Last Edit: Oct 11, 2012, 04:50 pm by wdb22 Reason: 1
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):

Code: [Select]

#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

johnwasser


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."


Of course it fails since Google.com is not serving on port 81.

What does it display for your "IP Address"?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

wdb22

Thanks for the response.

I thought that changing to "client.connect(gserver, 81)" would make my Arduino use its own port 81 for that communication. Thanks for clarifying.

The first serial messages that I get are:
Attempting to connect to SSID: belkin.3c2
Connected to wifi
SSID: belkin.3c2
IP Address: 192.xxx.2.27

except digits in place of 'x's - I just don't want to put the whole ip address up on the web. I can successfully go to this ip address in my browser and retrieve the "Hello" greeting from my arduino when the setup()'s WiFiClient code is commented out.

As another update, there's a current thread where someone is trying to do the same thing using the Ethernet shield at (http://arduino.cc/forum/index.php/topic,126545.0.html). And a good response is at (http://arduino.cc/forum/index.php/topic,80137.15.html). I'm not sure what all the relevant differences are between doing it with Ethernet and WiFi, though. One point that was brought up in that thread is that "client.stop()" can fail if there is data in the receive buffer, so that might be related to my problem.

johnwasser


IP Address: 192.xxx.2.27

except digits in place of 'x's - I just don't want to put the whole ip address up on the web.


Don't worry.  192.168.x.x is one of the "non-routable" Ip address ranges used by LANs.  Nobody can reach that address from outside your network unless you set up port forwarding on your router.  10.x.x.x is another such range.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

wdb22

Oh, I see. Yeah it's 192.168.2.27 - I'm learning a lot here.

Go Up