Jeon byungchul
/
aws-iot-example
this is fork and i will modify for STM32
Fork of AWS-test by
main.cpp@1:3f75eb8d46f4, 2017-04-12 (annotated)
- Committer:
- peyo
- Date:
- Wed Apr 12 14:09:46 2017 +0200
- Revision:
- 1:3f75eb8d46f4
- Child:
- 2:db59f45ad3d3
add main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peyo |
1:3f75eb8d46f4 | 1 | /* |
peyo |
1:3f75eb8d46f4 | 2 | * Copyright (c) 2017, CATIE, All Rights Reserved |
peyo |
1:3f75eb8d46f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
peyo |
1:3f75eb8d46f4 | 4 | * |
peyo |
1:3f75eb8d46f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
peyo |
1:3f75eb8d46f4 | 6 | * you may not use this file except in compliance with the License. |
peyo |
1:3f75eb8d46f4 | 7 | * You may obtain a copy of the License at |
peyo |
1:3f75eb8d46f4 | 8 | * |
peyo |
1:3f75eb8d46f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
peyo |
1:3f75eb8d46f4 | 10 | * |
peyo |
1:3f75eb8d46f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
peyo |
1:3f75eb8d46f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
peyo |
1:3f75eb8d46f4 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
peyo |
1:3f75eb8d46f4 | 14 | * See the License for the specific language governing permissions and |
peyo |
1:3f75eb8d46f4 | 15 | * limitations under the License. |
peyo |
1:3f75eb8d46f4 | 16 | */ |
peyo |
1:3f75eb8d46f4 | 17 | #include "mbed.h" |
peyo |
1:3f75eb8d46f4 | 18 | #include "EthernetInterface.h" |
peyo |
1:3f75eb8d46f4 | 19 | |
peyo |
1:3f75eb8d46f4 | 20 | #include "aws_iot_config.h" |
peyo |
1:3f75eb8d46f4 | 21 | #include "aws_iot_mqtt_client_interface.h" |
peyo |
1:3f75eb8d46f4 | 22 | |
peyo |
1:3f75eb8d46f4 | 23 | namespace { |
peyo |
1:3f75eb8d46f4 | 24 | #define READ_DURATION 500 |
peyo |
1:3f75eb8d46f4 | 25 | #define DEMO_PAYLOAD "Hello, world!" |
peyo |
1:3f75eb8d46f4 | 26 | #define PUBLISH_TOPIC "sdkTest/sub" |
peyo |
1:3f75eb8d46f4 | 27 | #define PUBLISH_TOPIC_LEN 11 |
peyo |
1:3f75eb8d46f4 | 28 | } |
peyo |
1:3f75eb8d46f4 | 29 | |
peyo |
1:3f75eb8d46f4 | 30 | void subscribe_callback(AWS_IoT_Client *client, char *topic_name, |
peyo |
1:3f75eb8d46f4 | 31 | uint16_t topic_name_length, IoT_Publish_Message_Params *params, |
peyo |
1:3f75eb8d46f4 | 32 | void *data) |
peyo |
1:3f75eb8d46f4 | 33 | { |
peyo |
1:3f75eb8d46f4 | 34 | printf("Subscribe callback"); |
peyo |
1:3f75eb8d46f4 | 35 | printf("%.*s\t%.*s", topic_name_length, topic_name, (int)params->payloadLen, (char *)params->payload); |
peyo |
1:3f75eb8d46f4 | 36 | } |
peyo |
1:3f75eb8d46f4 | 37 | |
peyo |
1:3f75eb8d46f4 | 38 | void disconnect_handler(AWS_IoT_Client *client, void *data) |
peyo |
1:3f75eb8d46f4 | 39 | { |
peyo |
1:3f75eb8d46f4 | 40 | printf("MQTT Disconnect"); |
peyo |
1:3f75eb8d46f4 | 41 | IoT_Error_t rc = FAILURE; |
peyo |
1:3f75eb8d46f4 | 42 | |
peyo |
1:3f75eb8d46f4 | 43 | if (!client) { |
peyo |
1:3f75eb8d46f4 | 44 | return; |
peyo |
1:3f75eb8d46f4 | 45 | } |
peyo |
1:3f75eb8d46f4 | 46 | |
peyo |
1:3f75eb8d46f4 | 47 | if (aws_iot_is_autoreconnect_enabled(client)) { |
peyo |
1:3f75eb8d46f4 | 48 | printf("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
peyo |
1:3f75eb8d46f4 | 49 | } else { |
peyo |
1:3f75eb8d46f4 | 50 | printf("Auto Reconnect not enabled. Starting manual reconnect..."); |
peyo |
1:3f75eb8d46f4 | 51 | rc = aws_iot_mqtt_attempt_reconnect(client); |
peyo |
1:3f75eb8d46f4 | 52 | if (NETWORK_RECONNECTED == rc) { |
peyo |
1:3f75eb8d46f4 | 53 | printf("Manual Reconnect Successful"); |
peyo |
1:3f75eb8d46f4 | 54 | } else { |
peyo |
1:3f75eb8d46f4 | 55 | printf("Manual Reconnect Failed - %d", rc); |
peyo |
1:3f75eb8d46f4 | 56 | } |
peyo |
1:3f75eb8d46f4 | 57 | } |
peyo |
1:3f75eb8d46f4 | 58 | } |
peyo |
1:3f75eb8d46f4 | 59 | |
peyo |
1:3f75eb8d46f4 | 60 | EthernetInterface net; |
peyo |
1:3f75eb8d46f4 | 61 | |
peyo |
1:3f75eb8d46f4 | 62 | int main() |
peyo |
1:3f75eb8d46f4 | 63 | { |
peyo |
1:3f75eb8d46f4 | 64 | net.connect(); |
peyo |
1:3f75eb8d46f4 | 65 | AWS_IoT_Client client; |
peyo |
1:3f75eb8d46f4 | 66 | |
peyo |
1:3f75eb8d46f4 | 67 | /* |
peyo |
1:3f75eb8d46f4 | 68 | * MQTT Init |
peyo |
1:3f75eb8d46f4 | 69 | */ |
peyo |
1:3f75eb8d46f4 | 70 | |
peyo |
1:3f75eb8d46f4 | 71 | IoT_Client_Init_Params mqtt_init_params = iotClientInitParamsDefault; |
peyo |
1:3f75eb8d46f4 | 72 | mqtt_init_params.enableAutoReconnect = false; // We enable this later below |
peyo |
1:3f75eb8d46f4 | 73 | char mqtt_host[] = AWS_IOT_MQTT_HOST; |
peyo |
1:3f75eb8d46f4 | 74 | mqtt_init_params.pHostURL = mqtt_host; |
peyo |
1:3f75eb8d46f4 | 75 | mqtt_init_params.port = AWS_IOT_MQTT_PORT; |
peyo |
1:3f75eb8d46f4 | 76 | mqtt_init_params.disconnectHandler = disconnect_handler; |
peyo |
1:3f75eb8d46f4 | 77 | mqtt_init_params.disconnectHandlerData = NULL; |
peyo |
1:3f75eb8d46f4 | 78 | |
peyo |
1:3f75eb8d46f4 | 79 | IoT_Error_t mqtt_init_status = aws_iot_mqtt_init(&client, &mqtt_init_params); |
peyo |
1:3f75eb8d46f4 | 80 | if (mqtt_init_status != SUCCESS) { |
peyo |
1:3f75eb8d46f4 | 81 | printf("aws_iot_mqtt_init returned error : %d ", mqtt_init_status); |
peyo |
1:3f75eb8d46f4 | 82 | while (true) { Thread::wait(1000); } |
peyo |
1:3f75eb8d46f4 | 83 | } |
peyo |
1:3f75eb8d46f4 | 84 | |
peyo |
1:3f75eb8d46f4 | 85 | /* |
peyo |
1:3f75eb8d46f4 | 86 | * MQTT Connect |
peyo |
1:3f75eb8d46f4 | 87 | */ |
peyo |
1:3f75eb8d46f4 | 88 | |
peyo |
1:3f75eb8d46f4 | 89 | IoT_Client_Connect_Params connect_params = iotClientConnectParamsDefault; |
peyo |
1:3f75eb8d46f4 | 90 | |
peyo |
1:3f75eb8d46f4 | 91 | connect_params.keepAliveIntervalInSec = 10; |
peyo |
1:3f75eb8d46f4 | 92 | connect_params.isCleanSession = true; |
peyo |
1:3f75eb8d46f4 | 93 | connect_params.MQTTVersion = MQTT_3_1_1; |
peyo |
1:3f75eb8d46f4 | 94 | char client_id[] = AWS_IOT_MQTT_CLIENT_ID; |
peyo |
1:3f75eb8d46f4 | 95 | connect_params.pClientID = client_id; |
peyo |
1:3f75eb8d46f4 | 96 | connect_params.clientIDLen = (uint16_t)strlen(AWS_IOT_MQTT_CLIENT_ID); |
peyo |
1:3f75eb8d46f4 | 97 | connect_params.isWillMsgPresent = false; |
peyo |
1:3f75eb8d46f4 | 98 | |
peyo |
1:3f75eb8d46f4 | 99 | IoT_Error_t mqtt_connect_status = aws_iot_mqtt_connect(&client, &connect_params); |
peyo |
1:3f75eb8d46f4 | 100 | if (mqtt_connect_status != SUCCESS) { |
peyo |
1:3f75eb8d46f4 | 101 | printf("Error(%d) connecting to %s:%d", mqtt_connect_status, |
peyo |
1:3f75eb8d46f4 | 102 | mqtt_init_params.pHostURL, mqtt_init_params.port); |
peyo |
1:3f75eb8d46f4 | 103 | while (true) { Thread::wait(1000); } |
peyo |
1:3f75eb8d46f4 | 104 | } |
peyo |
1:3f75eb8d46f4 | 105 | |
peyo |
1:3f75eb8d46f4 | 106 | /* |
peyo |
1:3f75eb8d46f4 | 107 | * Set MQTT autoreconnect |
peyo |
1:3f75eb8d46f4 | 108 | */ |
peyo |
1:3f75eb8d46f4 | 109 | |
peyo |
1:3f75eb8d46f4 | 110 | IoT_Error_t mqtt_autoreconnect_status = aws_iot_mqtt_autoreconnect_set_status(&client, true); |
peyo |
1:3f75eb8d46f4 | 111 | if (mqtt_autoreconnect_status != SUCCESS) { |
peyo |
1:3f75eb8d46f4 | 112 | printf("Unable to set Auto Reconnect to true - %d", mqtt_autoreconnect_status); |
peyo |
1:3f75eb8d46f4 | 113 | while (true) { Thread::wait(1000); } |
peyo |
1:3f75eb8d46f4 | 114 | } |
peyo |
1:3f75eb8d46f4 | 115 | |
peyo |
1:3f75eb8d46f4 | 116 | /* |
peyo |
1:3f75eb8d46f4 | 117 | * MQTT Subscribe |
peyo |
1:3f75eb8d46f4 | 118 | */ |
peyo |
1:3f75eb8d46f4 | 119 | |
peyo |
1:3f75eb8d46f4 | 120 | IoT_Error_t mqtt_subscribe_status = aws_iot_mqtt_subscribe( |
peyo |
1:3f75eb8d46f4 | 121 | &client, "sdkTest/sub", 11, QOS0, subscribe_callback, NULL); |
peyo |
1:3f75eb8d46f4 | 122 | if (mqtt_subscribe_status != SUCCESS) { |
peyo |
1:3f75eb8d46f4 | 123 | printf("Error subscribing: %d ", mqtt_subscribe_status); |
peyo |
1:3f75eb8d46f4 | 124 | while (true) { Thread::wait(1000); } |
peyo |
1:3f75eb8d46f4 | 125 | } |
peyo |
1:3f75eb8d46f4 | 126 | |
peyo |
1:3f75eb8d46f4 | 127 | /* |
peyo |
1:3f75eb8d46f4 | 128 | * MQTT Publish |
peyo |
1:3f75eb8d46f4 | 129 | */ |
peyo |
1:3f75eb8d46f4 | 130 | |
peyo |
1:3f75eb8d46f4 | 131 | IoT_Publish_Message_Params mqtt_publish_params; |
peyo |
1:3f75eb8d46f4 | 132 | mqtt_publish_params.qos = QOS0; |
peyo |
1:3f75eb8d46f4 | 133 | char payload[] = DEMO_PAYLOAD; |
peyo |
1:3f75eb8d46f4 | 134 | mqtt_publish_params.payload = payload; |
peyo |
1:3f75eb8d46f4 | 135 | mqtt_publish_params.payloadLen = strlen(payload); |
peyo |
1:3f75eb8d46f4 | 136 | mqtt_publish_params.isRetained = 0; |
peyo |
1:3f75eb8d46f4 | 137 | |
peyo |
1:3f75eb8d46f4 | 138 | // Main loop |
peyo |
1:3f75eb8d46f4 | 139 | IoT_Error_t mqtt_status; |
peyo |
1:3f75eb8d46f4 | 140 | while (true) { |
peyo |
1:3f75eb8d46f4 | 141 | //Max time the yield function will wait for read messages |
peyo |
1:3f75eb8d46f4 | 142 | mqtt_status = aws_iot_mqtt_yield(&client, READ_DURATION); |
peyo |
1:3f75eb8d46f4 | 143 | if (mqtt_status == NETWORK_ATTEMPTING_RECONNECT) { |
peyo |
1:3f75eb8d46f4 | 144 | // If the client is attempting to reconnect we will skip the rest of the loop. |
peyo |
1:3f75eb8d46f4 | 145 | continue; |
peyo |
1:3f75eb8d46f4 | 146 | } |
peyo |
1:3f75eb8d46f4 | 147 | |
peyo |
1:3f75eb8d46f4 | 148 | mqtt_status = aws_iot_mqtt_publish( |
peyo |
1:3f75eb8d46f4 | 149 | &client, PUBLISH_TOPIC, PUBLISH_TOPIC_LEN, &mqtt_publish_params); |
peyo |
1:3f75eb8d46f4 | 150 | if (mqtt_status != SUCCESS) { |
peyo |
1:3f75eb8d46f4 | 151 | printf("Error publishing: %d", mqtt_status); |
peyo |
1:3f75eb8d46f4 | 152 | } |
peyo |
1:3f75eb8d46f4 | 153 | } |
peyo |
1:3f75eb8d46f4 | 154 | } |