NuMaker mbed cloud v1.2 example
This example has been used on the following platforms
Follow these instructions to get credentials https://cloud.mbed.com/docs/v1.2/quick-start/connecting-your-device-to-mbed-cloud.html
main.cpp@10:3cad9e255e41, 2018-03-20 (annotated)
- Committer:
- cyliang
- Date:
- Tue Mar 20 16:20:59 2018 +0800
- Revision:
- 10:3cad9e255e41
- Parent:
- 9:5c7c3e367ce6
- Child:
- 11:96f41e7c9638
Support M487 & NUC472 on-board SD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarceloSalazar | 0:e13a8a944e25 | 1 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 2 | // Copyright 2016-2017 ARM Ltd. |
MarceloSalazar | 0:e13a8a944e25 | 3 | // |
MarceloSalazar | 0:e13a8a944e25 | 4 | // SPDX-License-Identifier: Apache-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 5 | // |
MarceloSalazar | 0:e13a8a944e25 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
MarceloSalazar | 0:e13a8a944e25 | 7 | // you may not use this file except in compliance with the License. |
MarceloSalazar | 0:e13a8a944e25 | 8 | // You may obtain a copy of the License at |
MarceloSalazar | 0:e13a8a944e25 | 9 | // |
MarceloSalazar | 0:e13a8a944e25 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 11 | // |
MarceloSalazar | 0:e13a8a944e25 | 12 | // Unless required by applicable law or agreed to in writing, software |
MarceloSalazar | 0:e13a8a944e25 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
MarceloSalazar | 0:e13a8a944e25 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MarceloSalazar | 0:e13a8a944e25 | 15 | // See the License for the specific language governing permissions and |
MarceloSalazar | 0:e13a8a944e25 | 16 | // limitations under the License. |
MarceloSalazar | 0:e13a8a944e25 | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 18 | |
MarceloSalazar | 1:1ccf36276cd3 | 19 | #include "mbed.h" |
MarceloSalazar | 1:1ccf36276cd3 | 20 | #include "mbed-trace/mbed_trace.h" |
MarceloSalazar | 1:1ccf36276cd3 | 21 | #include "mbed-trace-helper.h" |
MarceloSalazar | 0:e13a8a944e25 | 22 | #include "simple-mbed-cloud-client.h" |
MarceloSalazar | 1:1ccf36276cd3 | 23 | #include "key-config-manager/kcm_status.h" |
MarceloSalazar | 1:1ccf36276cd3 | 24 | #include "key-config-manager/key_config_manager.h" |
MarceloSalazar | 1:1ccf36276cd3 | 25 | #include "SDBlockDevice.h" |
MarceloSalazar | 1:1ccf36276cd3 | 26 | #include "FATFileSystem.h" |
MarceloSalazar | 1:1ccf36276cd3 | 27 | #include "EthernetInterface.h" |
MarceloSalazar | 1:1ccf36276cd3 | 28 | |
cyliang | 10:3cad9e255e41 | 29 | //#define SDBLOCK_SPI_MODE |
cyliang | 10:3cad9e255e41 | 30 | |
MarceloSalazar | 4:a107dae867fb | 31 | // Placeholder to hardware that trigger events (timer, button, etc) |
MarceloSalazar | 4:a107dae867fb | 32 | Ticker timer; |
MarceloSalazar | 0:e13a8a944e25 | 33 | |
bridadan | 5:c18fab181ede | 34 | // Placeholder for storage |
cyliang | 10:3cad9e255e41 | 35 | #ifdef SDBLOCK_SPI_MODE |
cyliang | 10:3cad9e255e41 | 36 | // SPI mode |
cyliang | 9:5c7c3e367ce6 | 37 | SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); |
cyliang | 10:3cad9e255e41 | 38 | #else |
cyliang | 10:3cad9e255e41 | 39 | // SD mode |
cyliang | 10:3cad9e255e41 | 40 | #include "NuSDBlockDevice.h" |
cyliang | 10:3cad9e255e41 | 41 | NuSDBlockDevice sd; |
cyliang | 10:3cad9e255e41 | 42 | #endif |
cyliang | 9:5c7c3e367ce6 | 43 | |
bridadan | 5:c18fab181ede | 44 | FATFileSystem fs("sd"); |
bridadan | 5:c18fab181ede | 45 | |
MarceloSalazar | 0:e13a8a944e25 | 46 | // Pointers to the resources that will be created in main_application(). |
MarceloSalazar | 1:1ccf36276cd3 | 47 | static MbedCloudClientResource* pattern_ptr; |
MarceloSalazar | 0:e13a8a944e25 | 48 | |
MarceloSalazar | 0:e13a8a944e25 | 49 | // Pointer to mbedClient, used for calling close function. |
MarceloSalazar | 0:e13a8a944e25 | 50 | static SimpleMbedCloudClient *client; |
MarceloSalazar | 0:e13a8a944e25 | 51 | |
MarceloSalazar | 1:1ccf36276cd3 | 52 | static bool button_pressed = false; |
MarceloSalazar | 1:1ccf36276cd3 | 53 | |
MarceloSalazar | 1:1ccf36276cd3 | 54 | void button_press() { |
MarceloSalazar | 1:1ccf36276cd3 | 55 | button_pressed = true; |
MarceloSalazar | 1:1ccf36276cd3 | 56 | } |
MarceloSalazar | 1:1ccf36276cd3 | 57 | |
MarceloSalazar | 1:1ccf36276cd3 | 58 | void pattern_updated(const char *) { |
bridadan | 5:c18fab181ede | 59 | printf("PUT received, new value: %s\n", pattern_ptr->get_value().c_str()); |
MarceloSalazar | 2:6ed27f413b30 | 60 | // Placeholder for PUT action |
MarceloSalazar | 0:e13a8a944e25 | 61 | } |
MarceloSalazar | 0:e13a8a944e25 | 62 | |
MarceloSalazar | 0:e13a8a944e25 | 63 | void blink_callback(void *) { |
bridadan | 5:c18fab181ede | 64 | String pattern_str = pattern_ptr->get_value(); |
bridadan | 5:c18fab181ede | 65 | const char *pattern = pattern_str.c_str(); |
MarceloSalazar | 2:6ed27f413b30 | 66 | printf("POST received. LED pattern = %s\n", pattern); |
MarceloSalazar | 2:6ed27f413b30 | 67 | // Placeholder for POST action |
MarceloSalazar | 0:e13a8a944e25 | 68 | // The pattern is something like 500:200:500, so parse that. |
MarceloSalazar | 0:e13a8a944e25 | 69 | } |
MarceloSalazar | 0:e13a8a944e25 | 70 | |
MarceloSalazar | 2:6ed27f413b30 | 71 | void button_callback(const M2MBase& object, const NoticationDeliveryStatus status) |
MarceloSalazar | 0:e13a8a944e25 | 72 | { |
MarceloSalazar | 2:6ed27f413b30 | 73 | printf("Button notification. Callback: (%s)\n", object.uri_path()); |
MarceloSalazar | 2:6ed27f413b30 | 74 | // Placeholder for GET |
MarceloSalazar | 0:e13a8a944e25 | 75 | } |
MarceloSalazar | 0:e13a8a944e25 | 76 | |
MarceloSalazar | 0:e13a8a944e25 | 77 | |
MarceloSalazar | 0:e13a8a944e25 | 78 | int main(void) |
MarceloSalazar | 0:e13a8a944e25 | 79 | { |
MarceloSalazar | 2:6ed27f413b30 | 80 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
MarceloSalazar | 2:6ed27f413b30 | 81 | // Older versions: workaround to prevent possible deletion of credentials: |
MarceloSalazar | 0:e13a8a944e25 | 82 | wait(2); |
MarceloSalazar | 0:e13a8a944e25 | 83 | |
MarceloSalazar | 1:1ccf36276cd3 | 84 | // Misc OS setup |
MarceloSalazar | 1:1ccf36276cd3 | 85 | srand(time(NULL)); |
MarceloSalazar | 1:1ccf36276cd3 | 86 | |
MarceloSalazar | 2:6ed27f413b30 | 87 | // Placeholder for network |
MarceloSalazar | 1:1ccf36276cd3 | 88 | EthernetInterface net; |
MarceloSalazar | 2:6ed27f413b30 | 89 | |
MarceloSalazar | 1:1ccf36276cd3 | 90 | printf("Start Simple Mbed Cloud Client\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 91 | |
MarceloSalazar | 1:1ccf36276cd3 | 92 | // Initialize SD card |
MarceloSalazar | 1:1ccf36276cd3 | 93 | int status = sd.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 94 | if (status != BD_ERROR_OK) { |
MarceloSalazar | 1:1ccf36276cd3 | 95 | printf("Failed to init SD card\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 96 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 97 | } |
MarceloSalazar | 0:e13a8a944e25 | 98 | |
MarceloSalazar | 1:1ccf36276cd3 | 99 | // Mount the file system (reformatting on failure) |
MarceloSalazar | 1:1ccf36276cd3 | 100 | status = fs.mount(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 101 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 102 | printf("Failed to mount FAT file system, reformatting...\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 103 | status = fs.reformat(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 104 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 105 | printf("Failed to reformat FAT file system\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 106 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 107 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 108 | printf("Reformat and mount complete\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 109 | } |
MarceloSalazar | 1:1ccf36276cd3 | 110 | } |
MarceloSalazar | 1:1ccf36276cd3 | 111 | |
MarceloSalazar | 1:1ccf36276cd3 | 112 | printf("Connecting to the network using Ethernet...\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 113 | |
MarceloSalazar | 1:1ccf36276cd3 | 114 | status = net.connect(); |
MarceloSalazar | 1:1ccf36276cd3 | 115 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 116 | printf("Connection to Network Failed %d!\n", status); |
MarceloSalazar | 1:1ccf36276cd3 | 117 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 118 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 119 | const char *ip_addr = net.get_ip_address(); |
MarceloSalazar | 1:1ccf36276cd3 | 120 | printf("Connected successfully\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 121 | printf("IP address %s\n", ip_addr); |
MarceloSalazar | 1:1ccf36276cd3 | 122 | } |
MarceloSalazar | 1:1ccf36276cd3 | 123 | |
MarceloSalazar | 1:1ccf36276cd3 | 124 | SimpleMbedCloudClient mbedClient(&net); |
MarceloSalazar | 0:e13a8a944e25 | 125 | // Save pointer to mbedClient so that other functions can access it. |
MarceloSalazar | 0:e13a8a944e25 | 126 | client = &mbedClient; |
MarceloSalazar | 0:e13a8a944e25 | 127 | |
MarceloSalazar | 1:1ccf36276cd3 | 128 | status = mbedClient.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 129 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 130 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 131 | } |
MarceloSalazar | 1:1ccf36276cd3 | 132 | |
MarceloSalazar | 0:e13a8a944e25 | 133 | printf("Client initialized\r\n"); |
MarceloSalazar | 0:e13a8a944e25 | 134 | |
MarceloSalazar | 1:1ccf36276cd3 | 135 | // Mbed Cloud Client resource setup |
MarceloSalazar | 1:1ccf36276cd3 | 136 | MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 137 | button->set_value("0"); |
MarceloSalazar | 1:1ccf36276cd3 | 138 | button->methods(M2MMethod::GET); |
MarceloSalazar | 1:1ccf36276cd3 | 139 | button->observable(true); |
bridadan | 5:c18fab181ede | 140 | button->attach_notification_callback(button_callback); |
MarceloSalazar | 0:e13a8a944e25 | 141 | |
MarceloSalazar | 1:1ccf36276cd3 | 142 | MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 143 | pattern->set_value("500:500:500:500"); |
MarceloSalazar | 1:1ccf36276cd3 | 144 | pattern->methods(M2MMethod::GET | M2MMethod::PUT); |
bridadan | 5:c18fab181ede | 145 | pattern->attach_put_callback(pattern_updated); |
MarceloSalazar | 1:1ccf36276cd3 | 146 | pattern_ptr = pattern; |
MarceloSalazar | 0:e13a8a944e25 | 147 | |
MarceloSalazar | 1:1ccf36276cd3 | 148 | MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 149 | blink->methods(M2MMethod::POST); |
bridadan | 5:c18fab181ede | 150 | blink->attach_post_callback(blink_callback); |
MarceloSalazar | 0:e13a8a944e25 | 151 | |
MarceloSalazar | 0:e13a8a944e25 | 152 | mbedClient.register_and_connect(); |
MarceloSalazar | 0:e13a8a944e25 | 153 | |
bridadan | 6:254a7e7fbef1 | 154 | // Wait for client to finish registering |
bridadan | 6:254a7e7fbef1 | 155 | while (!mbedClient.is_client_registered()) { |
bridadan | 6:254a7e7fbef1 | 156 | wait_ms(100); |
bridadan | 6:254a7e7fbef1 | 157 | } |
bridadan | 6:254a7e7fbef1 | 158 | |
bridadan | 6:254a7e7fbef1 | 159 | // Placeholder for callback to update local resource when GET comes. |
bridadan | 6:254a7e7fbef1 | 160 | timer.attach(&button_press, 5.0); |
bridadan | 6:254a7e7fbef1 | 161 | |
MarceloSalazar | 0:e13a8a944e25 | 162 | // Check if client is registering or registered, if true sleep and repeat. |
MarceloSalazar | 0:e13a8a944e25 | 163 | while (mbedClient.is_register_called()) { |
MarceloSalazar | 0:e13a8a944e25 | 164 | static int button_count = 0; |
MarceloSalazar | 1:1ccf36276cd3 | 165 | wait_ms(100); |
MarceloSalazar | 1:1ccf36276cd3 | 166 | |
MarceloSalazar | 1:1ccf36276cd3 | 167 | if (button_pressed) { |
MarceloSalazar | 1:1ccf36276cd3 | 168 | button_pressed = false; |
MarceloSalazar | 4:a107dae867fb | 169 | printf("Simulated button clicked %d times\r\n", ++button_count); |
MarceloSalazar | 1:1ccf36276cd3 | 170 | button->set_value(button_count); |
MarceloSalazar | 0:e13a8a944e25 | 171 | } |
MarceloSalazar | 0:e13a8a944e25 | 172 | } |
MarceloSalazar | 0:e13a8a944e25 | 173 | |
MarceloSalazar | 0:e13a8a944e25 | 174 | // Client unregistered, exit program. |
MarceloSalazar | 0:e13a8a944e25 | 175 | return 0; |
MarceloSalazar | 0:e13a8a944e25 | 176 | } |