test

Dependencies:   MQTT

Committer:
kernel2418
Date:
Wed Mar 14 03:03:16 2018 +0000
Revision:
0:9d5f28595388
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kernel2418 0:9d5f28595388 1 # Example for Connection with AWS IoT thru MQTT/HTTPS on Mbed OS
kernel2418 0:9d5f28595388 2
kernel2418 0:9d5f28595388 3 This is an example to demonstrate connection with [AWS IoT](https://aws.amazon.com/iot)
kernel2418 0:9d5f28595388 4 on Nuvoton Mbed-enabled boards.
kernel2418 0:9d5f28595388 5
kernel2418 0:9d5f28595388 6 ## Supported platforms
kernel2418 0:9d5f28595388 7 On Mbed OS, connection with AWS IoT requires Mbed TLS. It requires more than 64 KB RAM.
kernel2418 0:9d5f28595388 8 Currently, the following Nuvoton Mbed-enalbed boards can afford such memory footprint:
kernel2418 0:9d5f28595388 9 - [NuMaker-PFM-NUC472](https://developer.mbed.org/platforms/Nuvoton-NUC472/)
kernel2418 0:9d5f28595388 10 - [NuMaker-PFM-M487](https://developer.mbed.org/platforms/NUMAKER-PFM-M487/)
kernel2418 0:9d5f28595388 11
kernel2418 0:9d5f28595388 12 ## Access and manage AWS IoT Service
kernel2418 0:9d5f28595388 13 To run the example, you need to register one [AWS account](https://aws.amazon.com/)
kernel2418 0:9d5f28595388 14 to access and manage AWS IoT Service for your device to connect with.
kernel2418 0:9d5f28595388 15 This [link](https://docs.aws.amazon.com/iot/latest/developerguide/what-is-aws-iot.html) gives detailed
kernel2418 0:9d5f28595388 16 information about it.
kernel2418 0:9d5f28595388 17
kernel2418 0:9d5f28595388 18 1. Sign in to [AWS Management Console](https://aws.amazon.com/console/).
kernel2418 0:9d5f28595388 19 1. Enter AWS IoT Service.
kernel2418 0:9d5f28595388 20 1. In AWS IoT Service, create a thing.
kernel2418 0:9d5f28595388 21 The Console may prompt you to also create a certificate and a policy. Skip for creating them later.
kernel2418 0:9d5f28595388 22 1. In AWS IoT Service, create a policy. A workable example would be below.
kernel2418 0:9d5f28595388 23 Note that you need to replace **REGION** and **ACCOUNT** to match your case.
kernel2418 0:9d5f28595388 24
kernel2418 0:9d5f28595388 25 <pre>
kernel2418 0:9d5f28595388 26 {
kernel2418 0:9d5f28595388 27 "Version": "2012-10-17",
kernel2418 0:9d5f28595388 28 "Statement": [
kernel2418 0:9d5f28595388 29 {
kernel2418 0:9d5f28595388 30 "Effect": "Allow",
kernel2418 0:9d5f28595388 31 "Action": "iot:Connect",
kernel2418 0:9d5f28595388 32 "Resource": "arn:aws:iot:<b>REGION</b>:<b>ACCOUNT</b>:client/*"
kernel2418 0:9d5f28595388 33 },
kernel2418 0:9d5f28595388 34 {
kernel2418 0:9d5f28595388 35 "Effect": "Allow",
kernel2418 0:9d5f28595388 36 "Action": "iot:Subscribe",
kernel2418 0:9d5f28595388 37 "Resource": ["arn:aws:iot:<b>REGION</b>:<b>ACCOUNT</b>:topicfilter/*"]
kernel2418 0:9d5f28595388 38 },
kernel2418 0:9d5f28595388 39 {
kernel2418 0:9d5f28595388 40 "Effect": "Allow",
kernel2418 0:9d5f28595388 41 "Action": ["iot:Publish", "iot:Receive"],
kernel2418 0:9d5f28595388 42 "Resource": "arn:aws:iot:<b>REGION</b>:<b>ACCOUNT</b>:topic/*"
kernel2418 0:9d5f28595388 43 },
kernel2418 0:9d5f28595388 44 {
kernel2418 0:9d5f28595388 45 "Effect": "Allow",
kernel2418 0:9d5f28595388 46 "Action": ["iot:UpdateThingShadow", "iot:GetThingShadow", "iot:DeleteThingShadow"],
kernel2418 0:9d5f28595388 47 "Resource": "arn:aws:iot:<b>REGION</b>:<b>ACCOUNT</b>:thing/*"
kernel2418 0:9d5f28595388 48 }
kernel2418 0:9d5f28595388 49 ]
kernel2418 0:9d5f28595388 50 }
kernel2418 0:9d5f28595388 51 </pre>
kernel2418 0:9d5f28595388 52
kernel2418 0:9d5f28595388 53 1. In AWS IoT Service, create a certificate. You would get 4 security credential files from it.
kernel2418 0:9d5f28595388 54 Download them for later use.
kernel2418 0:9d5f28595388 55 - AWS IoT's CA certificate
kernel2418 0:9d5f28595388 56 - User certificate
kernel2418 0:9d5f28595388 57 - User private key
kernel2418 0:9d5f28595388 58 - User public key
kernel2418 0:9d5f28595388 59
kernel2418 0:9d5f28595388 60 After creating the certificate, do:
kernel2418 0:9d5f28595388 61 1. Activate the certificate
kernel2418 0:9d5f28595388 62 1. Attach the thing created above to the certificate
kernel2418 0:9d5f28595388 63 1. Attach the policy created above to the certificate
kernel2418 0:9d5f28595388 64
kernel2418 0:9d5f28595388 65 ## Configure your device with AWS IoT
kernel2418 0:9d5f28595388 66 Before connecting your device with AWS IoT, you need to configure security credential and
kernel2418 0:9d5f28595388 67 protocol dependent parameters into your device. These configurations are all centralized in `main.cpp`.
kernel2418 0:9d5f28595388 68
kernel2418 0:9d5f28595388 69 ### Configure certificate into your device
kernel2418 0:9d5f28595388 70 From above, you've got 4 security credential files: CA certificate and user certificate/private key/public key.
kernel2418 0:9d5f28595388 71 Configure CA certificate, user certificate, and user private key into your device.
kernel2418 0:9d5f28595388 72 User public key has been included in user certificate and is not used here.
kernel2418 0:9d5f28595388 73 1. Replace CA certificate with downloaded from the Console.
kernel2418 0:9d5f28595388 74 ```
kernel2418 0:9d5f28595388 75 const char SSL_CA_CERT_PEM[] = "-----BEGIN CERTIFICATE-----\n"
kernel2418 0:9d5f28595388 76 "MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\n"
kernel2418 0:9d5f28595388 77 "yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\n"
kernel2418 0:9d5f28595388 78 ```
kernel2418 0:9d5f28595388 79
kernel2418 0:9d5f28595388 80 1. Replace user certificate with downloaded from the Console.
kernel2418 0:9d5f28595388 81 ```
kernel2418 0:9d5f28595388 82 const char SSL_USER_CERT_PEM[] = "-----BEGIN CERTIFICATE-----\n"
kernel2418 0:9d5f28595388 83 "MIIDWjCCAkKgAwIBAgIVALN/H7tr8cgpl2zwg0JjEE106XilMA0GCSqGSIb3DQEB\n"
kernel2418 0:9d5f28595388 84 "CwUAME0xSzBJBgNVBAsMQkFtYXpvbiBXZWIgU2VydmljZXMgTz1BbWF6b24uY29t\n"
kernel2418 0:9d5f28595388 85 ```
kernel2418 0:9d5f28595388 86
kernel2418 0:9d5f28595388 87 1. Replace user private key with downloaded from the Console.
kernel2418 0:9d5f28595388 88 ```
kernel2418 0:9d5f28595388 89 const char SSL_USER_PRIV_KEY_PEM[] = "-----BEGIN RSA PRIVATE KEY-----\n"
kernel2418 0:9d5f28595388 90 ```
kernel2418 0:9d5f28595388 91
kernel2418 0:9d5f28595388 92 **NOTE:** The credential hard-coded in source code is deactivated or deleted.
kernel2418 0:9d5f28595388 93 Use your own credential for connection with AWS IoT.
kernel2418 0:9d5f28595388 94
kernel2418 0:9d5f28595388 95 ### Connect through MQTT
kernel2418 0:9d5f28595388 96 To connect your device with AWS IoT through MQTT, you need to configure the following parameters.
kernel2418 0:9d5f28595388 97
kernel2418 0:9d5f28595388 98 1. Enable connection through MQTT.
kernel2418 0:9d5f28595388 99 ```
kernel2418 0:9d5f28595388 100 #define AWS_IOT_MQTT_TEST 1
kernel2418 0:9d5f28595388 101 ```
kernel2418 0:9d5f28595388 102
kernel2418 0:9d5f28595388 103 1. Replace server name (endpoint). **Endpoint** has the following format and you just
kernel2418 0:9d5f28595388 104 need to modify **IDENTIFIER** and **REGION** to match your case.
kernel2418 0:9d5f28595388 105 <pre>
kernel2418 0:9d5f28595388 106 #define AWS_IOT_MQTT_SERVER_NAME "<b>IDENTIFIER</b>.iot.<b>REGION</b>.amazonaws.com"
kernel2418 0:9d5f28595388 107 </pre>
kernel2418 0:9d5f28595388 108
kernel2418 0:9d5f28595388 109 1. Server port number is fixed. Don't change it.
kernel2418 0:9d5f28595388 110 ```
kernel2418 0:9d5f28595388 111 #define AWS_IOT_MQTT_SERVER_PORT 8883
kernel2418 0:9d5f28595388 112 ```
kernel2418 0:9d5f28595388 113
kernel2418 0:9d5f28595388 114 1. Replace **THINGNAME** to match your case. The **THINGNAME** is just the name of the thing you've created above.
kernel2418 0:9d5f28595388 115 <pre>
kernel2418 0:9d5f28595388 116 #define AWS_IOT_MQTT_THINGNAME "<b>THINGNAME</b>"
kernel2418 0:9d5f28595388 117 </pre>
kernel2418 0:9d5f28595388 118
kernel2418 0:9d5f28595388 119 1. Replace **CLIENTNAME** to match your case. If you adopt the example policy above,
kernel2418 0:9d5f28595388 120 you can modify it arbitrarily because the policy permits any client name bound to your account.
kernel2418 0:9d5f28595388 121 <pre>
kernel2418 0:9d5f28595388 122 #define AWS_IOT_MQTT_CLIENTNAME "<b>CLIENTNAME</b>"
kernel2418 0:9d5f28595388 123 </pre>
kernel2418 0:9d5f28595388 124
kernel2418 0:9d5f28595388 125 AWS IoT MQTT protocol supports topic subscribe/publish. The example demonstrates:
kernel2418 0:9d5f28595388 126 - Subscribe/publish with user topic
kernel2418 0:9d5f28595388 127 - Subscribe/publish with reserved topic (starting with $) to:
kernel2418 0:9d5f28595388 128 - Update thing shadow
kernel2418 0:9d5f28595388 129 - Get thing shadow
kernel2418 0:9d5f28595388 130 - Delete thing shadow
kernel2418 0:9d5f28595388 131
kernel2418 0:9d5f28595388 132 ### Connect through HTTPS
kernel2418 0:9d5f28595388 133 To connect your device with AWS IoT through HTTPS, you need to configure the following parameters.
kernel2418 0:9d5f28595388 134
kernel2418 0:9d5f28595388 135 1. Enable connection through HTTPS.
kernel2418 0:9d5f28595388 136 ```
kernel2418 0:9d5f28595388 137 #define AWS_IOT_HTTPS_TEST 1
kernel2418 0:9d5f28595388 138 ```
kernel2418 0:9d5f28595388 139
kernel2418 0:9d5f28595388 140 1. Replace server name (endpoint). **Endpoint** has the following format and you just
kernel2418 0:9d5f28595388 141 need to modify **IDENTIFIER** and **REGION** to match your case.
kernel2418 0:9d5f28595388 142 <pre>
kernel2418 0:9d5f28595388 143 #define AWS_IOT_HTTPS_SERVER_NAME "<b>IDENTIFIER</b>.iot.<b>REGION</b>.amazonaws.com"
kernel2418 0:9d5f28595388 144 </pre>
kernel2418 0:9d5f28595388 145
kernel2418 0:9d5f28595388 146 1. Server port number is fixed. Don't change it.
kernel2418 0:9d5f28595388 147 ```
kernel2418 0:9d5f28595388 148 #define AWS_IOT_HTTPS_SERVER_PORT 8443
kernel2418 0:9d5f28595388 149 ```
kernel2418 0:9d5f28595388 150
kernel2418 0:9d5f28595388 151 1. Replace **THINGNAME** to match your case. The **THINGNAME** is just the name of the thing you've created above.
kernel2418 0:9d5f28595388 152 <pre>
kernel2418 0:9d5f28595388 153 #define AWS_IOT_HTTPS_THINGNAME "<b>THINGNAME</b>"
kernel2418 0:9d5f28595388 154 </pre>
kernel2418 0:9d5f28595388 155
kernel2418 0:9d5f28595388 156 AWS IoT HTTPS protocol supports topic publish-only and RESTful API. The example demonstrates:
kernel2418 0:9d5f28595388 157 - Publish to user topic
kernel2418 0:9d5f28595388 158 - Publish to reserved topic (starting with $) to:
kernel2418 0:9d5f28595388 159 - Update thing shadow
kernel2418 0:9d5f28595388 160 - Get thing shadow
kernel2418 0:9d5f28595388 161 - Delete thing shadow
kernel2418 0:9d5f28595388 162 - RESTful API to:
kernel2418 0:9d5f28595388 163 - Update thing shadow RESTfully through HTTPS/POST method
kernel2418 0:9d5f28595388 164 - Get thing shadow RESTfully through HTTPS/GET method
kernel2418 0:9d5f28595388 165 - Delete thing shadow RESTfully through HTTPS/DELETE method
kernel2418 0:9d5f28595388 166
kernel2418 0:9d5f28595388 167 ## Patch MQTT library
kernel2418 0:9d5f28595388 168 Currently, MQTT library has one issue with unsubscribe from topic multiple times. Fix it:
kernel2418 0:9d5f28595388 169
kernel2418 0:9d5f28595388 170 In `MQTT/MQTTClient.h` > `MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle`,
kernel2418 0:9d5f28595388 171 <pre>
kernel2418 0:9d5f28595388 172 switch (packet_type)
kernel2418 0:9d5f28595388 173 {
kernel2418 0:9d5f28595388 174 default:
kernel2418 0:9d5f28595388 175 // no more data to read, unrecoverable. Or read packet fails due to unexpected network error
kernel2418 0:9d5f28595388 176 rc = packet_type;
kernel2418 0:9d5f28595388 177 goto exit;
kernel2418 0:9d5f28595388 178 case 0: // timed out reading packet
kernel2418 0:9d5f28595388 179 break;
kernel2418 0:9d5f28595388 180 case CONNACK:
kernel2418 0:9d5f28595388 181 case PUBACK:
kernel2418 0:9d5f28595388 182 case SUBACK:
kernel2418 0:9d5f28595388 183 <b>
kernel2418 0:9d5f28595388 184 case UNSUBACK:
kernel2418 0:9d5f28595388 185 </b>
kernel2418 0:9d5f28595388 186 break;
kernel2418 0:9d5f28595388 187 case PUBLISH:
kernel2418 0:9d5f28595388 188 {
kernel2418 0:9d5f28595388 189 </pre>
kernel2418 0:9d5f28595388 190
kernel2418 0:9d5f28595388 191 ## Monitor the application
kernel2418 0:9d5f28595388 192 If you configure your terminal program with **9600/8-N-1**, you would see output similar to:
kernel2418 0:9d5f28595388 193
kernel2418 0:9d5f28595388 194 **NOTE:** Make sure that the network is functional before running the application.
kernel2418 0:9d5f28595388 195
kernel2418 0:9d5f28595388 196 <pre>
kernel2418 0:9d5f28595388 197 Starting AWS IoT test
kernel2418 0:9d5f28595388 198 Using Mbed OS 5.7.1
kernel2418 0:9d5f28595388 199 [EasyConnect] IPv4 mode
kernel2418 0:9d5f28595388 200 Connecting with a1fbcwaqfqeozo.iot.us-east-1.amazonaws.com:8883
kernel2418 0:9d5f28595388 201 Connecting to a1fbcwaqfqeozo.iot.us-east-1.amazonaws.com:8883
kernel2418 0:9d5f28595388 202 </pre>
kernel2418 0:9d5f28595388 203
kernel2418 0:9d5f28595388 204 If you get here successfully, it means configurations with security credential are correct.
kernel2418 0:9d5f28595388 205 <pre>
kernel2418 0:9d5f28595388 206 Starting the TLS handshake...
kernel2418 0:9d5f28595388 207 TLS connection to a1fbcwaqfqeozo.iot.us-east-1.amazonaws.com:8883 established
kernel2418 0:9d5f28595388 208 Server certificate:
kernel2418 0:9d5f28595388 209 cert. version : 3
kernel2418 0:9d5f28595388 210 serial number : 3C:AC:B3:D3:3E:D8:6A:C9:2B:EF:D2:C5:B1:DC:BF:66
kernel2418 0:9d5f28595388 211 issuer name : C=US, O=Symantec Corporation, OU=Symantec Trust Network, CN=Symantec Class 3 ECC 256 bit SSL CA - G2
kernel2418 0:9d5f28595388 212 subject name : C=US, ST=Washington, L=Seattle, O=Amazon.com, Inc., CN=*.iot.us-east-1.amazonaws.com
kernel2418 0:9d5f28595388 213 issued on : 2017-03-07 00:00:00
kernel2418 0:9d5f28595388 214 expires on : 2018-03-08 23:59:59
kernel2418 0:9d5f28595388 215 signed using : ECDSA with SHA256
kernel2418 0:9d5f28595388 216 EC key size : 256 bits
kernel2418 0:9d5f28595388 217 basic constraints : CA=false
kernel2418 0:9d5f28595388 218 subject alt name : iot.us-east-1.amazonaws.com, *.iot.us-east-1.amazonaws.com
kernel2418 0:9d5f28595388 219 key usage : Digital Signature
kernel2418 0:9d5f28595388 220 ext key usage : TLS Web Server Authentication, TLS Web Client Authentication
kernel2418 0:9d5f28595388 221 Certificate verification passed
kernel2418 0:9d5f28595388 222
kernel2418 0:9d5f28595388 223 Connects with a1fbcwaqfqeozo.iot.us-east-1.amazonaws.com:8883 OK
kernel2418 0:9d5f28595388 224 </pre>
kernel2418 0:9d5f28595388 225
kernel2418 0:9d5f28595388 226 MQTT handshake goes:
kernel2418 0:9d5f28595388 227 <pre>
kernel2418 0:9d5f28595388 228 MQTT connects OK
kernel2418 0:9d5f28595388 229
kernel2418 0:9d5f28595388 230 Subscribing/publishing user topic
kernel2418 0:9d5f28595388 231 MQTT subscribes to Nuvoton/Mbed/+ OK
kernel2418 0:9d5f28595388 232 Message to publish:
kernel2418 0:9d5f28595388 233 { "message": "Hello from Nuvoton Mbed device" }
kernel2418 0:9d5f28595388 234 MQTT publishes message to Nuvoton/Mbed/D001 OK
kernel2418 0:9d5f28595388 235 Message arrived: qos 1, retained 0, dup 0, packetid 1
kernel2418 0:9d5f28595388 236 Payload:
kernel2418 0:9d5f28595388 237 { "message": "Hello from Nuvoton Mbed device" }
kernel2418 0:9d5f28595388 238
kernel2418 0:9d5f28595388 239 MQTT unsubscribes from Nuvoton/Mbed/+ OK
kernel2418 0:9d5f28595388 240 Subscribes/publishes user topic OK
kernel2418 0:9d5f28595388 241
kernel2418 0:9d5f28595388 242 Subscribing/publishing UpdateThingShadow topic
kernel2418 0:9d5f28595388 243 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/update/accepted OK
kernel2418 0:9d5f28595388 244 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/update/rejected OK
kernel2418 0:9d5f28595388 245 Message to publish:
kernel2418 0:9d5f28595388 246 { "state": { "reported": { "attribute1": 3, "attribute2": "1" } } }
kernel2418 0:9d5f28595388 247 MQTT publishes message to $aws/things/Nuvoton-Mbed-D001/shadow/update OK
kernel2418 0:9d5f28595388 248 Message arrived: qos 1, retained 0, dup 0, packetid 1
kernel2418 0:9d5f28595388 249 Payload:
kernel2418 0:9d5f28595388 250 {"state":{"reported":{"attribute1":3,"attribute2":"1"}},"metadata":{"reported":{"attribute1":{"timestamp":1514962195},"attribute2":{"timestamp":1514962195}}},"version":77,"timestamp":1514962195}
kernel2418 0:9d5f28595388 251
kernel2418 0:9d5f28595388 252 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/update/accepted OK
kernel2418 0:9d5f28595388 253 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/update/rejected OK
kernel2418 0:9d5f28595388 254 Subscribes/publishes UpdateThingShadow topic OK
kernel2418 0:9d5f28595388 255
kernel2418 0:9d5f28595388 256 Subscribing/publishing GetThingShadow topic
kernel2418 0:9d5f28595388 257 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/get/accepted OK
kernel2418 0:9d5f28595388 258 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/get/rejected OK
kernel2418 0:9d5f28595388 259 Message to publish:
kernel2418 0:9d5f28595388 260
kernel2418 0:9d5f28595388 261 MQTT publishes message to $aws/things/Nuvoton-Mbed-D001/shadow/get OK
kernel2418 0:9d5f28595388 262 Message arrived: qos 1, retained 0, dup 0, packetid 1
kernel2418 0:9d5f28595388 263 Payload:
kernel2418 0:9d5f28595388 264 {"state":{"reported":{"attribute1":3,"attribute2":"1"}},"metadata":{"reported":{"attribute1":{"timestamp":1514962195},"attribute2":{"timestamp":1514962195}}},"version":77,"timestamp":1514962198}
kernel2418 0:9d5f28595388 265
kernel2418 0:9d5f28595388 266 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/get/accepted OK
kernel2418 0:9d5f28595388 267 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/get/rejected OK
kernel2418 0:9d5f28595388 268 Subscribes/publishes GetThingShadow topic OK
kernel2418 0:9d5f28595388 269
kernel2418 0:9d5f28595388 270 Subscribing/publishing DeleteThingShadow topic
kernel2418 0:9d5f28595388 271 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/delete/accepted OK
kernel2418 0:9d5f28595388 272 MQTT subscribes to $aws/things/Nuvoton-Mbed-D001/shadow/delete/rejected OK
kernel2418 0:9d5f28595388 273 Message to publish:
kernel2418 0:9d5f28595388 274
kernel2418 0:9d5f28595388 275 MQTT publishes message to $aws/things/Nuvoton-Mbed-D001/shadow/delete OK
kernel2418 0:9d5f28595388 276 Message arrived: qos 1, retained 0, dup 0, packetid 1
kernel2418 0:9d5f28595388 277 Payload:
kernel2418 0:9d5f28595388 278 {"version":77,"timestamp":1514962202}
kernel2418 0:9d5f28595388 279
kernel2418 0:9d5f28595388 280 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/delete/accepted OK
kernel2418 0:9d5f28595388 281 MQTT unsubscribes from $aws/things/Nuvoton-Mbed-D001/shadow/delete/rejected OK
kernel2418 0:9d5f28595388 282 Subscribes/publishes DeleteThingShadow topic OK
kernel2418 0:9d5f28595388 283
kernel2418 0:9d5f28595388 284 MQTT disconnects OK
kernel2418 0:9d5f28595388 285 </pre>
kernel2418 0:9d5f28595388 286
kernel2418 0:9d5f28595388 287 Dynamic memory footprint (heap) is output below.
kernel2418 0:9d5f28595388 288 Static memory footprint (global/stack) could be obtained by inspecting MAP file.
kernel2418 0:9d5f28595388 289 You could get total memory footprint by adding these two together.
kernel2418 0:9d5f28595388 290 <pre>
kernel2418 0:9d5f28595388 291 Current heap size: 1351
kernel2418 0:9d5f28595388 292 Max heap size: 63022
kernel2418 0:9d5f28595388 293 </pre>
kernel2418 0:9d5f28595388 294
kernel2418 0:9d5f28595388 295 ## Trouble-shooting
kernel2418 0:9d5f28595388 296 - Over ESP8266 WiFi,
kernel2418 0:9d5f28595388 297 if you make a loop test like below (`main.cpp`), you may always meet errors in the following loops
kernel2418 0:9d5f28595388 298 after some network error has happened in the previous one.
kernel2418 0:9d5f28595388 299 <pre>
kernel2418 0:9d5f28595388 300 <b>while (true) {</b>
kernel2418 0:9d5f28595388 301 #if AWS_IOT_MQTT_TEST
kernel2418 0:9d5f28595388 302 AWS_IoT_MQTT_Test *mqtt_test = new AWS_IoT_MQTT_Test(AWS_IOT_MQTT_SERVER_NAME, AWS_IOT_MQTT_SERVER_PORT, network);
kernel2418 0:9d5f28595388 303 mqtt_test->start_test();
kernel2418 0:9d5f28595388 304 delete mqtt_test;
kernel2418 0:9d5f28595388 305 #endif // End of AWS_IOT_MQTT_TEST
kernel2418 0:9d5f28595388 306
kernel2418 0:9d5f28595388 307 #if AWS_IOT_HTTPS_TEST
kernel2418 0:9d5f28595388 308 AWS_IoT_HTTPS_Test *https_test = new AWS_IoT_HTTPS_Test(AWS_IOT_HTTPS_SERVER_NAME, AWS_IOT_HTTPS_SERVER_PORT, network);
kernel2418 0:9d5f28595388 309 https_test->start_test();
kernel2418 0:9d5f28595388 310 delete https_test;
kernel2418 0:9d5f28595388 311 #endif // End of AWS_IOT_HTTPS_TEST
kernel2418 0:9d5f28595388 312 <b>}</b>
kernel2418 0:9d5f28595388 313 </pre>
kernel2418 0:9d5f28595388 314 This issue would be caused by failure of ESP8266 AT commands **CLOSE**/**DISCONNECT**
kernel2418 0:9d5f28595388 315 because ESP8266 F/W is still busy in handling previous unfinished network transfer
kernel2418 0:9d5f28595388 316 due to bad network status and fails these commands.
kernel2418 0:9d5f28595388 317 These commands must be OK for ESP8266 F/W to reset connection state correctly.
kernel2418 0:9d5f28595388 318 If that happens, try enlarging [ESP8266 driver's](https://github.com/ARMmbed/esp8266-driver) timeout configuration.
kernel2418 0:9d5f28595388 319 For example, enlarge `ESP8266_SEND_TIMEOUT`/`ESP8266_RECV_TIMEOUT`/`ESP8266_MISC_TIMEOUT` (defined in
kernel2418 0:9d5f28595388 320 [ESP8266Interface.cpp](https://github.com/ARMmbed/esp8266-driver/blob/master/ESP8266Interface.cpp))
kernel2418 0:9d5f28595388 321 to 5000/5000/5000 ms respectively (through `mbed_app.json`).
kernel2418 0:9d5f28595388 322 <pre>
kernel2418 0:9d5f28595388 323 {
kernel2418 0:9d5f28595388 324 "macros": [
kernel2418 0:9d5f28595388 325 "MBED_CONF_APP_MAIN_STACK_SIZE=4096",
kernel2418 0:9d5f28595388 326 "MBEDTLS_USER_CONFIG_FILE=\"mbedtls_user_config.h\"",
kernel2418 0:9d5f28595388 327 "MBED_HEAP_STATS_ENABLED=1",
kernel2418 0:9d5f28595388 328 "MBED_MEM_TRACING_ENABLED=1",
kernel2418 0:9d5f28595388 329 <b>"ESP8266_SEND_TIMEOUT=5000",</b>
kernel2418 0:9d5f28595388 330 <b>"ESP8266_RECV_TIMEOUT=5000",</b>
kernel2418 0:9d5f28595388 331 <b>"ESP8266_MISC_TIMEOUT=5000"</b>
kernel2418 0:9d5f28595388 332 ],
kernel2418 0:9d5f28595388 333 "config": {
kernel2418 0:9d5f28595388 334 </pre>