Workshop documentation on building your own nodeMCU based overheating alarm using Microsoft flow.
- DHT pin 1 to 3,3V
- DHT pin 2 to D4
- DHT pin 4 to GND
- Resistor to 3,3V and DHT pin 2
- Install the Arduino.cc IDE software and launch the IDE and allow firewall access
- File > Preferences, check the “Display line numbers option“
- Add the URL http://arduino.esp8266.com/stable/package_esp8266com_index.json to the “Addition Boards Manager URLs” field and press the “Ok” button
- Tools > Boards > Boards Manager, install the "esp8266" board package
- Sketch > Include Library > Manage Libraries, install the "DHT sensor library" and the "Adafruit Unified Sensor" libraries
- Close and re-open Arduino IDE 1. Select the proper board from Tools->Board->Board ”nodeMCU 1.0 (ESP-12E Module)”
When Connectivity to the board does not work:
- Install the CP210x USB to UART driver and make sure you reboot your machine afterward (I suffered strange behaviors using the driver without rebooting) https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
- After rebooting open the Arduino IDE and select the proper board from Tools->Board->Board ”nodeMCU 1.0 (ESP-12E Module)”
- Load the example sketch “Blink” into the Arduino IDE from File->Examples->01 Basics->Blink
- Try compiling it by pressing the Verify button:
- When ok, connect the nodeMCU to your laptop: - Connect the nodeMCU with the USB cable - Via Tools -> Port, check if the correct COMxx port is selected - Upload the sketch by pressing the Upload button:
- Wait till the sketch is compiled and uploaded - The blue led on the nodeMCU should now start blinking
- Load the example sketch “DHTtester” into the Arduino IDE from File->Examples->DHT sensor library->DHTtester
- Try compiling it by pressing the Verify button
- When ok:
- Connect the nodeMCU with the USB cable
- Via Tools -> Port, check if the correct COMxx port is selected
- Upload the sketch by pressing the Upload button:
- Wait till the sketch is compiled and uploaded
- Open the monitor Tools->Serial Monitor
- Message with the temperature and humidity should appear within the monitor
Microsoft Flow is a brand new SaaS offering, for automating workflows across the growing number of applications and SaaS services that business users rely on. How much time do we spend sifting through streams of messages for the few notifications that matter? How much manual labor is spent transferring information from one place to another, or entering data into tracking systems? Too often these tasks are done manually, or not done at all.
We will pass the telemetry to an email address provided by you.
Note: in this workshop, you are introduced into Microsoft Flow. See for more information.
Follow these steps to create an endpoint in Microsoft Flow to send telemetry data to. From there we can use the data in an "If Then Else" flow.
Note: If you have no account yet, please sign up first (You can sign up for free using the button at the top of the Microsoft Flow portal).
Log into
the Microsoft flow portal. You will be asked to provide Microsoft Flow credentials if needed
-
Select
My flows
-
Normally, all your flows will appear here. Because there is no flow yet, a short introduction is shown
-
Select
Create from blank
-
An empty flow is shown
-
First give the flow a proper name. Name it
Mail Temperature trigger
(by replacing 'Untitled') -
You are invited to select one of many flow steps.
Scroll down
until a step called 'Request' is shown -
Then select the
Request
step. The following fields are shown -
This is an incoming API call and we will use our nodeMCU to trigger this flow. Note: The URL will be generated after save
-
The Azure Function will post a Json object to this Request URL. Flow can not handle this Json object directly. Therefore, enter the following 'Request Body JSON Schema'. This is used to transform the Json object into an entity. This way, Microsoft Flow can handle the separate fields in the message
{ "type": "object", "properties": { "temperature": { "type": "integer" }, "deviceId": { "type": "string" }, "time": { "type": "string" } }, "required": [ "deviceId" ] }
-
The Request step is ready now. We will retrieve the URL after creation of the Flow
-
Select
New step
-
In this flow, we will mail conditionally. So select
Add a condition
-
This is the heart of the Flow. We provide a condition (like 'Temperature is higher than 24'). And if it's true, a certain step will be executed. Otherwise, the other step will be executed. Note: The first or the latter are optional
-
Enter the left field with 'Choose a value'. The previous Request step will output an entity with fields like 'deviceId', 'time' and 'temperature'. So here you can compare one of the fields with another value
-
Select the
temperature
field -
Because we want to be warned when the temperature is higher than a certain value, select
is greater than
operator -
Finally, enter
23
in the right field -
We have created a condition, now we can fill in the 'if then else' blocks with steps. In the left block, Change 'IF YES, DO NOTHING' into 'IF YES' by selecting
Add an Action
-
Select the
Mail - Send email
step -
Create a connection for Mail.
Accept
the SendGrid terms and privacy policy Note: SendGrid is a third party email provider -
Enter
your own email address
in the 'To' field -
Enter
Overheating alarm for device
plus the entity field 'deviceId' in the 'Subject' field -
Enter
Hurry up, the temperature just got to
plus the entity field 'temperature' in the 'Email body' field -
This Mail step is ready, and so is the flow. Select
Create flow
-
The flow is now being created. We have to wait a moment to get it starting up. Select
Done
Although the flow is created and almost available we still need to do one thing more.
By this time, the endpoint of the Request step is created. Before we can pass telemetry to this flow, we need to have that API endpoint.
-
Select
My flows
-
All your flows will appear here. The flow we just created is shown
-
Edit the flow by clicking on it.
-
The flow is shown, in detail. Select the first step, the
Request step
so the fields are shown -
The URL of the Request step endpoint is now available to copy
-
Copy and write down the Url in the
HTTP POST to this URL
field -
Select `X Close' to close this page. Do not save any changes
Now we have the URL of the endpoint. We will call it inside the Azure Function.
-
Load the example sketch “HTTPSRequest” into the Arduino IDE from File->Examples->ESP8266Wifi->HTTPSRequest
-
Find the line with the *ssid and change it to the ssid given to you in the workshop
-
Find the line with the *password and change it to password given to you in the workshop
-
Find the line with the *host and change it to the host in the URL from Flow you got before (like prod-08.westeurope.logic.azure.com)
-
Below the host line add this declaration to hold the json payload:
-
// data string to hold json payload String data;
-
Find the line with url and change it to the remainder of the URL you got from Flow. (like /workflows/40665.....)
-
Find the part with the code:
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
-
And replace it with:
int temperature = 24; // Temperature so alarm is triggered data = "\{ \"temperature\": "; data = data + String(temperature); data = data + ", \"deviceId\": \""; data = data + "my_device"; // your device name data = data + "\", \"time\": \""; data = data + "my_time"; // your device time data = data + "\" \}"; Serial.print("Posting data: "); Serial.println(data); client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: BuildFailureDetectorESP8266\r\n" + "Content-Length: " + data.length() + "\r\n" + "Content-Type: application/json\r\n" + "\r\n" + data + "\r\n");
-
Try compiling it by pressing the Verify button
-
When ok:
- Connect the nodeMCU with the USB cable
- Via Tools -> Port, check if the correct COMxx port is selected
- Upload the sketch by pressing the Upload button
- Wait till the sketch is compiled and uploaded
- Open the monitor
-
connecting to <your ssid> ... WiFi connected IP address: 192.168.1.30 connecting to prod-08.westeurope.logic.azure.com certificate doesn't match requesting URL: /workflows/<your_url> Posting data: { "temperature": 24, "deviceId": "my_device", "time": "my_time" } request sent headers received esp8266/Arduino CI has failed reply was: ========== ========== closing connection
-
As you can see above the nodeMCU connects to the wifi router and then opens a connection to the flow server posting the json data. (There are some errors here as you can see, not important for now, we will go into these further down the line)
Microsoft Flow is passing the telemetry to the email address you provided. Microsoft flow provides a list of runs. We can check how our flow is doing.
-
Go to the Microsoft Flow portal
-
Select
My flows
-
All your flows will appear here. The flow we created will be shown
-
Select the run list of the flow by selecting
the i
(for Information) icon -
By now, one or more runs must be listed
-
Select a row and `click' on it
-
The execution of the flow is shown in detail. Green ticks will mark which steps are executed successfully. In the following example, the telemetry is received, the condition is checked but the 'IF NO, DO NOTHING' block is executed (the 'IF YES' block has no green tick)
-
But is the condition is right, the level is too low, both steps will have a green tick
-
Finally, open an email client and check your mail. Did you receive any mail?
Now we are able to measure the temperature from the DHT-22 sensor and send data to Flow it is time to combine the two sketches into one.
Hints:
- Arduino's use 2 standard functions to handle the setup of the device and sensors and a loop that it runs through indefinitely.
- Use the setup to initialize the DHT-22 and connect to the wifi router
- Use loop to retrieve the temperature and send it to Flow
- Warning: do not overrun Flow... Use a delay(10000); in your loop so there is a pause of 10 seconds in between your sends...
Within the monitor you have seen this error: certificate doesn't match. This indicates us that the certificate SHA1 fingerprint we set is not the same as the one we receive back from the Flow backend.
Now the challenge is to resolve this error by checking with the correct fingerprint.
Hints:
-
Enable debugging by adding under the #include <WiFiClientSecure.h>:
-
#define DEBUG_SSL #define DEBUGV
-
And the following line within setup:
-
Serial.setDebugOutput(true);
-
Get the Flow server certificate by checking the url with your browser and retrieve the SHA1 fingerprint. Put that in the fingerprint variable within the sketch.
Within the sketch we do not have the proper time set and passed on to Flow. Now retrieve the time somehow and use it to send the proper time and date alongside the temperature.
Hints:
- Why not use NTP servers to retrieve the time? (use a NTPclient example?)
- Calculate the date and time and put this within the payload replacing my_time
Up till now we used my_device as the identifier of the nodeMCU. What if we could use a proper identification which is unique like the MAC address?
Hints:
- Use Google and find example code to retrieve the nodeMCU's MAC address
- Pass the MAC address to Flow replacing my_time
- Example code below>>>>>>> (scroll way down)
#include <ESP8266WiFi.h>
uint8_t MAC_array[6];
char MAC_char[18];
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.macAddress(MAC_array);
for (int i = 0; i < sizeof(MAC_array); ++i){
sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
}
Serial.println(MAC_char);
}
void loop() {
}