by

Sending a Message by way of EWS from an Arduino MKR1000

Sending a Message by way of EWS from an Arduino MKR1000 The Web of Issues is all the craze as of late and probably the most fashionable prototyping platform for any digital initiatives as of late is the Arduino. The brand new MKR1000 Arduino board combines the performance from the Zero board with a WifiSheild on a compact board and fairly low-cost $35. It seems like

These boards are fairly cool and take you again in some methods to 80’s with among the programming challenges that include reminiscence restriction units. Eg the MRK1000 has 32Kb of SRAM which is half that of Commodore 64 though it runs at 48Mhz vs the 1.33 Mhz the Commodore might handle (I do know this is not a good technical comparability simply making an attempt present a little bit of distinction).

The good factor with this board is also as a result of it implements a cryptoauthentication chip it could actually do SSL communication which will be drawback with low energy boards due to the quantity of processing energy required for SSL. So on the naked bones this board can run code and logic that may permit HTTPS communication.

The next is an easy the best way to for sending an e-mail from an Change Mailbox utilizing EWS in Workplace365 from considered one of these units utilizing the next two libraries

https://github.com/arduino-libraries/ArduinoHttpClient  (used for the HTTPS communication)

https://github.com/agdl/Base64 (used to create the Base64 Authentication header)

Preparation

Earlier than you should utilize the board to speak to Workplace365 it’s good to load the SSL certificates required for SSL communication. This may increasingly sound a bit bizarre to most individuals as this course of you’d by no means have to fret about with most coding you’d do. Nonetheless due to the restricted energy/dimension of this unit it could actually solely maintain 10 certificates for WebSites you need to speak to and it has no capacity to load them dynamically so that you first want to repeat the cert to the machine. To do that it’s good to use the next sketch https://github.com/arduino-libraries/WiFi101-FirmwareUpdater after which use the add certificates to Wifi in the Wifi101 firmware replace which is within the Arduino IDE https://www.arduino.cc/en/Primary/Software program (eg simply add the basis certificates for outlook.workplace365.com see beneath)

To make use of the instance sketch I’ve created it’s good to set up the next Libraries within the Arduino IDE

https://github.com/arduino-libraries/ArduinoHttpClient  (used for the HTTPS communication)

https://github.com/agdl/Base64 (used to create the Base64 Authentication header)

When you do that you simply prepared to make use of the next Sketch the code is relativity easy, with EWS on Workplace365 it helps Fundamental and Oauth authentication as that is only for prototyping I’ve gone with fundamental auth which suggests you simply want a Base64 library to do the encoding of the authentication header.

The code use the Wifi Sketch as a base which lets you specify the SSID and password to make use of to your wifi within the following two variables

char ssid[] = “ssid”; // your community SSID (title)
char cross[] = “password”;

then whenever you add the sketch and connect with the serial port on the MRK1000 it’s going to attempt to hook up with the WIFI after which run the SendMessageEWS operate.

I’ve the next variables within the sketch to carry the Workplace365 username and password and knowledge for the message Topic, Physique and To Recipients

//Workplace365 Credentials
String ExUserName = “user@domaiin.onmicrosoft.com”;
String ExPassword = “passwprd”;

//Message Particulars
String Auth = ExUserName + “:” + ExPassword;
String Topic = “Subject of the Message”;
String To = “user@domain.com”;
String Physique = “Something happening in the Body”;

The SendMessageEWS operate first creates a variable that comprises the EWS SOAP Message to ship. (At this level it’s good to watch out of how large this string might get). The remainder of the code makes a connection to the Workplace365 Service utilizing the Arduino http shopper library that makes use of that Crypo chip for SSL. It then begins sending the headers crucial for the EWS POST and submits the POST String because the payload. I haven’t got something written to parse the response however the uncooked output is witten again to the serial port so you may see if it was efficiently or if one thing went mistaken. I’ve put a duplicate of the Sketch right here on GitHub https://github.com/gscales/Arduino-MRK1000/blob/grasp/EWS-Workplace365SendSample.ino the code seems like

#embody <Base64.h>
#embody <ArduinoHttpClient.h>

/*
This instance creates a connection to Workplace365 and Ship an E mail
by way of SOAP

Makes use of code from the next examples

https://www.arduino.cc/en/Tutorial/WiFiWebClient


*/

