Mbed Cloud Example Project - LPC546xx (Completed Version)

Fork of mbed-cloud-example-lpc546xx by Clark Jarvis

Committer:
clarkjarvis
Date:
Thu Oct 11 04:05:03 2018 +0000
Revision:
4:5994ee6e8f63
Parent:
0:1f42bb53bff5
Child:
5:e521ea1f4c22
Fork of Mbed Cloud Example for LPC546xx - Arm TechCon 2018

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
maclobdell 0:1f42bb53bff5 37 FATFileSystem fs("sd");
maclobdell 0:1f42bb53bff5 38
maclobdell 0:1f42bb53bff5 39 // Pointers to the resources that will be created in main_application().
maclobdell 0:1f42bb53bff5 40 static MbedCloudClientResource* pattern_ptr;
maclobdell 0:1f42bb53bff5 41
maclobdell 0:1f42bb53bff5 42 static bool button_pressed = false;
maclobdell 0:1f42bb53bff5 43 void button_press() {
maclobdell 0:1f42bb53bff5 44 button_pressed = true;
maclobdell 0:1f42bb53bff5 45 }
maclobdell 0:1f42bb53bff5 46
clarkjarvis 4:5994ee6e8f63 47 void led_toggle() {
clarkjarvis 4:5994ee6e8f63 48 led = !led;
clarkjarvis 4:5994ee6e8f63 49 }
clarkjarvis 4:5994ee6e8f63 50
clarkjarvis 4:5994ee6e8f63 51 void blink_rate_updated(const char *) {
clarkjarvis 4:5994ee6e8f63 52 printf("PUT received, Storing LED Blink Rate: %s (ms)\r\n", pattern_ptr->get_value().c_str());
maclobdell 0:1f42bb53bff5 53 }
maclobdell 0:1f42bb53bff5 54
clarkjarvis 4:5994ee6e8f63 55 void blink_enable_callback(void *) {
maclobdell 0:1f42bb53bff5 56 String pattern_str = pattern_ptr->get_value();
maclobdell 0:1f42bb53bff5 57 const char *pattern = pattern_str.c_str();
clarkjarvis 4:5994ee6e8f63 58 printf("POST received. Enabling LED Blink Rate = %s (ms)\n", pattern);
clarkjarvis 4:5994ee6e8f63 59
clarkjarvis 4:5994ee6e8f63 60 float value = atoi(pattern_ptr->get_value().c_str())/1000.0;
clarkjarvis 4:5994ee6e8f63 61 timer.detach();
clarkjarvis 4:5994ee6e8f63 62 timer.attach(&led_toggle,value);
maclobdell 0:1f42bb53bff5 63 }
maclobdell 0:1f42bb53bff5 64
maclobdell 0:1f42bb53bff5 65 void button_callback(const M2MBase& object, const NoticationDeliveryStatus status)
maclobdell 0:1f42bb53bff5 66 {
maclobdell 0:1f42bb53bff5 67 printf("Button notification. Callback: (%s)\n", object.uri_path());
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
clarkjarvis 4:5994ee6e8f63 104 // Connect to Network (obtain IP address)
maclobdell 0:1f42bb53bff5 105 printf("Connecting to the network using Ethernet...\n");
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
clarkjarvis 4:5994ee6e8f63 116 // Mbed Cloud Client Initialization
maclobdell 0:1f42bb53bff5 117 SimpleMbedCloudClient mbedClient(&net);
maclobdell 0:1f42bb53bff5 118 status = mbedClient.init();
maclobdell 0:1f42bb53bff5 119 if (status) {
maclobdell 0:1f42bb53bff5 120 return -1;
maclobdell 0:1f42bb53bff5 121 }
maclobdell 0:1f42bb53bff5 122 printf("Client initialized\r\n");
maclobdell 0:1f42bb53bff5 123
maclobdell 0:1f42bb53bff5 124 // Mbed Cloud Client resource setup
maclobdell 0:1f42bb53bff5 125 MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource");
maclobdell 0:1f42bb53bff5 126 button->set_value("0");
maclobdell 0:1f42bb53bff5 127 button->methods(M2MMethod::GET);
maclobdell 0:1f42bb53bff5 128 button->observable(true);
maclobdell 0:1f42bb53bff5 129 button->attach_notification_callback(button_callback);
maclobdell 0:1f42bb53bff5 130
clarkjarvis 4:5994ee6e8f63 131 MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "blink_rate_resource");
clarkjarvis 4:5994ee6e8f63 132 pattern->set_value("500");
maclobdell 0:1f42bb53bff5 133 pattern->methods(M2MMethod::GET | M2MMethod::PUT);
clarkjarvis 4:5994ee6e8f63 134 pattern->observable(false);
clarkjarvis 4:5994ee6e8f63 135 pattern->attach_put_callback(blink_rate_updated);
maclobdell 0:1f42bb53bff5 136 pattern_ptr = pattern;
maclobdell 0:1f42bb53bff5 137
clarkjarvis 4:5994ee6e8f63 138 MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_enable_resource");
maclobdell 0:1f42bb53bff5 139 blink->methods(M2MMethod::POST);
clarkjarvis 4:5994ee6e8f63 140 blink->attach_post_callback(blink_enable_callback);
maclobdell 0:1f42bb53bff5 141
clarkjarvis 4:5994ee6e8f63 142 // Mbed Cloud Register Client and Connect
maclobdell 0:1f42bb53bff5 143 mbedClient.register_and_connect();
maclobdell 0:1f42bb53bff5 144
maclobdell 0:1f42bb53bff5 145 // Wait for client to finish registering
maclobdell 0:1f42bb53bff5 146 while (!mbedClient.is_client_registered()) {
maclobdell 0:1f42bb53bff5 147 wait_ms(100);
maclobdell 0:1f42bb53bff5 148 }
maclobdell 0:1f42bb53bff5 149
clarkjarvis 4:5994ee6e8f63 150 // Setup LED and Push BUtton
clarkjarvis 4:5994ee6e8f63 151 timer.attach(&led_toggle, 0.5);
clarkjarvis 4:5994ee6e8f63 152 sw2.fall(&button_press);
clarkjarvis 4:5994ee6e8f63 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;
clarkjarvis 4:5994ee6e8f63 161 printf("Button Pressed %d time(s).\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 }