Pages: [1]   Go Down
Author Topic: client.available() returning strange data  (Read 200 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 525
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought client.available() is supposed to return the number of bytes available for reading.  See: http://arduino.cc/en/Reference/ClientAvailable
But when I use it like this:
Code:
  Serial.println(client.available());

I get stiff like this:
Code:
H344
T343
T342
P341
/340
1339
.338
1337
 336
2335

Here's my entire skecth
Code:
#include <Ethernet.h>
#include <SPI.h>

long unsigned int time = 0;

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] =  { 192, 168, 216, 50 };   // IP address of Ethernet Shield

char serverName[] = "api.pushingbox.com";
char url[] = "/pushingbox";
EthernetClient client; // create a client that connects to Google
boolean lastConnected = false; // State of the connection last time through the main loop

void setup()

  Serial.begin(9600);
  delay(1000);
  Serial.println("Begin Setup");

  Ethernet.begin(mac, ip);
  delay(1000); // give the Ethernet shield a second to initialize
  Serial.print(F("My IP address: "));
  Serial.println(Ethernet.localIP());
}

 
void loop()
{
  byte temp[4];
  static byte cnt;
 
  if ( millis() > time + 30000)
  {
    client.connect(serverName, 80);
    delay(1000);

    temp[0] = cnt++;
    temp[1] = 61;
    temp[2] = 92;
    temp[3] = millis();

   
    char sensorTemp[10];
    char feedData[150];
   
    strcpy(feedData, "devid=vB************8A");   // removed real devid for forum post
    for(int i=0; i<4; i++)
    {
      sprintf(sensorTemp, "&T%d=%d", i+1, temp[i]);
      strcat(feedData, sensorTemp);
    }
    postRequest(serverName, url, feedData);
    time = millis();
  }

  Serial.println(client.available());
  delay(100);

  // print the response from server
  // Should get HTTP/1.1 200 OK
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected)
  {
    Serial.println(F("disconnecting..."));
    client.stop();
  }
  lastConnected = client.connected();

}

void postRequest(char *hostName, char *url, String feedData)
{
  String buf = "POST " + String(url) + " HTTP/1.1";
 
  Serial.println(F("Data sent to server: "));
  Serial.println(buf);
  Serial.println("Host: " + String(hostName));
  Serial.println("Content-Type: application/x-www-form-urlencoded");
  Serial.println("Content-Length: " + String(feedData.length()));
  Serial.println("");
  Serial.println(feedData);
  Serial.println("");
  Serial.println("");

  client.println(buf);
  client.println("Host: " + String(hostName));
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Content-Length: " + String(feedData.length()));
  client.println("");
  client.println(feedData);
  client.println("");
  client.println("");
}
Logged

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

You are printing both the input and the count. If I break it up like this...
Code:
H  344
T  343
T  342
P  341
/  340
1  339
.  338
1  337
   336
2  335
As you read the characters "HTTP/1.1", the count decreases.
Logged

New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 525
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.  I didn't realize the remaining bytes from client.available() were getting mixed in with the  Serial.print(c); characters.  I see my mistake now.
Logged

Pages: [1]   Go Up
Jump to: