UbiMakerMaka Hernandez
Published © CC BY-NC-SA

Send Data to Ubidots Using a Hologram Nova + Raspberry Pi

Use your Hologram Nova to retrofit infrastructure. Set up the Hologram Nova using a Raspberry Pi to send (temperature) data to Ubidots.

BeginnerProtip4 hours876
Send Data to Ubidots Using a Hologram Nova + Raspberry Pi

Story

Read more

Code

Code snippet #25

Plain text
from Hologram.HologramCloud import HologramCloud
import os
import time
import json

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-0000056915de/w1_slave'

# Hologram Cloud instance
hologram = HologramCloud(dict(), network='cellular')

# Connect to cellular network
result = hologram.network.connect()
if result == False:
    print ' Failed to connect to cell network'

def temp_raw():
    f = open(temp_sensor, 'r')
    lines = f.readlines()
    f.close()
    return lines

# Reads the sensor values and build the payload to be sent
def read_temp():
    lines = temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw()
    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string = lines[1].strip()[temp_output+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        payload = {"temp_celsius": temp_c, "temp_fahrenheit": temp_f}
        return payload

while True:
        print('Posting temperatures values to Hologram Cloud')
        response_code = hologram.sendMessage(json.dumps(read_temp())) # Send Message to Hologram Cloud
        print hologram.getResultString(response_code) # Prints 'Message sent successfully'.
        hologram.network.disconnect() # Disconnect from cellular network
        time.sleep(10)

Code snippet #26

Plain text
from Hologram.HologramCloud import HologramCloud
import os
import time
import json

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-0000056915de/w1_slave'

# Hologram Cloud instance
hologram = HologramCloud(dict(), network='cellular')

# Connect to cellular network
result = hologram.network.connect()
if result == False:
    print ' Failed to connect to cell network'

def temp_raw():
    f = open(temp_sensor, 'r')
    lines = f.readlines()
    f.close()
    return lines

# Reads the sensor values and build the payload to be sent
def read_temp():
    lines = temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw()
    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string = lines[1].strip()[temp_output+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        payload = {"temp_celsius": temp_c, "temp_fahrenheit": temp_f}
        return payload

while True:
        print('Posting temperatures values to Hologram Cloud')
        response_code = hologram.sendMessage(json.dumps(read_temp())) # Send Message to Hologram Cloud
        print hologram.getResultString(response_code) # Prints 'Message sent successfully'.
        hologram.network.disconnect() # Disconnect from cellular network
        time.sleep(10)

Credits

UbiMaker

UbiMaker

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

Maka Hernandez

29 projects • 122 followers

Comments