Introduction: GmailBox With Zapier and Adafruit
In this instructable i will explain you step by step how to build a Gmail notifier with the ESP8266.
What do you need:
- Gmail account
- Zapier account
- Adafruit account
- Arduino IDE
- NodeMCU ESP8266
- Servomotor (I'm using the SG90)
- LED light (i'm using a Neopixel LED strip, other lights will work as well but may require another library)
Step 1: Setting Up Zapier
Create a Zapier account and make a new Zap. You will need Zapier to receive data from Gmail and that will go afterwards to Adafruit. More about this later.
Step 2: Creating a Trigger
We want to make sure that if you receive a mail in Gmail, that something else is going to happen so you will need your Gmail account. In your new Zap choose Gmail as application trigger and sign in to Gmail.
Step 3: Connecting Zapier With Adafruit
Now you want to send the data that you received in Gmail to Adafruit. In the "Do this" section search for Adafruit and connect with your Adafruit account. Then select "Create Feed Data" as action event.
Step 4: Setting Up Adafruit
Now go to io.adafruit.com and create an account if you do not have one yet. Go to your Feeds and create a new Feed. In this case the name of my feed is "gmailbox", you will need this later.
If you can't find your Adafruit Key, you can find it in the top right corner.
Step 5: Feed Data From Zapier
Now go back to Zapier to arrange your feed data section. At feed key choose the "Use A Custom Value" option. The "Custum Value For Feed Key" has to be the same as the name from your Feed in Adafruit, so be careful with capital letters.
At "Value" fill in wat you want to see as value in Adafruit. The same value will be visible later on in the serial monitor in Arduino.
Step 6: Testing With Zapier, Adafruit and Gmail
Now you can test your Zap. Click on Test & Continue (dont forget to turn on the Zap afterwards). Then go back to Adafruit to your feed and under the graph you can see if the data has arrived. After you turned on your Zap you can also test this by sending yourself an mail.
Step 7: Setting Up Arduino
Plug in your ESP8266 and open Arduino. Create a new sketch and a new tab as indicated in the image above. Name it "config.h" (you can name it whatever you want). Paste the code down below in the "config" tab.
With this code you can connect to your WiFi and Adafruit. I got it from a previously used project. I still use it and it works well for me.
/************************ Adafruit IO Config *******************************/ // visit io.adafruit.com if you need to create an account, // or if you need your Adafruit IO key. #define IO_USERNAME "your adafruit username" #define IO_KEY "adafruit key" /******************************* WIFI **************************************/ #define WIFI_SSID "your wifi ssd" #define WIFI_PASS "wifi password" #include "AdafruitIO_WiFi.h"
Step 8: Setting Up Your NodeMCU
Connect the servomotor and the LED strip to your ESP8266.
Wires from the servomotor (SG90): Brown goes in G (ground), Red goes in 3V, Orange goes in D6 (or another digital pin). I also used some extra cables for extension.
Wires from the Neopixel: GDN goes to G (ground), DIN goes to D4 (or another digital pin), +5V goes to 3V.
Step 9: Setting Up Libraries and Hardware
Now go to your Arduino sketch you just created. First you have to include the libraries you need in the sketch. You will need need the config.h you just created, the library for the ESP8622 and for the Neopixel LED strip.
You put this in the code as follows:
#include "config.h" #include <ESP8622WiFi.h> #include <Adafruit_NeoPixel.h>
Next you're going to include the hardware you're using
#include <Servo.h> Servo servo; #define PIXEL_PIN D4 #define PIXEL_COUNT 10 #define PIXEL_TYPE NEO_GRB + NEO_KHZ800 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Add this line to link this sketch to the feed in Adafruit:
AdafruitIO_Feed *gmailbox = io.feed("your feed name");
Step 10: Void Setup Code
In the "void setup" you will connect to Adafruit, WiFi and set the servomotor to the assigned pin. After this you can check in the serial monitor whether a connection has been made.
I advise you to retype the code instead of copy-pasting it. This makes it easier to remember for later and you begin to learn what you write exactly.
The code will look like this:
void setup() { // put your setup code here, to run once: // start the serial connection with the serial monitor Serial.begin(115200) // wait for serial monitor to open while(!Serial); // connect to io.adafruit.com Serial.print("Connecting to Adafruit IO"); io.connect(); // set up a message handler for the 'your feed name' feed. // the handleMessage function (defined below) will be called whenever a message is // received from Adafruit. // wait for connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); pixels.begin(); pixels.show(); } // we are connected Serial.println(); Serial.println(io.statusText()); yourfeedname->get(); // the pin of the servomotor servo.attach(D6); servo.write(0); nameofyourfeed->onMessage(handleMessage); }
Step 11: Void Loop Code
Next you have make sure Adafruit is constantly running, we have to put this in the void loop so i runs constantly.
Do this as follows:
void loop() { // put your main code here, to run repeatedly: io.run(); }
Step 12: The Function for the Servomotor and LEDs As Output
Now we are going to make sure that when you receive an email, the servomotor and the LEDs will respond. We are going to create a function that ensures this. The function will be named "handleMessage" we used earlier in the code. We will use the value that we see Adafruit here.
If you receive a new email the servomotor needs to make a 90 degree turn and the LEDs need to light up. For the LEDs we are using red as color but you can change it in whatever you want. You also want to make sure that not only the first LED of the strip is on, so you have to put it in a loop to light the whole strip.
And again, try to retype the code instead of copy-pasting it.
The code will look like this:
void handleMessage(AdafruitIO_Data *data) { if (data > 0) { servo.write(90); delay(1000); Serial.println("You got mail!"); for( int i = 0; i<PIXEL_COUNT; i++) { pixels.setPixelColor(i, 250, 0, 0); //color is red pixels.show(); } } else { servo.write(0); delay(1000); Serial.println("No new mail"); } }
Step 13: Upload to Your NodeMCU and Test It
Upload the sketch to your ESP8266. You can do this by clicking on the right pointing arrow next to the check mark in the left upper corner.
Afterwards you can try to send yourself an email to test it and to make sure it works.
Step 14: The Finishing Touch
You are done with the coding part. Feeling creative? Build a mailbox housing for what you just made and stick a flag on the servomotor. The flag will go up every time you receive an email!
Thank you for following this manual and i hope it helped.