Before we run the script, we'll need to change ADAFRUIT_IO_USERNAME
and ADAFRUIT_IO_KEY
to the username and key for your Adafruit IO account.
- If you need the AIO Key, navigate to your Adafruit IO Profile
# Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
Next, we'll create an instance of the Adafruit IO Client and set up the feed we created earlier.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) digital = aio.feeds('digital')
The next chunk of code sets up the LED to digital pin 5 (board.D5
) and sets the direction of the LED to an output.
# led set up led = digitalio.DigitalInOut(board.D5) led.direction = digitalio.Direction.OUTPUT
To set up the led, assign it to digital pin 5:
led = digitalio.DigitalInOut(board.D5)
We want the LED to light up. To do this, its pin needs to be set to an output:
led.direction = Direction.OUTPUT
Inside the while True
loop,. the code retrieves the the current value of the feed and sets the LED to the value.
while True: data = aio.receive(digital.key) if int(data.value) == 1: print('received <- ON\n') elif int(data.value) == 0: print('received <- OFF\n') # set the LED to the feed value led.value = int(data.value) # timeout so we dont flood adafruit-io with requests time.sleep(0.5)
Make sure you're within the /io-client-python/examples/basics directory.
If you're not sure which directory you're in, you can check this by running pwd
and you should see the following output from your terminal:
~/io-client-python/examples/basics
Let's run the script. In your terminal, run:
python3 digital_in.py
Toggle the button on your Adafruit IO dashboard, and you should see the following in the the terminal of your Pi.
You can now toggle the button on your Adafruit IO dashboard, and you should see your LED turn on and off.
""" 'digital_out.py' =================================== Example of turning on and off a LED from the Adafruit IO Python Client Author(s): Brent Rubell, Todd Treece """ # Import standard python modules import time # import Adafruit Blinka import digitalio import board # import Adafruit IO REST client. from Adafruit_IO import Client, Feed, RequestError # Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME' # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) try: # if we have a 'digital' feed digital = aio.feeds('digital') except RequestError: # create a digital feed feed = Feed(name="digital") digital = aio.create_feed(feed) # led set up led = digitalio.DigitalInOut(board.D5) led.direction = digitalio.Direction.OUTPUT while True: data = aio.receive(digital.key) if int(data.value) == 1: print('received <- ON\n') elif int(data.value) == 0: print('received <- OFF\n') # set the LED to the feed value led.value = int(data.value) # timeout so we dont flood adafruit-io with requests time.sleep(0.5)
Text editor powered by tinymce.