Skip to content

Instantly share code, notes, and snippets.

@AndresPinguino
Last active September 26, 2018 07:39
Show Gist options
  • Save AndresPinguino/97b9ff185c0d8731d872 to your computer and use it in GitHub Desktop.
Save AndresPinguino/97b9ff185c0d8731d872 to your computer and use it in GitHub Desktop.
Código para activar las líneas GPIO de la placa Raspberry Pi desde el servicio de Ubidots con Python
# https://www.youtube.com/watch?v=cFAB5uWnuZQ
# www.ubidots.com
# https://pypi.python.org/pypi/ubidots/1.6.1
# Se declaran las librerias a utilizar
from ubidots import ApiClient
import RPi.GPIO as GPIO
import sys
# Excepcion para verificar si se conecto correctamente con la API de ubidots
try:
print "CONECTADO API..."
api = ApiClient('Reemplazar con la API KEY de Ubidots')
except:
print "FALLO LA CONEXION API"
pin=24
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
# set up GPIO output channel
GPIO.setup(pin, GPIO.OUT)
# Ciclo donde se desarrolla todo el programa
while True:
try:
# Se toma la variable de entrada de ubidots
led = api.get_variable('Reemplazar con el ID de Ubidots')
# Se toma el valor la variable de entrada
new_value = led.get_values(1)
# Se convierte este valor de tipo INFO a LISTA
lista = str(new_value)
# Se toma de la LISTA el valor 177 el cual corresponde a 1 o 0
valor=lista[177]
# Se compara el valor si es 1 o 0, y se envia este mismo
if valor=='1':
print "Encendido"
GPIO.output(pin,GPIO.HIGH)
elif valor=='0':
print "Apagado"
GPIO.output(pin,GPIO.LOW)
except KeyboardInterrupt:
print "Cerrando...."
GPIO.cleanup()
sys.exit()
#print valor #para ver el estado del pulsador
#print lista #para ver la lista recibida de Ubidots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment