Need help posting data using RN171 wifi shield and wifly library | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Need help posting data using RN171 wifi shield and wifly library
Avatar
heinb
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
1
June 3, 2016 - 2:34 am
sp_Permalink sp_Print

 Hi there

 

I am completely stuck and would really like some input.  I can get a stable connection to Thingspeak, but all my attempts to post data re unsuccessful.  I can either post an empty entry (It shows I have a new entry, with null values for the fields) or I can get a 400 Bad Request returned, but that's it.  Any pointers will be welcome!

I am using Wifly.h and connects successfully to the wifi and external sites.

Here is the code for the Bad Request:

I call this function in my main loop:

ThingSpeakUpdate("field1="+String(x)+"&field2="+String(y));

And here is the function:

void ThingSpeakUpdate(String tsData)
{
Serial.println("Data string: " + tsData);

Serial.println("...Connecting to Thingspeak");

// Connecting and sending data to Thingspeak

if(Wifly.connect("api.thingspeak.com","80"))
{
Serial.println("...Connection succesful, updating datastreams");

Wifly.println("POST /update HTTP/1.1");
Wifly.println("Host: api.thingspeak.com");
Wifly.println("Connection: close");
Wifly.println("X-THINGSPEAKAPIKEY: "+myWriteAPIKey);
Wifly.println("Content-Type: application/x-www-form-urlencoded");
Wifly.println("Content-Length: ");
Wifly.println(tsData.length());
Wifly.println();
Wifly.println(tsData);
delay(200);
Serial.println("Thingspeak update sent.");
}
else{
// Failed to connect to Thingspeak
Serial.println("Unable to connect to Thingspeak.");
}

Avatar
heinb
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
2
June 3, 2016 - 7:58 am
sp_Permalink sp_Print

I managed to solve the issue.  Here are the steps that might help, even for those who uses other hardware and libraries:

1. I knew that the url to post worked when I put it in a web browser - it updated my feed:

https://api.thingspeak.com/update?key=xxxxxxxxxxxxxx&field1=10    (obviously I replaced the x's with my write key)

2. I was thinking that my shield was adding info to my header and this caused  the "Bad Request" error, so I went to 

http://web-sniffer.net/

and posted my url into the box provided.  This then returned the info sent to the server and it looked like this:

Connect to 52.7.53.111 on port 443 ... ok

GET /update?key=PONK48XZ3V2APBKW&field1=10 HTTP/1.1[CRLF]
Host: api.thingspeak.com[CRLF]
Connection: close[CRLF]
User-Agent: Web-sniffer/1.1.0 (+http://web-sniffer.net/)[CRLF]
Accept-Encoding: gzip[CRLF]
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
Cache-Control: no-cache[CRLF]
Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
Referer: http://web-sniffer.net/[CRLF]
[CRLF]


From there I created my commands to the website by changing the code in my function.  This is the working function:

Command to call the function in my main loop:

ThingSpeakUpdate("field1="+String(x)+"&field2="+String(y));
delay(15000);


And here is my working function:

void ThingSpeakUpdate(String tsData)
{
 Serial.println("Data string: " + tsData);

 Serial.println("...Connecting to Thingspeak");

 // Connecting and sending data to Thingspeak
 
 if(Wifly.connect("api.thingspeak.com","80"))
 {
 Serial.println("...Connection succesful, updating datastreams");
 
 Wifly.println("GET /update?key=PONK48XZ3V2APBKW&"+tsData+" HTTP/1.1");
 Wifly.println("Host: api.thingspeak.com");
 Wifly.println("Connection: close");
 delay(200);
 Serial.println("Thingspeak update sent.");
 }
 else{
 // Failed to connect to Thingspeak
 Serial.println("Unable to connect to Thingspeak.");
 }

Interestingly enough, I did not have to specify the packet lenght.  I hope this helps others aswell.
Avatar
loli72
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
3
May 29, 2017 - 4:59 pm
sp_Permalink sp_Print

Hello, could you please post your entire code. At least the code to connect this wifi schield to Thingspeak as a client. I manage to connect whit a ethernet schield but I'm lost with this one.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
28 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

candacele1

WilliamBer

roxieob18

Metawatch

ozayagw

ubabeutiti

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee