Mbed Cloud Example Project - LPC546xx (Starting Version)

Fork of mbed-cloud-example-lpc546xx by Mac Lobdell

Committer:
clarkjarvis
Date:
Thu Oct 11 18:52:54 2018 +0000
Revision:
10:5b42f7323c71
Parent:
9:5836af0b0a9c
Reverted generated file

Who changed what in which revision?

UserRevisionLine numberNew 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 8:99d61dd3bfb9 38 // To-Do #2: Add global pointer declaration for Rate Resource
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 8:99d61dd3bfb9 49 // To-Do #3: Add Cloud Client Resource Callback Functions
clarkjarvis 4:5994ee6e8f63 50
maclobdell 0:1f42bb53bff5 51
maclobdell 0:1f42bb53bff5 52 int main(void)
maclobdell 0:1f42bb53bff5 53 {
maclobdell 0:1f42bb53bff5 54 // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364)
maclobdell 0:1f42bb53bff5 55 // Older versions: workaround to prevent possible deletion of credentials:
maclobdell 0:1f42bb53bff5 56 wait(2);
maclobdell 0:1f42bb53bff5 57
maclobdell 0:1f42bb53bff5 58 // Misc OS setup
maclobdell 0:1f42bb53bff5 59 srand(time(NULL));
maclobdell 0:1f42bb53bff5 60
maclobdell 0:1f42bb53bff5 61 // Placeholder for network
maclobdell 0:1f42bb53bff5 62 EthernetInterface net;
maclobdell 0:1f42bb53bff5 63
maclobdell 0:1f42bb53bff5 64 printf("Start Simple Mbed Cloud Client\n");
maclobdell 0:1f42bb53bff5 65
maclobdell 0:1f42bb53bff5 66 // Initialize SD card
maclobdell 0:1f42bb53bff5 67 int status = sd.init();
maclobdell 0:1f42bb53bff5 68 if (status != BD_ERROR_OK) {
maclobdell 0:1f42bb53bff5 69 printf("Failed to init SD card\r\n");
maclobdell 0:1f42bb53bff5 70 return -1;
maclobdell 0:1f42bb53bff5 71 }
maclobdell 0:1f42bb53bff5 72
maclobdell 0:1f42bb53bff5 73 // Mount the file system (reformatting on failure)
maclobdell 0:1f42bb53bff5 74 status = fs.mount(&sd);
maclobdell 0:1f42bb53bff5 75 if (status) {
maclobdell 0:1f42bb53bff5 76 printf("Failed to mount FAT file system, reformatting...\r\n");
maclobdell 0:1f42bb53bff5 77 status = fs.reformat(&sd);
maclobdell 0:1f42bb53bff5 78 if (status) {
maclobdell 0:1f42bb53bff5 79 printf("Failed to reformat FAT file system\r\n");
maclobdell 0:1f42bb53bff5 80 return -1;
maclobdell 0:1f42bb53bff5 81 } else {
maclobdell 0:1f42bb53bff5 82 printf("Reformat and mount complete\r\n");
maclobdell 0:1f42bb53bff5 83 }
maclobdell 0:1f42bb53bff5 84 }
maclobdell 0:1f42bb53bff5 85
clarkjarvis 4:5994ee6e8f63 86 // Connect to Network (obtain IP address)
maclobdell 0:1f42bb53bff5 87 printf("Connecting to the network using Ethernet...\n");
maclobdell 0:1f42bb53bff5 88 status = net.connect();
maclobdell 0:1f42bb53bff5 89 if (status) {
maclobdell 0:1f42bb53bff5 90 printf("Connection to Network Failed %d!\n", status);
maclobdell 0:1f42bb53bff5 91 return -1;
maclobdell 0:1f42bb53bff5 92 } else {
maclobdell 0:1f42bb53bff5 93 const char *ip_addr = net.get_ip_address();
maclobdell 0:1f42bb53bff5 94 printf("Connected successfully\n");
maclobdell 0:1f42bb53bff5 95 printf("IP address %s\n", ip_addr);
maclobdell 0:1f42bb53bff5 96 }
maclobdell 0:1f42bb53bff5 97
clarkjarvis 4:5994ee6e8f63 98 // Mbed Cloud Client Initialization
maclobdell 0:1f42bb53bff5 99 SimpleMbedCloudClient mbedClient(&net);
maclobdell 0:1f42bb53bff5 100 status = mbedClient.init();
maclobdell 0:1f42bb53bff5 101 if (status) {
maclobdell 0:1f42bb53bff5 102 return -1;
maclobdell 0:1f42bb53bff5 103 }
maclobdell 0:1f42bb53bff5 104 printf("Client initialized\r\n");
maclobdell 0:1f42bb53bff5 105
clarkjarvis 8:99d61dd3bfb9 106 // To-Do #1: Add Mbed Cloud Client resources
maclobdell 0:1f42bb53bff5 107
maclobdell 0:1f42bb53bff5 108
clarkjarvis 4:5994ee6e8f63 109 // Mbed Cloud Register Client and Connect
maclobdell 0:1f42bb53bff5 110 mbedClient.register_and_connect();
maclobdell 0:1f42bb53bff5 111
maclobdell 0:1f42bb53bff5 112 // Wait for client to finish registering
maclobdell 0:1f42bb53bff5 113 while (!mbedClient.is_client_registered()) {
maclobdell 0:1f42bb53bff5 114 wait_ms(100);
maclobdell 0:1f42bb53bff5 115 }
maclobdell 0:1f42bb53bff5 116
clarkjarvis 4:5994ee6e8f63 117 // Setup LED and Push BUtton
clarkjarvis 4:5994ee6e8f63 118 timer.attach(&led_toggle, 0.5);
clarkjarvis 4:5994ee6e8f63 119 sw2.fall(&button_press);
clarkjarvis 9:5836af0b0a9c 120
maclobdell 0:1f42bb53bff5 121 // Check if client is registering or registered, if true sleep and repeat.
maclobdell 0:1f42bb53bff5 122 while (mbedClient.is_register_called()) {
maclobdell 0:1f42bb53bff5 123 static int button_count = 0;
maclobdell 0:1f42bb53bff5 124 wait_ms(100);
clarkjarvis 9:5836af0b0a9c 125
maclobdell 0:1f42bb53bff5 126 if (button_pressed) {
maclobdell 0:1f42bb53bff5 127 button_pressed = false;
clarkjarvis 4:5994ee6e8f63 128 printf("Button Pressed %d time(s).\r\n",(++button_count));
clarkjarvis 8:99d61dd3bfb9 129 // To-Do #4: Add function to set button resource value
clarkjarvis 9:5836af0b0a9c 130
maclobdell 0:1f42bb53bff5 131 }
maclobdell 0:1f42bb53bff5 132 }
maclobdell 0:1f42bb53bff5 133
maclobdell 0:1f42bb53bff5 134 // Client unregistered, exit program.
maclobdell 0:1f42bb53bff5 135 return 0;
maclobdell 0:1f42bb53bff5 136 }