Skip to content

Commit 1fa5346

Browse files
authored
Merge pull request #40 from LDong-Arm/new_AWS_client
Use the new AWS client in the example
2 parents 8e91b18 + 6fbd894 commit 1fa5346

File tree

9 files changed

+386
-186
lines changed

9 files changed

+386
-186
lines changed

.travis.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ matrix:
7878
- pip install future==0.16.0
7979
- pip install "Jinja2>=2.10.1,<2.11"
8080
- pip install "intelhex>=1.3,<=2.2.1"
81+
# Fetch mbed-os: We use manual clone, with depth=1 and --single-branch to save time.
82+
- >-
83+
git clone --depth=1 --single-branch https://github.com/ARMmbed/mbed-os.git;
84+
- >-
8185
script:
82-
- mbedtools checkout
83-
- echo mbedtools build -t GCC_ARM -m ${TARGET_NAME} -b ${PROFILE}
84-
- mbedtools build -t GCC_ARM -m ${TARGET_NAME} -b ${PROFILE}
86+
- mbedtools deploy
87+
- echo mbedtools compile -t GCC_ARM -m ${TARGET_NAME} -b ${PROFILE}
88+
- mbedtools compile -t GCC_ARM -m ${TARGET_NAME} -b ${PROFILE}
8589
- ccache -s
8690

8791
- <<: *cmake-build-test

CMakeLists.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)
55

66
set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "")
7-
set(MBED_CONFIG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/.mbedbuild CACHE INTERNAL "")
7+
set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "")
88
set(APP_TARGET mbed-os-example-for-aws)
99

1010
include(${MBED_PATH}/tools/cmake/app.cmake)
1111

1212
add_subdirectory(${MBED_PATH})
1313
add_subdirectory(mbed-client-for-aws)
1414

15-
if("wifi_ism43362" IN_LIST MBED_TARGET_LABELS)
15+
if("-DMBED_CONF_ISM43362_PROVIDE_DEFAULT=1" IN_LIST MBED_CONFIG_DEFINITIONS)
1616
add_subdirectory(wifi-ism43362)
1717
endif()
1818

1919
add_executable(${APP_TARGET})
2020

2121
mbed_configure_app_target(${APP_TARGET})
2222

23-
mbed_set_mbed_target_linker_script(${APP_TARGET})
24-
2523
project(${APP_TARGET})
2624

