Pages: [1]   Go Down
Author Topic: client.println output won't show in serial window  (Read 316 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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() {

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: