Azure ESP32 IoT DevKit State
In this tutorial, you can monitor WiFi status (SSID and RSSI) surrounding the ESP32 device and let the device restart using Azure IoT Hub device twins.
What you need
Finish the Getting Started Guide to:
- Get basic knowledge of ESP32 device.
- Prepare the development environment.
An active Azure subscription. If you do not have one, you can register via one of these two methods:
- Activate a free 30-day trial Microsoft Azure account.
- Claim your Azure credit if you are MSDN or Visual Studio subscriber.
Open the project folder
Start VS Code
- Start Visual Studio Code.
- Make sure Azure IoT Device Workbench is installed.
- Connect ESP32 to your PC.
Open Azure IoT Device Workbench Examples
Cmd+Shift+P) to open the command palette, type Azure IoT Device Workbench, and then select Open Examples....
Select ESP32 Arduino.
Then the IoT Device Workbench Example window is shown up.
Find ESP32 State and click Open Sample button. A new VS Code window with a project folder in it opens.
Provision Azure Services
In the solution window, open the command palette and select Azure IoT Device Workbench: Provision Azure Services....
Then VS Code guides you through provisioning the required Azure services.
The whole process includes:
- Select an existing IoT Hub or create a new IoT Hub.
- Select an existing IoT Hub device or create a new IoT Hub device.
- Create a new Function App.
Please take a note of the Function App name and IoT Hub device name you created. It will be used in the next section.
Modify code for Azure Functions
Open esp32-state\run.csx and modify the following line with the device name you provisioned in previous step:
static string deviceName = "";
Deploy Azure Functions
Open the command palette and select Azure IoT Device Workbench: Deploy to Azure....
Config Device Code
Open the source file(.ino) for device code and update the following lines with your WiFi ssid and password:
// Please input the SSID and password of WiFi const char* ssid = ""; const char* password = "";
Open the command palette and select Azure IoT Device Workbench: Config Device Settings....
Select Copy device connection string.
This copies the connection string that is retrieved from the
Provision Azure servicesstep.
Paste the device connection string into the following line in device code
/*String containing Hostname, Device Id & Device Key in the format: */ /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */ static const char* connectionString = "";
Build and upload the device code
Open the command palette and select Azure IoT Device Workbench: Upload Device Code.
VS Code then starts verifying and uploading the code to your DevKit.
The ESP32 device reboots and starts running the code.
[NOTE] M5Stack-Core-ESP32 is set as the default board after the IoT project is created. To change the setting, use
Cmd+Shift+P) to open the command palette, type and select Arduino: Board Config. Change to use other ESP32 board in the Arduino Board Configuration window.
Monitor WiFi information in Browser
- Input the Function App name you write down.
- Click connect button.
- You should see wiFi information in a few seconds.
Restart ESP32 Device
Click Restart button on the web page. You may need to wait for a while to see 'Restarting'.
You can also see the restart information from serial monitor.