cancel
Showing results for 
Search instead for 
Did you mean: 

IoT Button - Microsoft Flow

 

IoT Button: Process automation with Microsoft Flow using NodeMCU and Arduino IDE

 

In this article it will be developed an IoT button applied to the scenario of maintenance of a coffee machine using Microsoft Flow. However, it can be easily adapted to any other scenario or application.

 

Requirements

 

  • Access to Microsoft Flow or Azure Logic Apps
  • Arduino IDE
  • NodeMCU development board
  • Push Button
  • 1 x 330 Ω resistor
  • 1 x 1M Ω resistor
  • Jumpers
  • Breadboard
  • Micro USB cable

Setup Microsoft Flow  Environment

 

    1)    Microsoft Flow portal

 

Access Microsoft Flow, log in and click "My Flows".

 

1.png

 

  2)    Create from blank

 

Click "Create from blank" to create a new workflow.

 

2.png

 

    3)    Request/Response

 

Give a name to your Flow. Select the Trigger "Request/Response".

 

3.png

 

    4)    Method GET

 

In "Advanced Options", choose  "Method GET".

 

 

4.png

 

 

     5)    Add an action

 

Click "Add an action" to add a new action.

 

5.png

 

    6)    Send an email

 

Choose the action "Office 365 Outlook - Send an email".

 

6.png 

 

     7)    Create Flow

 

Complete all required fields (as you wish), and then click "Create Flow".

 

ingles 1.png

  

 

 

    8)    HTTP GET URL

 

Then copy and save the HTTP GET URL:

 

 

https://prod-32.westus.logic.azure.com:443/workflows/<ID>/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=<ID>

 

 

8.png

 

 

 

Hardware Setup

 

    1)    Building a Circuit on Breadboard

 

Build the circuit like the one shown below.

 

MicrosoftFlow-LogicApps-Button-Frittzing Project_bb.png

 

Software

 

The ESP8266 NodeMcu comes with a firmware that lets you program the chip with the Lua scripting language. But if you are already familiar with the Arduino way of doing things, you can also use the Arduino IDE to progam the ESP. In this tutorial we'll use the Arduino IDE.

 

 

 IDE Arduino setup

 

     1)    Package ESP8266

 

Download the IDE, and install it. Open the IDE; Choose File -> Preferences, in "Additional Boards Manager URLs" insert the URL "http://arduino.esp8266.com/stable/package_esp8266com_index.json" and than click "OK". After this steps, your download will start automatically. Once it is finished, restart the IDE.

 

9.png

 

    Softaware Setup

 

Download the file "MicrosoftFlow_IoT_JoaoLucindo.zip" attached and replace the values:

 

  • SSID by your wireless network name
  • PASSWORD by your wireless network password
  • HOST by the strings of the HTTP GET URL before 443 (in this case:  "https://prod-32.westus.logic.azure.com" )
  • URL  by the strings of the HTTP GET URL after 443 (in this case "/workflows/<ID>/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=<ID>")

 

 By doing that, the final code will be:

 

#include <ESP8266WiFi.h>

//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;

int inPin = 16;   // pushbutton connected to digital pin 0   
int val = 0;     // variable to store the read value
//Include the SSL client
#include <WiFiClientSecure.h>

char ssid[] = "<SSID>";       // your network SSID (name)
char password[] = "<PASSWORD>";  // your network key

//Add a SSL client
WiFiClientSecure client;


void setup() {

  pinMode(inPin, INPUT);      // sets the digital pin 1 as input

   Serial.begin(115200);

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }


Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);

}

String MicrosoftFlow() {
 
  char host[] = "prod-37.westus.logic.azure.com";

  if (client.connect(host, 443)) {
    Serial.println("connected");

    String URL = "/workflows/<ID>/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=<ID>";

    Serial.println(URL);

    client.println("GET " + URL + " HTTP/1.1");
    client.print("Host: "); client.println(host);
    client.println("User-Agent: arduino/1.0");
    client.println("");
    }
}

void loop() {
  
  
  val = digitalRead(inPin);  // read input value
  delay(200);
  //Serial.println(val);

  if(val==HIGH){
    MicrosoftFlow();
    delay(1000);
    setup(); 
    }
  
}

 

Now you can compile and upload the code from your computer to the device. You can see the result (after press the push button) in the picture below.

 12 - Copy.png

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Hi,

 

I tried this with two different ESP8266 boards (NodeMCU 1.0, Wemos D1 Mini Pro). Both will reset once the push button is pressed. Do you have an updated version of this code?

 

Thanks.

@dp79   I know it's been a few months, but it's resetting because the setup function is being called in the loop-- get rid of that line, you don't need it.

Meet Our Blog Authors
  • Working daily with Microsoft Cloud to deliver the needs of my company, my customers and various Microsoft communities and forums. | Office 365 | Flow | PowerShell | PowerApps | SharePoint |
  • Co-founder of https://plumsail.com, Office 365 and SharePoint expert. Passionate about design and development of easy to use, convenient and flexible products.
  • Microsoft Business Apps MVP. Owner of ThriveFast, an Office 365 consulting company.
  • 7x Microsoft Business Solutions MVP (CRM)
  • I'm keen in MS technologies, SharePoint, Office 365 and development for them
  • Daniel is a Business Productivity Consultant & Microsoft Business Solutions MVP who is very enthusiastic about all things Office 365, Microsoft Flow, PowerApps, Azure & SharePoint (Online). Since the preview, Daniel has been working with Microsoft Flow and later on with Microsoft PowerApps. That led to him being awarded an MVP Award for Business Solutions. He loves to blog, present and evangelize about improving productivity in the modern workspace with these amazing tools!
  • Michelle is an Office 365 solution architect in Twin Cities, MN. She has been delivering business collaboration solutions for years with her focus on SharePoint and Office 365. Michelle is a recent board member of the Minnesota Office 365 User Group and has been a member of the SharePoint community since 2009. She is a frequent speaker at MNSPUG and SharePoint Saturday and co-chaired the Legal SharePoint User Group for 4 years. Her most frequent projects have involved rolling out a large deployment of Office 365, SharePoint Online intranet, build of a "CHAMPS" Office 365 user adoption program and most recently, SharePoint On-Premise to Online Migration. Michelle is very excited about cloud technology as it is shifting her IT Pro focus to collaboration strategy and technical adoption.
  • I'm a Microsoft Office Servers and Services MVP with a special interest in SharePoint, Office 365, Microsoft Flow, Microsoft Teams and PowerApps. I work at Triad Group Plc ( https://triad.co.uk)
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor