Skip to content

Commit 4a6cc54

Browse files
authored
Merge pull request #24 from adafruit/get-mqtt-method
Use /get MQTT topic to trigger resend of feed data.
2 parents 80ac0bc + 2ac1fe7 commit 4a6cc54

File tree

16 files changed

+215
-135
lines changed

16 files changed

+215
-135
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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ void setup() {
3232

3333
Serial.print("Connecting to Adafruit IO");
3434

35-
// because Adafruit IO doesn't support the MQTT
36-
// retain flag right now, we need to load the
37-
// last value for the "counter" feed manually and
38-
// send it our handleMessage function
39-
if (counter->exists()) {
40-
handleMessage(counter->lastValue());
41-
}
42-
4335
// start MQTT connection to io.adafruit.com
4436
io.connect();
4537

@@ -52,12 +44,16 @@ void setup() {
5244
// wait for an MQTT connection
5345
// NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
5446
// method to check on MQTT connection status specifically
55-
5647
while(io.mqttStatus() < AIO_CONNECTED) {
5748
Serial.print(".");
5849
delay(500);
5950
}
6051

52+
// Because Adafruit IO doesn't support the MQTT retain flag, we can use the
53+
// get() function to ask IO to resend the last value for this feed to just
54+
// this MQTT client after the io client is connected.
55+
counter->get();
56+
6157
// we are connected
6258
Serial.println();
6359
Serial.println(io.statusText());
@@ -72,6 +68,9 @@ void loop() {
7268
// io.adafruit.com, and processes any incoming data.
7369
io.run();
7470

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

7776
// 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

0 commit comments

Comments
 (0)