7 years ago.

Posting to Ubidots

I'm looking to make an IoT project and am using an ESP8266 loaded with NodeMCU's Lua interpretter and what I want to do is post to Ubidots.com it says to POST a request to:

http://things.ubidots.com/api/v1.6/datasources/{ds_id}/variables Indicating at least the name of the Variable in a JSON object in the Http body:

{"name":"Temperature"}

with an example:

POST /api/v1.6/datasources/{DS_ID}/variables/?token={TOKEN} HTTP/1.1 Host: things.ubidots.com Content-Type: application/json Content-Length: 35

{"name":"Temperature", "unit":"ºC"}

Remember to change {DS_ID} with the ID of the Device where you want to create the variable, and {TOKEN} with a valid token from your account.

How would you implement this in mbed? I have already got my ESP8266 connected to my network.

1 Answer

6 years, 6 months ago.

Hi there, I used the code below to send data once a time ago, please use it as reference

include the mbed library with this snippet

function sendValue(value,token,varID)
srv = net.createConnection(net.TCP, 0)
srv:connect(80,"things.ubidots.com")
print("Sending POST")

-- Waits for connection befor of sending values
srv:on("connection", function(sck, c) token = c return token end)


var = '{"value": '..dato..'}';
num = string.len(var);
local cadenaPOST = "POST /api/v1.6/variables/"..varID.."/values
HTTP/1.1\r\n"
.."Content-Type: application/json\r\n"
.."Content-Length: "..num.."\r\n"
.."X-Auth-Token: "..token .."\r\n"
.."Host: things.ubidots.com\r\n\n"
..var.."\r\n";
sck:send(cadenaPOST)
end)

srv:close();
end

Also, at this thread: http://community.ubidots.com/t/solved-400-bad-request-using-lua-w-esp8266/1105 you may find an useful conversation about sending data to Ubidots using LUA.

Regards