Skip to content

Commit fdf9d66

Browse files
committed
example udates
- add feed->get() calls to all subscribed feeds - use millis() tracking instead of delay() for sketches with pub + sub - extend delay() values on sketches that publish to avoid rate limiting
1 parent 5dbc762 commit fdf9d66

File tree

13 files changed

+179
-131
lines changed

13 files changed

+179
-131
lines changed

examples/adafruitio_00_publish/adafruitio_00_publish.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ void loop() {
6666
// increment the count by 1
6767
count++;
6868

69-
// wait one second (1000 milliseconds == 1 second)
70-
delay(1000);
69+
// Adafruit IO is rate limited for publishing, so a delay is required in
70+
// between feed->save events. In this example, we will wait three seconds
71+
// (1000 milliseconds == 1 second) during each loop.
72+
delay(3000);
7173

7274
}

examples/adafruitio_01_subscribe/adafruitio_01_subscribe.ino

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ void setup() {
5151

5252
// Because Adafruit IO doesn't support the MQTT retain flag, we can use the
5353
// get() function to ask IO to resend the last value for this feed to just
54-
// this MQTT client.
55-
//
56-
// Behind the scenes, the library is MQTT publishing an empty message to the
57-
// {username}/f/counter/csv/get topic to trigger a resend on the
58-
// {username}/f/counter/csv topic that this client is already subscribed to.
59-
// That means calling get() will cause the handleMessage function we attached
60-
// to this feed to be called with the most recent data record published to
61-
// the feed.
54+
// this MQTT client after the io client is connected.
6255
counter->get();
6356

6457
// we are connected
@@ -75,6 +68,9 @@ void loop() {
7568
// io.adafruit.com, and processes any incoming data.
7669
io.run();
7770

71+
// Because this sketch isn't publishing, we don't need
72+
// a delay() in the main program loop.
73+
7874
}
7975

8076
// this function is called whenever a 'counter' message

examples/adafruitio_02_pubsub/adafruitio_02_pubsub.ino

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
// this int will hold the current count for our sketch
2323
int count = 0;
2424

25+
// Track time of last published messages and limit feed->save events to once
26+
// every IO_LOOP_DELAY milliseconds.
27+
//
28+
// Because this sketch is publishing AND subscribing, we can't use a long
29+
// delay() function call in the main loop since that would prevent io.run()
30+
// from being called often enough to receive all incoming messages.
31+
//
32+
// Instead, we can use the millis() function to get the current time in
33+
// milliseconds and avoid publishing until IO_LOOP_DELAY milliseconds have
34+
// passed.
35+
#define IO_LOOP_DELAY 5000
36+
unsigned long lastUpdate = 0;
37+
2538
// set up the 'counter' feed
2639
AdafruitIO_Feed *counter = io.feed("counter");
2740

@@ -53,6 +66,7 @@ void setup() {
5366
// we are connected
5467
Serial.println();
5568
Serial.println(io.statusText());
69+
counter->get();
5670

5771
}
5872

@@ -64,16 +78,18 @@ void loop() {
6478
// io.adafruit.com, and processes any incoming data.
6579
io.run();
6680

67-
// save count to the 'counter' feed on Adafruit IO
68-
Serial.print("sending -> ");
69-
Serial.println(count);
70-
counter->save(count);
81+
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
82+
// save count to the 'counter' feed on Adafruit IO
83+
Serial.print("sending -> ");
84+
Serial.println(count);
85+
counter->save(count);
7186

72-
// increment the count by 1
73-
count++;
87+
// increment the count by 1
88+
count++;
7489

75-
// wait one second (1000 milliseconds == 1 second)
76-
delay(1000);
90+
// after publishing, store the current time
91+
lastUpdate = millis();
92+
}
7793

7894
}
7995

examples/adafruitio_03_multiple_feeds/adafruitio_03_multiple_feeds.ino

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ int count = 0;
2424
// holds the boolean (true/false) state of the light
2525
bool is_on = false;
2626

27+
// track time of last published messages and limit feed->save events to once
28+
// every IO_LOOP_DELAY milliseconds
29+
#define IO_LOOP_DELAY 15000
30+
unsigned long lastUpdate;
31+
2732
// set up the 'counter' feed
2833
AdafruitIO_Feed *counter = io.feed("counter");
2934

@@ -65,47 +70,54 @@ void setup() {
6570
Serial.println();
6671
Serial.println(io.statusText());
6772

73+
// make sure all feeds get their current values right away
74+
counter->get();
75+
counter_two->get();
76+
light->get();
77+
6878
}
6979

