DirectMethods gives you an option of calling methods in your device from Azure IoTHub.This feature can be used to control your devices say from turning on a LED on till fan. Ofcourse you can work on home automation in future as well (we would see an example of that later)
For this tutorial we would be using ESP32 microcontrolller (instead of ESP8266 which comes with our NodeMCU board).ESP32 is a successor of ESP8266 and adds one more core,faster WifFi,Bluetooth and more GPIO pins.It has been nicely documented on how to configure and use this board (here and here ).The board for ESP32 which I would be using is DOIT ESP32 DEVKIT V1 .
Code which we would be using in this post exists in my github repo .This code was written for ESP32 but with few minor tweaks it can be tested against ESP8266 as well.
Connection remains the same as what we did while connecting ESP8266\NodeMCU with DHT11 sensor.The + and – from DHT11 sensor should go to 3V3 and GND pin in ESP32.The data pin should go to anyone of your GPIO pin (2 pin in our case).Then what’s the change in code?
1)#define DHTPIN 2 (The digital pin we are connected to) Here we give an integer value instead of D1,D2 etc as we did with ESP8266.
2)#include <WiFi.h>. The WiFi library to include instead of ESP8266WiFi.h which we were using with ESP8266 (NodeMCU).
With these small changes the same code would work for both ESP8266 and ESP32 boards.
Lets see what our code does.If you go back to previous post the code mostly remains same (connecting to Azure IoTHub ,reading value from DHT and then sending it to Azure IoT Hub).Additional methods introduced are deviceMethodCallback,start and stop method.
deviceMethodCallback accepts a char and checks what is the name of method which is being called.In our case we would keep it simple without any payload and call the start and stop method.Start method sets a boolean value messageSending to true and stop sets this value to false.We control sending temperature and humidity value to AzureIoTHub using these boolean value in this if block
if (!messagePending && messageSending)
After downloading the code please edit these values as per your settings and then flash ESP32 or ESp8266 board.
If everything goes well you can see Temperature and Humidity data being sent to Azure IoT Hub as we saw in previous post.Next is how can we call our direct methods from Azure IoT hub.
For that you can go to your hub in Azure portal and click on IoTDevices and select your device as shown below
Once you select your device which was registered in portal you can see an option of DirectMethod
Click on that option which would give us a nice tool using which we call methods in our devices.
Here in this tool you can enter start as Method Name if you want to start sending data and stop if you want to stop sending data to Azure IoT hub from your device.Please do increase Connection Timeout and Method Timeout to something like 90 seconds or so.Now you can call the methods in your device from AzureIoTHub as shown below.
Later we would see how to send messages from AzureIoTHub to our devices.