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
This commit is contained in:
Adam Bachman 2018-04-20 11:57:39 -04:00
parent 5dbc762e4d
commit fdf9d661e2
13 changed files with 168 additions and 120 deletions

View file

@ -66,7 +66,9 @@ void loop() {
// increment the count by 1
count++;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
// Adafruit IO is rate limited for publishing, so a delay is required in
// between feed->save events. In this example, we will wait three seconds
// (1000 milliseconds == 1 second) during each loop.
delay(3000);
}

View file

@ -51,14 +51,7 @@ void setup() {
// Because Adafruit IO doesn't support the MQTT retain flag, we can use the
// get() function to ask IO to resend the last value for this feed to just
// this MQTT client.
//
// Behind the scenes, the library is MQTT publishing an empty message to the
// {username}/f/counter/csv/get topic to trigger a resend on the
// {username}/f/counter/csv topic that this client is already subscribed to.
// That means calling get() will cause the handleMessage function we attached
// to this feed to be called with the most recent data record published to
// the feed.
// this MQTT client after the io client is connected.
counter->get();
// we are connected
@ -75,6 +68,9 @@ void loop() {
// io.adafruit.com, and processes any incoming data.
io.run();
// Because this sketch isn't publishing, we don't need
// a delay() in the main program loop.
}
// this function is called whenever a 'counter' message

View file

@ -22,6 +22,19 @@
// this int will hold the current count for our sketch
int count = 0;
// Track time of last published messages and limit feed->save events to once
// every IO_LOOP_DELAY milliseconds.
//
// Because this sketch is publishing AND subscribing, we can't use a long
// delay() function call in the main loop since that would prevent io.run()
// from being called often enough to receive all incoming messages.
//
// Instead, we can use the millis() function to get the current time in
// milliseconds and avoid publishing until IO_LOOP_DELAY milliseconds have
// passed.
#define IO_LOOP_DELAY 5000
unsigned long lastUpdate = 0;
// set up the 'counter' feed
AdafruitIO_Feed *counter = io.feed("counter");
@ -53,6 +66,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
counter->get();
}
@ -64,16 +78,18 @@ void loop() {
// io.adafruit.com, and processes any incoming data.
io.run();
// save count to the 'counter' feed on Adafruit IO
Serial.print("sending -> ");
Serial.println(count);
counter->save(count);
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
// save count to the 'counter' feed on Adafruit IO
Serial.print("sending -> ");
Serial.println(count);
counter->save(count);
// increment the count by 1
count++;
// increment the count by 1
count++;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
// after publishing, store the current time
lastUpdate = millis();
}
}

View file

@ -24,6 +24,11 @@ int count = 0;
// holds the boolean (true/false) state of the light
bool is_on = false;
// track time of last published messages and limit feed->save events to once
// every IO_LOOP_DELAY milliseconds
#define IO_LOOP_DELAY 15000
unsigned long lastUpdate;
// set up the 'counter' feed
AdafruitIO_Feed *counter = io.feed("counter");
@ -65,6 +70,11 @@ void setup() {
Serial.println();
Serial.println(io.statusText());
// make sure all feeds get their current values right away
counter->get();
counter_two->get();
light->get();
}
void loop() {
@ -72,40 +82,42 @@ void loop() {
// process messages and keep connection alive
io.run();
Serial.println();
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
Serial.println();
// save current count to 'counter'
Serial.print("sending -> counter ");
Serial.println(count);
counter->save(count);
// save current count to 'counter'
Serial.print("sending -> counter ");
Serial.println(count);
counter->save(count);
// increment the count by 1 and save the value to 'counter-two'
Serial.print("sending -> counter-two ");
Serial.println(count + 1);
counter_two->save(count + 1);
// increment the count by 1 and save the value to 'counter-two'
Serial.print("sending -> counter-two ");
Serial.println(count + 1);
counter_two->save(count + 1);
// print out the light value we are sending to Adafruit IO
Serial.print("sending -> light ");
if(is_on)
Serial.println("is on.\n");
else
Serial.println("is off.\n");
// print out the light value we are sending to Adafruit IO
Serial.print("sending -> light ");
if(is_on)
Serial.println("is on.\n");
else
Serial.println("is off.\n");
// save state of light to 'light' feed
light->save(is_on);
// save state of light to 'light' feed
light->save(is_on);
// increment count value
count++;
// increment count value
count++;
// for the purpose of this demo, toggle the
// light state based on the count value
if((count % 2) == 0)
is_on = true;
else
is_on = false;
// for the purpose of this demo, toggle the
// light state based on the count value
if((count % 2) == 0)
is_on = true;
else
is_on = false;
// wait two seconds (2000 milliseconds == 2 seconds)
delay(2000);
// update timer
lastUpdate = millis();
}
}

View file

