In this tutorial we will use an Arduino Nano 33 IoT, to send and email using an online automation tool that connects applications and services, named IFTTT.
The goals of this project are:
An online automation tool is used to connect applications and services to automate repetitive tasks without having to code that process or relying on developers to build the integration. The workflows followed by these tools are "If this, do this". The first part of the process (if this), starts with a trigger, the trigger could be anything from turning on a light, to posting something on your Instagram account. The second part (do this), will run an action step of your choice, every time the set trigger event occurs. For example: if a new row is added to a google spreadsheet, then send me and email.
The NINA-W102 module is a stand-alone multiradio MCU modules module that integrate a powerful microcontroller (MCU) and a radio for wireless communication. The radio provides support for Wi-Fi 802.11 b/g/n in the 2.4 Hz ISM band and BluetoothĀ® v4.2 (BluetoothĀ® BR/EDR and BluetoothĀ® low energy) communications.
If you want to learn more about Wi-Fi and how it works, you can check this link.
The NINA-W102 includes the wireless MCU, Flash memory, crystal, and components for matching, filtering, antenna and decoupling, making it a very compact stand-alone multiradio module. The module can be used to design solutions with top grade security, thanks to integrated cryptographic hardware accelerators.
The WiFiNINA library allows us to use the Arduino Nano 33 IoT NINA-W102 module, without having to go into complicated programming. The library supports WEP, WPA2 Personal and WPA2 Enterprise encryptions and it can serve as either a server accepting incoming connections or a client making outgoing ones. Some of the most common applications for the module are the following:
If you want to read more about the NINA-W102 module see here.
In this tutorial we will use the library to connect the board to the Wi-Fi network and send a HTTP request to a specific URL to trigger an action.
1. Setting up
Let's start by opening the Arduino Web Editor, create a new sketch and name it email_sender. Then, navigate to the Libraries tab, search for the WIFININA library and click on the Include button.
2. Connecting the board
Next, connect the Arduino Nano 33 IoT to the computer and make sure that the Web Editor recognizes it. If so, the board and port should appear as shown in the image below. If they don't appear, follow the instructions to install the plugin that will allow the Editor to recognize your board.
3.Sending the HTTP request
After including the library, we can begin by building the code.
Let's start by creating a secret header file to store our credential for the Wi-Fi network which we will connect to. In this way we don't accidentally store our credentials in a code we may share on the internet. To do this, we need to click the downward arrow next to our sketch tab, and click Add Secret Tab. This will create a tab called Secret.
We now need to head back to the original sketch file, and enter the following code:
1//please enter your sensitive data in the Secret tab2char ssid[] = SECRET_SSID; // your network SSID (name)3char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
This will automatically create two fields in the Secret tab. If we go back to this tab, we can enter our credentials there.
Now that are network credentials are safe, we can go back to the sketch and continue coding. We need to create a client that can connect to a specified internet IP address and port, using the
WiFiClient
function. We want to keep the code as simple as we possible, so in this case the name of the client will just be client
, but you can name it however you please.1WiFiClient client;
Then, we need to initialize three new variables called
HOST_NAME[]
, PATH_NAME
and queryString
that will store the URL of the webpage we are sending the HTTP request to and the path we need to follow to trigger the action.1char HOST_NAME[] = "maker.ifttt.com";2String PATH_NAME = "/trigger/event-name/with/key/your-key"; // change your EVENT-NAME and YOUR-KEY3String queryString = "?value1=57&value2=25";
Note: Keep in mind that the
will be updated once we create the action on the online automation tool.PATH_NAME
In the
setup()
, we need to start connecting to Wi-Fi using a WiFi.begin()
function and then we start the serial communication at 9600 bauds, followed by while(!Serial)
, which basically means that unless we open the Serial Monitor, the program will not run. We then use an if else
statement to check if the connection to the web server was successful or not.1void setup() {2 // initialize WiFi connection3 WiFi.begin(ssid, pass);4
5 Serial.begin(9600);6 while (!Serial);7
8 // connect to web server on port 80:9 if (client.connect(HOST_NAME, 80)) {10 // if connected:11 Serial.println("Connected to server");12 }13 else {// if not connected:14 Serial.println("connection failed");15 }16}
Inside the
loop()
, we will be adding an if()
function to check if the data enter in the Serial Monitor matches. If the data matches, we start making a HTTP request using some .println()
functions.1void loop() {2 if (Serial.read() == 's') {3
4 // make a HTTP request:5 // send HTTP header6 client.println("GET " + PATH_NAME + queryString + " HTTP/1.1");7 client.println("Host: " + String(HOST_NAME));8 client.println("Connection: close");9 client.println(); // end HTTP header
We then add a function that will create a loop where while the client is connected (
while (client.connected())
), if the client have any data available for reading (if (client.available())
), will be read and print in the Serial Monitor.1while (client.connected()) {2 if (client.available()) {3 // read an incoming byte from the server and print it to serial monitor:4 char c = client.read();5 Serial.print(c);6 }7 }
Lastly, we add a
.stop()
function that will stop the communication with the client, when the client is not connected, and we use a Serial.println()
function to print that the client has disconnected.1// the server's disconnected, stop the client:2 client.stop();3 Serial.println();4 Serial.println("disconnected");5 }6}
4. Complete code
If you choose to skip the code building section, the complete code can be found below:
1#include <WiFiNINA.h>2
3//please enter your sensitive data in the Secret tab4char ssid[] = SECRET_SSID;5char pass[] = SECRET_PASSWORD;6
7WiFiClient client;8
9char HOST_NAME[] = "maker.ifttt.com";10String PATH_NAME = "/trigger/event-name/with/key/your-key"; // change your EVENT-NAME and YOUR-KEY11String queryString = "?value1=57&value2=25";12
13void setup() {14 // initialize WiFi connection15 WiFi.begin(ssid, pass);16
17 Serial.begin(9600);18 while (!Serial);19
20 // connect to web server on port 80:21 if (client.connect(HOST_NAME, 80)) {22 // if connected:23 Serial.println("Connected to server");24 }25 else {// if not connected:26 Serial.println("connection failed");27 }28}29
30void loop() {31 if (Serial.read() == 's') {32
33 // make a HTTP request:34 // send HTTP header35 client.println("GET " + PATH_NAME + queryString + " HTTP/1.1");36 client.println("Host: " + String(HOST_NAME));37 client.println("Connection: close");38 client.println(); // end HTTP header39
40
41 while (client.connected()) {42 if (client.available()) {43 // read an incoming byte from the server and print it to serial monitor:44 char c = client.read();45 Serial.print(c);46 }47 }48
49 // the server's disconnected, stop the client:50 client.stop();51 Serial.println();52 Serial.println("disconnected");53 }54}
First we need to create the trigger and the action of our choice, using an online automation tool. In this case, we will use the IFTTT (If This Then That) as our online automation tool, but you can use any tool you prefer. The process will be similar.
The first step is to log in, if you don't have an account you can create one. Then we need to click on the Create button on the top right. We will see the following:
Now we need to click the Add button to add the trigger. We will see that we can choose a great amount of services to create a trigger, each of them consisting of different triggers. The one we need for this tutorial is called Webhooks, this services will activate the trigger every time the Maker services receives a web request. Click on Receive a web request and set an event name. We have set sketch_received as event name. Once we have chosen an event name click the Create trigger button.
The next part is to create an action that will happen every time Webhooks receives a web request. Click the Add button on the Then That section. As before we can choose between a lot of applications and services, but the one we are going to use here is Gmail. The action will be to receive and email in your own account, so we need to click on the Send yourself an email action. Then we just need to set the subject, the body and if we want, attach an URL. Once we have set all the info we just need to click the Create action and then Continue. It should looks like the image below.
Once we have created the trigger and the action, we can change the title of the applet and click the Finish button. The last step is to click on the Webhooks symbol and on the Documentation button in the top right. Here we can see the Key. We need to copy this key, go to the sketch we created before and paste the key to the variable
PATH_NAME
in the space for your-key
. Then we need to change the event-name
to the event name we choose in our sketch before, in this case sketch_received
. Lastly, we just need to upload to code to the board.Everything is ready! Now we just need to open the Serial Monitor, to see if the client is connected and whenever we enter an
s
and email will be send to your own account and we will see the trigger has been activated successfully in the Serial Monitor.Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot:
In this tutorial, we have learned how to simply connect to a Wi-Fi network by using the credentials in the code and send a HTTP request. We also learned how to use an online automation tool to use the HTTP request as a trigger that will execute an action on a different application or service.