thing_shadow_sample.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * Additions Copyright 2016 Espressif Systems (Shanghai) PTE LTD
  4. *
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License").
  7. * You may not use this file except in compliance with the License.
  8. * A copy of the License is located at
  9. *
  10. * http://aws.amazon.com/apache2.0
  11. *
  12. * or in the "license" file accompanying this file. This file is distributed
  13. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  14. * express or implied. See the License for the specific language governing
  15. * permissions and limitations under the License.
  16. */
  17. /**
  18. * @file thing_shadow_sample.c
  19. * @brief A simple connected window example demonstrating the use of Thing Shadow
  20. *
  21. * See example README for more details.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include <unistd.h>
  27. #include <limits.h>
  28. #include <string.h>
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/task.h"
  31. #include "freertos/event_groups.h"
  32. #include "esp_system.h"
  33. #include "esp_wifi.h"
  34. #include "esp_event_loop.h"
  35. #include "esp_log.h"
  36. #include "esp_vfs_fat.h"
  37. #include "driver/sdmmc_host.h"
  38. #include "aws_iot_config.h"
  39. #include "aws_iot_log.h"
  40. #include "aws_iot_version.h"
  41. #include "aws_iot_mqtt_client_interface.h"
  42. #include "aws_iot_shadow_interface.h"
  43. /*!
  44. * The goal of this sample application is to demonstrate the capabilities of shadow.
  45. * This device(say Connected Window) will open the window of a room based on temperature
  46. * It can report to the Shadow the following parameters:
  47. * 1. temperature of the room (double)
  48. * 2. status of the window (open or close)
  49. * It can act on commands from the cloud. In this case it will open or close the window based on the json object "windowOpen" data[open/close]
  50. *
  51. * The two variables from a device's perspective are double temperature and bool windowOpen
  52. * The device needs to act on only on windowOpen variable, so we will create a primitiveJson_t object with callback
  53. The Json Document in the cloud will be
  54. {
  55. "reported": {
  56. "temperature": 0,
  57. "windowOpen": false
  58. },
  59. "desired": {
  60. "windowOpen": false
  61. }
  62. }
  63. */
  64. static const char *TAG = "shadow";
  65. #define ROOMTEMPERATURE_UPPERLIMIT 32.0f
  66. #define ROOMTEMPERATURE_LOWERLIMIT 25.0f
  67. #define STARTING_ROOMTEMPERATURE ROOMTEMPERATURE_LOWERLIMIT
  68. #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200
  69. /* The examples use simple WiFi configuration that you can set via
  70. 'make menuconfig'.
  71. If you'd rather not, just change the below entries to strings with
  72. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  73. */
  74. #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  75. #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  76. /* FreeRTOS event group to signal when we are connected & ready to make a request */
  77. static EventGroupHandle_t wifi_event_group;
  78. /* The event group allows multiple bits for each event,
  79. but we only care about one event - are we connected
  80. to the AP with an IP? */
  81. const int CONNECTED_BIT = BIT0;
  82. /* CA Root certificate, device ("Thing") certificate and device
  83. * ("Thing") key.
  84. Example can be configured one of two ways:
  85. "Embedded Certs" are loaded from files in "certs/" and embedded into the app binary.
  86. "Filesystem Certs" are loaded from the filesystem (SD card, etc.)
  87. See example README for more details.
  88. */
  89. #if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS)
  90. extern const uint8_t aws_root_ca_pem_start[] asm("_binary_aws_root_ca_pem_start");
  91. extern const uint8_t aws_root_ca_pem_end[] asm("_binary_aws_root_ca_pem_end");
  92. extern const uint8_t certificate_pem_crt_start[] asm("_binary_certificate_pem_crt_start");
  93. extern const uint8_t certificate_pem_crt_end[] asm("_binary_certificate_pem_crt_end");
  94. extern const uint8_t private_pem_key_start[] asm("_binary_private_pem_key_start");
  95. extern const uint8_t private_pem_key_end[] asm("_binary_private_pem_key_end");
  96. #elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS)
  97. static const char * DEVICE_CERTIFICATE_PATH = CONFIG_EXAMPLE_CERTIFICATE_PATH;
  98. static const char * DEVICE_PRIVATE_KEY_PATH = CONFIG_EXAMPLE_PRIVATE_KEY_PATH;
  99. static const char * ROOT_CA_PATH = CONFIG_EXAMPLE_ROOT_CA_PATH;
  100. #else
  101. #error "Invalid method for loading certs"
  102. #endif
  103. /**
  104. * @brief Default MQTT HOST URL is pulled from the aws_iot_config.h which
  105. * uses menuconfig to find a default.
  106. */
  107. char HostAddress[255] = AWS_IOT_MQTT_HOST;
  108. /**
  109. * @brief Default MQTT port is pulled from the aws_iot_config.h which
  110. * uses menuconfig to find a default.
  111. */
  112. uint32_t port = AWS_IOT_MQTT_PORT;
  113. static esp_err_t event_handler(void *ctx, system_event_t *event)
  114. {
  115. switch(event->event_id) {
  116. case SYSTEM_EVENT_STA_START:
  117. esp_wifi_connect();
  118. break;
  119. case SYSTEM_EVENT_STA_GOT_IP:
  120. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  121. break;
  122. case SYSTEM_EVENT_STA_DISCONNECTED:
  123. /* This is a workaround as ESP32 WiFi libs don't currently
  124. auto-reassociate. */
  125. esp_wifi_connect();
  126. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  127. break;
  128. default:
  129. break;
  130. }
  131. return ESP_OK;
  132. }
  133. static void simulateRoomTemperature(float *pRoomTemperature) {
  134. static float deltaChange;
  135. if(*pRoomTemperature >= ROOMTEMPERATURE_UPPERLIMIT) {
  136. deltaChange = -0.5f;
  137. } else if(*pRoomTemperature <= ROOMTEMPERATURE_LOWERLIMIT) {
  138. deltaChange = 0.5f;
  139. }
  140. *pRoomTemperature += deltaChange;
  141. }
  142. static bool shadowUpdateInProgress;
  143. void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status,
  144. const char *pReceivedJsonDocument, void *pContextData) {
  145. IOT_UNUSED(pThingName);
  146. IOT_UNUSED(action);
  147. IOT_UNUSED(pReceivedJsonDocument);
  148. IOT_UNUSED(pContextData);
  149. shadowUpdateInProgress = false;
  150. if(SHADOW_ACK_TIMEOUT == status) {
  151. ESP_LOGE(TAG, "Update timed out");
  152. } else if(SHADOW_ACK_REJECTED == status) {
  153. ESP_LOGE(TAG, "Update rejected");
  154. } else if(SHADOW_ACK_ACCEPTED == status) {
  155. ESP_LOGI(TAG, "Update accepted");
  156. }
  157. }
  158. void windowActuate_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) {
  159. IOT_UNUSED(pJsonString);
  160. IOT_UNUSED(JsonStringDataLen);
  161. if(pContext != NULL) {
  162. ESP_LOGI(TAG, "Delta - Window state changed to %d", *(bool *) (pContext->pData));
  163. }
  164. }
  165. void aws_iot_task(void *param) {
  166. IoT_Error_t rc = FAILURE;
  167. char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
  168. size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]);
  169. float temperature = 0.0;
  170. bool windowOpen = false;
  171. jsonStruct_t windowActuator;
  172. windowActuator.cb = windowActuate_Callback;
  173. windowActuator.pData = &windowOpen;
  174. windowActuator.pKey = "windowOpen";
  175. windowActuator.type = SHADOW_JSON_BOOL;
  176. jsonStruct_t temperatureHandler;
  177. temperatureHandler.cb = NULL;
  178. temperatureHandler.pKey = "temperature";
  179. temperatureHandler.pData = &temperature;
  180. temperatureHandler.type = SHADOW_JSON_FLOAT;
  181. ESP_LOGI(TAG, "AWS IoT SDK Version %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
  182. // initialize the mqtt client
  183. AWS_IoT_Client mqttClient;
  184. ShadowInitParameters_t sp = ShadowInitParametersDefault;
  185. sp.pHost = AWS_IOT_MQTT_HOST;
  186. sp.port = AWS_IOT_MQTT_PORT;
  187. #if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS)
  188. sp.pClientCRT = (const char *)certificate_pem_crt_start;
  189. sp.pClientKey = (const char *)private_pem_key_start;
  190. sp.pRootCA = (const char *)aws_root_ca_pem_start;
  191. #elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS)
  192. sp.pClientCRT = DEVICE_CERTIFICATE_PATH;
  193. sp.pClientKey = DEVICE_PRIVATE_KEY_PATH;
  194. sp.pRootCA = ROOT_CA_PATH;
  195. #endif
  196. sp.enableAutoReconnect = false;
  197. sp.disconnectHandler = NULL;
  198. #ifdef CONFIG_EXAMPLE_SDCARD_CERTS
  199. ESP_LOGI(TAG, "Mounting SD card...");
  200. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  201. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  202. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  203. .format_if_mount_failed = false,
  204. .max_files = 3,
  205. };
  206. sdmmc_card_t* card;
  207. esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  208. if (ret != ESP_OK) {
  209. ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem.");
  210. abort();
  211. }
  212. #endif
  213. /* Wait for WiFI to show as connected */
  214. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  215. false, true, portMAX_DELAY);
  216. ESP_LOGI(TAG, "Shadow Init");
  217. rc = aws_iot_shadow_init(&mqttClient, &sp);
  218. if(SUCCESS != rc) {
  219. ESP_LOGE(TAG, "aws_iot_shadow_init returned error %d, aborting...", rc);
  220. abort();
  221. }
  222. ShadowConnectParameters_t scp = ShadowConnectParametersDefault;
  223. scp.pMyThingName = CONFIG_AWS_EXAMPLE_THING_NAME;
  224. scp.pMqttClientId = CONFIG_AWS_EXAMPLE_CLIENT_ID;
  225. scp.mqttClientIdLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID);
  226. ESP_LOGI(TAG, "Shadow Connect");
  227. rc = aws_iot_shadow_connect(&mqttClient, &scp);
  228. if(SUCCESS != rc) {
  229. ESP_LOGE(TAG, "aws_iot_shadow_connect returned error %d, aborting...", rc);
  230. abort();
  231. }
  232. /*
  233. * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h
  234. * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
  235. * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
  236. */
  237. rc = aws_iot_shadow_set_autoreconnect_status(&mqttClient, true);
  238. if(SUCCESS != rc) {
  239. ESP_LOGE(TAG, "Unable to set Auto Reconnect to true - %d, aborting...", rc);
  240. abort();
  241. }
  242. rc = aws_iot_shadow_register_delta(&mqttClient, &windowActuator);
  243. if(SUCCESS != rc) {
  244. ESP_LOGE(TAG, "Shadow Register Delta Error");
  245. }
  246. temperature = STARTING_ROOMTEMPERATURE;
  247. // loop and publish a change in temperature
  248. while(NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc) {
  249. rc = aws_iot_shadow_yield(&mqttClient, 200);
  250. if(NETWORK_ATTEMPTING_RECONNECT == rc || shadowUpdateInProgress) {
  251. rc = aws_iot_shadow_yield(&mqttClient, 1000);
  252. // If the client is attempting to reconnect, or already waiting on a shadow update,
  253. // we will skip the rest of the loop.
  254. continue;
  255. }
  256. ESP_LOGI(TAG, "=======================================================================================");
  257. ESP_LOGI(TAG, "On Device: window state %s", windowOpen ? "true" : "false");
  258. simulateRoomTemperature(&temperature);
  259. rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
  260. if(SUCCESS == rc) {
  261. rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &temperatureHandler,
  262. &windowActuator);
  263. if(SUCCESS == rc) {
  264. rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
  265. if(SUCCESS == rc) {
  266. ESP_LOGI(TAG, "Update Shadow: %s", JsonDocumentBuffer);
  267. rc = aws_iot_shadow_update(&mqttClient, CONFIG_AWS_EXAMPLE_THING_NAME, JsonDocumentBuffer,
  268. ShadowUpdateStatusCallback, NULL, 4, true);
  269. shadowUpdateInProgress = true;
  270. }
  271. }
  272. }
  273. ESP_LOGI(TAG, "*****************************************************************************************");
  274. vTaskDelay(1000 / portTICK_RATE_MS);
  275. }
  276. if(SUCCESS != rc) {
  277. ESP_LOGE(TAG, "An error occurred in the loop %d", rc);
  278. }
  279. ESP_LOGI(TAG, "Disconnecting");
  280. rc = aws_iot_shadow_disconnect(&mqttClient);
  281. if(SUCCESS != rc) {
  282. ESP_LOGE(TAG, "Disconnect error %d", rc);
  283. }
  284. vTaskDelete(NULL);
  285. }
  286. static void initialise_wifi(void)
  287. {
  288. tcpip_adapter_init();
  289. wifi_event_group = xEventGroupCreate();
  290. ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  291. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  292. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  293. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  294. wifi_config_t wifi_config = {
  295. .sta = {
  296. .ssid = EXAMPLE_WIFI_SSID,
  297. .password = EXAMPLE_WIFI_PASS,
  298. },
  299. };
  300. ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  301. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  302. ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  303. ESP_ERROR_CHECK( esp_wifi_start() );
  304. }
  305. void app_main()
  306. {
  307. initialise_wifi();
  308. /* Temporarily pin task to core, due to FPU uncertainty */
  309. xTaskCreatePinnedToCore(&aws_iot_task, "aws_iot_task", 16384+1024, NULL, 5, NULL, 1);
  310. }