All Collections
Connect your Devices
Connect your Intel Edison to Ubidots using Python and MRAA library
Connect your Intel Edison to Ubidots using Python and MRAA library

Learn how to setup the Intel Edison to talk to Ubidots using Python.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

Intel Edison and Ubidots (using Python and MRAA library)

The Intel Edison is a tiny computer-on-module offered by Intel as a development system for the Internet of Things. It features a dual-core Intel Quark x86 CPU at 400 MHz communicating via Bluetooth and Wi-Fi.

Requirements

Software Setup

Please go through Intel’s Get Started guide to do a basic setup of your Intel Edison.

Before doing this tutorial, please make sure your Edison is running the latest Yocto image. This guide was tested with Yocto 3.0. To get the latest image go to Intel® Edison Board Software Downloads.

1. Access to the Intel Edison with the IP address of the board as root via ssh with the following command - ssh root@<IP_addres> 

Once you have entered the password for your Intel Edison, verify if you have access to the board properly; reference image below for help. If you see root@edison like that which is highlighted below, you are connected.

2. Next, type the command below into your computer terminal to install the required package:

$ opkg install python-pip

IMPORTANT NOTE: you will have to install the Ubidots Python Library as well. Please follow the installation steps in the README of this repository on Github and return to this this tutorial when finished.

3. For managing the analog pins or digital pins of the Edison you’ll need to download the MRAA library. Please reference to this video tutorial to learn how to install simply:

Ubidots Setup

  1. Enter to your Ubidots account to creates the variables needed to manage the data. Click on the Device tab and press "Add Device", assign to the device a friendly name of your preference; I decided to call ours "Edison."

2. Open the device you've just created, press "Add Variable" to create a variable for each sensor; sensor1; sensor2; relay. 

Once the variables are created, you will have to assign the variables ID of each sensor to the code. If you do not know how to find the variable ID, reference to the article below:

Also you need to assign your Ubidots TOKEN to the code, if you don't know how get it, please reference this article below:

3. Go to the Dashboard section and add a control widget which will be tasked with sending the different values to the variable relay. 

In the upper right side of the webpage, select "Create widget":

 Then select control > switch > edison > relay > finish.


As you can see, the control widget is created on your dashboard, and now you can proceed with this guide. Once you've finished with this go back to the dashboard to change the status of the variable and visualize how the relay status changes.

Hardware Setup

Place the Arduino grove Shield within the Arduino expansion board, lining up the holes on Arduino grove shield with those of the expansion board. Press down on Arduino grove shield to create a firm connection.

The connections for the sensors are the following for in this guide:

Please, reference to the image below to verify the connections we made:

Management of Data with Ubidots

Now that you're board is assembled, please follow these steps below to manage your data with the Ubidots cloud and your custom application.

1. Create the folder and the script that we're going to run. To do this, enter the commands below into the computer terminal:

root@edison:~# mkdir ubidots 
root@edison:~# cd ubidots/
root@edison:~/ubidots# touch inteledison.py

2. Once the script called "inteledison.py" is created we're going to open it with nano editor, executing this line:

root@edison:~/ubidots# nano inteledison.py

Now you have to attach the code below to the newly opened editor. Once you've pasted the code you'll have to assign the TOKEN and variables IDs where indicated within the code.

'''
This script sends/receive values to/from Ubidots

'''

'''
Libraries to import
'''

import time
import os
import mraa

from ubidots import ApiClient

'''
Define Constants
'''
TOKEN = "Assing_your_Ubidots_token_here"  # Put here your TOKEN
VAR_ID_SENSOR1 = "Assing_the_variable_ID_here"  # Put here your variable's ID
VAR_ID_SENSOR2 = "Assing_the_variable_ID_here"  # Put here your variable's ID
VAR_ID_RELAY = "Assing_the_variable_ID_here"  # Put here your variable's ID

'''
Initial variables
'''
relay_value = 0

'''
Initialize the pins as AIO/GPIO and set its mode
'''

# AIO pins - Analogs pin
a0 = mraa.Aio(0)
a1 = mraa.Aio(1)

# DPIO pin - Digital pin
relaypin = mraa.Gpio(7)
relaypin.dir(mraa.DIR_OUT)


def sendValues(api):

    # Retrieve the variable you'd like the value to be saved to
    sensor1 = api.get_variable(VAR_ID_SENSOR1)
    sensor2 = api.get_variable(VAR_ID_SENSOR2)

    # Saving the sensor values
    print "sending sensor 1 value"
    sensor1_value = sensor1.save_value({'value': a0.read()})
    print "sending sensor 2 value"
    sensor2_value = sensor2.save_value({'value': a1.read()})

    print "Values sent to Ubidos"


def getValue(api):
    global relay_value, relaypin
     
    relay = api.get_variable(VAR_ID_RELAY)

    # Retrieve the variable you'd like the value to be saved to
    last_value = relay.get_values(1)

    # Getting the last value to the variable from Ubidots
    relay_value = int(last_value[0]['value'])
   
    #print relay_value # Uncomment this line to visualize the status of the pin

    # Relay's control
    if relay_value >= 1:
        print "Switch ON"
        relay_value = 1

    else:
print "Switch OFF"
relay_value = 0
   
    print "Values received from Ubidots"

if __name__ == '__main__':
   
    api = ApiClient(token=TOKEN)
    while True:
        sendValues(api)
        getValue(api)
        relaypin.write(relay_value)

    time.sleep(1)

To save the script press "control + X", then key in "y" to save the changes and press enter.

3. Now its time to run the script with the following line in your computer terminal:

root@edison:~/ubidots# python inteledison.py

Now, go back to your Ubidots application to check the data is being received in the Device section of your custom application:

Also, on the Dashboard section you are able to control the relay using the "Switch" control widget:

Result

Now it is time to create a dashboard to control and manage the variables of your Intel Edison. To learn more about Ubidots widgets and events, check out these video tutorials.

Happy hacking :)

Did this answer your question?