If you have a Raspberry Pi, Onion Omega, Beaglebone or other Linux-based device, or if you just want to create a script in your PC to interact with Ubidots, here's a brief example on how to do it.

Requests: HTTP for Humans

Requests is a popular Python library that simplifies making HTTP requests. At this point I assume you're familiar with how to install Python packages using pip, so let's install the library:

$ pip install requests

Theory

First, let's look at how Ubidots API expects an HTTP request in order to process data:

  • Method: HTTP allows for several methods (GET, POST, PUT, DELETE, etc). To create a value in Ubidots we use a POST request.
  • URL:  The URL of the resource you want to access (things.ubidots.com/api/v1.6/your-device-label/?token=your-token)
  • HTTP Headers: Ubidots' API uses JSON as the data type, so the header would be "Content-Type:application/json"
  • Body: A JSON string containing the label of the variable(s) and their values. For example: {"temperature":23, "humidity":98}

Code

Finally, let's put these three items into an infinite loop in Python. For the purpose of this example I'll use Python's math library to create a sinusoidal wave so it looks cool in your Ubidots account ;) but in your case you want to change those math expressions for the actual measuring that you are doing:

import time
import requests
import math

c = 0

while(1):
    c = c + 1
    payload = {'temperature': round(20*math.cos(c),2), 'humidity': round(20*math.sin(c),2)}
    r = requests.post('http://things.ubidots.com/api/v1.6/devices/machine/?token=YOUR-TOKEN', data=payload)
    time.sleep(0.5)

Result

Run this code and you'll get something like this in your Ubidots account:

Did this answer your question?