1
+ """
2
+ 'digital_in.py'
3
+ ==================================
4
+ Example of sending button values
5
+ to an Adafruit IO feed.
6
+
7
+ Author(s): Brent Rubell, Todd Treece
8
+ """
9
+ # Import standard python modules
10
+ import time
11
+
12
+ # import Adafruit Blinka
13
+ import board
14
+ import digitalio
15
+
16
+ # import Adafruit IO REST client.
17
+ from Adafruit_IO import Client , Feed , RequestError
18
+
19
+ # Set to your Adafruit IO key.
20
+ # Remember, your key is a secret,
21
+ # so make sure not to publish it when you publish this code!
22
+ ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
23
+
24
+ # Set to your Adafruit IO username.
25
+ # (go to https://accounts.adafruit.com to find your username)
26
+ ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
27
+
28
+ # Create an instance of the REST client.
29
+ aio = Client (ADAFRUIT_IO_USERNAME , ADAFRUIT_IO_KEY )
30
+
31
+ try : # if we have a 'digital' feed
32
+ digital = aio .feeds ('digital' )
33
+ except RequestError : # create a digital feed
34
+ feed = Feed (name = "digital" )
35
+ digital = aio .create_feed (feed )
36
+
37
+ # button set up
38
+ button = digitalio .DigitalInOut (board .D12 )
39
+ button .direction = digitalio .Direction .INPUT
40
+ button .pull = digitalio .Pull .UP
41
+ button_current = 0
42
+
43
+
44
+ while True :
45
+ if not button .value :
46
+ button_current = 1
47
+ else :
48
+ button_current = 0
49
+
50
+ print ('Button -> ' , button_current )
51
+ aio .send (digital .key , button_current )
52
+
53
+ # avoid timeout from adafruit io
54
+ time .sleep (1 )
0 commit comments