Post Data to ThingSpeak - Bad Request Problem Solved

In this post we will see the series of AT Commands that you may use from any serial terminal in order to successfully post data to ThingSpeak IoT Cloud.  Many people have reported of getting a Bad Request error returned when they tried to post data to the ThingSpeak server. It was indeed very tricky to figure this out a way to fix the Bad Request problem. Hence I am sharing this information as it was very troublesome to find all the required information in one place on the Internet.

Pre-Requisites

This post assumes that you already have an ESP8266 and a FTDI FT232RL USB to TTL Converter connected properly. You can refer for full connection details here.

The Data Logging Session

For sending data to ThingSpeak use the following series of AT commands from the Arduino Serial Monitor or from any other terminal of your choice. Set the Serial baud rate at 11500 and Both NL & CR. Then execute the following AT Commands.

AT

AT+CWLAP

AT+CWJAP="your-wifi-ssid","your-password"

AT+CIPSTART="TCP","184.106.153.149",80

AT+CIPSEND=60

GET /update?key=<your-channel-api-write-key>&field1=56\r\n        (from my Arduino IDE's Serial Monitor, I had to send this line 2 times...I don't know why yet)

AT+CWQAP

That is all! This should work fine. My Serial window snip is shown below for reference.

Post Data to ThingSpeak - Bad Request Problem Solved


Summary - The Trick

In the series of AT commands above notice the line "AT+CIPSEND=60", the number 60 here means the number of text characters (technically bytes) that will be sent in the request. It is referred to as the Data Length. Read below and see how to manipulate this Data Length setting in order to solve the Bad Request problem.

The request text is in the line "GET /update?key=<your-channel-api-write-key>&field1=56\r\n". In my case, the number of characters in this request text was 46 (including the last \r\n characters, you will have to count them as well). However, I specified 60 as the Data Length. This is the trick. You will have to mention the Data Length a little bit higher than the actual size of the Request Text length.

Cheers!
KBRC


If you liked this post, then your encouragement by performing a G+ and a share is most welcome and highly appreciated!

No comments:

Post a Comment