"You've got mail" IoT E-Mailbox with Gmail Notification
Tags: iot, wia, wiadotone, servo
Published: 2019-07-29 14:25:13

# E-Mailbox 
_In this tutorial we will be going over how to use a Wia Dot One with a Grove Servo Motor to make the servo lift a flag on a mailbox whenever you have a new email._

![
](https://lh3.googleusercontent.com/0t-_rK0HwhOPFlDhXYNrbpLNuz6GE_TQQ38YwDpnhXNiNlm7CkqzwsjgC_fgo_NhIQtEuFTBF6Gs "Figure 1")
_Figure 1_

## Requirements 
* Wia Dot One [Buy yours here](https://store.wia.io/products/dot-one)
* Servo Motor [Buy yours here](https://www.amazon.com/Longruner-Helicopter-Airplane-Controls-KY66-10/dp/B01NA80LUR?th=1)
* MicroUSB cable [Buy yours here](https://store.wia.io/products/micro-usb-cable)
* Account on [Wia](https://www.wia.io/)

## Preparation
1. **Account  at Wia**
You will need to be registered and /or logged in to your Wia account at  https://www.wia.io/.
2. **Set up your Dot One**
You will need to have a set up Dot One and you can find the tutorial on how to do that [Here](https://wia.developerhub.io/dot-one/setting-up-a-dot-one).
3. **Set up your code project**
Now you will need to create a space and then a block project on your [Wia Dashboard](https://dashboard.wia.io/spaces)

## Creating the block Project
* Now you will need to set up a code block project. You can follow along from what is shown in figure 4. 
* Be careful when doing this and remember that capital letters make a difference when naming your variables which is especially important when it comes to giving your states the same names.
* If you would like the code version of this so you can copy and paste code rather than doing the block project feel free; It is also below.
* Lastly if you would like to try and make it yourself in code try that and then match it to my code if you are having any issues!

![
](https://lh3.googleusercontent.com/oWF_vRwgTKX_u9h4a_JFahW_27pxUM58VzkEEcE40GNuxzafcdcqVyiY0XXzY_L8bAaHXDV2Pbo_ "Figure 2")
_Figure 2_
``` C++
#include <WiFi.h>
#include <Wia.h>
#include <ESP32_Servo.h>

String currentState;
String previousState;

Wia wiaClient = Wia();
Servo myServo19;

void setup() {
  WiFi.begin();
  delay(2500);
  myServo19.attach(19);

  currentState = "";
  previousState = "";

}

void loop() {
  currentState = (wiaClient.getDeviceState("emailReceived"));
  delay(1000);
  if (currentState != previousState) {
    myServo19.write(180);
    delay(150000);
  }
  myServo19.write(20);
  previousState = currentState;
  delay(60000);

}
```

## Setting up Zapier
* We are going to use the Zapier platform with webhooks to detect when you have a new email and this will trigger an event on the Wia Cloud Platform.

1. To start go to Zapier and create an account and once you have reached the page as seen in figure 3 you need to start filling in the options for your Zap
* For the section **Connect this app...** enter **Gmail** and then for the section **with this one!** enter **Webhooks by Zapier**.
* Then for the section **When this happens...** enter **New email** or if you want some other option in your email to trigger the email box then select that. Lastly for **then do this!** select **POST** and press the **Use Zap** button! As seen in figure 3!
* ![
](https://lh3.googleusercontent.com/c9e2Dhg1J-sMvE5adTJuRJi_T_rQYPgMpvKr2IkKU0cPyJGd3TcFeHPARYQ0rLS3seZ5CM6SMnRS "Figure 3")
_Figure 3_
2. Then follow the instructions to connect your account and press **continue** and follow the instruction for what types of emails you would like to trigger Zap.
3. Next when you see the **Choose and action app** search for **Webhooks** and select **Post** and **Continue** as seen in figure 4 and figure 5.
![
](https://lh3.googleusercontent.com/XpT3hc-QaCfroTaGoom_L7j4JGQce_9a3pBcITkZRoQP-Ttm03uFoe0Y48uY5kI836SlLmrGXf-O "Figure 4")
_Figure 4_
![
](https://lh3.googleusercontent.com/6bhgXmrkwB22Om4sfStnnMapi52JGnWC3nLA5IUXtX6QfDxCcnTRcpRSJKKDGGZNjWb-V6Q4mpr0 "Figure 5")
_Figure 5_
4. Now for the next section we will need to fill in some information from the Wia Platform as explained below and refer to Figure 6 and Figure 7 and Figure 8 if needed.
* For the **URL** section put in **https://api.wia.io/v1/events**
* Leave **payload** empty. 
* For the **Data** in the left box put **name** and in the right box put the name of your event which for this example is **emailReceived** 
* In **Wrap Request Array** select **no**
* Leave **File** empty.
* In **Unflatten** select **yes**
* Leave **Basic Auth** empty.
* For the **Headers** in the the left box put **Authorization** and in the right box we will need our device ID.
* You can find your device ID by going to the [Wia Dashboard](https://dashboard.wia.io/spaces) and go into the space where your Dot One is.
* Then go to the **Things** tab, select your Dot One and go to the **Configuration** section and you should see **Your Device secret key is:** copy that key as seen in Figure 8 in the red box.
* Now go back to Zapier and in the **Headers** section in the right box input **Bearer (your secret key)** thus paste the key you copied so an example would look something like "Bearer d_sk_712yhbbasd8192yesn" if your secret key is "d_sk_712yhbbasd8192yesn".

![
](https://lh3.googleusercontent.com/yHEMVMhgzb7slp7sOoxMWIMA3WuSSY7Ut_FD8C-M-k_T7Y5HCp1QUWsqSgDOLgMPDMHoRma8q1Hk "Figure 6")
_Figure 6_
![
](https://lh3.googleusercontent.com/c6J0amCOL0UhKpcZu5CoVzH67QLp5AafjDCnTgNDRYZKBODWsCB5wTm5YwQd9pcvq52Ty37Umtxx "Figure 7")
_Figure 7_
![
](https://lh3.googleusercontent.com/EJBWLmTiOtcLTlZNxY9Y-3Qmxq_MsukpHuXtrWIwTCVJljfjv0uQ4-NKyjgb3FFcH-1t2dX5VRFk "Figure 8")
_Figure 8_

5. Then click **Continue** and **Finish** on the next page and you're all set with Webhook!
* You can test this by going through the tests on Zapier and click **Test This Step** under the **Action** section 
* Once Zapier has prompted that you have completed the test go to the [Wia Dashboard](https://dashboard.wia.io/spaces)  and in the **Things** tab select your dot one
* Then go to the **Events** section and you should see an event with the name you inputted on Zapier. As you can see in Figure 9 I have had many events come in!

![
](https://lh3.googleusercontent.com/ug4TXH6-d-BURiG6Yu2L8rfyqMM6fzNuRvhbYIiDMa2Xjv_rHiNXVE6c6HGNO1XiPsBHAO7yVT2V "Figure 9")
_Figure 9_
## Setting up your Flow
* Now that we have Zapier all set up with the webhooks to trigger events on our Dot One we need to set up a flow to recognize that event and tell your Dot One what to do.

1. To start off go to the [Wia Dashboard](https://dashboard.wia.io/spaces)  and go to the space with your Dot One and select the **Flows** tab to **Create a Flow**.
2. Now once in your new flow drag an **Event Created** node into the workspace found in **Trigger** under **Wia**.
3. Then name the event and select your Dot One from the List. 
* **Note:** It is very important that you name your event the same as the event name you put in your Webhook on Zapier which for our example was **emailReceived** as you can see in Figure 10.

![
](https://lh3.googleusercontent.com/7S7tAEH0Yd3EFWPyrFrFoIZX7xiYry9aW2w10MK4uxUGq78SRE9lJHQbGqf6-ha0YR6WXIiRBNlO "Figure 10")
_Figure 10_
4. Now drag a **Run Function** node into the workspace found in **Logic** under **Wia**.
5. In this run function copy and paste the following code into the editor:
``` JavaScript
output.body = device.state.emailReceived ? (parseInt(device.state.emailReceived) + 1) : 1;
```
* This code takes the current state and increments it by one if the state exists and if not it sets the current state to one. 
* This is used by our code previously to change the state so that the code can check if the state has changed and if it has that means an event was sent from webhooks thus a new email is in your email box!
6. Now we need to update the state so drag an **Update State** node into the workspace found in **Logic** under **Wia**.
* In this node we need to fill out some information about the state so select your device and for **State** put the state we are working with, for our example it is **emailReceived** and for value put **${input.body}** which takes the output of the run function and updates the state with its value.
7. Then connect the nodes as seen in Figure 11 and turn on the flow and you should be all set!

![
](https://lh3.googleusercontent.com/nA5Dma6djZatql7aezc1lQfkjPg1lFJFt2gD78VRL1C8QMCEfdWBJipQrUyYE17-XUZOjT-QeyMg "Figure 11")
_Figure 11_