Table of Contents
- Introduction
- Step 1: Prerequisites
- Step 2: Prepare your Device
- Step 3: Configure and Run the Sample
Introduction
About this document
TeXXmo IoT Button device with Azure IoT Hub. This multi-step process includes:
- Configuring Azure IoT Hub
- Registering your IoT device
- Configure the device and run the IoT sample
Step 1: Prerequisites
You should have the following items ready before beginning the process:
- Setup your IoT hub
- Provision your device and get its credentials
- teXXmo IoT Button device.
Set up and manage Azure IoT Hub
Azure IoT Hub is a fully managed service that enables reliable and secure bi-directional communications between millions of IoT devices and an application backend. You can learn more about Azure IoT Hub visiting the documentation site .
Before you can communicate with IoT hub from a device you must create at IoT hub instance in your Azure subscription and then provision your device in your IoT hub .
Because of developers preferences and constraints, there are several ways you can create an instance of Azure IoT Hub service and manage this instance. Below are the steps required to setup the IoT hub and manage it.
Create an Azure IoT hub …
- … using the Azure portal
- … using the Azure CLI 2.0 – Python command line
- … using the Azure CLI 1.0 – Node.js command line
- … using PowerShell and a resource manager template
- … using C # and a resource manager template
- … using C # and the resource provider REST APIs
Manage at Azure IoT hub
Once you have deployed to Azure IoT hub instance, you want to manage and interact with the following operations:
- Work with the device registry (Create, Update, Delete device IDs)
- Retrieve device credentials
- Retrieve user credentials
- Send Cloud to Device messages to devices
- Work with Device Twins
- Invoke Device Direct Methods
- Monitor operations of the service
The various service client SDKs are available through the Azure portal.
Retrieving user credentials to interact with the service (not as a device!)
SDKs is to retrieve user credentials. The first thing you want to do is to start working on an application that interacts with the IoT hub.
It is important to understand the difference between user credentials and device credentials:
- The device credentials are managed by the IoT Hub identity registry and are used by code on devices
- The user credentials are set at the IoT hub settings level and allow to define user access policies for applications thatwill manage the IoT hub. Details on Control access to IoT Hub can be found here .
C2D messages, and to work with the Device Twins and Methods. The user credentials can be found on the Azure portal in the „Shared Access Policies“ section of the IoT hub settings blade . Now you have the credentials, you can create
Create new device in the IoT Hub device identity registry …
- … using iothub-explorer (node.js command line tool)
- … using Device Explorer (Windows desktop application)
- … using the service client SDK …
- … for C #
- … for Node.js
- … for java
- … for Python
- … using Azure CLI v2.0 (Python command line tool)
Monitor IoT Hub operations
There is a way to monitor Azure IoT hub operations. This can help debug applications IoT hub. Everything you need to know about IoT Hub operations monitoring is here .
Manage IoT Hub
IoT Hub, you must add details of that device to the IoT Hub device identity registry. When you add a device to your IoT hub device identity registry, the hub generates the connection string that the device must use when it establishes its secure connection to your hub. You can therefore use the device identity to disable a device and prevent it from connecting to your hub.
Use the iothub explorer tool to provision a device
The iothub explorer tool is a cross-platform, command-line tool (written in Node.js) for managing your devices in IoT hub. You want to pre-install Node.js for your platform including npm package manager from nodejs.org
To install this tool in your environment, run the following command in a terminal / shell window on your machine:
sudo npm install -g iothub-explorer
To provision a new device:
- Get the connection string for your IoT hub.
- Run the following command to register your device with your IoT hub. When you run the command, replace
<iothub-connection-string>
with the IoT Hub connection string from the previous step and replace it<device-name>
with a name for your device search as mydevice .iothub-explorer login <iothub-connection-string>
iothub-explorer create <device-name> --connection-string
You should see a response like this:
Created device mydevice
-
...
-
connectionString: HostName=<hostname>;DeviceId=mydevice;SharedAccessKey=<device-key>
Copy the device connection string information for later use. The samples in this repository use connection strings in the format HostName=<iothub-name>.azure-devices.net;DeviceId=<device-name>;SharedAccessKey=<device-key>
.
To get help on how to use iothub explorer tool to perform other tasks.
iothub-explorer --help
Use the Device Explorer tool to provision a device
The Device Explorer tool is a Windows-only graphical tool for managing your devices in IoT Hub.
Azure IoT SDKs releases . The easiest way to install the Device Explorer is to download the pre-built version by clicking here . Scroll down to the Downloads section to locate the download link for the SetupDeviceExplorer.msi installer. Download and run the installer.
To run the Device Explorer tool, double-click the DeviceExplorer.exe file in Windows Explorer. The default installation folder for this application is C: \ Program Files (x86) \ Microsoft \ DeviceExplorer .
Tip: Pin the DeviceExplorer.exe application to your Windows taskbar for easier access.
You must connect Device Explorer to your IoT hub:
- Get the connection string for your IoT hub.
- On the Configuration tab, paste the IoT Hub connection string for your IoT hub into the IoT Hub connection string and click Update :
To register a new device with IoT Hub:
- Click the Management tab to manage the devices connected to the IoT hub.
- On the Management tab, click Create to register a new device with your IoT hub. The Create Device dialog appears. In the Device ID field, type a unique name for your device search as mydevice , or select Auto Generate ID to generate a unique ID. Then click Create .
- The Device Created window appears to be successfully registered with this IoT hub.
- Right-click on a selected device to retrieve the connection string for your device:
- Select Copy connection string to copy the device connection string to the clipboard. You can now paste this connection-string into the source code of the device. The samples in this repository use connection strings in the format
HostName=<iothub-name>.azure-devices.net;DeviceId=<device-name>;SharedAccessKey=<device-key>
.
Step 3: Run the sample
3.1 Configure the Azure IoT settings
- LED Flashes from Green Flash to Yellow, then Red flash. When LED flashes in RED, the device is in AP mode. It can be smartphone as well
- From any desktop machine, connect to the device via WiFi using the SSID: ESP_ <Last 3 digits of MAC Address>.
- Access to Web Interface and REST API with 192.168.4.1
- Use IoT Hub Configuration to set IoT Hub Device connection string.
- Use WiFi settings to connect to a local WiFi connection.
- Use User JSON to set JSON data to append to message.
- Make sure to call Shutdown to save setting.
3.2 Send Telemetry to IoT Hub Sample:
- Single Click the power button. This will send message to IoT Hub. See Manage IoT hub to learn how to observe the messages IoT hub Receives from the application.
You have now learned how to run a sample application that collects sensor data and sends it to your IoT hub. To explore how to store, analyze and visualize the data from this application in Azure using a variety of different services, please click on the following lessons:
- Manage cloud device messaging with iothub explorer
- Save IoT Hub messages to Azure data storage
- Use Power BI to visualize real-time sensor data from Azure IoT Hub
- Use Azure Web Apps to visualize real-time sensor data from Azure IoT Hub
- Weather forecast using the sensor data from your IoT hub in Azure Machine Learning
- Remote monitoring and notifications with Logic Apps
Source material: https://github.com/Azure/azure-iot-device-ecosystem/blob/master/get_started/micropython-teXXmo-iot-button–python.md#step-2-prepare-your-device