Mbed Cloud Example Project - LPC546xx (Completed Version)
Fork of mbed-cloud-example-lpc546xx by
main.cpp@5:e521ea1f4c22, 2018-10-11 (annotated)
- Committer:
- clarkjarvis
- Date:
- Thu Oct 11 14:29:07 2018 +0000
- Revision:
- 5:e521ea1f4c22
- Parent:
- 4:5994ee6e8f63
- Child:
- 8:99d61dd3bfb9
Cleaned Starting Version
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; |
clarkjarvis | 4:5994ee6e8f63 | 31 | InterruptIn sw2(SW2); |
clarkjarvis | 4:5994ee6e8f63 | 32 | DigitalOut led(LED1,1); |
maclobdell | 0:1f42bb53bff5 | 33 | |
maclobdell | 0:1f42bb53bff5 | 34 | // Placeholder for storage |
maclobdell | 0:1f42bb53bff5 | 35 | SDBlockDevice sd(D11, D12, D13, D10); //MOSI, MISO, SCLK, CS |
maclobdell | 0:1f42bb53bff5 | 36 | FATFileSystem fs("sd"); |
maclobdell | 0:1f42bb53bff5 | 37 | |
clarkjarvis | 5:e521ea1f4c22 | 38 | // To-Do: Add global pointer declaration for |
maclobdell | 0:1f42bb53bff5 | 39 | |
maclobdell | 0:1f42bb53bff5 | 40 | static bool button_pressed = false; |
maclobdell | 0:1f42bb53bff5 | 41 | void button_press() { |
maclobdell | 0:1f42bb53bff5 | 42 | button_pressed = true; |
maclobdell | 0:1f42bb53bff5 | 43 | } |
maclobdell | 0:1f42bb53bff5 | 44 | |
clarkjarvis | 4:5994ee6e8f63 | 45 | void led_toggle() { |
clarkjarvis | 4:5994ee6e8f63 | 46 | led = !led; |
clarkjarvis | 4:5994ee6e8f63 | 47 | } |
clarkjarvis | 4:5994ee6e8f63 | 48 | |
clarkjarvis | 5:e521ea1f4c22 | 49 | // To Do: Add Cloud Client Resource Callback Functions |
clarkjarvis | 4:5994ee6e8f63 | 50 | |
maclobdell | 0:1f42bb53bff5 | 51 | |
maclobdell | 0:1f42bb53bff5 | 52 | |
maclobdell | 0:1f42bb53bff5 | 53 | int main(void) |
maclobdell | 0:1f42bb53bff5 | 54 | { |
maclobdell | 0:1f42bb53bff5 | 55 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
maclobdell | 0:1f42bb53bff5 | 56 | // Older versions: workaround to prevent possible deletion of credentials: |
maclobdell | 0:1f42bb53bff5 | 57 | wait(2); |
maclobdell | 0:1f42bb53bff5 | 58 | |
maclobdell | 0:1f42bb53bff5 | 59 | // Misc OS setup |
maclobdell | 0:1f42bb53bff5 | 60 | srand(time(NULL)); |
maclobdell | 0:1f42bb53bff5 | 61 | |
maclobdell | 0:1f42bb53bff5 | 62 | // Placeholder for network |
maclobdell | 0:1f42bb53bff5 | 63 | EthernetInterface net; |
maclobdell | 0:1f42bb53bff5 | 64 | |
maclobdell | 0:1f42bb53bff5 | 65 | printf("Start Simple Mbed Cloud Client\n"); |
maclobdell | 0:1f42bb53bff5 | 66 | |
maclobdell | 0:1f42bb53bff5 | 67 | // Initialize SD card |
maclobdell | 0:1f42bb53bff5 | 68 | int status = sd.init(); |
maclobdell | 0:1f42bb53bff5 | 69 | if (status != BD_ERROR_OK) { |
maclobdell | 0:1f42bb53bff5 | 70 | printf("Failed to init SD card\r\n"); |
maclobdell | 0:1f42bb53bff5 | 71 | return -1; |
maclobdell | 0:1f42bb53bff5 | 72 | } |
maclobdell | 0:1f42bb53bff5 | 73 | |
maclobdell | 0:1f42bb53bff5 | 74 | // Mount the file system (reformatting on failure) |
maclobdell | 0:1f42bb53bff5 | 75 | status = fs.mount(&sd); |
maclobdell | 0:1f42bb53bff5 | 76 | if (status) { |
maclobdell | 0:1f42bb53bff5 | 77 | printf("Failed to mount FAT file system, reformatting...\r\n"); |
maclobdell | 0:1f42bb53bff5 | 78 | status = fs.reformat(&sd); |
maclobdell | 0:1f42bb53bff5 | 79 | if (status) { |
maclobdell | 0:1f42bb53bff5 | 80 | printf("Failed to reformat FAT file system\r\n"); |
maclobdell | 0:1f42bb53bff5 | 81 | return -1; |
maclobdell | 0:1f42bb53bff5 | 82 | } else { |
maclobdell | 0:1f42bb53bff5 | 83 | printf("Reformat and mount complete\r\n"); |
maclobdell | 0:1f42bb53bff5 | 84 | } |
maclobdell | 0:1f42bb53bff5 | 85 | } |
maclobdell | 0:1f42bb53bff5 | 86 | |
clarkjarvis | 4:5994ee6e8f63 | 87 | // Connect to Network (obtain IP address) |
maclobdell | 0:1f42bb53bff5 | 88 | printf("Connecting to the network using Ethernet...\n"); |
maclobdell | 0:1f42bb53bff5 | 89 | status = net.connect(); |
maclobdell | 0:1f42bb53bff5 | 90 | if (status) { |
maclobdell | 0:1f42bb53bff5 | 91 | printf("Connection to Network Failed %d!\n", status); |
maclobdell | 0:1f42bb53bff5 | 92 | return -1; |
maclobdell | 0:1f42bb53bff5 | 93 | } else { |
maclobdell | 0:1f42bb53bff5 | 94 | const char *ip_addr = net.get_ip_address(); |
maclobdell | 0:1f42bb53bff5 | 95 | printf("Connected successfully\n"); |
maclobdell | 0:1f42bb53bff5 | 96 | printf("IP address %s\n", ip_addr); |
maclobdell | 0:1f42bb53bff5 | 97 | } |
maclobdell | 0:1f42bb53bff5 | 98 | |
clarkjarvis | 4:5994ee6e8f63 | 99 | // Mbed Cloud Client Initialization |
maclobdell | 0:1f42bb53bff5 | 100 | SimpleMbedCloudClient mbedClient(&net); |
maclobdell | 0:1f42bb53bff5 | 101 | status = mbedClient.init(); |
maclobdell | 0:1f42bb53bff5 | 102 | if (status) { |
maclobdell | 0:1f42bb53bff5 | 103 | return -1; |
maclobdell | 0:1f42bb53bff5 | 104 | } |
maclobdell | 0:1f42bb53bff5 | 105 | printf("Client initialized\r\n"); |
maclobdell | 0:1f42bb53bff5 | 106 | |
clarkjarvis | 5:e521ea1f4c22 | 107 | // To Do: Add Mbed Cloud Client resources |
maclobdell | 0:1f42bb53bff5 | 108 | |
maclobdell | 0:1f42bb53bff5 | 109 | |
maclobdell | 0:1f42bb53bff5 | 110 | |
clarkjarvis | 4:5994ee6e8f63 | 111 | // Mbed Cloud Register Client and Connect |
maclobdell | 0:1f42bb53bff5 | 112 | mbedClient.register_and_connect(); |
maclobdell | 0:1f42bb53bff5 | 113 | |
maclobdell | 0:1f42bb53bff5 | 114 | // Wait for client to finish registering |
maclobdell | 0:1f42bb53bff5 | 115 | while (!mbedClient.is_client_registered()) { |
maclobdell | 0:1f42bb53bff5 | 116 | wait_ms(100); |
maclobdell | 0:1f42bb53bff5 | 117 | } |
maclobdell | 0:1f42bb53bff5 | 118 | |
clarkjarvis | 4:5994ee6e8f63 | 119 | // Setup LED and Push BUtton |
clarkjarvis | 4:5994ee6e8f63 | 120 | timer.attach(&led_toggle, 0.5); |
clarkjarvis | 4:5994ee6e8f63 | 121 | sw2.fall(&button_press); |
clarkjarvis | 4:5994ee6e8f63 | 122 | |
maclobdell | 0:1f42bb53bff5 | 123 | // Check if client is registering or registered, if true sleep and repeat. |
maclobdell | 0:1f42bb53bff5 | 124 | while (mbedClient.is_register_called()) { |
maclobdell | 0:1f42bb53bff5 | 125 | static int button_count = 0; |
maclobdell | 0:1f42bb53bff5 | 126 | wait_ms(100); |
maclobdell | 0:1f42bb53bff5 | 127 | |
maclobdell | 0:1f42bb53bff5 | 128 | if (button_pressed) { |
maclobdell | 0:1f42bb53bff5 | 129 | button_pressed = false; |
clarkjarvis | 4:5994ee6e8f63 | 130 | printf("Button Pressed %d time(s).\r\n",(++button_count)); |
clarkjarvis | 5:e521ea1f4c22 | 131 | // To Do: Add function to set button resource value |
maclobdell | 0:1f42bb53bff5 | 132 | } |
maclobdell | 0:1f42bb53bff5 | 133 | } |
maclobdell | 0:1f42bb53bff5 | 134 | |
maclobdell | 0:1f42bb53bff5 | 135 | // Client unregistered, exit program. |
maclobdell | 0:1f42bb53bff5 | 136 | return 0; |
maclobdell | 0:1f42bb53bff5 | 137 | } |