#embody <SPI.h>
#embody <WiFi101.h>

char ssid[] = "ssid"; // your community SSID (title)
char cross[] = "password"; // your community password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your community key Index quantity (wanted just for WEP)

//Workplace365 Credentials
String ExUserName = "user@domaiin.onmicrosoft.com";
String ExPassword = "passwprd";

//Message Particulars
String Auth = ExUserName + ":" + ExPassword;
String Topic = "Subject of the Message";
String To = "user@domain.com";
String Physique = "Something happening in the Body";



int cCount = 0;

int standing = WL_IDLE_STATUS;
WiFiSSLClient shopper;

void setup() {
//Initialize serial and look ahead to port to open:
Serial.start(9600);
whereas (!Serial) {
; // look ahead to serial port to attach. Wanted for native USB port solely
}

// verify for the presence of the protect:
if (WiFi.standing() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// do not proceed:
whereas (true);
}

// try to hook up with Wifi community:
whereas (standing != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect with WPA/WPA2 community. Change this line if utilizing open or WEP community:
standing = WiFi.start(ssid, cross);

// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
SendMessageEWS(Auth,To,Topic,Physique);

}


void SendMessageEWS(String AuthString,String MessageTo, String MessageSubject, String MessageBody)
{
//Auth Header
char basicHeader[AuthString.size()+1];
AuthString.toCharArray(basicHeader,AuthString.size()+1);
int inputStringLength = sizeof(basicHeader);
int encodedLength = Base64.encodedLength(inputStringLength);
char encodedString[encodedLength];
Base64.encode(encodedString, basicHeader, inputStringLength);
String AuthHeader = "Basic ";

//EWS SOAP Request
String content material = "<?xml model="1.0" encoding="utf-8"?>";
content material += "<cleaning soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/alternate/companies/2006/messages" xmlns:t="http://schemas.microsoft.com/alternate/companies/2006/sorts" xmlns:cleaning soap="http://schemas.xmlsoap.org/cleaning soap/envelope/">";
content material += "<soap:Header>";
content material += "<t:RequestServerVersion Model="Change2013_SP1" />";
content material += "</soap:Header>";
content material += "<soap:Body>";
content material += "<m:CreateItem MessageDisposition="SendAndSaveCopy">";
content material += "<m:SavedItemFolderId>";
content material += "<t:DistinguishedFolderId Id="sentitems" />";
content material += "</m:SavedItemFolderId>";
content material += "<m:Items>";
content material += "<t:Message>";
content material += "<t:Subject>" + MessageSubject + "</t:Subject>";
content material += "<t:Physique BodyType="HTML">" + MessageBody + "</t:Body>";
content material += "<t:ToRecipients>";
content material += "<t:Mailbox>";
content material += "<t:EmailAddress>" + MessageTo + "</t:EmailAddress>";
content material += "</t:Mailbox>";
content material += "</t:ToRecipients>";
content material += "</t:Message>";
content material += "</m:Items>";
content material += "</m:CreateItem>";
content material += "</soap:Body>";
content material += "</soap:Envelope>";


Serial.println("nBeginning connection to server...");
// if you happen to get a connection, report again by way of serial:
if (shopper.connectSSL("outlook.office365.com", 443)) {
Serial.println("connected to server");
shopper.print("POST ");
shopper.print("https://outlook.office365.com/EWS/Exchange.asmx");
shopper.println(" HTTP/1.1");
shopper.print("Host: ");
shopper.println("outlook.office365.com");
shopper.print("Authorization: Basic ");
shopper.println(encodedString);
shopper.println("Connection: close");
shopper.print("Content-Type: ");
shopper.println("text/xml");
shopper.println("User-Agent: mrk1000Sender");
shopper.print("Content-Length: ");
shopper.println(content material.size());
shopper.println();
shopper.println(content material);
}

}

void loop() {
// if there are incoming bytes accessible
// from the server, learn them and print them:
whereas (shopper.accessible()) {
char c = shopper.learn();
Serial.print(c);
}

// if the server's disconnected, cease the shopper:
if (!shopper.related()) {
Serial.println();
Serial.println("disconnecting from server.");
shopper.cease();
// do nothing forevermore:
whereas (true);
}
}


void printWifiStatus() {
// print the SSID of the community you are hooked up to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi protect's IP tackle:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the acquired sign power:
lengthy rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}