[SOLVED] HowTo: Send output of a program to Ubidots with curl?


#1

Hello Ubidots Community!

I have an USB Air Sensor for the Raspberry Pi and I want to send it’s values to my Ubidots account. The program to get the airquality is called “airsensor”:

./airsensor -v
450
453
457
460
453

How can I send these continuous (~ every 15 seconds) output values to Ubidots?

I already tried piping it with curl, but without success. :confused:

./airsensor -v | curl -X POST --data-urlencode value@- https://things.ubidots.com/api/v1.6/variables/[VARIABLE-ID]/values/?token=[TOKEN-ID]


#2

Greetings, if you are using a RPI I would advise you to use python for sending data, in this article you can find a ‘how to’.

Now, if you want to use curl, below you can find an example of the command structure:

curl -X POST -H "Content-Type: application/json" -d '{"value":11.2, "context":{"lat":1.12, "lng":2.019}, "timestamp":1000199992}' http://things.ubidots.com/api/v1.6/devices/PUT_HERE_YOUR_DEVICE_LABEL/PUT_HERE_YOUR_VAR_LABEL/values?token=PUT_HERE_YOUR_TOKEN

About your question using the pipe utility of Linux, I would advise you to use a concatenated command instead of trying to pipe your result, below you can see some examples:

echo -e '{"value":11.2, "context":{"lat":1.12, "lng":2.019}, "timestamp":1000199992}' | curl -X POST -H "Content-Type: application/json" -d @- "http://things.ubidots.com/api/v1.6/devices/[DEVICE]/[VARIABLE]/values?token=[TOKEN]"
 A=$(echo -e '10') && curl -X POST -d "value=$A" "http://things.ubidots.com/api/v1.6/variables/[VAR-ID]/values?token=[TOKEN]"

Hope it helps you.


#4

I tried your 3rd command, but I can’t find out how to send the numbers from the ./airsensor -v tool instead of static values.

 A=$(echo -e ./airsensor -v) && curl -X POST -d "value=$A" "http://things.ubidots.com/api/v1.6/variables/[VAR-ID]/values?token=[TOKEN]"

leads to : {"value": ["A valid number is required."]}

:tired_face:


#5

Hi there, as you are using bash commands that returns a value in a infinite loop with a fixed time difference you should capture that result using the ‘read’ command in an infinite loop, I believe that something like the snippet below should work properly for your case:

./airsensor -v | while read line; do curl -X POST -d "value=$line" "http://things.ubidots.com/api/v1.6/variables/[VAR-ID]/values?token=[TOKEN]" ; done

Hope it helps you.

Regards


#6

I am at best confused and at worst dazed. I have tried to POST data to my ubidots device using curl without any success. I just get, to me, un-helpful replies.

I used the curl example above as it was written:

curl -X POST -H “Content-Type: application/json” -d ‘{“value”:11.2, “context”:{“lat”:1.12, “lng”:2.019}, “timestamp”:1000199992}’ http://things.ubidots.com/api/v1.6/devices/PUT_HERE_YOUR_DEVICE_LABEL/PUT_HERE_YOUR_VAR_LABEL/values?token=PUT_HERE_YOUR_TOKEN

with my device, variable labels and API-Token in place of the placeholders, and in response I received:
curl: (3) [globbing] unmatched close brace/bracket in column 10
curl: (3) [globbing] unmatched close brace/bracket in column 21
{“detail”: “Authentication credentials were not provided”}

So I edited the command to:

curl -X POST -H “Content-Type: application/json” -d ‘{“value”:11.2}’ http://things.ubidots.com/api/v1.6/devices/PUT_HERE_YOUR_DEVICE_LABEL/PUT_HERE_YOUR_VAR_LABEL/values?token=PUT_HERE_YOUR_TOKEN

and sent that. In response, I got:

{“detail”: “Authentication credentials were not provided”}

So I edited the line further to:

curl -X POST -d ‘{“value”:11.2}’ http://things.ubidots.com/api/v1.6/devices/PUT_HERE_YOUR_DEVICE_LABEL/PUT_HERE_YOUR_VAR_LABEL/values?token=PUT_HERE_YOUR_TOKEN

and in response got:

{“value”: [“This field is required.”]}

So I figured out how to get rid of the “globbing” error by using -g or escaping the double quotes in the JSON and enclosing the -d data with double quotes. But why does the last one give me the “This field is required” message? And why did the previous attempt give me the “Authentication credentials were not provided” message but the last didn’t when I used the same credentials for both. And finally, how can I actually send a POST using curl? I assume I am doing something incorrectly but what that is, is not apparent to me.


#7

I finally figured out my problem. My variable label was “Temperature” and I have been sending it “temperature”. When I sent this:

curl -g -X POST -H “Content-Type: application/json” -d “{“value”:11.2}” http://things.ubidots.com/api/v1.6/devices/demo/Temperature/values?token=[API Token]

it worked. Unfortunately, none of the error messages/responses led me to that.


#8

Hi there @larrycook99 , you have to make available the server’s answer to make additional debug, if you use curl put “-vvv” at the end of your curl command to get all the feedback from the server.

Regards