Skip to content

Commit 6cedd82

Browse files
committed
add feedName method to data class
1 parent 164e367 commit 6cedd82

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

AdafruitIO_Data.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "AdafruitIO_Data.h"
2+
#include "AdafruitIO_Feed.h"
23

34
AdafruitIO_Data::AdafruitIO_Data()
45
{
@@ -152,6 +153,14 @@ void AdafruitIO_Data::setValue(double value, double lat, double lon, double ele,
152153
setLocation(lat, lon, ele);
153154
}
154155

156+
char* AdafruitIO_Data::feedName()
157+
{
158+
if(! feed)
159+
return (char*)"";
160+
161+
return (char *)feed->name;
162+
}
163+
155164
char* AdafruitIO_Data::value()
156165
{
157166
return toChar();

AdafruitIO_Data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class AdafruitIO_Data {
2929
void setValue(float value, double lat=0, double lon=0, double ele=0, int precision=6);
3030
void setValue(double value, double lat=0, double lon=0, double ele=0, int precision=6);
3131

32+
char* feedName();
33+
3234
char* value();
3335
char* toChar();
3436
String toString();

examples/multiple_feeds/multiple_feeds.ino

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ bool is_on = false;
3131

3232
// set up the 'counter' feed
3333
AdafruitIO_Feed *counter = io.feed("counter");
34+
35+
// set up the 'counter-two' feed
36+
AdafruitIO_Feed *counter_two = io.feed("counter-two");
37+
3438
// set up the 'light' feed
3539
AdafruitIO_Feed *light = io.feed("light");
3640

@@ -47,10 +51,13 @@ void setup() {
4751
// connect to io.adafruit.com
4852
io.connect(IO_USERNAME, IO_KEY);
4953

50-
// set up a message handler for the counter feed.
54+
// attach message handler for the counter feed.
5155
counter->onMessage(handleCount);
5256

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.
5461
light->onMessage(handleLight);
5562

5663
// wait for a connection
@@ -70,22 +77,25 @@ void loop() {
7077
// process messages and keep connection alive
7178
io.run();
7279

73-
// print out the count we are sending to Adafruit IO
7480
Serial.println();
75-
Serial.print("sending -> count ");
76-
Serial.println(count);
7781

78-
// save current count to 'counter' feed
82+
// save current count to 'counter'
83+
Serial.print("sending -> counter ");
84+
Serial.println(count);
7985
counter->save(count);
8086

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+
8192
// print out the light value we are sending to Adafruit IO
8293
Serial.print("sending -> light ");
8394
if(is_on)
8495
Serial.println("is on.\n");
8596
else
8697
Serial.println("is off.\n");
8798

88-
8999
// save state of light to 'light' feed
90100
light->save(is_on);
91101

@@ -104,9 +114,8 @@ void loop() {
104114

105115
}
106116

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
110119
void handleLight(AdafruitIO_Data *data) {
111120

112121
// print out the received light value
@@ -121,10 +130,21 @@ void handleLight(AdafruitIO_Data *data) {
121130

122131
}
123132

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.
124137
void handleCount(AdafruitIO_Data *data) {
125138

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
128148
Serial.println(data->value());
129149

130150
}

0 commit comments

Comments
 (0)