Raspberry Pi has become a widely used device, not only for prototyping and educational purposes, but also for production projects within companies.

Besides its small size, low cost and fully operational Linux OS, the greatest difference from your Destkop PC lies in its ability to interact with other peripherals through GPIO pins (General Purpose Input/Ourput Pins).

This allows you to code pretty robust hardware applications without having to be an expert in embedded electronics.

At the end of this guide you’ll be able to read and send data values from your Pi to Ubidots.

Requirements

To complete this tutorial, you’ll need:

  • A Raspberry Pi connected to the Internet

Setup

This guide assumes your Raspberry Pi has been configured and is already connected to the Internet. To learn how to do this, we suggest following this quick start guide from Raspberry Pi Foundation.

If you’re using a WiFi dongle, we suggest using Wicd to manage your WiFi connection.

Install Ubidots’ Python Library

We offer a Python library that makes it easier to send data to Ubidots from your Raspberry Pi, or any other computer. Type these steps in your terminal to install it.

Check your Internet connection

$ ping -c 2 8.8.8.8

You should see a response like this:

PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=53 time=60.212 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=53 time=64.001 ms

Update apt-get repositories:

$ sudo apt-get update
$ sudo apt-get upgrade

Install Ubidots Python Library

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install ubidots

Send one value to Ubidots

Let’s create a python script using your favorite text-editor. We’ll use “nano” in this case. This script sends a random value to Ubidots but it can be easily adapted to send values from digital inputs in your Raspberry Pi, or even analog readings when using special shields.

Create Python script

$ nano ubi_test.py

Copy this code:

from ubidots import ApiClient
import math
import time

# Create an ApiClient object

api = ApiClient(token='XXXXXXXXXXXXXXXXXX')

# Get a Ubidots Variable

variable = api.get_variable('521d792df91b2816f35c8587')

# Here is where you usually put the code to capture the data, either through your GPIO pins or as a calculation. We'll simply put an artificial signal here:

while(1):

    # Write the value to your variable in Ubidots
    response = variable.save_value({"value": 20*math.sin(10)})
    print response
    time.sleep(1)

Run the script

$ python ubi_test.py

Get one value from Ubidots

1.- Create a python script using your favorite text-editor. We’ll use “nano” in this case. See the step one

$ nano ubi-test-get.py

2.- Put the step two code into the created file. Please note the fields where you should put your API key and your variable ID.

from ubidots import ApiClient
import random

#Create an "API" object

api = ApiClient(token='XXXXXXXXXXXXXXXXXX')

#Create a "Variable" object

variable = api.get_variable("521d792df91b2816f35c8587")

#Get the value from Ubidots
last_value = variable.get_values(1)
print last_value[0]['value']

3.- Run the step three code several times to get the last value of a variable.

$ python ubi-test-get.py

Send multiple values to Ubidots

1.- Create a python script using your favorite text-editor. We’ll use “nano” in this case. See the step one

$ nano ubi-test-save-values.py

2.- Put the step two code into the created file. Please note the fields where you should put your API key and your variable ID.

from ubidots import ApiClient
import random#Create an "API" objectapi = ApiClient(token='XXXXXXXXXXXXXXXXXX')api.save_collection([
  {'variable': '557f686f7625426a41a42f49', 'value': 10*math.sin(10)},
  {'variable': '557f68747625426b97263cba', 'value': 20*math.sin(10)},
  {'variable': '557f68747625426fdd246872', 'value': 20*math.sin(10)}
])

3.- This script will send multiple values to Ubidots in a single request. You just run the Step Three code for this.

$ python ubi-test-save-values.py

Did this answer your question?