Simple Mbed Cloud client application using features of K64 & K66 including Ethernet and SD Card
Fork of mbed-cloud-example_K64_K66 by
DEPRECATED
This example application is not maintained and not recommended. It uses an old version of Mbed OS, Pelion DM, and Arm toolchain. It doesn't work with Mbed Studio.
Please use: https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-pelion/
main.cpp@2:6ed27f413b30, 2018-02-14 (annotated)
- Committer:
- MarceloSalazar
- Date:
- Wed Feb 14 13:20:30 2018 +0000
- Revision:
- 2:6ed27f413b30
- Parent:
- 1:1ccf36276cd3
- Child:
- 4:a107dae867fb
Update Simple MCC lib
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 | |
MarceloSalazar | 1:1ccf36276cd3 | 29 | InterruptIn button(BUTTON1); |
MarceloSalazar | 0:e13a8a944e25 | 30 | |
MarceloSalazar | 0:e13a8a944e25 | 31 | // Pointers to the resources that will be created in main_application(). |
MarceloSalazar | 1:1ccf36276cd3 | 32 | static MbedCloudClientResource* pattern_ptr; |
MarceloSalazar | 0:e13a8a944e25 | 33 | |
MarceloSalazar | 0:e13a8a944e25 | 34 | // Pointer to mbedClient, used for calling close function. |
MarceloSalazar | 0:e13a8a944e25 | 35 | static SimpleMbedCloudClient *client; |
MarceloSalazar | 0:e13a8a944e25 | 36 | |
MarceloSalazar | 1:1ccf36276cd3 | 37 | static bool button_pressed = false; |
MarceloSalazar | 1:1ccf36276cd3 | 38 | |
MarceloSalazar | 1:1ccf36276cd3 | 39 | void button_press() { |
MarceloSalazar | 1:1ccf36276cd3 | 40 | button_pressed = true; |
MarceloSalazar | 1:1ccf36276cd3 | 41 | } |
MarceloSalazar | 1:1ccf36276cd3 | 42 | |
MarceloSalazar | 1:1ccf36276cd3 | 43 | void pattern_updated(const char *) { |
MarceloSalazar | 1:1ccf36276cd3 | 44 | printf("PUT received, new value: %s\n", pattern_ptr->get_value()); |
MarceloSalazar | 2:6ed27f413b30 | 45 | // Placeholder for PUT action |
MarceloSalazar | 0:e13a8a944e25 | 46 | } |
MarceloSalazar | 0:e13a8a944e25 | 47 | |
MarceloSalazar | 0:e13a8a944e25 | 48 | void blink_callback(void *) { |
MarceloSalazar | 1:1ccf36276cd3 | 49 | const char *pattern = pattern_ptr->get_value(); |
MarceloSalazar | 2:6ed27f413b30 | 50 | printf("POST received. LED pattern = %s\n", pattern); |
MarceloSalazar | 2:6ed27f413b30 | 51 | // Placeholder for POST action |
MarceloSalazar | 0:e13a8a944e25 | 52 | // The pattern is something like 500:200:500, so parse that. |
MarceloSalazar | 0:e13a8a944e25 | 53 | } |
MarceloSalazar | 0:e13a8a944e25 | 54 | |
MarceloSalazar | 2:6ed27f413b30 | 55 | void button_callback(const M2MBase& object, const NoticationDeliveryStatus status) |
MarceloSalazar | 0:e13a8a944e25 | 56 | { |
MarceloSalazar | 2:6ed27f413b30 | 57 | printf("Button notification. Callback: (%s)\n", object.uri_path()); |
MarceloSalazar | 2:6ed27f413b30 | 58 | // Placeholder for GET |
MarceloSalazar | 0:e13a8a944e25 | 59 | } |
MarceloSalazar | 0:e13a8a944e25 | 60 | |
MarceloSalazar | 0:e13a8a944e25 | 61 | |
MarceloSalazar | 0:e13a8a944e25 | 62 | int main(void) |
MarceloSalazar | 0:e13a8a944e25 | 63 | { |
MarceloSalazar | 2:6ed27f413b30 | 64 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
MarceloSalazar | 2:6ed27f413b30 | 65 | // Older versions: workaround to prevent possible deletion of credentials: |
MarceloSalazar | 0:e13a8a944e25 | 66 | wait(2); |
MarceloSalazar | 0:e13a8a944e25 | 67 | |
MarceloSalazar | 1:1ccf36276cd3 | 68 | // Misc OS setup |
MarceloSalazar | 1:1ccf36276cd3 | 69 | srand(time(NULL)); |
MarceloSalazar | 1:1ccf36276cd3 | 70 | |
MarceloSalazar | 2:6ed27f413b30 | 71 | // Placeholder for network |
MarceloSalazar | 1:1ccf36276cd3 | 72 | EthernetInterface net; |
MarceloSalazar | 2:6ed27f413b30 | 73 | |
MarceloSalazar | 2:6ed27f413b30 | 74 | // Placeholder for storage |
MarceloSalazar | 1:1ccf36276cd3 | 75 | SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); |
MarceloSalazar | 1:1ccf36276cd3 | 76 | FATFileSystem fs("sd"); |
MarceloSalazar | 1:1ccf36276cd3 | 77 | |
MarceloSalazar | 1:1ccf36276cd3 | 78 | printf("Start Simple Mbed Cloud Client\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 79 | |
MarceloSalazar | 1:1ccf36276cd3 | 80 | // Initialize SD card |
MarceloSalazar | 1:1ccf36276cd3 | 81 | int status = sd.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 82 | if (status != BD_ERROR_OK) { |
MarceloSalazar | 1:1ccf36276cd3 | 83 | printf("Failed to init SD card\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 84 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 85 | } |
MarceloSalazar | 0:e13a8a944e25 | 86 | |
MarceloSalazar | 1:1ccf36276cd3 | 87 | // Mount the file system (reformatting on failure) |
MarceloSalazar | 1:1ccf36276cd3 | 88 | status = fs.mount(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 89 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 90 | printf("Failed to mount FAT file system, reformatting...\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 91 | status = fs.reformat(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 92 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 93 | printf("Failed to reformat FAT file system\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 94 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 95 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 96 | printf("Reformat and mount complete\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 97 | } |
MarceloSalazar | 1:1ccf36276cd3 | 98 | } |
MarceloSalazar | 1:1ccf36276cd3 | 99 | |
MarceloSalazar | 1:1ccf36276cd3 | 100 | printf("Connecting to the network using Ethernet...\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 101 | |
MarceloSalazar | 1:1ccf36276cd3 | 102 | status = net.connect(); |
MarceloSalazar | 1:1ccf36276cd3 | 103 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 104 | printf("Connection to Network Failed %d!\n", status); |
MarceloSalazar | 1:1ccf36276cd3 | 105 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 106 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 107 | const char *ip_addr = net.get_ip_address(); |
MarceloSalazar | 1:1ccf36276cd3 | 108 | printf("Connected successfully\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 109 | printf("IP address %s\n", ip_addr); |
MarceloSalazar | 1:1ccf36276cd3 | 110 | } |
MarceloSalazar | 1:1ccf36276cd3 | 111 | |
MarceloSalazar | 2:6ed27f413b30 | 112 | // Placeholder for GET requests. Initialize button interrupt. |
MarceloSalazar | 2:6ed27f413b30 | 113 | button.fall(&button_press); |
MarceloSalazar | 2:6ed27f413b30 | 114 | |
MarceloSalazar | 1:1ccf36276cd3 | 115 | SimpleMbedCloudClient mbedClient(&net); |
MarceloSalazar | 0:e13a8a944e25 | 116 | // Save pointer to mbedClient so that other functions can access it. |
MarceloSalazar | 0:e13a8a944e25 | 117 | client = &mbedClient; |
MarceloSalazar | 0:e13a8a944e25 | 118 | |
MarceloSalazar | 1:1ccf36276cd3 | 119 | status = mbedClient.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 120 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 121 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 122 | } |
MarceloSalazar | 1:1ccf36276cd3 | 123 | |
MarceloSalazar | 0:e13a8a944e25 | 124 | printf("Client initialized\r\n"); |
MarceloSalazar | 0:e13a8a944e25 | 125 | |
MarceloSalazar | 1:1ccf36276cd3 | 126 | // Mbed Cloud Client resource setup |
MarceloSalazar | 1:1ccf36276cd3 | 127 | MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 128 | button->set_value("0"); |
MarceloSalazar | 1:1ccf36276cd3 | 129 | button->methods(M2MMethod::GET); |
MarceloSalazar | 1:1ccf36276cd3 | 130 | button->observable(true); |
MarceloSalazar | 2:6ed27f413b30 | 131 | button->attach_notification(M2MMethod::GET, (void*)button_callback); |
MarceloSalazar | 0:e13a8a944e25 | 132 | |
MarceloSalazar | 1:1ccf36276cd3 | 133 | MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 134 | pattern->set_value("500:500:500:500"); |
MarceloSalazar | 1:1ccf36276cd3 | 135 | pattern->methods(M2MMethod::GET | M2MMethod::PUT); |
MarceloSalazar | 1:1ccf36276cd3 | 136 | pattern->attach(M2MMethod::PUT, (void*)pattern_updated); |
MarceloSalazar | 1:1ccf36276cd3 | 137 | pattern_ptr = pattern; |
MarceloSalazar | 0:e13a8a944e25 | 138 | |
MarceloSalazar | 1:1ccf36276cd3 | 139 | MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 140 | blink->methods(M2MMethod::POST); |
MarceloSalazar | 1:1ccf36276cd3 | 141 | blink->attach(M2MMethod::POST, (void*)blink_callback); |
MarceloSalazar | 0:e13a8a944e25 | 142 | |
MarceloSalazar | 0:e13a8a944e25 | 143 | mbedClient.register_and_connect(); |
MarceloSalazar | 0:e13a8a944e25 | 144 | |
MarceloSalazar | 0:e13a8a944e25 | 145 | // Check if client is registering or registered, if true sleep and repeat. |
MarceloSalazar | 0:e13a8a944e25 | 146 | while (mbedClient.is_register_called()) { |
MarceloSalazar | 0:e13a8a944e25 | 147 | static int button_count = 0; |
MarceloSalazar | 1:1ccf36276cd3 | 148 | wait_ms(100); |
MarceloSalazar | 1:1ccf36276cd3 | 149 | |
MarceloSalazar | 1:1ccf36276cd3 | 150 | if (button_pressed) { |
MarceloSalazar | 1:1ccf36276cd3 | 151 | button_pressed = false; |
MarceloSalazar | 1:1ccf36276cd3 | 152 | printf("Button clicked %d times\r\n", ++button_count); |
MarceloSalazar | 1:1ccf36276cd3 | 153 | button->set_value(button_count); |
MarceloSalazar | 0:e13a8a944e25 | 154 | } |
MarceloSalazar | 0:e13a8a944e25 | 155 | } |
MarceloSalazar | 0:e13a8a944e25 | 156 | |
MarceloSalazar | 0:e13a8a944e25 | 157 | // Client unregistered, exit program. |
MarceloSalazar | 0:e13a8a944e25 | 158 | return 0; |
MarceloSalazar | 0:e13a8a944e25 | 159 | } |