Skip to content

Commit a0102c8

Browse files
committed
Merge branch 'feature/improve_config_and_docs_and_loop_message' into 'master'
feat: improve config and docs and loop message See merge request esp-components/esp-azure!3 I have tested this change in such and such a way and it works as expected.
2 parents 39d0a9b + f6a0c09 commit a0102c8

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.settings
22
.project
33
.cproject
4+
.vscode/
45

56
build
67
sdkconfig

README.md

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# A MQTT Demo that Connects ESP device to Azure Cloud
22

33
# Table of Contents
4+
45
- [Introduction](#introduction)
56
- [Preparation](#preparation)
67
- [Configuring and Building](#configuring-and-building)
78
- [Checking Result](#checking-result)
89
- [Troubleshooting](#troubleshooting)
910

1011
## Introduction
12+
1113
<a name="Introduction"></a>
1214

1315
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).
@@ -23,11 +25,14 @@ Main workflow:
2325
## Preparation
2426

2527
<a name="preparation"></a>
28+
2629
### 1. Hardware
30+
2731
- An **ubuntu environment** should be set up to build your demo;
2832
- Any **[ESP device](https://www.espressif.com/en/products/hardware/modules)** can be used to run your demo.
2933

3034
### 2. Azure IoT Hub
35+
3136
- [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:
3237

3338
```
@@ -36,6 +41,7 @@ HostName=yourname-ms-lot-hub.azure-devices.cn;SharedAccessKeyName=iothubowner;Sh
3641
- For step-by-step instructions, please click [here](doc/IoT_Suite.md).
3742

3843
### 3. iothub-explorer
44+
3945
- Install [Node.js](https://nodejs.org/en/);
4046
- Install [iothub-explorer](https://www.npmjs.com/package/iothub-explorer) with command line `npm install -g iothub-explorer`.
4147
- If failed, please check [here](http://thinglabs.io/workshop/esp8266/setup-azure-iot-hub/) for more information.
@@ -50,6 +56,7 @@ $ iothub-explorer -V
5056
After that, you should be able to use iothub-explorer to manage your iot-device.
5157

5258
### 4. Device Connection String
59+
5360
- login with the **iothub connection string (primary key)** you got earlier with command lines;
5461
- create your device, and get a **device connection string**. An example can be seen:
5562

@@ -60,41 +67,58 @@ After that, you should be able to use iothub-explorer to manage your iot-device.
6067
For detailed instruction, please click [Here](doc/iothub_explorer.md).
6168

6269
### 5. SDK
70+
6371
- [AZURE-SDK](https://github.com/espressif/esp-azure) can be implemented to connect your ESP devices to Azure, using MQTT protocol.
6472
- Espressif SDK
6573
- For ESP32 platform: [ESP-IDF](https://github.com/espressif/esp-idf)
6674
- For ESP8266 platform: [ESP8266_RTOS_SDK](https://github.com/espressif/ESP8266_RTOS_SDK)
6775

6876
### 6. Compiler
77+
6978
- For ESP32 platform: [Here](https://github.com/espressif/esp-idf/blob/master/README.md)
7079
- For ESP8266 platform: [Here](https://github.com/espressif/ESP8266_RTOS_SDK/blob/master/README.md)
7180

7281
## Configuring and Building
7382

7483
<a name="Configuring_and_Building"></a>
7584

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).
85+
### 1. Cloning Git submodules
7886

79-
```
80-
static const char* connectionString = '[azure connection string]';
81-
```
82-
- The azure connection string contains Hostname, DeviceId, and SharedAccessKey in the format:
87+
This repo uses [Git Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) for its dependancies. To successfully clone these other repositories, after cloning this repo, use the following command in the root:
8388

84-
```
85-
"HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
89+
``` bash
90+
git submodule update --init --recursive
8691
```
8792

88-
### 2. Configuring your Wi-Fi and serial port
89-
- Go to `make menuconfig` -> `Example configuration` to configure your Wi-Fi SSID and Password;
93+
### 2. Configuring your Azure IOT Hub Device Connection String, Wi-Fi and serial port
94+
95+
- Go to `make menuconfig` -> `Example configuration` to configure your Azure IOT Hub Device Connection String, Wi-Fi SSID and Password;
9096
- Go to `make menuconfig` -> `Serial flasher config` to configure you serial port.
9197

9298
### 3. Building your demo and flash to ESP device with `$make flash`.
99+
93100
If failed, please:
101+
94102
- make sure your ESP device had connected to PC with serial port;
95103
- make sure you have selected the corrected serial port;
96104
- command `> sudo usermod -a -G dialout $USER` can also be used.
97105

106+
To monitor the device output while running, run
107+
108+
``` bash
109+
make monitor
110+
```
111+
112+
To exit the monitor, hit Control-]
113+
114+
You can also run the build and monitor in onte step and run with multiple compiler threads:
115+
116+
``` bash
117+
make -j4 flash monitor
118+
```
119+
120+
This will build with four concurrent build processes to take advantage of more cores on your workstation.
121+
98122
## Checking Result
99123

100124
<a name="Checking_Result"></a>

main/Kconfig.projbuild

+11
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@ config WIFI_PASSWORD
1414

1515
Can be left blank if the network has no security set.
1616

17+
config IOTHUB_CONNECTION_STRING
18+
string "IOT Hub Device Connection String"
19+
default "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
20+
help
21+
String containing Hostname, Device Id & Device Key in the format:
22+
23+
HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>
24+
HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>
25+
26+
You can get this from the iothub-explorer CLI or the Azure Portal
27+
1728
endmenu

main/iothub_client_sample_mqtt.c

+16-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66

7+
#include "esp_system.h"
8+
79
#include "iothub_client.h"
810
#include "iothub_message.h"
911
#include "azure_c_shared_utility/threadapi.h"
@@ -18,13 +20,15 @@
1820
/*String containing Hostname, Device Id & Device Key in the format: */
1921
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
2022
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
21-
static const char* connectionString = "";
23+
#define EXAMPLE_IOTHUB_CONNECTION_STRING CONFIG_IOTHUB_CONNECTION_STRING
24+
static const char* connectionString = EXAMPLE_IOTHUB_CONNECTION_STRING;
2225

2326
static int callbackCounter;
2427
static char msgText[1024];
2528
static char propText[1024];
2629
static bool g_continueRunning;
27-
#define MESSAGE_COUNT 5
30+
31+
#define MESSAGE_COUNT 0 // Number of telemetry messages to send before exiting, or 0 to keep sending forever
2832
#define DOWORK_LOOP_NUM 3
2933

3034

@@ -108,7 +112,7 @@ void iothub_client_sample_mqtt_run(void)
108112
printf("\nFile:%s Compile Time:%s %s\n",__FILE__,__DATE__,__TIME__);
109113
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
110114

111-
EVENT_INSTANCE messages[MESSAGE_COUNT];
115+
EVENT_INSTANCE messages[MESSAGE_COUNT || 1];
112116

113117
g_continueRunning = true;
114118
srand((unsigned int)time(NULL));
@@ -153,24 +157,26 @@ void iothub_client_sample_mqtt_run(void)
153157

154158
do
155159
{
156-
if (iterator < MESSAGE_COUNT && (iterator<= callbackCounter))
160+
if ((!MESSAGE_COUNT || (iterator < MESSAGE_COUNT)) && (iterator<= callbackCounter))
157161
{
162+
EVENT_INSTANCE *thisMessage = &messages[MESSAGE_COUNT ? iterator : 0];
163+
158164
sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"AirConditionDevice_001\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2));
159165
printf("Ready to Send String:%s\n",(const char*)msgText);
160-
if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
166+
if ((thisMessage->messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
161167
{
162168
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
163169
}
164170
else
165171
{
166-
messages[iterator].messageTrackingId = iterator;
167-
MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
172+
thisMessage->messageTrackingId = iterator;
173+
MAP_HANDLE propMap = IoTHubMessage_Properties(thisMessage->messageHandle);
168174
(void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
169175
if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
170176
{
171177
(void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
172178
}
173-
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
179+
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, thisMessage->messageHandle, SendConfirmationCallback, thisMessage) != IOTHUB_CLIENT_OK)
174180
{
175181
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
176182
}
@@ -182,7 +188,8 @@ void iothub_client_sample_mqtt_run(void)
182188
iterator++;
183189
}
184190
IoTHubClient_LL_DoWork(iotHubClientHandle);
185-
ThreadAPI_Sleep(1);
191+
printf("Sleeping for 5\n");
192+
ThreadAPI_Sleep(5000);
186193

187194
// if (callbackCounter>=MESSAGE_COUNT){
188195
// printf("done sending...\n");

0 commit comments

Comments
 (0)