Hi, I copied the code from http://ubidots.com/docs/devices/linkitone.html and I changed the GPRS to WiFi. Then I tried uploading the JSON to Ubidots but it did not work and I keep getting bad request HTTP response from JSON parsing error. Must the JSON string be formatted in a special way when uploaded from Linkit One. Some arduino forum posts talked about url encoding but I tried and it did not work.
Hereâs a sample code we wrote for your WiFi tests. We also got a lot of âbad requestâ responses while doing it. In fact the response is positive (201) and then comes a short 401, but starts positive again in the new loop:
/*
Based on the "Web client" example by Mediatek
This sketch connects to Ubidots and sends incremental values to a variable
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 20 Aug 2014
by MediaTek Inc.
modified 25 April 2015
by Ubidots Inc.
*/
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#define WIFI_AP "Abriles_LTE"
#define WIFI_PASSWORD "12345678"
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
// Ubidots information
#define URL "things.ubidots.com"
#define TOKEN "xcxcxscxscmPvcvcwauSvcvcJEA2vceEIn" // replace with your Ubidots token generated in your profile tab
#define VARIABLEID "553a952e7625420911d016f3" // create a variable in Ubidots and put its ID here (http://app.ubidots.com/ubi/datasources/)
int value = 0;
void setup()
{
LTask.begin();
LWiFi.begin();
Serial.begin(9600);
// keep retrying until connected to AP
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}
}
void loop()
{
value++;
save_value(String(value));
// delay(500); // Set here the desired update frequency
}
void save_value(String value){
Serial.println("Sending value to Ubidots...");
LWiFiClient c;
while (!c.connect(URL, 80))
{
Serial.println("Retrying to connect...");
delay(100);
}
String data = "{\"value\":"+ value + "}";
String thisLength = String(data.length());
// Build HTTP POST request
c.print("POST /api/v1.6/variables/");
c.print(VARIABLEID);
c.print("/values?token=");
c.print(TOKEN);
c.println(" HTTP/1.1");
c.println("Content-Type: application/json");
c.println("Content-Length: " + thisLength);
c.print("Host: ");
c.println(URL);
c.print("\n" + data);
c.print(char(26));
// read server response
while (c){
Serial.print((char)c.read());
}
c.stop();
}