I’m also seeing issues with TCP Client reads… if anyone can run this example and tell me what I’m doing wrong I’d appreciate it. I’m basically missing chunks of data, and I’m waiting plenty of time to get further chunks from the server. I’ve even increased the timeout in http_get() to 5 seconds and got the same response.
NOTE: When you run this you have to have your serial monitor closed, wait for the blue LED to come on, then open your monitor and press enter. It will pull data down from http://graph.facebook.com/sparkdevices and then timeout, and return to the main loop where it flashes the led until the next 10 second mark, and repeats the request. One thing to note client.connected() is always true until we call client.stop() so I didn’t use that for a timeout… had to use an actual timer. In the future client.connected() should be show the socket closed when the server terminates the connection, which should be after all of the data is sent.
I’m also seeing weird things with Serial TX… I need to put delays in there after large amounts of data or I don’t seem to see the data appear in the serial monitor.
#include "application.h"
TCPClient client;
String http_get(char const * hostname, String path) {
if (client.connect(hostname, 80)) {
client.print("GET ");
client.print(path);
client.print(" HTTP/1.1\r\n");
client.print("HOST: ");
client.println(hostname);
client.print("\r\n\r\n\r\n");
} else {
Serial.println("\r\n\r\nConnection Failed!");
client.stop();
return NULL;
}
int i = 0;
Serial.println("\r\n\r\n\r\nReading Facebook Data......");
uint32_t lastRead = millis();
while ((millis() - lastRead) < 1000) {
while (client.available()) {
char c = client.read();
Serial.print(c);
if(i++ > 50) {
delay(100);
i = 0;
}
lastRead = millis();
}
}
client.flush();
client.stop();
return "";
}
void setup() {
pinMode(D7, OUTPUT);
digitalWrite(D7, HIGH);
Serial.begin(115200);
while (!Serial.available()); // After Core D7 LED turns on, open serial monitor and press enter!
}
uint32_t nextTime = millis(); // next time to contact the server
int count = 19;
bool state = 1;
void loop() {
if (millis() > nextTime) {
nextTime = millis() + 500UL;
if (++count == 20) {
count = 0;
http_get("graph.facebook.com", "/sparkdevices");
} else { // toggle the led while we wait for 10 second mark
state = !state;
digitalWrite(D7, state);
}
}
}
This is what I get:
Reading Facebook Data......
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: text/javascript; charset=UTF-8
ETag: "38d35b9d9ec8dd27ed2ce90819473c873663d089"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no and pre-order the Spark Core and accessories at www.spark.io","category":"Computers\/technology","description":"Spark makes using and creating connected devices easy. The Spark Core an Arduino-compatible, Wi-Fi enabled development kit pairs with the Spark""},"products":"Spark Core, Spark Cloud","talking_about_count":395,"username":"sparkdevices","website":"http:\/\/www.spark.io","were_here_count":0,"id":"427120970660412","name":"Spark","link":"http:\/\/www.facebook