7080
void loop() {
7181

7282
// process messages and keep connection alive
7383
io.run();
7484

75-
Serial.println();
76-
77-
// save current count to 'counter'
78-
Serial.print("sending -> counter ");
79-
Serial.println(count);
80-
counter->save(count);
81-
82-
// increment the count by 1 and save the value to 'counter-two'
83-
Serial.print("sending -> counter-two ");
84-
Serial.println(count + 1);
85-
counter_two->save(count + 1);
86-
87-
// print out the light value we are sending to Adafruit IO
88-
Serial.print("sending -> light ");
89-
if(is_on)
90-
Serial.println("is on.\n");
91-
else
92-
Serial.println("is off.\n");
93-
94-
// save state of light to 'light' feed
95-
light->save(is_on);
96-
97-
// increment count value
98-
count++;
99-
100-
// for the purpose of this demo, toggle the
101-
// light state based on the count value
102-
if((count % 2) == 0)
103-
is_on = true;
104-
else
105-
is_on = false;
106-
107-
// wait two seconds (2000 milliseconds == 2 seconds)
108-
delay(2000);
85+
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
86+
Serial.println();
87+
88+
// save current count to 'counter'
89+
Serial.print("sending -> counter ");
90+
Serial.println(count);
91+
counter->save(count);
92+
93+
// increment the count by 1 and save the value to 'counter-two'
94+
Serial.print("sending -> counter-two ");
95+
Serial.println(count + 1);
96+
counter_two->save(count + 1);
97+
98+
// print out the light value we are sending to Adafruit IO
99+
Serial.print("sending -> light ");
100+
if(is_on)
101+
Serial.println("is on.\n");
102+
else
103+
Serial.println("is off.\n");
104+
105+
// save state of light to 'light' feed
106+
light->save(is_on);
107+
108+
// increment count value
109+
count++;
110+
111+
// for the purpose of this demo, toggle the
112+
// light state based on the count value
113+
if((count % 2) == 0)
114+
is_on = true;
115+
else
116+
is_on = false;
117+
118+
// update timer
119+
lastUpdate = millis();
120+
}
109121

110122
}
111123

examples/adafruitio_04_location/adafruitio_04_location.ino

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ double lat = 42.331427;
2727
double lon = -83.045754;
2828
double ele = 0;
2929

30+
// track time of last published messages and limit feed->save events to once
31+
// every IO_LOOP_DELAY milliseconds
32+
#define IO_LOOP_DELAY 5000
33+
unsigned long lastUpdate;
34+
3035
// set up the 'location' feed
3136
AdafruitIO_Feed *location = io.feed("location");
3237

@@ -55,6 +60,7 @@ void setup() {
5560
// we are connected
5661
Serial.println();
5762
Serial.println(io.statusText());
63+
location->get();
5864

5965
}
6066

@@ -63,28 +69,30 @@ void loop() {
6369
// process messages and keep connection alive
6470
io.run();
6571

66-
Serial.println("----- sending -----");
67-
Serial.print("value: ");
68-
Serial.println(value);
69-
Serial.print("lat: ");
70-
Serial.println(lat, 6);
71-
Serial.print("lon: ");
72-
Serial.println(lon, 6);
73-
Serial.print("ele: ");
74-
Serial.println(ele, 2);
75-
76-
// save value and location data to the
77-
// 'location' feed on Adafruit IO
78-
location->save(value, lat, lon, ele);
79-
80-
// shift all values (for demo purposes)
81-
value += 1;
82-
lat -= 0.01;
83-
lon += 0.02;
84-
ele += 1;
85-
86-
// wait one second (1000 milliseconds == 1 second)
87-
delay(1000);
72+
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
73+
Serial.println("----- sending -----");
74+
Serial.print("value: ");
75+
Serial.println(value);
76+
Serial.print("lat: ");
77+
Serial.println(lat, 6);
78+
Serial.print("lon: ");
79+
Serial.println(lon, 6);
80+
Serial.print("ele: ");
81+
Serial.println(ele, 2);
82+
83+
// save value and location data to the
84+
// 'location' feed on Adafruit IO
85+
location->save(value, lat, lon, ele);
86+
87+
// shift all values (for demo purposes)
88+
value += 1;
89+
lat -= 0.01;
90+
lon += 0.02;
91+
ele += 1;
92+
93+
// wait one second (1000 milliseconds == 1 second)
94+
lastUpdate = millis();
95+
}
8896