2725
target_include_directories(${APP_TARGET}
@@ -32,9 +30,11 @@ target_include_directories(${APP_TARGET}
3230
target_sources(${APP_TARGET}
3331
PRIVATE
3432
main.cpp
33+
demo_mqtt.cpp
34+
demo_shadow.cpp
3535
)
3636

37-
if("wifi_ism43362" IN_LIST MBED_TARGET_LABELS)
37+
if("-DMBED_CONF_ISM43362_PROVIDE_DEFAULT=1" IN_LIST MBED_CONFIG_DEFINITIONS)
3838
target_link_libraries(${APP_TARGET}
3939
PRIVATE
4040
wifi-ism43362
@@ -44,8 +44,6 @@ endif()
4444
target_link_libraries(${APP_TARGET}
4545
PRIVATE
4646
mbed-os
47-
mbed-netsocket
48-
mbed-mbedtls
4947
mbed-client-for-aws
5048
)
5149

README.md

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
# Mbed OS example for AWS cloud
44

5-
The example project is part of the [Arm Mbed OS Official Examples](https://os.mbed.com/code/). It contains an application that connects to an AWS MQTT broker and publishes a message every 1 second for 10 seconds or until a message is received.
5+
The example project is part of the [Arm Mbed OS Official Examples](https://os.mbed.com/code/). It contains two demos:
6+
* MQTT (default): publishes a message every 1 second for 10 messages or until a message is received.
7+
* Device Shadow service: retrieves a desired value from the cloud, then reports a string value and then an integer value to the cloud.
68

79
You can build the project with all supported [Mbed OS build tools](https://os.mbed.com/docs/mbed-os/latest/tools/index.html). However, this example project specifically refers to the command-line interface tool [Arm Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
810
(Note: To see a rendered example you can import into the Arm Online Compiler, please see our [import quick start](https://os.mbed.com/docs/mbed-os/latest/quick-start/online-with-the-online-compiler.html#importing-the-code).)
911

10-
## Downloading this project
11-
1. [Install Mbed CLI](https://os.mbed.com/docs/mbed-os/latest/quick-start/offline-with-mbed-cli.html).
12+
## Mbed OS build tools
1213

13-
1. Clone this repository on your system, and change the current directory to where the project was cloned:
14-
15-
```
16-
$ git clone https://github.com/ARMmbed/mbed-os-example-for-aws.git && cd mbed-os-example-for-aws
17-
$ mbed deploy
18-
```
19-
20-
Alternatively, you can download the example project with Arm Mbed CLI using the `import` subcommand:
21-
22-
```
23-
$ mbed import mbed-os-example-for-aws && cd mbed-os-example-for-aws
24-
```
14+
### Mbed CLI 2
15+
Starting with version 6.5, Mbed OS uses Mbed CLI 2. It uses Ninja as a build system, and CMake to generate the build environment and manage the build process in a compiler-independent manner. If you are working with Mbed OS version prior to 6.5 then check the section [Mbed CLI 1](#mbed-cli-1).
16+
1. [Install Mbed CLI 2](https://os.mbed.com/docs/mbed-os/latest/build-tools/install-or-upgrade.html).
17+
1. From the command-line, import the example: `mbed-tools import mbed-os-example-for-aws`
18+
1. Change the current directory to where the project was imported.
2519

20+
### Mbed CLI 1
21+
1. [Install Mbed CLI 1](https://os.mbed.com/docs/mbed-os/latest/quick-start/offline-with-mbed-cli.html).
22+
1. From the command-line, import the example: `mbed import mbed-os-example-for-aws`
23+
1. Change the current directory to where the project was imported.
2624

2725
## Configuring the AWS IoT Core service
2826

@@ -64,54 +62,95 @@ You can build the project with all supported [Mbed OS build tools](https://os.mb
6462
6563
## Building and running
6664
65+
### MQTT demo (default)
66+
6767
1. If using WiFi (e.g. on DISCO_L475VG_IOT01A), enter your network's SSID and password in [`mbed_app.json`](./mbed_app.json) (see [here](https://github.com/ARMmbed/mbed-os-example-wifi/blob/master/README.md#getting-started)). Keep any existing `\"`s. (If you use a different WiFi-enabled target, you may need to manually import its WiFi driver as described [here](https://github.com/ARMmbed/mbed-os-example-wifi#supported-hardware).)
6868
6969
1. For Ethernet (e.g on K64F), connect a cable to the port.
7070
7171
1. Connect a USB cable between the USB port on the board and the host computer.
7272
7373
1. <a name="build_cmd"></a> Run the following command to build the example project, program the microcontroller flash memory, and open a serial terminal:
74+
75+
* Mbed CLI 2
76+
77+
```bash
78+
$ mbed-tools compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm --baudrate=115200
7479
```
75-
$ mbed compile -m detect -t <TOOLCHAIN> --flash --sterm --baud 115200
80+
81+
* Mbed CLI 1
82+
83+
```bash
84+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm --baudrate=115200
7685
```
7786
7887
Alternatively, you can manually copy the binary to the board, which you mount on the host computer over USB.
79-
The binary is located at `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-for-aws.bin`.
88+
The binary is located at:
89+
* **Mbed CLI 2** - `./cmake_build/<TARGET>/<PROFILE>/<TOOLCHAIN>/mbed-os-example-for-aws.bin`</br>
90+
* **Mbed CLI 1** - `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-for-aws.bin`
8091
8192
Depending on the target, you can build the example project with the `GCC_ARM` or `ARM` toolchain. Run the command below to determine which toolchain supports your target:
8293
8394
```
8495
$ mbed compile -S
8596
```
8697
98+
### Device Shadow demo
99+
100+
Before running this demo, go to the AWS IoT Core console. In _Test_ -> _Subscribe to a topic_, subscribe to `$aws/things/<your thing>/shadow/#` (the `#` is a wildcard) on the AWS to monitor all Device Shadow updates. Then in _Publish to a topic_, publish to `$aws/things/<your thing>/shadow/update` the following payload:
101+
102+
```json
103+
{
104+
"state": {
105+
"desired": {
106+
"DemoNumber": 200
107+
}
108+
}
109+
}
110+
```
111+
112+
Build with the same steps as the MQTT demo, but also set `aws-client.shadow` to `true` in [`mbed_app.json`](./mbed_app.json) before compilation.
113+
87114
## Expected output
88115

116+
### MQTT demo (default)
117+
89118
Once the example starts to run, you can [monitor a serial terminal](https://os.mbed.com/docs/mbed-os/v6.0/tutorials/serial-comm.html) to see that the device connects to your network, exchanges some TLS handshakes, connects to AWS and publishes to the topic you just subscribed. This can be seen on the AWS console as incoming messages.
90119

91120
The application publishes a message every second for 10 seconds, or until it receives a message from the cloud:
92121
```
93-
[INFO][Main]: sending warning message: Warning: Only 10 second(s) left to say your name !
94-
[INFO][Main]: sending warning message: Warning: Only 9 second(s) left to say your name !
95-
[INFO][Main]: sending warning message: Warning: Only 8 second(s) left to say your name !
96-
[INFO][Main]: sending warning message: Warning: Only 7 second(s) left to say your name !
122+
[INFO][Main]: Publishing "10 messages left to send, or until we receive a reply" to topic <your topic>
123+
[INFO][Main]: Message sent successfully
124+
[INFO][Main]: Publishing "9 messages left to send, or until we receive a reply" to topic <your topic>
125+
[INFO][Main]: Message sent successfully
126+
[INFO][Main]: Publishing "8 messages left to send, or until we receive a reply" to topic <your topic>
127+
[INFO][Main]: Message sent successfully
97128
```
98129

99-
You can send a message to your device via the AWS IoT console (Test -> Publish to a topic). Use the same topic name you set in `aws-mqtt-topic.value` in [`mbed_app.json`](./mbed_app.json).
130+
You can send a message to your device via the AWS IoT Core console (_Test_ -> _Publish to a topic_). Use the same topic name you set in `aws-mqtt-topic.value` in [`mbed_app.json`](./mbed_app.json). Use the existing JSON structure and set "message" to one you want to send.
100131

101-
On receipt of a message, the application displays on the console the message prefixed with `Hello␣`.
132+
On receipt of a message, the application displays on the console the message you sent via the AWS IoT Core console.
133+
134+
### Device Shadow demo
135+
136+
The following will be printed to the serial:
137+
```
138+
[INFO][Main]: Device Shadow document downloaded
139+
[INFO][Main]: Desired value of DemoNumber: <value set on the AWS console>
140+
[INFO][Main]: Device Shadow reported string value published
141+
[INFO][Main]: Device Shadow reported integer value published
142+
```
102143

103144
## Troubleshooting
104145
If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
105146

106147
## Related Links
107148

108-
* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html).
149+
* [Mbed client for AWS IoT Core](https://github.com/ARMmbed/mbed-client-for-aws)
150+
* [AWS IoT Core](https://aws.amazon.com/iot-core/)
109151
* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html).
110152
* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html).
111-
* [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/latest/reference/mbed-os-bare-metal.html).
112153
* [Mbed boards](https://os.mbed.com/platforms/).
113-
* [AWS IoT Core](https://aws.amazon.com/iot-core/)
114-
* [AWS IoT Core - Embedded C SDK](https://github.com/aws/aws-iot-device-sdk-embedded-C/tree/v4_beta)
115154

116155
### License and contributions
117156

demo_mqtt.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2020-2021 Arm Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#if !MBED_CONF_AWS_CLIENT_SHADOW
7+
8+
#include "mbed.h"
9+
#include "mbed-trace/mbed_trace.h"
10+
#include "rtos/ThisThread.h"
11+
#include "AWSClient/AWSClient.h"
12+
13+
extern "C" {
14+
#include "core_json.h"
15+
}
16+
17+
#define TRACE_GROUP "Main"
18+
19+
static bool reply_received = false;
20+
21+
// Callback when a MQTT message has been added to the topic
22+
void on_message_callback(
23+
const char *topic,
24+
uint16_t topic_length,
25+
const void *payload,
26+
size_t payload_length)
27+
{
28+
char *json_value;
29+
size_t value_length;
30+
auto ret = JSON_Search((char *)payload, payload_length, "sender", strlen("sender"), &json_value, &value_length);
31+
if (ret == JSONSuccess && (strncmp(json_value, "device", strlen("device")) == 0)) {
32+
tr_info("Message sent successfully");
33+
} else {
34+
ret = JSON_Search((char *)payload, payload_length, "message", strlen("message"), &json_value, &value_length);
35+
if (ret == JSONSuccess) {
36+
reply_received = true;
37+
tr_info("Message received from the cloud: \"%.*s\"", value_length, json_value);
38+
} else {
39+
tr_error("Failed to extract message from the payload: \"%.*s\"", payload_length, (const char *) payload);
40+
}
41+
}
42+
}
43+
44+
void demo()
45+
{
46+
AWSClient &client = AWSClient::getInstance();
47+
48+
// Subscribe to the topic
49+
const char topic[] = MBED_CONF_APP_AWS_MQTT_TOPIC;
50+
int ret = client.subscribe(topic, strlen(topic));
51+
if (ret != MBED_SUCCESS) {
52+
tr_error("AWSClient::subscribe() failed");
53+
return;
54+
}
55+
56+
// Send ten message to the cloud (one per second)
57+
// Stop when we receive a cloud-to-device message
58+
char payload[128];
59+
for (int i = 0; i < 10; i++) {
60+
if (reply_received) {
61+
// If we have received a message from the cloud, don't send more messeges
62+
break;
63+
}
64+
65+
// The MQTT protocol does not distinguish between senders,
66+
// so we add a "sender" attribute to the payload
67+
const char base_message[] = "messages left to send, or until we receive a reply";
68+
sprintf(payload, "{\n"
69+
" \"sender\": \"device\",\n"
70+
" \"message\": \"%d %s\"\n"
71+
"}",
72+
10 - i, base_message);
73+
tr_info("Publishing \"%d %s\" to topic \"%s\"", 10 - i, base_message, topic);
74+
ret = client.publish(
75+
topic,
76+
strlen(topic),
77+
payload,
78+
strlen(payload)
79+
);
80+
if (ret != MBED_SUCCESS) {
81+
tr_error("AWSClient::publish() failed");
82+
goto unsubscribe;
83+
}
84+
85+
rtos::ThisThread::sleep_for(1s);
86+
}
87+
88+
// If the user didn't manage to send a cloud-to-device message earlier,
89+
// let's wait until we receive one
90+
while (!reply_received) {
91+
// Continue to receive messages in the communication thread
92+
// which is internally created and maintained by the Azure SDK.
93+
sleep();
94+
}
95+
96+
unsubscribe:
97+
// Unsubscribe from the topic
98+
ret = client.unsubscribe(topic, strlen(topic));
99+
if (ret != MBED_SUCCESS) {
100+
tr_error("AWSClient::unsubscribe() failed");
101+
}
102+
}
103+
104+
#endif // !MBED_CONF_AWS_CLIENT_SHADOW

0 commit comments

Comments
 (0)