1
+ // Adafruit IO Publish & Subscribe, Digital Input and Output Example
2
+ //
3
+ // Adafruit invests time and resources providing this open source code.
4
+ // Please support Adafruit and open source hardware by purchasing
5
+ // products from Adafruit!
6
+ //
7
+ // Written by Todd Treece for Adafruit Industries
8
+ // Modified by Brent Rubell for Adafruit Industries
9
+ // Copyright (c) 2020 Adafruit Industries
10
+ // Licensed under the MIT license.
11
+ //
12
+ // All text above must be included in any redistribution.
13
+
14
+ /* ************************* Configuration ***********************************/
15
+
16
+ // edit the config.h tab and enter your Adafruit IO credentials
17
+ // and any additional configuration needed for WiFi, cellular,
18
+ // or ethernet clients.
19
+ #include " config.h"
20
+
21
+ /* *********************** Example Starts Here *******************************/
22
+
23
+ // Button Pin
24
+ #define BUTTON_PIN 0
25
+
26
+ // LED Pin
27
+ #define LED_PIN LED_BUILTIN
28
+
29
+ // button state
30
+ bool btn_state = false ;
31
+ bool prv_btn_state = false ;
32
+
33
+ // set up the 'led' feed
34
+ AdafruitIO_Feed *led = io.feed(" led" );
35
+
36
+ // set up the 'button' feed
37
+ AdafruitIO_Feed *button = io.feed(" button" );
38
+
39
+ void setup () {
40
+
41
+ // set button pin as an input
42
+ pinMode (BUTTON_PIN, INPUT);
43
+
44
+ // set LED pin as an output
45
+ pinMode (LED_PIN, OUTPUT);
46
+
47
+ // start the serial connection
48
+ Serial.begin (115200 );
49
+
50
+ // wait for serial monitor to open
51
+ while (! Serial);
52
+
53
+ Serial.print (" Connecting to Adafruit IO" );
54
+
55
+ // connect to io.adafruit.com
56
+ io.connect ();
57
+
58
+ // set up a message handler for the count feed.
59
+ // the handleMessage function (defined below)
60
+ // will be called whenever a message is
61
+ // received from adafruit io.
62
+ led->onMessage (handleMessage);
63
+
64
+ // wait for a connection
65
+ while (io.status () < AIO_CONNECTED) {
66
+ Serial.print (" ." );
67
+ delay (500 );
68
+ }
69
+
70
+ // we are connected
71
+ Serial.println ();
72
+ Serial.println (io.statusText ());
73
+ led->get ();
74
+
75
+ }
76
+
77
+ void loop () {
78
+
79
+ // io.run(); is required for all sketches.
80
+ // it should always be present at the top of your loop
81
+ // function. it keeps the client connected to
82
+ // io.adafruit.com, and processes any incoming data.
83
+ io.run ();
84
+
85
+ // grab the btn_state state of the button.
86
+ if (digitalRead (BUTTON_PIN) == LOW)
87
+ btn_state = false ;
88
+ else
89
+ btn_state = true ;
90
+
91
+ // return if the btn state hasn't changed
92
+ if (btn_state == prv_btn_state)
93
+ return ;
94
+
95
+ // save the btn_state state to the 'button' feed on adafruit io
96
+ Serial.print (" sending button -> " ); Serial.println (btn_state);
97
+ button->save (btn_state);
98
+
99
+ // store last button state
100
+ prv_btn_state = btn_state;
101
+
102
+ }
103
+
104
+ // this function is called whenever a 'led' message
105
+ // is received from Adafruit IO. it was attached to
106
+ // the counter feed in the setup() function above.
107
+ void handleMessage (AdafruitIO_Data *data) {
108
+ Serial.print (" received <- " );
109
+
110
+ if (data->toPinLevel () == HIGH)
111
+ Serial.println (" HIGH" );
112
+ else
113
+ Serial.println (" LOW" );
114
+
115
+ digitalWrite (LED_PIN, data->toPinLevel ());
116
+ }
0 commit comments