Azure ESP32 IoT DevKit State

ESP32 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:

Open the project folder

Start VS Code

Open Azure IoT Device Workbench Examples

Use F1 or Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type Azure IoT Device Workbench, and then select Open Examples....

IoT Device Workbench: Examples

Select ESP32 Arduino.

IoT Device Workbench: Examples -> Select board

Then the IoT Device Workbench Example window is shown up.

IoT Device Workbench, Examples window

Find ESP32 State and click Open Sample button. A new VS Code window with a project folder in it opens.

IoT Device Workbench, select ESP32 State example

Provision Azure Services

In the solution window, open the command palette and select Azure IoT Device Workbench: Provision Azure Services....

IoT Device Workbench: Cloud -> Provision

Then VS Code guides you through provisioning the required Azure services.

IoT Device Workbench: Cloud -> Provision steps

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....

IoT Device Workbench: Cloud -> Deploy

Config Device Code

  1. 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 = "";
    
  2. Open the command palette and select Azure IoT Device Workbench: Config Device Settings....

    IoT Device Workbench: Device -> Settings

  3. Select Copy device connection string.

    IoT Device Workbench: Device copy connection string

    This copies the connection string that is retrieved from the Provision Azure services step.

  4. 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

  1. Open the command palette and select Azure IoT Device Workbench: Upload Device Code.

    IoT Device Workbench: Device -> Upload

  2. VS Code then starts verifying and uploading the code to your DevKit.

    IoT Device Workbench: Device -> Uploaded

  3. 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 F1 or Ctrl+Shift+P (macOS: 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.

change board

Monitor WiFi information in Browser

  1. Open web\index.html in browser.
  2. Input the Function App name you write down.
  3. Click connect button.
  4. You should see wiFi information in a few seconds. web page

Restart ESP32 Device

Click Restart button on the web page. You may need to wait for a while to see 'Restarting'. devkit state

You can also see the restart information from serial monitor. restart information