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