UbiMakerMaka Hernandez
Published © CC BY-NC-SA

Connect Your RevPi Core + RevPi DIO to Ubidots

Integrate Industrial grade controls (I/O) and monitoring with the RevPi DIO module and Ubidots; including setup for a Unit Counting App.

IntermediateProtip3 hours1,484
Connect Your RevPi Core + RevPi DIO to Ubidots

Things used in this project

Software apps and online services

Ubidots
Ubidots

Story

Read more

Code

Code snippet #15

Plain text
################################################################################
# This script send the Digital Input(DI) status of a pin from the Revpi DIO
# expansion module to the Ubidots Cloud (https://ubidots.com/)
#
# Authors: M. Hernandez
################################################################################

import requests
import time
import revpimodio2
from uuid import getnode as get_mac

TOKEN = "A1E-dfBFE58BECK3YgAia1Sss2H9WTvW4I" # Assign your Ubidots TOKEN
VARIABLE = "motion-detector" # Assign the Ubidots Variable label
DELAY = 2 # Set the delay desired to post the data

revpi = revpimodio2.RevPiModIO(autorefresh=True) # Instance to access to the IOs of the RevPi

s = requests.Session()
s.headers.update({'x-test': 'true'})

'''
This method build the JSON to be sent to the Ubidots Cloud
'''
def build_json(variable, value):
    try:
        # This structure send one value to the Ubidots Cloud. Please reference to the
        # Ubidots REST API Reference (https://ubidots.com/docs/api/) to learn how to
        # send more thant one value
        data = {variable: value}
        return data
    except:
        return None

'''
This method make the HTTP Request to the Ubidots Cloud
'''
def post_variable(device, variable, value):
    try:
        # Reference to the Ubidots REST API Reference (https://ubidots.com/docs/api/)
        # to learn how to build the HTTP Request to the Ubidors Server
        url = "https://things.ubidots.com/api/v1.6/devices/" + device
        headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
        data = build_json(variable, value)
        response = s.post(url=url, headers=headers, json=data)
        return response.json()
    except:
        pass

if __name__ == "__main__":
    while True:
        mac = get_mac() # get the mac address of your device
        device_mac = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
        # Read the status of the Input called 'I_1'
        sensor_status = float(revpi.io.I_1.value)
        # Send the POST Request to the Ubidots Server
        print("Posting values to Ubidots")
        request = post_variable(device_mac, VARIABLE, sensor_status)
        print(request)
        time.sleep(DELAY)

Code snippet #16

Plain text
################################################################################
# This script send the Digital Input(DI) status of a pin from the Revpi DIO
# expansion module to the Ubidots Cloud (https://ubidots.com/)
#
# Authors: M. Hernandez
################################################################################

import requests
import time
import revpimodio2
from uuid import getnode as get_mac

TOKEN = "A1E-dfBFE58BECK3YgAia1Sss2H9WTvW4I" # Assign your Ubidots TOKEN
VARIABLE = "motion-detector" # Assign the Ubidots Variable label
DELAY = 2 # Set the delay desired to post the data

revpi = revpimodio2.RevPiModIO(autorefresh=True) # Instance to access to the IOs of the RevPi

s = requests.Session()
s.headers.update({'x-test': 'true'})

'''
This method build the JSON to be sent to the Ubidots Cloud
'''
def build_json(variable, value):
    try:
        # This structure send one value to the Ubidots Cloud. Please reference to the
        # Ubidots REST API Reference (https://ubidots.com/docs/api/) to learn how to
        # send more thant one value
        data = {variable: value}
        return data
    except:
        return None

'''
This method make the HTTP Request to the Ubidots Cloud
'''
def post_variable(device, variable, value):
    try:
        # Reference to the Ubidots REST API Reference (https://ubidots.com/docs/api/)
        # to learn how to build the HTTP Request to the Ubidors Server
        url = "https://things.ubidots.com/api/v1.6/devices/" + device
        headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
        data = build_json(variable, value)
        response = s.post(url=url, headers=headers, json=data)
        return response.json()
    except:
        pass

