Posting data via HTTP Arduino Wifly library
I am trying to migrate a project that used to post from an Arduino with a Tinysine Wifly shield (from Roving networks) to a php form/mysql db using this code snippet:
void reportToCloud() {
data = "";
Serial.println("Reporting to cloud...");
if (wifly.available() > 0) {
char ch = wifly.read();
Serial.write(ch);
if (ch == '\n') {
/* add a carriage return */
Serial.write('\r');
}
}
if (wifly.open(site, 80)) {
Serial.print("Connected to ");
Serial.println(site); // Set data to send
static char outstr1[15];
static char outstr2[15];
static char outstr3[15];
static char outstr4[15]; String dataString1 = dtostrf(uvindex, 8, 2, outstr1);
String dataString2 = dtostrf(mq2ratio, 8, 2, outstr2);
String dataString3 = dtostrf(CO2PPM, 8, 2, outstr3);
String dataString4 = dtostrf(temperature, 8, 2, outstr4);
data = String("uvindex=" + dataString1 + "&mq2=" + dataString2 + "&age=" + dataString3 + "&name=" + dataString4);
Serial.print(data); //name = temp && age = co2
//Reset all values
uvindex = 0;
mq2ratio = 0;
CO2PPM = 0;
temperature = 0; /* Send the request */
wifly.println("POST /arduino/data_post.php HTTP/1.0");
wifly.println("Host: www.santiapps.com"); // SERVER ADDRESS HERE TOO
wifly.println("Content-Type: application/x-www-form-urlencoded" );
wifly.print("Content-Length: ");
wifly.println(data.length());
wifly.println();
wifly.print(data);
Serial.println("Posted successfully");
} else {
Serial.println(">>Failed to connect");
} if (Serial.available() > 0) {
wifly.write(Serial.read());
} //Added Sat 14 Nov @820am
wifly.close();
}Now Im trying to make it post to thingspeak for improved analysis capabilities and what not. I get a post successful but I get no data in my thingspeak channel. I've checked the string used and its fine:
void reportToCloud() {
data = "";
Serial.println("Reporting to cloud...");
if (wifly.available() > 0) {
char ch = wifly.read();
Serial.write(ch);
if (ch == '\n') {
/* add a carriage return */
Serial.write('\r');
}
}
if (wifly.open(site, 80)) {
Serial.print("Connected to ");
Serial.println(site); // Set data to send
static char outstr3[15];
static char outstr4[15];
String dataString3 = "33.33";//dtostrf(33.33, 8, 2, outstr3);//dtostrf(CO2PPM, 8, 2, outstr3);
String dataString4 = "44.44";//dtostrf(44.44, 8, 2, outstr4);//dtostrf(temperature, 8, 2, outstr4);
data = String("field3=" + dataString3 + "&field4=" + dataString4);
//Serial.print(data); //for debugging //Reset all individual values
CO2PPM = 0;
temperature = 0; /* Send the request */
String postData = "POST /update?api_key=mykey&"+data+" HTTP/1.0";
//Serial.println(postData);
wifly.println("Host: api.thingspeak.com"); // SERVER ADDRESS HERE TOO
wifly.println("Content-Type: application/x-www-form-urlencoded" );
wifly.print("Content-Length: ");
wifly.println(postData.length());
wifly.println();
wifly.print(postData);
Serial.println("Posted successfully");
} else {
Serial.println(">>Failed to connect");
} if (Serial.available() > 0) {
wifly.write(Serial.read());
} //Added Sat 14 Nov @820am
wifly.close();
}postData is printed with this result:
printing co2... 87 105 102 108 121 45 87 101 98 false returned Free memory: 670 setPrompt hasnt been called Already joined network DeviceID: Wifly-WebClient2 Reporting to cloud... open santiapps.com 80 Connected to santiapps.com POST /update?api_key=mykey&field3=33.33&field4=44.44 HTTP/1.0 Posted successfully close: failed, no *CLOS* Reporting to cloud...
open santiapps.com 80 Connected to santiapps.com POST /update?api_key=mykey=33.33&field4=44.44 HTTP/1.0 Posted successfully close: failed, no *CLOS*
So basically the first snippet works fine and posts to the db via my php form and i can then read it off the db. The second snippet returns what seems to be a successful post but the data is not posted to thingspeak.
15 Comments
Time DescendingYou might be disconnecting before the server acknowledges your post. Thins may result in your post or its data being rejected. I'm not clear which ones are snipper 1 and 2 above. Which one works and which one doesn't?
I noticed in one part you said
"POST /update?api_key=mykey=33.33&field4=44.44 HTTP/1.0"
This is missing a "&field..."