Skip to content

Commit 227552a

Browse files
author
brentru
committed
07: digital out
1 parent e670152 commit 227552a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
'adafruitio_07_digital_out.py'
3+
===================================
4+
Example of turning on and off a LED
5+
from the Adafruit IO Python Client
6+
7+
Author(s): Brent Rubell, Todd Treece
8+
"""
9+
# import python system libraries
10+
import time
11+
12+
# import Adafruit Blinka
13+
from digitalio import DigitalInOut, Direction
14+
import digitalio
15+
import board
16+
17+
# import Adafruit IO REST client.
18+
from Adafruit_IO import Client, Feed, RequestError
19+
20+
# Set to your Adafruit IO key.
21+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
22+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
23+
24+
# Create an instance of the REST client.
25+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
26+
27+
try: # if we have a 'digital' feed
28+
digital = aio.feeds('digital')
29+
except RequestError: # create a digital feed
30+
feed = Feed(name="digital")
31+
digital = aio.create_feed(feed)
32+
33+
# led set up
34+
led = digitalio.DigitalInOut(board.D5)
35+
led.direction = Direction.OUTPUT
36+
37+
while True:
38+
data = aio.receive(digital.key)
39+
if int(data.value) == 1:
40+
print('received <- ON\n')
41+
elif int(data.value) == 0:
42+
print('received <- OFF\n')
43+
44+
# set the LED to the feed value
45+
led.value = int(data.value)
46+
print(led.value)
47+
# timeout so we dont flood adafruitio with requests
48+
time.sleep(0.5)

0 commit comments

Comments
 (0)