@@ -31,6 +31,10 @@ bool is_on = false;
31
31
32
32
// set up the 'counter' feed
33
33
AdafruitIO_Feed *counter = io.feed(" counter" );
34
+
35
+ // set up the 'counter-two' feed
36
+ AdafruitIO_Feed *counter_two = io.feed(" counter-two" );
37
+
34
38
// set up the 'light' feed
35
39
AdafruitIO_Feed *light = io.feed(" light" );
36
40
@@ -47,10 +51,13 @@ void setup() {
47
51
// connect to io.adafruit.com
48
52
io.connect (IO_USERNAME, IO_KEY);
49
53
50
- // set up a message handler for the counter feed.
54
+ // attach message handler for the counter feed.
51
55
counter->onMessage (handleCount);
52
56
53
- // set up a message handler for the light feed.
57
+ // attach the same message handler for the second counter feed.
58
+ counter_two->onMessage (handleCount);
59
+
60
+ // attach a new message handler for the light feed.
54
61
light->onMessage (handleLight);
55
62
56
63
// wait for a connection
@@ -70,22 +77,25 @@ void loop() {
70
77
// process messages and keep connection alive
71
78
io.run ();
72
79
73
- // print out the count we are sending to Adafruit IO
74
80
Serial.println ();
75
- Serial.print (" sending -> count " );
76
- Serial.println (count);
77
81
78
- // save current count to 'counter' feed
82
+ // save current count to 'counter'
83
+ Serial.print (" sending -> counter " );
84
+ Serial.println (count);
79
85
counter->save (count);
80
86
87
+ // increment the count by 1 and save the value to 'counter-two'
88
+ Serial.print (" sending -> counter-two " );
89
+ Serial.println (count + 1 );
90
+ counter_two->save (count + 1 );
91
+
81
92
// print out the light value we are sending to Adafruit IO
82
93
Serial.print (" sending -> light " );
83
94
if (is_on)
84
95
Serial.println (" is on.\n " );
85
96
else
86
97
Serial.println (" is off.\n " );
87
98
88
-
89
99
// save state of light to 'light' feed
90
100
light->save (is_on);
91
101
@@ -104,9 +114,8 @@ void loop() {
104
114
105
115
}
106
116
107
- // you can set a separate message handler for each
108
- // feed (as we do in this example), or attach
109
- // multiple feeds to the same meesage handler function.
117
+ // you can set a separate message handler for a single feed,
118
+ // as we do in this example for the light feed
110
119
void handleLight (AdafruitIO_Data *data) {
111
120
112
121
// print out the received light value
@@ -121,10 +130,21 @@ void handleLight(AdafruitIO_Data *data) {
121
130
122
131
}
123
132
133
+ // you can also attach multiple feeds to the same
134
+ // meesage handler function. both counter and counter-two
135
+ // are attached to this callback function, and messages
136
+ // will be received by this function.
124
137
void handleCount (AdafruitIO_Data *data) {
125
138
126
- // print out the received count value
127
- Serial.print (" received <- count " );
139
+ Serial.print (" received <- " );
140
+
141
+ // since we are using the same function to handle
142
+ // messages for two feeds, we can use feedName() in
143
+ // order to find out which feed the message came from.
144
+ Serial.print (data->feedName ());
145
+ Serial.print (" " );
146
+
147
+ // print out the received count or counter-two value
128
148
Serial.println (data->value ());
129
149
130
150
}
0 commit comments