Let's take a dive into the code that powers our Gmailbox. It's not too hard to understand and this code can serve as an introduction to Arduino and Adafruit IO.
We'll start by setting up the servo holding the flag. We wired the servo to the Feather HUZZAH's pin 14. If you need to change that, you can modify SERVO_PIN
. We also define the flag's up and down positions (what angle to set the servo's maximum to) as FLAG_UP
and FLAG_DOWN
.
Want the servo's flag to be held up longer? You can do that by modifying the FLAG_DELAY
variable.
// pin used to control the servo #define SERVO_PIN 14 // Flag's up position, in degrees #define FLAG_UP 0 // Flag's down position, in degrees #define FLAG_DOWN 180 // How long to hold the flag up, in seconds #define FLAG_DELAY 2
Next, we're going to create an instance of the servo class and set up the gmail feed from Adafruit IO.
Servo servo; AdafruitIO_Feed *gmail_feed = io.feed("gmail");
The setup()
function starts a serial connection, attaches the servo to SERVO_PIN
, and attempts to connect to Adafruit IO. Then, it creates a message handler to listen to the gmail feed. We'll also write the servo to its' resting state (the FLAG_DOWN
position).
void setup() { // start the serial connection Serial.begin(115200); // wait for serial monitor to open while(! Serial); // tell the servo class which pin we are using servo.attach(SERVO_PIN); // connect to io.adafruit.com Serial.print("Connecting to Adafruit IO"); io.connect(); // set up a message handler for the 'servo' feed. // the handleMessage function (defined below) // will be called whenever a message is // received from adafruit io. gmail_feed->onMessage(handleMessage); // wait for a connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); } // we are connected Serial.println(); Serial.println(io.statusText()); gmail_feed->get(); // write flag to down position servo.write(FLAG_DOWN); }
The loop()
is empty aside from a call to io.run()
, a function which keeps the client connected to io.adafruit.com.
void loop() { io.run(); }
The handleMessage
function is called whenever a new data value is received from the gmail feed on Adafruit IO. Inside this function, we'll print to the serial monitor that a new email has been received.
Then, we'll set the flag to the FLAG_UP
position to indicate a new email has been received. We'll wait FLAG_DELAY
seconds and then move the flag back down by writing the servo to its' FLAG_DOWN
position.
The handleMessage
function is called when the gmail feed is updated by Zapier or IFTTT.
When it receives new data from the gmail feed, it will write the servo to the flagUp
position, waits a second so that you can see it, and moves it back to the flagDown
position.
void handleMessage(AdafruitIO_Data *data) { Serial.println("You've got mail!"); servo.write(FLAG_UP); // wait FLAG_DELAY seconds delay(FLAG_DELAY * 1000); servo.write(FLAG_DOWN); }
Upload the code to your Feather and open the Arduino Serial Monitor (Tools -> Serial Monitor).
IFTTT Gmailbox Connecting to Adafruit IO .. Connected to Adafruit IO!
If you see the above, we're all set up to receive emails! Compose an email to yourself and send it! When the gmail feed receives new data, the serial monitor will print You've got mail!, put the flag up, and gently lower it.
On the gmail feed page, you can click on the actions dropdown -> add data. You can enter any value, it doesn't matter for this project, and click create. You'll see a new data-point dropped onto the IO feed.
First, check that the feed is set up correctly. Visit your feed page for gmail and ensure your feed was updated when an email arrived.
If your feed did update, but the servo flag didn't move, there's a chance it was wired incorrectly. Re-read the wiring diagram on the Wiring and Assembly page. Make sure SERVO_PIN
in the sketch is connected to the correct pin on your Feather HUZZAH.
Check over your Zap or IFTTT Applet configuration. Make sure that the email is connected correctly.
// Adafruit IO IFTTT Example - Gmailbox // Tutorial Link: https://learn.adafruit.com/gmailbox // // Adafruit invests time and resources providing this open source code. // Please support Adafruit and open source hardware by purchasing // products from Adafruit! // // Written by Brent Rubell for Adafruit Industries // Copyright (c) 2018 Adafruit Industries // Licensed under the MIT license. // // All text above must be included in any redistribution. /************************** Configuration ***********************************/ // edit the config.h tab and enter your Adafruit IO credentials // and any additional configuration needed for WiFi, cellular, // or ethernet clients. #include "config.h" // Import Servo Libraries #if defined(ARDUINO_ARCH_ESP32) // ESP32Servo Library (https://github.com/madhephaestus/ESP32Servo) // installation: library manager -> search -> "ESP32Servo" #include <ESP32Servo.h> #else #include <Servo.h> #endif /************************ Example Starts Here *******************************/ // pin used to control the servo #define SERVO_PIN 14 // Flag's up position, in degrees #define FLAG_UP 0 // Flag's down position, in degrees #define FLAG_DOWN 180 // How long to hold the flag up, in seconds #define FLAG_DELAY 2 // create an instance of the servo class Servo servo; // set up the 'servo' feed AdafruitIO_Feed *gmail_feed = io.feed("gmail"); void setup() { // start the serial connection Serial.begin(115200); // wait for serial monitor to open while(! Serial); Serial.print("IFTTT Gmailbox"); // tell the servo class which pin we are using servo.attach(SERVO_PIN); // connect to io.adafruit.com Serial.print("Connecting to Adafruit IO"); io.connect(); // set up a message handler for the 'servo' feed. // the handleMessage function (defined below) // will be called whenever a message is // received from adafruit io. gmail_feed->onMessage(handleMessage); // wait for a connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); } // we are connected Serial.println(); Serial.println(io.statusText()); gmail_feed->get(); // write flag to down position servo.write(FLAG_DOWN); } void loop() { // io.run(); is required for all sketches. // it should always be present at the top of your loop // function. it keeps the client connected to // io.adafruit.com, and processes any incoming data. io.run(); } // this function is called whenever a 'gmail' message // is received from Adafruit IO. it was attached to // the gmail feed in the setup() function above. void handleMessage(AdafruitIO_Data *data) { Serial.println("You've got mail!"); servo.write(FLAG_UP); // wait FLAG_DELAY seconds delay(FLAG_DELAY * 1000); servo.write(FLAG_DOWN); }
Text editor powered by tinymce.