|
| 1 | +// |
| 2 | +// Adafruit IO FONA Example |
| 3 | +// |
| 4 | +// Written by Todd Treece for Adafruit Industries |
| 5 | +// Copyright (c) 2016 Adafruit Industries |
| 6 | +// Licensed under the MIT license. |
| 7 | +// |
| 8 | +// All text above must be included in any redistribution. |
| 9 | +// |
| 10 | + |
| 11 | +/************************** Cellular Setup **********************************/ |
| 12 | + |
| 13 | +#define APN "" |
| 14 | +#define APN_USER "" |
| 15 | +#define APN_PASS "" |
| 16 | + |
| 17 | +/************************* Adafruit IO Setup ********************************/ |
| 18 | + |
| 19 | +#define IO_USERNAME "your_username" |
| 20 | +#define IO_KEY "your_key" |
| 21 | + |
| 22 | +/*************************** Client Setup ***********************************/ |
| 23 | + |
| 24 | +#include "AdafruitIO_FONA.h" |
| 25 | +AdafruitIO_FONA io(APN, APN_USER, APN_PASS); |
| 26 | + |
| 27 | +/************************ Example Starts Here *******************************/ |
| 28 | + |
| 29 | +// this int will hold the current count for our sketch |
| 30 | +int count = 0; |
| 31 | + |
| 32 | +// set up the 'counter' feed |
| 33 | +AdafruitIO_Feed *counter = io.feed("counter"); |
| 34 | + |
| 35 | +void setup() { |
| 36 | + |
| 37 | + // start the serial connection |
| 38 | + Serial.begin(115200); |
| 39 | + |
| 40 | + // wait for serial monitor to open |
| 41 | + while(! Serial); |
| 42 | + |
| 43 | + Serial.print("Connecting to Adafruit IO"); |
| 44 | + |
| 45 | + // connect to io.adafruit.com |
| 46 | + io.connect(IO_USERNAME, IO_KEY); |
| 47 | + |
| 48 | + // set up a message handler for the count feed. |
| 49 | + // the handleMessage function (defined below) |
| 50 | + // will be called whenever a message is |
| 51 | + // received from adafruit io. |
| 52 | + counter->onMessage(handleMessage); |
| 53 | + |
| 54 | + // wait for a connection |
| 55 | + while(io.status() < AIO_CONNECTED) { |
| 56 | + Serial.print("."); |
| 57 | + delay(500); |
| 58 | + } |
| 59 | + |
| 60 | + // we are connected |
| 61 | + Serial.println(); |
| 62 | + Serial.println(io.statusText()); |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + |
| 68 | + // this should be present at the top of your loop |
| 69 | + // function. it keeps the client connected to |
| 70 | + // io.adafruit.com, and processes any data from |
| 71 | + // the server |
| 72 | + io.run(); |
| 73 | + |
| 74 | + // save count to the 'counter' feed on Adafruit IO |
| 75 | + Serial.print("sending -> "); |
| 76 | + Serial.println(count); |
| 77 | + counter->save(count); |
| 78 | + |
| 79 | + // increment the count by 1 |
| 80 | + count++; |
| 81 | + |
| 82 | + // wait one second (1000 milliseconds == 1 second) |
| 83 | + delay(1000); |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +// this function is called whenever a 'counter' message |
| 88 | +// is received from Adafruit IO. it was attached to |
| 89 | +// the counter feed in the setup() function above. |
| 90 | +void handleMessage(AdafruitIO_Data *data) { |
| 91 | + |
| 92 | + Serial.print("received <- "); |
| 93 | + Serial.println(data->value()); |
| 94 | + |
| 95 | +} |
0 commit comments