|
1 |
| -# a MQTT Demo that Connect ESP32 to Azure Cloud |
| 1 | +# A MQTT Demo that Connects ESP device to Azure Cloud |
| 2 | + |
2 | 3 | # Table of Contents
|
3 |
| -- [Introduction](#Introduction) |
4 |
| -- [Part 1: Prerequisites](#prerequisites) |
5 |
| -- [Part 2: Prepare your iothub](#prepare) |
6 |
| -- [Part 3: SDK and Tools Preparation](#tools_prepare) |
7 |
| -- [Part 4: Configuring and building](#config_build) |
8 |
| -- [Part 5: Result shows](#results) |
9 |
| -- [TroubleShoot](#troubleshoot) |
10 |
| - |
11 |
| -<span id = "Introduction">Introduction</span> |
12 |
| ------------------------------- |
13 |
| -###### ESP32 is one of gorgeous ioT device that can interface with other systems to provide Wi-Fi and Bluetooth functionality through the SPI / SDIO or I2C / UART interfaces.for more details, click https://espressif.com/en/products/hardware/esp32/overview |
14 |
| -###### Azure cloud is one of wonderful cloud that could collect data from lot device or push data to lot device,for more details, click https://www.azure.cn/home/features/iot-hub/ |
15 |
| - **Aim:** |
16 |
| - ##### This page would guide you connecting your device(ESP32 or lot device with ESP32) to Azure by MQTT protocol, and then send data to Azure,receive message from Azure.Main workflow: |
17 |
| - https://github.com/ustccw/RepoForShareData/blob/master/Microsoft/AzureData/Photos/ESP32AzureWorkflow.png |
18 |
| - |
19 |
| - <span id = "prerequisites">Part 1: Prerequisites</span> |
20 |
| - ------------------------------ |
21 |
| -- **ubuntu environment** for building your demo. |
22 |
| -- **ESP32 device** for running the demo. |
23 |
| -https://github.com/ustccw/RepoForShareData/blob/master/Microsoft/AzureData/Photos/ESP32-DevKitC.png |
24 |
| - |
25 |
| - |
26 |
| - |
27 |
| - <span id = "prepare">Part 2: Prepare your iothub</span> |
28 |
| - ------------------------------ |
29 |
| -follow the guide: https://github.com/ustccw/RepoForShareData/blob/master/Microsoft/AzureData/start_Iothub.docx |
30 |
| -you would get an **iothub login connect string** like that: |
| 4 | +- [Introduction](#introduction) |
| 5 | +- [Preparation](#preparation) |
| 6 | +- [Configuring and Building](#configuring-and-building) |
| 7 | +- [Checking Result](#checking-result) |
| 8 | +- [Troubleshooting](#troubleshooting) |
| 9 | + |
| 10 | +## Introduction |
| 11 | +<a name="Introduction"></a> |
| 12 | + |
| 13 | +Espressif offers a wide range of fully-certified Wi-Fi & BT modules powered by our own advanced SoCs. For more details, see [Espressif Modules](https://www.espressif.com/en/products/hardware/modules). |
| 14 | + |
| 15 | +Azure cloud is one of the most wonderful clouds that collects data from lots of devices or pushes data to IoT devices. For more details, see [Azure IoT Hub](https://www.azure.cn/en-us/home/features/iot-hub/). |
| 16 | + |
| 17 | +This demo demonstrates how to firstly connect your device (ESP devices or IoT devices with ESP devices inside) to Azure, using MQTT protocol, then send data to Azure as well as receive message from Azure. |
| 18 | + |
| 19 | +Main workflow: |
| 20 | + |
| 21 | +  |
| 22 | + |
| 23 | +## Preparation |
| 24 | + |
| 25 | +<a name="preparation"></a> |
| 26 | +### 1. Hardware |
| 27 | +- An **ubuntu environment** should be set up to build your demo; |
| 28 | +- Any **[ESP device](https://www.espressif.com/en/products/hardware/modules)** can be used to run your demo. |
| 29 | + |
| 30 | +### 2. Azure IoT Hub |
| 31 | +- [Get iothub connection string (primary key)](https://www.azure.cn/en-us/pricing/1rmb-trial-full/?form-type=identityauth) from the Azure IoT Hub, which will be used later. An example can be seen below: |
| 32 | + |
31 | 33 | ```
|
32 | 34 | HostName=yourname-ms-lot-hub.azure-devices.cn;SharedAccessKeyName=iothubowner;SharedAccessKey=zMeLQ0JTlZXVcHBVOwRFVmlFtcCz+CtbDpUPBWexbIY=
|
33 | 35 | ```
|
| 36 | +- For step-by-step instructions, please click [here](doc/IoT_Suite.md). |
34 | 37 |
|
35 |
| - <span id = "tools_prepare">Part 3: SDK and Tools Preparation</span> |
36 |
| - ------------------------------ |
37 |
| - #### 3.1 iothub-explorer install |
38 |
| - The iothub-explorer tool enables you to provision, monitor, and delete devices in your IoT hub. It runs on any operating system where Node.js is available. |
39 |
| -- Download and install Node.js from here. https://nodejs.org/en/ |
40 |
| -- From a command line (Command Prompt on Windows, or Terminal on Mac OS X), execute the following: |
41 |
| - ``` |
42 |
| - npm install -g iothub-explorer |
43 |
| - ``` |
44 |
| -##### if success, you can get version information like: |
45 |
| -```shell |
| 38 | +### 3. iothub-explorer |
| 39 | +- Install [Node.js](https://nodejs.org/en/); |
| 40 | +- Install [iothub-explorer](https://www.npmjs.com/package/iothub-explorer) with command line `npm install -g iothub-explorer`. |
| 41 | + - If failed, please check [here](http://thinglabs.io/workshop/esp8266/setup-azure-iot-hub/) for more information. |
| 42 | + - If succeeded, please check the version information with the command lines below: |
| 43 | +``` |
46 | 44 | $ node -v
|
47 | 45 | v6.9.5
|
48 | 46 | $ iothub-explorer -V
|
49 | 47 | 1.1.6
|
50 | 48 | ```
|
51 |
| -##### if failed,please click http://thinglabs.io/workshop/esp8266/setup-azure-iot-hub/ |
52 |
| - |
53 |
| -##### after finished: |
54 |
| -then you can use your iothub-explorer to manager your iot-device.click https://github.com/ustccw/RepoForShareData/blob/master/Microsoft/AzureData/iothub-explorer |
55 | 49 |
|
56 |
| -login with: **iothub login connect string** that gets from Part 2 |
| 50 | +After that, you should be able to use iothub-explorer to manage your iot-device. |
57 | 51 |
|
58 |
| -then you can get one **device connect string** after you create one device like that: |
59 |
| -``` |
| 52 | +### 4. Device Connection String |
| 53 | +- login with the **iothub connection string (primary key)** you got earlier with command lines; |
| 54 | +- create your device, and get a **device connection string**. An example can be seen: |
| 55 | + |
| 56 | +``` |
60 | 57 | "HostName=esp-hub.azure-devices.net;DeviceId=yourdevice;SharedAccessKey=L7tvFTjFuVTQHtggEtv3rp+tKEJzQLLpDnO0edVGKCg=";
|
61 | 58 | ```
|
62 |
| -keep this **device connect string** in mind. |
63 | 59 |
|
64 |
| - |
65 |
| - #### 3.2 SDK get |
66 |
| - you can get AZURE-SDK from https://github.com/ustccw/AzureESP32 |
67 |
| - this SDK can implement that connect ESP32 to Azure by MQTT protocol. |
68 |
| - you can get IDF-SDK from https://github.com/espressif/esp-idf |
69 |
| - this SDK can make ESP32 work well |
70 |
| - |
71 |
| - #### 3.3 Compiler get |
72 |
| - follow the guide: http://esp-idf.readthedocs.io/en/latest/get-started/linux-setup.html |
73 |
| - |
| 60 | +For detailed instruction, please click [Here](doc/iothub_explorer.md). |
74 | 61 |
|
75 |
| - <span id = "config_build">Part 4: Configuring and building</span> |
76 |
| - ------------------------------ |
77 |
| -### 4.1 Update Variables |
78 |
| -[/examples/project_template/user/iothub_client_sample_mqtt.c](#) |
| 62 | +### 5. SDK |
| 63 | +- [AZURE-SDK](https://github.com/espressif/esp-azure) can be implemented to connect your ESP devices to Azure, using MQTT protocol. |
| 64 | +- Espressif SDK |
| 65 | + - For ESP32 platform: [ESP-IDF](https://github.com/espressif/esp-idf) |
| 66 | + - For ESP8266 platform: [ESP8266_RTOS_SDK](https://github.com/espressif/ESP8266_RTOS_SDK) |
| 67 | + |
| 68 | +### 6. Compiler |
| 69 | +- For ESP32 platform: [Here](https://github.com/espressif/esp-idf/blob/master/README.md) |
| 70 | +- For ESP8266 platform: [Here](https://github.com/espressif/ESP8266_RTOS_SDK/blob/master/README.md) |
| 71 | + |
| 72 | +## Configuring and Building |
| 73 | + |
| 74 | +<a name="Configuring_and_Building"></a> |
| 75 | + |
| 76 | +### 1. Updating variables |
| 77 | +- Replace the `connectionString` variable with your device connection string in [esp-azure/main/iothub_client_sample_mqtt.c](https://github.com/espressif/esp-azure/blob/master/main/iothub_client_sample_mqtt.c). |
79 | 78 |
|
80 |
| -Update the connectionString variable to the device-specific connection string you got earlier from the Setup Azure IoT step: |
81 | 79 | ```
|
82 |
| -static const char* connectionString = '[azure connection string]' |
| 80 | +static const char* connectionString = '[azure connection string]'; |
83 | 81 | ```
|
84 |
| -The azure connection string contains Hostname, DeviceId, and SharedAccessKey in the format: |
| 82 | +- The azure connection string contains Hostname, DeviceId, and SharedAccessKey in the format: |
| 83 | + |
85 | 84 | ```
|
86 | 85 | "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
|
87 |
| - ``` |
88 |
| - ### 4.2 config your Wifi |
89 |
| - ``` |
90 |
| - make menuconfig |
91 |
| - ``` |
92 |
| - choose example configuration to **set Wifi SSID and Password!** |
93 |
| - |
94 |
| - ### 4.3 build your demo and flash to ESP32 |
95 |
| - ``` |
96 |
| - $make flash |
97 |
| - ``` |
98 |
| - if failed,try: |
99 |
| - - make sure that ESP32 had connect to PC by serial port |
100 |
| - - make sure you flash to correct serial port |
101 |
| - - try type command: |
102 |
| - > sudo usermod -a -G dialout $USER |
103 |
| - |
104 |
| -<span id = "results">Part 5: Result shows</span> |
105 |
| - ------------------------------ |
106 |
| -login iothub-explorer,and monitor events: |
107 |
| -``` |
108 |
| -iothub-explorer monitor-events AirConditionDevice_001 --login 'HostName=youriothub-ms-lot-hub.azure-devices.cn;SharedAccessKeyName=iothubowner;SharedAccessKey=zMeLQ0JTlZXVcHBVOwRFVmlFtcCz+CtbDpUPBWexbIY=' |
109 | 86 | ```
|
110 |
| -- restart ESP32 after bin had flashed,you would see the ESP32 send data to lothub-explorer by minicom,and iothub-explorer would receive data! |
111 |
| -- At the same time,you can send message to ESP32 by iothub-explorer until you send a quit message |
112 |
| - |
113 |
| - <span id = "troubleshoot">TroubleShoot</span> |
114 |
| - ------------------------------ |
115 |
| - - close some firewall settings |
116 |
| - - build failed,try: |
117 |
| - - git submodule init |
118 |
| - - git submodule update |
119 |
| - - export your compiler path |
120 |
| - - export your SDK path |
121 |
| - - get start with http://espressif.com/en/support/download/documents?keys=&field_type_tid%5B%5D=13 |
122 |
| - - make sure you had input correct device connect-string which get from Part 3 |
123 |
| - |
| 87 | + |
| 88 | +### 2. Configuring your Wi-Fi and serial port |
| 89 | +- Go to `make menuconfig` -> `Example configuration` to configure your Wi-Fi SSID and Password; |
| 90 | +- Go to `make menuconfig` -> `Serial flasher config` to configure you serial port. |
| 91 | + |
| 92 | +### 3. Building your demo and flash to ESP device with `$make flash`. |
| 93 | +If failed, please: |
| 94 | +- make sure your ESP device had connected to PC with serial port; |
| 95 | +- make sure you have selected the corrected serial port; |
| 96 | + - command `> sudo usermod -a -G dialout $USER` can also be used. |
| 97 | + |
| 98 | +## Checking Result |
| 99 | + |
| 100 | +<a name="Checking_Result"></a> |
| 101 | + |
| 102 | +Please check results on both the iothub and device side: |
| 103 | + |
| 104 | +- iothub: log into iothub-explorer, and monitor events with command `iothub-explorer monitor-events yourdevice --login 'yourprimarykey'` |
| 105 | +- ESP device: monitor events with command `make monitor` |
| 106 | + |
| 107 | +ESP device would send data to the Azure cloud, and then you would be able to receive data at the iothub side. |
| 108 | + |
| 109 | +## Troubleshooting |
| 110 | +<a name="Troubleshooting"></a> |
| 111 | + |
| 112 | +1. Some common problems can be fixed by disabling the firewall. |
| 113 | + |
| 114 | +2. You can try with the followings, if your build fails: |
| 115 | + - git submodule init |
| 116 | + - git submodule update |
| 117 | + - export your compiler path |
| 118 | + - export your IDF path |
| 119 | + - get start from [Here](https://www.espressif.com/en/support/download/documents) |
| 120 | + |
| 121 | +3. Make sure the device connection string you are using, which you get from Azure IoT Hub, is correct. |
0 commit comments