UbiMakerMaka Hernandez
Published © CC BY-NC-SA

Connect Your RevPi Core To Ubidots

Launch your Industrial applications with Revolution Pi and Ubidots – energy management, process monitoring, machine health and more.

BeginnerProtip3 hours787
Connect Your RevPi Core To Ubidots

Story

Read more

Code

Code snippet #7

Plain text
################################################################################
# This script simulates different sensors values using the random module and make
# a HTTP request to Ubidots Cloud (https://ubidots.com/)
#
# Author: M. Hernandez
################################################################################

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

# Assign your Ubidots TOKEN
TOKEN = "{Assign_your_Ubidots_token}"
# Set the delay desired to post the data 
DELAY = 1

''' 
This method build the JSON to be sent to the Ubidots Cloud
'''
def build_json(variable_1, value_1, variable_2, value_2, variable_3, value_3):
    try:
        data = {variable_1: value_1, variable_2: value_2, variable_3: value_3}
        return data
    except:
        return None

'''   
This method make the HTTP Request to the Ubidots Cloud
'''
def post_variable(device, value_1, value_2, value_3):
    try:
        url = "https://things.ubidots.com/api/v1.6/devices/" + device
        headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
        data = build_json("temperature", value_1, "humidity", value_2, "pressure", value_3)
        response = requests.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))
        temp_value = random.randint(0,15)*2
        hum_value = random.randint(20,50)
        press_value = random.randint(2,50)*2
        print post_variable(device_mac, temp_value, hum_value, press_value)
        time.sleep(DELAY)

Code snippet #8

Plain text
################################################################################
# This script simulates different sensors values using the random module and make
# a HTTP request to Ubidots Cloud (https://ubidots.com/)
#
# Author: M. Hernandez
################################################################################

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

# Assign your Ubidots TOKEN
TOKEN = "{Assign_your_Ubidots_token}"
# Set the delay desired to post the data 
DELAY = 1

''' 
This method build the JSON to be sent to the Ubidots Cloud
'''
def build_json(variable_1, value_1, variable_2, value_2, variable_3, value_3):
    try:
        data = {variable_1: value_1, variable_2: value_2, variable_3: value_3}
        return data
    except:
        return None

'''   
This method make the HTTP Request to the Ubidots Cloud
'''
def post_variable(device, value_1, value_2, value_3):
    try:
        url = "https://things.ubidots.com/api/v1.6/devices/" + device
        headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
        data = build_json("temperature", value_1, "humidity", value_2, "pressure", value_3)
        response = requests.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))
        temp_value = random.randint(0,15)*2
        hum_value = random.randint(20,50)
        press_value = random.randint(2,50)*2
        print post_variable(device_mac, temp_value, hum_value, press_value)
        time.sleep(DELAY)

Credits

UbiMaker
53 projects • 231 followers
Maker @ ubidots.com
Contact
Maka Hernandez
29 projects • 124 followers
Contact

Comments

Please log in or sign up to comment.