8997
}
9098

examples/adafruitio_05_type_conversion/adafruitio_05_type_conversion.ino

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ float float_val = 2.4901;
3333
// set up variable that will allow us to flip between sending types
3434
int current_type = 0;
3535

36+
// track time of last published messages and limit feed->save events to once
37+
// every IO_LOOP_DELAY milliseconds
38+
#define IO_LOOP_DELAY 5000
39+
unsigned long lastUpdate;
40+
3641
// set up the 'type' feed
3742
AdafruitIO_Feed *type = io.feed("type");
3843

@@ -69,61 +74,62 @@ void loop() {
6974
// process messages and keep connection alive
7075
io.run();
7176

72-
Serial.println("----- sending -----");
73-
74-
// in order to demonstrate sending values
75-
// as different types, we will switch between
76-
// types in a loop using the current_type variable
77-
if(current_type == 0) {
78-
Serial.print("char: ");
79-
Serial.println(char_val);
80-
type->save(char_val);
81-
} else if(current_type == 1) {
82-
Serial.print("string: ");
83-
Serial.println(string_val);
84-
type->save(string_val);
85-
} else if(current_type == 2) {
86-
Serial.print("bool: ");
87-
Serial.println(bool_val);
88-
type->save(bool_val);
89-
} else if(current_type == 3) {
90-
Serial.print("int: ");
91-
Serial.println(int_val);
92-
type->save(int_val);
93-
} else if(current_type == 4) {
94-
Serial.print("unsigned int: ");
95-
Serial.println(uint_val);
96-
type->save(uint_val);
97-
} else if(current_type == 5) {
98-
Serial.print("long: ");
99-
Serial.println(long_val);
100-
type->save(long_val);
101-
} else if(current_type == 6) {
102-
Serial.print("unsigned long: ");
103-
Serial.println(ulong_val);
104-
type->save(ulong_val);
105-
} else if(current_type == 7) {
106-
Serial.print("double: ");
107-
Serial.println(double_val);
108-
type->save(double_val);
109-
} else if(current_type == 8) {
110-
Serial.print("float: ");
111-
Serial.println(float_val);
112-
type->save(float_val);
77+
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
78+
Serial.println("----- sending -----");
79+
80+
// in order to demonstrate sending values
81+
// as different types, we will switch between
82+
// types in a loop using the current_type variable
83+
if(current_type == 0) {
84+
Serial.print("char: ");
85+
Serial.println(char_val);
86+
type->save(char_val);
87+
} else if(current_type == 1) {
88+
Serial.print("string: ");
89+
Serial.println(string_val);
90+
type->save(string_val);
91+
} else if(current_type == 2) {
92+
Serial.print("bool: ");
93+
Serial.println(bool_val);
94+
type->save(bool_val);
95+
} else if(current_type == 3) {
96+
Serial.print("int: ");
97+
Serial.println(int_val);
98+
type->save(int_val);
99+
} else if(current_type == 4) {
100+
Serial.print("unsigned int: ");
101+
Serial.println(uint_val);
102+
type->save(uint_val);
103+
} else if(current_type == 5) {
104+
Serial.print("long: ");
105+
Serial.println(long_val);
106+
type->save(long_val);
107+
} else if(current_type == 6) {
108+
Serial.print("unsigned long: ");
109+
Serial.println(ulong_val);
110+
type->save(ulong_val);
111+
} else if(current_type == 7) {
112+
Serial.print("double: ");
113+
Serial.println(double_val);
114+
type->save(double_val);
115+
} else if(current_type == 8) {
116+
Serial.print("float: ");
117+
Serial.println(float_val);
118+
type->save(float_val);
119+
}
120+
121+
// move to the next type
122+
current_type++;
123+
124+
// reset type if we have reached the end
125+
if(current_type > 8)
126+
current_type = 0;
127+
128+
Serial.println();
129+
130+
lastUpdate = millis();
113131
}
114132

115-
// move to the next type
116-
current_type++;
117-
118-
// reset type if we have reached the end
119-
if(current_type > 8)
120-
current_type = 0;
121-
122-
Serial.println();
123-
124-
// wait two seconds (2000 milliseconds == 2 seconds)
125-
delay(2000);
126-
127133
}
128134

129135
// this function will demonstrate how to convert

0 commit comments

Comments
 (0)