if __name__ == "__main__":
    while True:
        mac = get_mac() # get the mac address of your device
        device_mac = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
        # Read the status of the Input called 'I_1'
        sensor_status = float(revpi.io.I_1.value)
        # Send the POST Request to the Ubidots Server
        print("Posting values to Ubidots")
        request = post_variable(device_mac, VARIABLE, sensor_status)
        print(request)
        time.sleep(DELAY)

Code snippet #21

Plain text
################################################################################
# This script control an Output(DO) status of a pin from the Revpi DIO
# expansion module from the Ubidots Cloud (https://ubidots.com/)
#
# Authors: M. Hernandez
################################################################################

import requests
import time
import revpimodio2
from uuid import getnode as get_mac

TOKEN = "A1E-dfBFE58BECK3YgAia1Sss2H9WTvW4I" # Assign your Ubidots TOKEN
VARIABLE = "light" # Assign the Ubidots Variable label to be controlled
DELAY = 5 # Set the delay desired to post the data

revpi = revpimodio2.RevPiModIO(autorefresh=True) # Instance to access to the IOs of the RevPi

s = requests.Session()
s.headers.update({'x-test': 'true'})

'''
This method make the HTTP Request to the Ubidots Cloud
'''

def get_lastvalue(device_label, variable_label):
    try:
        # Reference to the Ubidots REST API Reference (https://ubidots.com/docs/api/)
        # to learn how to build the HTTP Request to the Ubidors Server
        url = "https://things.ubidots.com/api/v1.6/devices/" + device_label + "/" + variable_label + "/lv"
        headers = {"X-Auth-Token": TOKEN}
        response = s.get(url=url, headers=headers)
        return response.json()
    except:
        pass

if __name__ == "__main__":
    while True:
        mac = get_mac() # get the mac address of your device
        device_mac = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
        # Send the GET Request to the Ubidots Server
        print("Getting values from Ubidots")
        light_status = get_lastvalue(device_mac, VARIABLE)
        if light_status == 1.00:
            revpi.io.O_1.value = True
            print("Light TURNED ON")
        else:
            revpi.io.O_1.value = False
            print("Light TURNED OFF") 
        time.sleep(DELAY)

Code snippet #22

Plain text
################################################################################
# This script control an Output(DO) status of a pin from the Revpi DIO
# expansion module from the Ubidots Cloud (https://ubidots.com/)
#
# Authors: M. Hernandez
################################################################################

import requests
import time
import revpimodio2
from uuid import getnode as get_mac

TOKEN = "A1E-dfBFE58BECK3YgAia1Sss2H9WTvW4I" # Assign your Ubidots TOKEN
VARIABLE = "light" # Assign the Ubidots Variable label to be controlled
DELAY = 5 # Set the delay desired to post the data

revpi = revpimodio2.RevPiModIO(autorefresh=True) # Instance to access to the IOs of the RevPi

s = requests.Session()
s.headers.update({'x-test': 'true'})

'''
This method make the HTTP Request to the Ubidots Cloud
'''

def get_lastvalue(device_label, variable_label):
    try:
        # Reference to the Ubidots REST API Reference (https://ubidots.com/docs/api/)
        # to learn how to build the HTTP Request to the Ubidors Server
        url = "https://things.ubidots.com/api/v1.6/devices/" + device_label + "/" + variable_label + "/lv"
        headers = {"X-Auth-Token": TOKEN}
        response = s.get(url=url, headers=headers)
        return response.json()
    except:
        pass

if __name__ == "__main__":
    while True:
        mac = get_mac() # get the mac address of your device
        device_mac = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
        # Send the GET Request to the Ubidots Server
        print("Getting values from Ubidots")
        light_status = get_lastvalue(device_mac, VARIABLE)
        if light_status == 1.00:
            revpi.io.O_1.value = True
            print("Light TURNED ON")
        else:
            revpi.io.O_1.value = False
            print("Light TURNED OFF") 
        time.sleep(DELAY)

Credits

UbiMaker

UbiMaker

53 projects • 229 followers
Maker @ ubidots.com
Maka Hernandez

Maka Hernandez

29 projects • 123 followers

Comments