@ -27,6 +27,11 @@ double lat = 42.331427;
double lon = -83.045754;
double ele = 0;
// track time of last published messages and limit feed->save events to once
// every IO_LOOP_DELAY milliseconds
#define IO_LOOP_DELAY 5000
unsigned long lastUpdate;
// set up the 'location' feed
AdafruitIO_Feed *location = io.feed("location");
@ -55,6 +60,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
location->get();
}
@ -63,28 +69,30 @@ void loop() {
// process messages and keep connection alive
io.run();
Serial.println("----- sending -----");
Serial.print("value: ");
Serial.println(value);
Serial.print("lat: ");
Serial.println(lat, 6);
Serial.print("lon: ");
Serial.println(lon, 6);
Serial.print("ele: ");
Serial.println(ele, 2);
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
Serial.println("----- sending -----");
Serial.print("value: ");
Serial.println(value);
Serial.print("lat: ");
Serial.println(lat, 6);
Serial.print("lon: ");
Serial.println(lon, 6);
Serial.print("ele: ");
Serial.println(ele, 2);
// save value and location data to the
// 'location' feed on Adafruit IO
location->save(value, lat, lon, ele);
// save value and location data to the
// 'location' feed on Adafruit IO
location->save(value, lat, lon, ele);
// shift all values (for demo purposes)
value += 1;
lat -= 0.01;
lon += 0.02;
ele += 1;
// shift all values (for demo purposes)
value += 1;
lat -= 0.01;
lon += 0.02;
ele += 1;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
// wait one second (1000 milliseconds == 1 second)
lastUpdate = millis();
}
}

View file

@ -33,6 +33,11 @@ float float_val = 2.4901;
// set up variable that will allow us to flip between sending types
int current_type = 0;
// track time of last published messages and limit feed->save events to once
// every IO_LOOP_DELAY milliseconds
#define IO_LOOP_DELAY 5000
unsigned long lastUpdate;
// set up the 'type' feed
AdafruitIO_Feed *type = io.feed("type");
@ -69,61 +74,62 @@ void loop() {
// process messages and keep connection alive
io.run();
Serial.println("----- sending -----");
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
Serial.println("----- sending -----");
// in order to demonstrate sending values
// as different types, we will switch between
// types in a loop using the current_type variable
if(current_type == 0) {
Serial.print("char: ");
Serial.println(char_val);
type->save(char_val);
} else if(current_type == 1) {
Serial.print("string: ");
Serial.println(string_val);
type->save(string_val);
} else if(current_type == 2) {
Serial.print("bool: ");
Serial.println(bool_val);
type->save(bool_val);
} else if(current_type == 3) {
Serial.print("int: ");
Serial.println(int_val);
type->save(int_val);
} else if(current_type == 4) {
Serial.print("unsigned int: ");
Serial.println(uint_val);
type->save(uint_val);
} else if(current_type == 5) {
Serial.print("long: ");
Serial.println(long_val);
type->save(long_val);
} else if(current_type == 6) {
Serial.print("unsigned long: ");
Serial.println(ulong_val);
type->save(ulong_val);
} else if(current_type == 7) {
Serial.print("double: ");
Serial.println(double_val);
type->save(double_val);
} else if(current_type == 8) {
Serial.print("float: ");
Serial.println(float_val);
type->save(float_val);
// in order to demonstrate sending values
// as different types, we will switch between
// types in a loop using the current_type variable
if(current_type == 0) {
Serial.print("char: ");
Serial.println(char_val);
type->save(char_val);
} else if(current_type == 1) {
Serial.print("string: ");
Serial.println(string_val);
type->save(string_val);
} else if(current_type == 2) {
Serial.print("bool: ");
Serial.println(bool_val);
type->save(bool_val);
} else if(current_type == 3) {
Serial.print("int: ");
Serial.println(int_val);
type->save(int_val);
} else if(current_type == 4) {
Serial.print("unsigned int: ");
Serial.println(uint_val);
type->save(uint_val);
} else if(current_type == 5) {
Serial.print("long: ");
Serial.println(long_val);
type->save(long_val);
} else if(current_type == 6) {
Serial.print("unsigned long: ");
Serial.println(ulong_val);
type->save(ulong_val);
} else if(current_type == 7) {
Serial.print("double: ");
Serial.println(double_val);
type->save(double_val);
} else if(current_type == 8) {
Serial.print("float: ");
Serial.println(float_val);
type->save(float_val);
}
// move to the next type
current_type++;
// reset type if we have reached the end
if(current_type > 8)
current_type = 0;
Serial.println();
lastUpdate = millis();
}
// move to the next type
current_type++;
// reset type if we have reached the end
if(current_type > 8)
current_type = 0;
Serial.println();
// wait two seconds (2000 milliseconds == 2 seconds)
delay(2000);
}
// this function will demonstrate how to convert

View file

@ -56,6 +56,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
digital->get();
}

View file

@ -77,6 +77,9 @@ void loop() {
// store last photocell state
last = current;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
// wait three seconds (1000 milliseconds == 1 second)
//
// because there are no active subscriptions, we can use delay()
// instead of tracking millis()
delay(3000);
}

View file

@ -53,6 +53,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
analog->get();
}

View file

@ -71,6 +71,6 @@ void loop() {
// increment the count_2 by 2
count_2 *= 2;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
// wait four seconds (1000 milliseconds == 1 second)
delay(4000);
}

View file

@ -56,6 +56,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
color->get();
// set analogWrite range for ESP8266
#ifdef ESP8266

View file

@ -57,6 +57,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
color->get();
// neopixel init
pixels.begin();

View file

@ -61,6 +61,7 @@ void setup() {
// we are connected
Serial.println();
Serial.println(io.statusText());
servo_feed->get();
}