The code below is part of the sketch that PUTS and GETS data from Pachube feeds. The PUT and GET parts over the internet are working. I can see that my Pachube feed is being updated by the Arduino by looking at it. I can see the GET is working by the screen scrape of the Monitor below with Serial.println of value 'c' enabled as below, 'c' holding the GET returned string. What is not working is that the returned data from Pachube is not geting to the point of Serial.Prinln. I can tell from inserting Serial.Print tests that the last part of the 'if' statement "if (found_session_id && (!found_CSV))" is not coming up with the rigth answer as I'll highlight below. Could somebody please advise how to fix this as I'm only just learning about this.
HERE IS WHAT IS SHOWING ON THE SERIAL MONITOR, The row "19.1,76,244,8.0,58.0HTTP/1.1 200 OK" is the returned data from Pachube starting with temp etc. so I know it's coming back to the Arduino OK but the loop part of the sketch that should be printing this data is not happening.
MONITOR SCRAPE
reset ethernet
Ready to connect: 98041
Connecting...
GET request to retrieve
finished GET now PUT, to update
finished PUT: 98177
HTTP/1.1 200 OK
Status 200
Server: nginx/0.6.34
Date: Wed, 07 Jul 2010 18:16:36 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Last-Modified: Wed, 07 Jul 2010 18:14:55 GMT
X-Runtime: 167
Cache-Control: private, max-age=0, must-revalidate
Content-Length: 20
Set-Cookie: _pachube_app_session=my_api_key_is_here; path=/; expires=Wed, 07 Jul 2010 18:36:36 GMT; HttpOnly
Vary: Accept-Encoding
Vary: Accept-Encoding
19.1,76,244,8.0,58.0HTTP/1.1 200 OK //the returned string
Status 200
Server: nginx/0.6.34
Date: Wed, 07 Jul 2010 18:16:37 GMT
Content-Type: text/plain; charset=utf-8
Connection: close
The sketch is jumping to the row I have hopefully highlighted in red. Can somebody help me fix this please. The row selected in blue does not appear to be giving the right answer. Which I am guessing is because it is not parsing the returned GET string correctly but I could be wrong.
char pachube_data[70];
boolean found_status_200 = false;
boolean found_session_id = false;
boolean found_CSV = false;
char *found;
unsigned int successes = 0;
unsigned int failures = 0;
boolean ready_to_update = true;
boolean reading_pachube = false;
boolean request_pause = false;
boolean found_content = false;
unsigned long last_connect;
int content_length;
void pachube_in_out(){
if (millis() < last_connect) last_connect = millis();
if (request_pause){
if ((millis() - last_connect) > interval){
ready_to_update = true;
reading_pachube = false;
request_pause = false;
found_status_200 = false;
found_session_id = false;
found_CSV = false;
Serial.print("Ready to connect: ");
Serial.println(millis());
}
}
if (ready_to_update){
Serial.println("Connecting...");
if (localClient.connect()) {
// here we assign comma-separated values to 'data', which will update Pachube datastreams
// we use all the analog-in values, but could of course use anything else millis(), digital
// inputs, etc. . i also like to keep track of successful and failed connection
// attempts, sometimes useful for determining whether there are major problems.
sprintf(pachube_data,"%d,%d,%d,%d,%d,%d,%d,%d",analogRead(0),analogRead(1),analogRead(2),analogRead(3),analogRead(4),analogRead(5), successes + 1, failures);
content_length = strlen(pachube_data);
Serial.println("GET request to retrieve");
localClient.print("GET /api/feeds/");
localClient.print(REMOTE_FEED_ID);
localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
localClient.print(PACHUBE_API_KEY);
localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
localClient.println("\n");
Serial.println("finished GET now PUT, to update");
localClient.print("PUT /api/feeds/");
localClient.print(SHARE_FEED_ID);
localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
localClient.print(PACHUBE_API_KEY);
localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
localClient.print("\nContent-Type: text/csv\nContent-Length: ");
localClient.print(content_length);
localClient.print("\nConnection: close\n\n");
localClient.print(pachube_data);
localClient.print("\n");
ready_to_update = false;
reading_pachube = true;
request_pause = false;
interval = UPDATE_INTERVAL;
Serial.print("finished PUT: ");
Serial.println(millis());
}
else {
Serial.print("connection failed!");
Serial.print(++failures);
found_status_200 = false;
found_session_id = false;
found_CSV = false;
ready_to_update = false;
reading_pachube = false;
request_pause = true;
last_connect = millis();
interval = RESET_INTERVAL;
setupEthernet();
}
}
while (reading_pachube){
while (localClient.available()) {
checkForResponse();
}
if (!localClient.connected()) {
disconnect_pachube();
}
}
}
void disconnect_pachube(){
Serial.println("disconnecting.\n=====\n\n");
localClient.stop();
ready_to_update = false;
reading_pachube = false;
request_pause = true;
last_connect = millis();
found_content = false;
resetEthernetShield();
}
void checkForResponse(){
char c = localClient.read();
Serial.print(c);
buff[pointer] = c;
if (pointer < 64) pointer++;
if (c == '\n') {
found = strstr(buff, "200 OK");
if (found != 0){
found_status_200 = true;
//Serial.println("Status 200");
}
buff[pointer]=0;
found_content = true;
clean_buffer();
}
if (found_session_id && (!found_CSV)){
found = strstr(buff, "HTTP/1.1");
if (found != 0){
char csvLine[strlen(buff)-9];
strncpy (csvLine,buff,strlen(buff)-9);
Serial.println("This is the retrieved CSV:");
Serial.println("---");
Serial.println(csvLine);
Serial.println("---");
Serial.println("\n--- updated: ");
Serial.println(pachube_data);
Serial.println("\n--- retrieved: ");
char delims[] = ",";
char result = NULL;
char * ptr;
result = strtok_r( buff, delims, &ptr );
int counter = 0;
while( result != NULL ) {
remoteSensor[counter++] = atof(result);
result = strtok_r( NULL, delims, &ptr );
}
for (int i = 0; i < REMOTE_FEED_DATASTREAMS; i++){
Serial.print( (int)remoteSensor); // because we can't print floats*
Serial.print("\t");
}
found_CSV = true;
Serial.print("\nsuccessful updates=");
Serial.println(++successes);
}
}
if (found_status_200){
found = strstr(buff, "_id=");
if (found != 0){
clean_buffer();
found_session_id = true;
}
}
}
regards
Mike