Frank
July 11, 2016, 1:40pm
1
How can i get last value from ubidots to switch a led on raspberry?
from ubidots import ApiClient
import RPi.GPIO as GPIO
api=ApiClient(token="TOKEN123")
variable=api.get_variable("5782b5977625426a9d647175")
last_value=variable.get_values(1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
print last_value[0]['value']
if last_value==1.0:
GPIO.output(23,1)
Hey @Frank !
Ubidots has a documentation about Rpi: http://ubidots.com/docs/devices/raspberrypi.html#raspberry-pi-wifi-ethernet
When you get the data you could use this package of NPM to set a GPIO pin of rasp:
https://www.npmjs.com/package/rpi-gpio
Best regards,
Metavix
Frank
July 11, 2016, 2:00pm
3
Thanks! But i don’t manage to understand what is wrong, i got the last value from ubidots but I can not control the LEDs
Frank
July 14, 2016, 10:26am
4
@Metavix Help! i got the last value from ubidots but I can not control the LEDs
@Frank it could be many things
Voltage of LED
You didn’t connect right the LED to VCC and GND
Do you have a multimeter?
Best regards,
Metavix
@Frank by the way you can read that post, maybe you are setting another GPIO.
gpio, python, rpi.gpio
Best reards,
Metavix
Frank
July 15, 2016, 2:40pm
7
@Metavix I’m sure of what i done, i know the difference between BOARD and BCM. I don’t understand what is wrong in this code :
from ubidots import ApiClient
import RPi.GPIO as GPIO
api=ApiClient(token="TOKEN123")
variable=api.get_variable("5782b5977625426a9d647175")
last_value=variable.get_values(1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
print last_value[0]['value']
if last_value==1.0:
GPIO.output(23,HIGH)
Hi, Frank!
Sorry for the late response, I was testing the code in my RPi to make sure it worked.
You have a little mistake in the conditional, it will always evaluate to False , given that last_value is a dict and you’re checking equality between a dict and a float .
Now, to fix that you just have to do:
if last_value[0].get('value') == 1.0:
And… in the last line, remember that HIGH is an attribute of the GPIO class that wraps a True value, you have that line perfect in the first post (you send a 1). It should be:
GPIO.output(23, GPIO.HIGH)
Hope this helps
Frank
July 15, 2016, 4:40pm
9
Thank you so much @juanda95 ! And if i would switch the led each time the value changes without start the file.py ?
@Frank You can implement the GET via polling every N seconds. I’m going to grab your code if you don’t mind and modify it below:
import RPi.GPIO as GPIO
from ubidots import ApiClient
from time import sleep
api = ApiClient(token="TOKEN123")
variable = api.get_variable("5782b5977625426a9d647175")
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
while True:
last_value = variable.get_values(1)
if last_value[0].get("value") == 1:
GPIO.output(23, GPIO.HIGH)
else:
GPIO.output(23, GPIO.LOW)
sleep(5) # Sleep for 5 seconds
This code will keep checking the last value of your variable every 5 seconds and do the desired action (turn ON or OFF the LED)
Frank
July 16, 2016, 3:31pm
11
@juanda95 Thank you! Fantastic!
Best regards,
Frank
You’re welcome We’re here to help, hope you enjoy Ubidots.