HTTPS Requests with Arduino MKR1000

Introduction

My full code: https://create.arduino.cc/editor/bd2-holt/10727619-6096-42a2-b08e-a9ca055ba597/preview

Capture.PNG

To replace Alexa, we are using IF This Then That (IFTTT). This service allows you to easily use applets for different platforms such as: YoutTube, Spotify, eMail, and SMS, among others. It works by reviving a trigger; ‘this’, then processing an output applet; ‘That’.

For our ‘This’ trigger we used the Webhooks applet. This applet allows you to send a HTTPS request (More specifically a POST or GET request, explained later) to webhooks, which will then trigger the output event. The easiest way to trigger webhooks is to copy and paste the unique URL into your browser and hit enter, however we needed to trigger it using Arduino…

Arduino MKR1000

The Arduino we are using for the internet connectivity is the Arduino MKR1000, a board specifically designed for Internet of Things (IoT) projects. The board uses a WiFi101 module which required the installation of libraries to use.

Setting up the MKR1000: To use the MKR1000, you must install the board type into arduino under Boards Manager. Install the SAMD 32bit ARM-cortex boards.

Next the firmware should be updated on your board, and any SSL (Secure Sockets Layer, used for encrypted HTTPS connections) certificates for web hosts should be added to the board. To do this you need to go to WiFi101 file>example sketches>Wifi101 and run the FirmwareUpdater sketch. Once this sketch is loaded go to tools, and run the firmware updater tool.

In this you should update your module to the newest version (See the text on your board’s WiFi101 module for whether you should use A or B version).

And also add SSL certificates for any website your connecting to, do this at the bottom by typing in the host address, e.g. http://www.google.co.uk.

WiFi 101

Using WiFi101 is relatively easy, however is a little more complex that ESP2866; the most common network module for Arduino.

WiFi101.h
WiFiClient.h
WiFiSSLClient.h
WiFiUdp.h
SPI.h

Include the above libraries.

//WiFi router setup
char ssid[] = "Bob's iPhone"; //network SSID (aka WiFi name)
char pass[] = "password123"; //network password
int status = WL_IDLE_STATUS;
const char* host = "maker.ifttt.com";
WiFiSSLClient sslClient;
WiFiClient client;

Setting up the router connection and defining the host we will connect to later: maker.ifttt.com.

void wifiSetup() {
// Check for the presence of the shield
Serial.print("WiFi101 shield: ");
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("NOT PRESENT");
return; // don't continue
}
Serial.println("DETECTED");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
printWifiStatus(); // you're connected now, so print out the status
}

Call this function using wifiSetup()'. WiFi.status() returns true if the module is connected properly. WiFi.begin(ssid, pass) initialises the connection.

The printWifiStatus() is a separate function, which prints to the serial connection information:

void printWifiStatus() {
// print the SSID of the network:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.println(ip);
}

After this is done your ready to start (trying) to send POST requests to IFTTT…

HTTPS with WiFi101

To do this you’ll need to use the following code:

void sendMessage() {
if (sslClient.connectSSL(host, 443)) { //also try .connectSSL
Serial.println("IFTTT request in Progress");
//change this to your Maker setting from //https://ifttt.com/services/maker/settings
String data = "randomdata";
sslClient.println("POST /trigger/tank_empty/with/key/bxazzvKgX-iohEv3KRPOr3 HTTP/1.1");
sslClient.println("Host: ");
sslClient.println(host);
sslClient.println("Content-Type: application/text/plain"); //originally //application/json
sslClient.print("Content-Length: ");
sslClient.println(data.length());
sslClient.println();
sslClient.print(data);
sslClient.stop();
Serial.println("IFTTT request Sucessful");
}
else {
Serial.println("IFTTT request failed");
}
}

sslClient.connectSSL(host, port) returns true if it can connect to the host (the port should be 80 or 443, default open networking ports): When I used maker.ifttt.com it failed at this point.

sslClient.println("POST /uniqueURL HTTP/1.1"); tells the host what request type your using; ‘POST’, followed by the specific domain’s url you want to access, followed by HTTP/1.1, the networking protocol used: This is where WiFi101 gets it’s name.

sslClient.println("Content-Type: application/text/plain"); tells the host what format the data you are sending it is. If you want to send extra conditions to IFTTT, this should be application/json.

The lines of code after this are saying that I’m about to send data, the length of the data, and the data itself.
sslClient.stop(); ends the POST request.

Conclusion

If this worked correctly your custom IFTTT applet should have triggered. Unfortunately I didn’t have the time to fully troubleshoot the problem. However I am fairly convinced it is in the formatting of my HTTP request. The formatting is very important and is occasionally server host specific. Alternatively it is because maker.ifttt.com is denying access for my specific Arduino MAC address, however this is very unlikely.

Using this code I successfully connected to and received data from google.co.uk, but it didn’t seem to work with maker.ifttt.com, but did work with zapier.com (an IFTTT alternative which seemed to have website flaws stopping me from using it). It also worked with ifttt.com

For our hand in we will be using a LCD screen to provide feedback instead.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s