Go Down

Topic: client.println output won't show in serial window (Read 376 times) previous topic - next topic

When I try to use this code below, I just want to see the result of my http request
through the serial monitor. Unfortunately, client.println is not displaying results.

Any reason why?


#include <SPI.h>
#include <WiFi.h>

char ssid[] = "username";          //  your network SSID (name)
char pass[] = "password";   // your network password

int status = WL_IDLE_STATUS;
char servername[]="www.arduino.cc";  // remote server we will connect to

WiFiClient client;

void setup() {
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    // don't do anything else:
    while(true);
  }
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
    if (client.connect(servername, 80)) {
      Serial.println("connected");
      // Make a HTTP request:
      // byte joe[2000];
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
      while (client.available()) {
        char c = client.read();
        Serial.write(c);
      }
        client.println();
    }
  }
}

void loop() {

}

PaulS

Quote
Unfortunately, client.println is not displaying results.

Perhaps because the Serial Monitor is not a client. Serial.print() and Serial.println() are used to send data to the Serial Monitor, not client.print() or client.println().

o boy... I feel really dumb right now haha. Thanks though. What can i use as a client? -suggestions

SurferTim

Do you want to see the request you are sending or the response from the server? If it is the server response you want to see, then you are not waiting for the response. This is the basic code. The server will close the connection when finished sending packets. After you send the request, this gets the response.
Code: [Select]
 while(client.connected()) {
     while (client.available()) {
       char c = client.read();
       Serial.write(c);
     }
 }
 client.stop();

This needs a timeout in that loop to prevent a lockup if the connection breaks.

apparently, there are just problems between the wifi shield and mac OSX. Thanks for the help

Go Up