I am attempting to use an Arduino Uno+WiFly setup to send data to Thingspeak. I have tried a few libraries by now. But nearly all of them don't have a working WebClient example (WebServer works fine). I am using Arduino 1.6.3 IDE for Arduino UNO R3 + Wifly-RN171 with Firmware 4.0
The libraries I have tried (found through Google and forums):
https://github.com/sparkfun/WiFly-Shield/tree/master/Libraries/Arduino
A ThingSpeak example is given but the execution stops when WiFly.begin() is called.
I tried WebClient and WebClient(Faster) examples from the same library and they are both stuck at the same line: WiFly.begin().
I believe this could be compatibility issue (WiFly firmware version/Arduino) but am unsure how to fix it.
Note: There seems to be a few versions for the SparkFun WiFly library from my search results. I tried whatever I found but only listed the latest one since if it is indeed a compatibility issue a newer version should be more likely to work.https://github.com/Seeed-Studio/WiFi_Shield
This one has a http example which I am attempting to use to send data to Thingspeak through http get/post. I am getting 'Bad Request' from their nginx server. Any help in the right direction will be much appreciated.
I hosted the sketch I tried here: codebender.cc/sketch:276925
p.s. I am not allowed to post>2 links due to reputation so the last one is plain text.
p.s.s Can someone with higher reputation help me add the new tags {thingspeak, wifly, rn171}
1 Answer 1
I solved my problem by sending a GET request to a hosted PHP script which in turn updates Carriots by means of a Curl request.
My PHP Script
<?php
$humidity=$_GET['humidity'];
$data = array("protocol"=>"v2", //Compose JSON stream
"at"=> "now",
"device"=>"DEVICE_NAME",
"data" => array ("humidity" => trim($humidity)),
"checksum" => "");
$data_string = json_encode($data);
$ch = curl_init('https://api.carriots.com/status'); //Compose the HTTP request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'carriots.apikey: YOUR_API_KEY',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch); //Send the stream
Note:
- I switched to Carriots from ThingSpeak due to personal preference.
- This is by no means a standard way to send stream to Carriots.This involves additional effort which slows down transmission of data but I decided that this setup will be enough for a hobby project.
- A good understanding of how POST requests are built should allow me to skip the extra PHP script completely.