Demo application for using the AT&T IoT Starter Kit Powered by AWS.

Dependencies:   SDFileSystem

Fork of ATT_AWS_IoT_demo by Anthony Phillips

IoT Starter Kit Powered by AWS Demo

This program demonstrates the AT&T IoT Starter Kit sending data directly into AWS IoT. It's explained and used in the Getting Started with the IoT Starter Kit Powered by AWS on starterkit.att.com.

What's required

  • AT&T IoT LTE Add-on (also known as the Cellular Shield)
  • NXP K64F - for programming
  • microSD card - used to store your AWS security credentials
  • AWS account
  • Python, locally installed

If you don't already have an IoT Starter Kit, you can purchase a kit here. The IoT Starter Kit Powered by AWS includes the LTE cellular shield, K64F, and a microSD card.

Committer:
rfinn
Date:
Tue Feb 07 16:18:57 2017 +0000
Revision:
27:2f486c766854
Parent:
18:6370da1de572
changed SDFileSystem library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ampembeng 18:6370da1de572 1 /*
ampembeng 18:6370da1de572 2 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
ampembeng 18:6370da1de572 3 *
ampembeng 18:6370da1de572 4 * Licensed under the Apache License, Version 2.0 (the "License").
ampembeng 18:6370da1de572 5 * You may not use this file except in compliance with the License.
ampembeng 18:6370da1de572 6 * A copy of the License is located at
ampembeng 18:6370da1de572 7 *
ampembeng 18:6370da1de572 8 * http://aws.amazon.com/apache2.0
ampembeng 18:6370da1de572 9 *
ampembeng 18:6370da1de572 10 * or in the "license" file accompanying this file. This file is distributed
ampembeng 18:6370da1de572 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
ampembeng 18:6370da1de572 12 * express or implied. See the License for the specific language governing
ampembeng 18:6370da1de572 13 * permissions and limitations under the License.
ampembeng 18:6370da1de572 14 */
ampembeng 18:6370da1de572 15 #ifndef AWS_IOT_SDK_SRC_IOT_SHADOW_H_
ampembeng 18:6370da1de572 16 #define AWS_IOT_SDK_SRC_IOT_SHADOW_H_
ampembeng 18:6370da1de572 17
ampembeng 18:6370da1de572 18
ampembeng 18:6370da1de572 19 /**
ampembeng 18:6370da1de572 20 * @file aws_iot_shadow_interface.h
ampembeng 18:6370da1de572 21 * @brief Interface for thing shadow
ampembeng 18:6370da1de572 22 *
ampembeng 18:6370da1de572 23 * These are the functions and structs to manage/interact the Thing Shadow(in the cloud).
ampembeng 18:6370da1de572 24 * This SDK will let you interact with your own thing shadow or any other shadow using its Thing Name.
ampembeng 18:6370da1de572 25 * There are totally 3 actions a device can perform on the shadow - Get, Update and Delete.
ampembeng 18:6370da1de572 26 *
ampembeng 18:6370da1de572 27 * Currently the device should use MQTT/S underneath. In the future this will also support other protocols. As it supports MQTT, the shadow needs to connect and disconnect.
ampembeng 18:6370da1de572 28 * It will also work on the pub/sub model. On performing any action, the acknowledgment will be received in either accepted or rejected. For Example:
ampembeng 18:6370da1de572 29 * If we want to perform a GET on the thing shadow the following messages will be sent and received:
ampembeng 18:6370da1de572 30 * 1. A MQTT Publish on the topic - $aws/things/{thingName}/shadow/get
ampembeng 18:6370da1de572 31 * 2. Subscribe to MQTT topics - $aws/things/{thingName}/shadow/get/accepted and $aws/things/{thingName}/shadow/get/rejected.
ampembeng 18:6370da1de572 32 * If the request was successful we will receive the things json document in the accepted topic.
ampembeng 18:6370da1de572 33 *
ampembeng 18:6370da1de572 34 *
ampembeng 18:6370da1de572 35 */
ampembeng 18:6370da1de572 36 #include "aws_iot_mqtt_interface.h"
ampembeng 18:6370da1de572 37 #include "aws_iot_shadow_json_data.h"
ampembeng 18:6370da1de572 38
ampembeng 18:6370da1de572 39 /*!
ampembeng 18:6370da1de572 40 * @brief Shadow Connect parameters
ampembeng 18:6370da1de572 41 *
ampembeng 18:6370da1de572 42 * As the Shadow SDK uses MQTT underneath, it could be connected and disconnected on events to save some battery.
ampembeng 18:6370da1de572 43 * @note Always use the \c ShadowParametersDefault to initialize this struct
ampembeng 18:6370da1de572 44 *
ampembeng 18:6370da1de572 45 *
ampembeng 18:6370da1de572 46 *
ampembeng 18:6370da1de572 47 */
ampembeng 18:6370da1de572 48 typedef struct {
ampembeng 18:6370da1de572 49 char *pMyThingName; ///< Every device has a Thing Shadow and this is the placeholder for name
ampembeng 18:6370da1de572 50 char *pMqttClientId; ///< Currently the Shadow uses MQTT to connect and it is important to ensure we have unique client id
ampembeng 18:6370da1de572 51 char *pHost; ///< This will be unique to a customer and can be retrieved from the console
ampembeng 18:6370da1de572 52 int port; ///< By default the port is 8883
ampembeng 18:6370da1de572 53 char *pRootCA; ///< Location with the Filename of the Root CA
ampembeng 18:6370da1de572 54 char *pClientCRT; ///< Location of Device certs signed by AWS IoT service
ampembeng 18:6370da1de572 55 char *pClientKey; ///< Location of Device private key
ampembeng 18:6370da1de572 56 } ShadowParameters_t;
ampembeng 18:6370da1de572 57
ampembeng 18:6370da1de572 58 /*!
ampembeng 18:6370da1de572 59 * @brief This is set to defaults from the configuration file
ampembeng 18:6370da1de572 60 * The certs are set to NULL because they need the path to the file. shadow_sample.c file demonstrates on how to get the relative path
ampembeng 18:6370da1de572 61 *
ampembeng 18:6370da1de572 62 * \relates ShadowParameters_t
ampembeng 18:6370da1de572 63 */
ampembeng 18:6370da1de572 64 extern const ShadowParameters_t ShadowParametersDefault;
ampembeng 18:6370da1de572 65
ampembeng 18:6370da1de572 66
ampembeng 18:6370da1de572 67 /**
ampembeng 18:6370da1de572 68 * @brief Initialize the Thing Shadow before use
ampembeng 18:6370da1de572 69 *
ampembeng 18:6370da1de572 70 * This function takes care of initializing the internal book-keeping data structures
ampembeng 18:6370da1de572 71 *
ampembeng 18:6370da1de572 72 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 73 * @return An IoT Error Type defining successful/failed Initialization
ampembeng 18:6370da1de572 74 */
ampembeng 18:6370da1de572 75 IoT_Error_t aws_iot_shadow_init(MQTTClient_t *pClient);
ampembeng 18:6370da1de572 76 /**
ampembeng 18:6370da1de572 77 * @brief Connect to the AWS IoT Thing Shadow service over MQTT
ampembeng 18:6370da1de572 78 *
ampembeng 18:6370da1de572 79 * This function does the TLSv1.2 handshake and establishes the MQTT connection
ampembeng 18:6370da1de572 80 *
ampembeng 18:6370da1de572 81 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 82 * @param pParams Shadow Conenction parameters like TLS cert location
ampembeng 18:6370da1de572 83 * @return An IoT Error Type defining successful/failed Connection
ampembeng 18:6370da1de572 84 */
ampembeng 18:6370da1de572 85 IoT_Error_t aws_iot_shadow_connect(MQTTClient_t *pClient, ShadowParameters_t *pParams);
ampembeng 18:6370da1de572 86 /**
ampembeng 18:6370da1de572 87 * @brief Yield function to let the background tasks of MQTT and Shadow
ampembeng 18:6370da1de572 88 *
ampembeng 18:6370da1de572 89 * This function could be use in a separate thread waiting for the incoming messages, ensuring the connection is kept alive with the AWS Service.
ampembeng 18:6370da1de572 90 * It also ensures the expired requests of Shadow actions are cleared and Timeout callback is executed.
ampembeng 18:6370da1de572 91 * @note All callbacks ever used in the SDK will be executed in the context of this function.
ampembeng 18:6370da1de572 92 *
ampembeng 18:6370da1de572 93 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 94 * @param timeout in milliseconds, This is the maximum time the yield function will wait for a message and/or read the messages from the TLS buffer
ampembeng 18:6370da1de572 95 * @return An IoT Error Type defining successful/failed Yield
ampembeng 18:6370da1de572 96 */
ampembeng 18:6370da1de572 97 IoT_Error_t aws_iot_shadow_yield(MQTTClient_t *pClient, int timeout);
ampembeng 18:6370da1de572 98 /**
ampembeng 18:6370da1de572 99 * @brief Disconnect from the AWS IoT Thing Shadow service over MQTT
ampembeng 18:6370da1de572 100 *
ampembeng 18:6370da1de572 101 * This will close the underlying TCP connection, MQTT connection will also be closed
ampembeng 18:6370da1de572 102 *
ampembeng 18:6370da1de572 103 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 104 * @return An IoT Error Type defining successful/failed disconnect status
ampembeng 18:6370da1de572 105 */
ampembeng 18:6370da1de572 106 IoT_Error_t aws_iot_shadow_disconnect(MQTTClient_t *pClient);
ampembeng 18:6370da1de572 107
ampembeng 18:6370da1de572 108 /**
ampembeng 18:6370da1de572 109 * @brief Thing Shadow Acknowledgment enum
ampembeng 18:6370da1de572 110 *
ampembeng 18:6370da1de572 111 * This enum type is use in the callback for the action response
ampembeng 18:6370da1de572 112 *
ampembeng 18:6370da1de572 113 */
ampembeng 18:6370da1de572 114 typedef enum {
ampembeng 18:6370da1de572 115 SHADOW_ACK_TIMEOUT, SHADOW_ACK_REJECTED, SHADOW_ACK_ACCEPTED
ampembeng 18:6370da1de572 116 } Shadow_Ack_Status_t;
ampembeng 15:6f2798e45099 117
ampembeng 18:6370da1de572 118 /**
ampembeng 18:6370da1de572 119 * @brief Thing Shadow Action type enum
ampembeng 18:6370da1de572 120 *
ampembeng 18:6370da1de572 121 * This enum type is use in the callback for the action response
ampembeng 18:6370da1de572 122 *
ampembeng 18:6370da1de572 123 */
ampembeng 18:6370da1de572 124 typedef enum {
ampembeng 18:6370da1de572 125 SHADOW_GET, SHADOW_UPDATE, SHADOW_DELETE
ampembeng 18:6370da1de572 126 } ShadowActions_t;
ampembeng 18:6370da1de572 127
ampembeng 18:6370da1de572 128
ampembeng 18:6370da1de572 129 /**
ampembeng 18:6370da1de572 130 * @brief Function Pointer typedef used as the callback for every action
ampembeng 18:6370da1de572 131 *
ampembeng 18:6370da1de572 132 * This function will be called from the context of \c aws_iot_shadow_yield() context
ampembeng 18:6370da1de572 133 *
ampembeng 18:6370da1de572 134 * @param pThingName Thing Name of the response received
ampembeng 18:6370da1de572 135 * @param action The response of the action
ampembeng 18:6370da1de572 136 * @param status Informs if the action was Accepted/Rejected or Timed out
ampembeng 18:6370da1de572 137 * @param pReceivedJsonDocument Received JSON document
ampembeng 18:6370da1de572 138 * @param pContextData the void* data passed in during the action call(update, get or delete)
ampembeng 18:6370da1de572 139 *
ampembeng 18:6370da1de572 140 */
ampembeng 18:6370da1de572 141 typedef void (*fpActionCallback_t)(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status,
ampembeng 18:6370da1de572 142 const char *pReceivedJsonDocument, void *pContextData);
ampembeng 18:6370da1de572 143
ampembeng 18:6370da1de572 144 /**
ampembeng 18:6370da1de572 145 * @brief This function is the one used to perform an Update action to a Thing Name's Shadow.
ampembeng 18:6370da1de572 146 *
ampembeng 18:6370da1de572 147 * update is one of the most frequently used functionality by a device. In most cases the device may be just reporting few params to update the thing shadow in the cloud
ampembeng 18:6370da1de572 148 * Update Action if no callback or if the JSON document does not have a client token then will just publish the update and not track it.
ampembeng 18:6370da1de572 149 *
ampembeng 18:6370da1de572 150 * @note The update has to subscribe to two topics update/accepted and update/rejected. This function waits 2 seconds to ensure the subscriptions are registered before publishing the update message.
ampembeng 18:6370da1de572 151 * The following steps are performed on using this function:
ampembeng 18:6370da1de572 152 * 1. Subscribe to Shadow topics - $aws/things/{thingName}/shadow/update/accepted and $aws/things/{thingName}/shadow/update/rejected
ampembeng 18:6370da1de572 153 * 2. wait for 2 seconds for the subscription to take effect
ampembeng 18:6370da1de572 154 * 3. Publish on the update topic - $aws/things/{thingName}/shadow/update
ampembeng 18:6370da1de572 155 * 4. In the \c aws_iot_shadow_yield() function the response will be handled. In case of timeout or if the response is received, the subscription to shadow response topics are un-subscribed from.
ampembeng 18:6370da1de572 156 * On the contrary if the persistent subscription is set to true then the un-subscribe will not be done. The topics will always be listened to.
ampembeng 18:6370da1de572 157 *
ampembeng 18:6370da1de572 158 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 159 * @param pThingName Thing Name of the shadow that needs to be Updated
ampembeng 18:6370da1de572 160 * @param pJsonString The update action expects a JSON document to send. The JSO String should be a null terminated string. This JSON document should adhere to the AWS IoT Thing Shadow specification. To help in the process of creating this document- SDK provides apis in \c aws_iot_shadow_json_data.h
ampembeng 18:6370da1de572 161 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
ampembeng 18:6370da1de572 162 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
ampembeng 18:6370da1de572 163 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
ampembeng 18:6370da1de572 164 * @param isPersistentSubscribe As mentioned above, every time if a device updates the same shadow then this should be set to true to avoid repeated subscription and unsubscription. If the Thing Name is one off update then this should be set to false
ampembeng 18:6370da1de572 165 * @return An IoT Error Type defining successful/failed update action
ampembeng 18:6370da1de572 166 */
ampembeng 18:6370da1de572 167 IoT_Error_t aws_iot_shadow_update(MQTTClient_t *pClient, const char *pThingName, char *pJsonString,
ampembeng 18:6370da1de572 168 fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe);
ampembeng 18:6370da1de572 169
ampembeng 18:6370da1de572 170 /**
ampembeng 18:6370da1de572 171 * @brief This function is the one used to perform an Get action to a Thing Name's Shadow.
ampembeng 18:6370da1de572 172 *
ampembeng 18:6370da1de572 173 * One use of this function is usually to get the config of a device at boot up.
ampembeng 18:6370da1de572 174 * It is similar to the Update function internally except it does not take a JSON document as the input. The entire JSON document will be sent over the accepted topic
ampembeng 18:6370da1de572 175 *
ampembeng 18:6370da1de572 176 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 177 * @param pThingName Thing Name of the JSON document that is needed
ampembeng 18:6370da1de572 178 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
ampembeng 18:6370da1de572 179 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
ampembeng 18:6370da1de572 180 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
ampembeng 18:6370da1de572 181 * @param isPersistentSubscribe As mentioned above, every time if a device gets the same Sahdow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off get then this should be set to false
ampembeng 18:6370da1de572 182 * @return An IoT Error Type defining successful/failed get action
ampembeng 18:6370da1de572 183 */
ampembeng 18:6370da1de572 184 IoT_Error_t aws_iot_shadow_get(MQTTClient_t *pClient, const char *pThingName, fpActionCallback_t callback,
ampembeng 18:6370da1de572 185 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe);
ampembeng 18:6370da1de572 186 /**
ampembeng 18:6370da1de572 187 * @brief This function is the one used to perform an Delete action to a Thing Name's Shadow.
ampembeng 18:6370da1de572 188 *
ampembeng 18:6370da1de572 189 * This is not a very common use case for device. It is generally the responsibility of the accompanying app to do the delete.
ampembeng 18:6370da1de572 190 * It is similar to the Update function internally except it does not take a JSON document as the input. The Thing Shadow referred by the ThingName will be deleted.
ampembeng 18:6370da1de572 191 *
ampembeng 18:6370da1de572 192 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 193 * @param pThingName Thing Name of the Shadow that should be deleted
ampembeng 18:6370da1de572 194 * @param callback This is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
ampembeng 18:6370da1de572 195 * @param pContextData This is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
ampembeng 18:6370da1de572 196 * @param timeout_seconds It is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
ampembeng 18:6370da1de572 197 * @param isPersistentSubscribe As mentioned above, every time if a device deletes the same Sahdow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off delete then this should be set to false
ampembeng 18:6370da1de572 198 * @return An IoT Error Type defining successful/failed delete action
ampembeng 18:6370da1de572 199 */
ampembeng 18:6370da1de572 200 IoT_Error_t aws_iot_shadow_delete(MQTTClient_t *pClient, const char *pThingName, fpActionCallback_t callback,
ampembeng 18:6370da1de572 201 void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscriptions);
ampembeng 18:6370da1de572 202
ampembeng 18:6370da1de572 203 /**
ampembeng 18:6370da1de572 204 * @brief This function is used to listen on the delta topic of #AWS_IOT_MY_THING_NAME mentioned in the aws_iot_config.h file.
ampembeng 18:6370da1de572 205 *
ampembeng 18:6370da1de572 206 * Any time a delta is published the Json document will be delivered to the pStruct->cb. If you don't want the parsing done by the SDK then use the jsonStruct_t key set to "state". A good example of this is displayed in the sample_apps/shadow_console_echo.c
ampembeng 18:6370da1de572 207 *
ampembeng 18:6370da1de572 208 * @param pClient MQTT Client used as the protocol layer
ampembeng 18:6370da1de572 209 * @param pStruct The struct used to parse JSON value
ampembeng 18:6370da1de572 210 * @return An IoT Error Type defining successful/failed delta registering
ampembeng 18:6370da1de572 211 */
ampembeng 18:6370da1de572 212 IoT_Error_t aws_iot_shadow_register_delta(MQTTClient_t *pClient, jsonStruct_t *pStruct);
ampembeng 18:6370da1de572 213
ampembeng 18:6370da1de572 214 /**
ampembeng 18:6370da1de572 215 * @brief Reset the last received version number to zero.
ampembeng 18:6370da1de572 216 * This will be useful if the Thing Shadow is deleted and would like to to reset the local version
ampembeng 18:6370da1de572 217 * @return no return values
ampembeng 18:6370da1de572 218 *
ampembeng 18:6370da1de572 219 */
ampembeng 18:6370da1de572 220 void aws_iot_shadow_reset_last_received_version(void);
ampembeng 18:6370da1de572 221 /**
ampembeng 18:6370da1de572 222 * @brief Version of a document is received with every accepted/rejected and the SDK keeps track of the last received version of the JSON document of #AWS_IOT_MY_THING_NAME shadow
ampembeng 18:6370da1de572 223 *
ampembeng 18:6370da1de572 224 * One exception to this version tracking is that, the SDK will ignore the version from update/accepted topic. Rest of the responses will be scanned to update the version number.
ampembeng 18:6370da1de572 225 * Accepting version change for update/accepted may cause version conflicts for delta message if the update message is received before the delta.
ampembeng 18:6370da1de572 226 *
ampembeng 18:6370da1de572 227 * @return version number of the last received response
ampembeng 18:6370da1de572 228 *
ampembeng 18:6370da1de572 229 */
ampembeng 18:6370da1de572 230 uint32_t aws_iot_shadow_get_last_received_version(void);
ampembeng 18:6370da1de572 231 /**
ampembeng 18:6370da1de572 232 * @brief Enable the ignoring of delta messages with old version number
ampembeng 18:6370da1de572 233 *
ampembeng 18:6370da1de572 234 * As we use MQTT underneath, there could be more than 1 of the same message if we use QoS 0. To avoid getting called for the same message, this functionality should be enabled. All the old message will be ignored
ampembeng 18:6370da1de572 235 */
ampembeng 18:6370da1de572 236 void aws_iot_shadow_enable_discard_old_delta_msgs(void);
ampembeng 18:6370da1de572 237 /**
ampembeng 18:6370da1de572 238 * @brief Disable the ignoring of delta messages with old version number
ampembeng 18:6370da1de572 239 */
ampembeng 18:6370da1de572 240 void aws_iot_shadow_disable_discard_old_delta_msgs(void);
ampembeng 18:6370da1de572 241
ampembeng 18:6370da1de572 242 #endif //AWS_IOT_SDK_SRC_IOT_SHADOW_H_
ampembeng 18:6370da1de572 243