[SOLVED] Linkit One HTTP POST JSON

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.

Can someone help me out? :smile:

Hi @tls,

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();
  
}

Note the response “201” right after the request:

Sending value to Ubidots…
…HTTP/1.1 201 CREATED
Server: nginx
Date: Sat, 25 Apr 2015 06:10:13 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept
Location: http://things.ubidots.com/api/v1.6/values/553b2fc576254264ec0306fa
Allow: GET, POST, HEAD, OPTIONS

b1
{“url”: “http://things.ubidots.com/api/v1.6/values/553b2fc576254264ec0306fa”, “value”: 216.0, “timestamp”: 1429942213828, “context”: {}, “created_at”: “2015-04-25T06:10:13.828”}
0

HTTP/1.1 400 Bad Request
Server: nginx
Date: Sat, 25 Apr 2015 06:10:13 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

Here the results: Ubidots |

Oh, I left out the UTF-8 encoding part. Works like a charm now. Thanks a million!

Awesome, you’re welcome.

Thank you for the hints.

It looks like ubidots doesn’t like the leading 0 as in 01.34.

Removing the leading 0 solved the problem.

I suggest to reword the explanations for the 2 fields:

  • API token in token[]
  • Variable ID as source key in path[]
1 Like