DEV Community

Andrew Berry
Andrew Berry

Posted on

"You've got mail" IoT E-Mailbox with Gmail Notification

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.

<br>
Figure 1

Requirements

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.
  3. Set up your code project Now you will need to create a space and then a block project on your Wia Dashboard

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!

<br>
Figure 2

#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
  2. For the section Connect this app... enter Gmail and then for the section with this one! enter Webhooks by Zapier.
  3. 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!
  4. 
Figure 3
  5. 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.
  6. 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. 
Figure 4 
Figure 5
  7. 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.
  8. For the URL section put in https://api.wia.io/v1/events
  9. Leave payload empty.
  10. 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
  11. In Wrap Request Array select no
  12. Leave File empty.
  13. In Unflatten select yes
  14. Leave Basic Auth empty.
  15. For the Headers in the the left box put Authorization and in the right box we will need our device ID.
  16. You can find your device ID by going to the Wia Dashboard and go into the space where your Dot One is.
  17. 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.
  18. 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".

<br>
Figure 6
<br>
Figure 7
<br>
Figure 8

  1. Then click Continue and Finish on the next page and you're all set with Webhook!
  2. You can test this by going through the tests on Zapier and click Test This Step under the Action section
  3. Once Zapier has prompted that you have completed the test go to the Wia Dashboard and in the Things tab select your dot one
  4. 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!

<br>
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 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.
  4. 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.

<br>
Figure 10

  1. Now drag a Run Function node into the workspace found in Logic under Wia.
  2. In this run function copy and paste the following code into the editor:
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!
  • 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.
  • Then connect the nodes as seen in Figure 11 and turn on the flow and you should be all set!

<br>
Figure 11

Top comments (0)