|
| 1 | +// Adafruit IO IFTTT Example - Gmailbox |
| 2 | +// Tutorial Link: https://learn.adafruit.com/gmailbox |
| 3 | +// |
| 4 | +// Adafruit invests time and resources providing this open source code. |
| 5 | +// Please support Adafruit and open source hardware by purchasing |
| 6 | +// products from Adafruit! |
| 7 | +// |
| 8 | +// Written by Brent Rubell for Adafruit Industries |
| 9 | +// Copyright (c) 2018 Adafruit Industries |
| 10 | +// Licensed under the MIT license. |
| 11 | +// |
| 12 | +// All text above must be included in any redistribution. |
| 13 | + |
| 14 | +/************************** Configuration ***********************************/ |
| 15 | + |
| 16 | +// edit the config.h tab and enter your Adafruit IO credentials |
| 17 | +// and any additional configuration needed for WiFi, cellular, |
| 18 | +// or ethernet clients. |
| 19 | +#include "config.h" |
| 20 | + |
| 21 | +// Import Servo Libraries |
| 22 | +#if defined(ARDUINO_ARCH_ESP32) |
| 23 | + // ESP32Servo Library (https://github.com/madhephaestus/ESP32Servo) |
| 24 | + // installation: library manager -> search -> "ESP32Servo" |
| 25 | + #include <ESP32Servo.h> |
| 26 | +#else |
| 27 | + #include <Servo.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +/************************ Example Starts Here *******************************/ |
| 31 | + |
| 32 | +// pin used to control the servo |
| 33 | +#define SERVO_PIN 14 |
| 34 | + |
| 35 | +// Flag's up position, in degrees |
| 36 | +#define FLAG_UP 0 |
| 37 | + |
| 38 | +// Flag's down position, in degrees |
| 39 | +#define FLAG_DOWN 180 |
| 40 | + |
| 41 | +// How long to hold the flag up, in seconds |
| 42 | +#define FLAG_DELAY 2 |
| 43 | + |
| 44 | +// create an instance of the servo class |
| 45 | +Servo servo; |
| 46 | + |
| 47 | +// set up the 'servo' feed |
| 48 | +AdafruitIO_Feed *gmail_feed = io.feed("gmail"); |
| 49 | + |
| 50 | +void setup() { |
| 51 | + |
| 52 | + // start the serial connection |
| 53 | + Serial.begin(115200); |
| 54 | + |
| 55 | + // wait for serial monitor to open |
| 56 | + while(! Serial); |
| 57 | + |
| 58 | + Serial.print("IFTTT Gmailbox"); |
| 59 | + |
| 60 | + // tell the servo class which pin we are using |
| 61 | + servo.attach(SERVO_PIN); |
| 62 | + |
| 63 | + // connect to io.adafruit.com |
| 64 | + Serial.print("Connecting to Adafruit IO"); |
| 65 | + io.connect(); |
| 66 | + |
| 67 | + // set up a message handler for the 'servo' feed. |
| 68 | + // the handleMessage function (defined below) |
| 69 | + // will be called whenever a message is |
| 70 | + // received from adafruit io. |
| 71 | + gmail_feed->onMessage(handleMessage); |
| 72 | + |
| 73 | + // wait for a connection |
| 74 | + while(io.status() < AIO_CONNECTED) { |
| 75 | + Serial.print("."); |
| 76 | + delay(500); |
| 77 | + } |
| 78 | + |
| 79 | + // we are connected |
| 80 | + Serial.println(); |
| 81 | + Serial.println(io.statusText()); |
| 82 | + gmail_feed->get(); |
| 83 | + |
| 84 | + // write flag to down position |
| 85 | + servo.write(FLAG_DOWN); |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +void loop() { |
| 90 | + |
| 91 | + // io.run(); is required for all sketches. |
| 92 | + // it should always be present at the top of your loop |
| 93 | + // function. it keeps the client connected to |
| 94 | + // io.adafruit.com, and processes any incoming data. |
| 95 | + io.run(); |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +// this function is called whenever a 'gmail' message |
| 100 | +// is received from Adafruit IO. it was attached to |
| 101 | +// the gmail feed in the setup() function above. |
| 102 | +void handleMessage(AdafruitIO_Data *data) { |
| 103 | + |
| 104 | + Serial.println("You've got mail!"); |
| 105 | + servo.write(FLAG_UP); |
| 106 | + // wait FLAG_DELAY seconds |
| 107 | + delay(FLAG_DELAY * 1000); |
| 108 | + servo.write(FLAG_DOWN); |
| 109 | +} |
0 commit comments