Pelion workshop with Grove sensors.

Committer:
maclobdell
Date:
Mon Feb 19 20:37:36 2018 +0000
Revision:
8:ace9e5de6491
Parent:
6:254a7e7fbef1
Child:
11:ae1f6fe932dc
customized template application to use button and led on K64 and K66.

Who changed what in which revision?

UserRevisionLine numberNew 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
maclobdell 8:ace9e5de6491 29 /* The following app uses Mbed Cloud with SD Card storage, button, & led */
maclobdell 8:ace9e5de6491 30
MarceloSalazar 4:a107dae867fb 31 // Placeholder to hardware that trigger events (timer, button, etc)
maclobdell 8:ace9e5de6491 32 //Ticker timer;
maclobdell 8:ace9e5de6491 33 /* K64 & K66 */
maclobdell 8:ace9e5de6491 34 InterruptIn sw2(SW2);
maclobdell 8:ace9e5de6491 35 DigitalOut led2(LED2);
maclobdell 8:ace9e5de6491 36 /* */
MarceloSalazar 0:e13a8a944e25 37
bridadan 5:c18fab181ede 38 // Placeholder for storage
maclobdell 8:ace9e5de6491 39 /* K64 & K66 */
bridadan 5:c18fab181ede 40 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
bridadan 5:c18fab181ede 41 FATFileSystem fs("sd");
maclobdell 8:ace9e5de6491 42 /* */
maclobdell 8:ace9e5de6491 43
MarceloSalazar 0:e13a8a944e25 44 // Pointers to the resources that will be created in main_application().
MarceloSalazar 1:1ccf36276cd3 45 static MbedCloudClientResource* pattern_ptr;
maclobdell 8:ace9e5de6491 46 static MbedCloudClientResource* button_ptr;
MarceloSalazar 0:e13a8a944e25 47
MarceloSalazar 0:e13a8a944e25 48 // Pointer to mbedClient, used for calling close function.
MarceloSalazar 0:e13a8a944e25 49 static SimpleMbedCloudClient *client;
MarceloSalazar 0:e13a8a944e25 50
MarceloSalazar 1:1ccf36276cd3 51 static bool button_pressed = false;
maclobdell 8:ace9e5de6491 52 static int button_count = 0;
maclobdell 8:ace9e5de6491 53
MarceloSalazar 1:1ccf36276cd3 54 void button_press() {
MarceloSalazar 1:1ccf36276cd3 55 button_pressed = true;
maclobdell 8:ace9e5de6491 56 ++button_count;
maclobdell 8:ace9e5de6491 57 button_ptr->set_value(button_count);
MarceloSalazar 1:1ccf36276cd3 58 }
MarceloSalazar 1:1ccf36276cd3 59
MarceloSalazar 1:1ccf36276cd3 60 void pattern_updated(const char *) {
bridadan 5:c18fab181ede 61 printf("PUT received, new value: %s\n", pattern_ptr->get_value().c_str());
MarceloSalazar 2:6ed27f413b30 62 // Placeholder for PUT action
MarceloSalazar 0:e13a8a944e25 63 }
MarceloSalazar 0:e13a8a944e25 64
MarceloSalazar 0:e13a8a944e25 65 void blink_callback(void *) {
bridadan 5:c18fab181ede 66 String pattern_str = pattern_ptr->get_value();
bridadan 5:c18fab181ede 67 const char *pattern = pattern_str.c_str();
MarceloSalazar 2:6ed27f413b30 68 printf("POST received. LED pattern = %s\n", pattern);
MarceloSalazar 2:6ed27f413b30 69 // Placeholder for POST action
MarceloSalazar 0:e13a8a944e25 70 // The pattern is something like 500:200:500, so parse that.
maclobdell 8:ace9e5de6491 71 // LED blinking is done while parsing.
maclobdell 8:ace9e5de6491 72
maclobdell 8:ace9e5de6491 73 while (*pattern != '\0') {
maclobdell 8:ace9e5de6491 74 //make a short blink on the led
maclobdell 8:ace9e5de6491 75 led2 = 0;
maclobdell 8:ace9e5de6491 76 wait_ms(20);
maclobdell 8:ace9e5de6491 77 led2 = 1;
maclobdell 8:ace9e5de6491 78 // Wait for requested time.
maclobdell 8:ace9e5de6491 79 wait_ms(atoi(pattern));
maclobdell 8:ace9e5de6491 80 // Search for next value.
maclobdell 8:ace9e5de6491 81 pattern = strchr(pattern, ':');
maclobdell 8:ace9e5de6491 82 if(!pattern) {
maclobdell 8:ace9e5de6491 83 //we're done, give one last blink to end the pattern
maclobdell 8:ace9e5de6491 84 led2 = 0;
maclobdell 8:ace9e5de6491 85 wait_ms(20);
maclobdell 8:ace9e5de6491 86 led2 = 1;
maclobdell 8:ace9e5de6491 87 break; // while
maclobdell 8:ace9e5de6491 88 }
maclobdell 8:ace9e5de6491 89 pattern++;
maclobdell 8:ace9e5de6491 90 }
MarceloSalazar 0:e13a8a944e25 91 }
MarceloSalazar 0:e13a8a944e25 92
MarceloSalazar 2:6ed27f413b30 93 void button_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MarceloSalazar 0:e13a8a944e25 94 {
MarceloSalazar 2:6ed27f413b30 95 printf("Button notification. Callback: (%s)\n", object.uri_path());
MarceloSalazar 2:6ed27f413b30 96 // Placeholder for GET
MarceloSalazar 0:e13a8a944e25 97 }
MarceloSalazar 0:e13a8a944e25 98
MarceloSalazar 0:e13a8a944e25 99
MarceloSalazar 0:e13a8a944e25 100 int main(void)
MarceloSalazar 0:e13a8a944e25 101 {
MarceloSalazar 2:6ed27f413b30 102 // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364)
MarceloSalazar 2:6ed27f413b30 103 // Older versions: workaround to prevent possible deletion of credentials:
MarceloSalazar 0:e13a8a944e25 104 wait(2);
MarceloSalazar 0:e13a8a944e25 105
MarceloSalazar 1:1ccf36276cd3 106 // Misc OS setup
MarceloSalazar 1:1ccf36276cd3 107 srand(time(NULL));
MarceloSalazar 1:1ccf36276cd3 108
MarceloSalazar 2:6ed27f413b30 109 // Placeholder for network
MarceloSalazar 1:1ccf36276cd3 110 EthernetInterface net;
MarceloSalazar 2:6ed27f413b30 111
MarceloSalazar 1:1ccf36276cd3 112 printf("Start Simple Mbed Cloud Client\n");
MarceloSalazar 1:1ccf36276cd3 113
MarceloSalazar 1:1ccf36276cd3 114 // Initialize SD card
MarceloSalazar 1:1ccf36276cd3 115 int status = sd.init();
MarceloSalazar 1:1ccf36276cd3 116 if (status != BD_ERROR_OK) {
MarceloSalazar 1:1ccf36276cd3 117 printf("Failed to init SD card\r\n");
MarceloSalazar 1:1ccf36276cd3 118 return -1;
MarceloSalazar 1:1ccf36276cd3 119 }
MarceloSalazar 0:e13a8a944e25 120
MarceloSalazar 1:1ccf36276cd3 121 // Mount the file system (reformatting on failure)
MarceloSalazar 1:1ccf36276cd3 122 status = fs.mount(&sd);
MarceloSalazar 1:1ccf36276cd3 123 if (status) {
MarceloSalazar 1:1ccf36276cd3 124 printf("Failed to mount FAT file system, reformatting...\r\n");
MarceloSalazar 1:1ccf36276cd3 125 status = fs.reformat(&sd);
MarceloSalazar 1:1ccf36276cd3 126 if (status) {
MarceloSalazar 1:1ccf36276cd3 127 printf("Failed to reformat FAT file system\r\n");
MarceloSalazar 1:1ccf36276cd3 128 return -1;
MarceloSalazar 1:1ccf36276cd3 129 } else {
MarceloSalazar 1:1ccf36276cd3 130 printf("Reformat and mount complete\r\n");
MarceloSalazar 1:1ccf36276cd3 131 }
MarceloSalazar 1:1ccf36276cd3 132 }
MarceloSalazar 1:1ccf36276cd3 133
MarceloSalazar 1:1ccf36276cd3 134 printf("Connecting to the network using Ethernet...\n");
MarceloSalazar 1:1ccf36276cd3 135
MarceloSalazar 1:1ccf36276cd3 136 status = net.connect();
MarceloSalazar 1:1ccf36276cd3 137 if (status) {
MarceloSalazar 1:1ccf36276cd3 138 printf("Connection to Network Failed %d!\n", status);
MarceloSalazar 1:1ccf36276cd3 139 return -1;
MarceloSalazar 1:1ccf36276cd3 140 } else {
MarceloSalazar 1:1ccf36276cd3 141 const char *ip_addr = net.get_ip_address();
MarceloSalazar 1:1ccf36276cd3 142 printf("Connected successfully\n");
MarceloSalazar 1:1ccf36276cd3 143 printf("IP address %s\n", ip_addr);
MarceloSalazar 1:1ccf36276cd3 144 }
MarceloSalazar 1:1ccf36276cd3 145
MarceloSalazar 1:1ccf36276cd3 146 SimpleMbedCloudClient mbedClient(&net);
MarceloSalazar 0:e13a8a944e25 147 // Save pointer to mbedClient so that other functions can access it.
MarceloSalazar 0:e13a8a944e25 148 client = &mbedClient;
MarceloSalazar 0:e13a8a944e25 149
MarceloSalazar 1:1ccf36276cd3 150 status = mbedClient.init();
MarceloSalazar 1:1ccf36276cd3 151 if (status) {
MarceloSalazar 1:1ccf36276cd3 152 return -1;
MarceloSalazar 1:1ccf36276cd3 153 }
MarceloSalazar 1:1ccf36276cd3 154
MarceloSalazar 0:e13a8a944e25 155 printf("Client initialized\r\n");
MarceloSalazar 0:e13a8a944e25 156
MarceloSalazar 1:1ccf36276cd3 157 // Mbed Cloud Client resource setup
MarceloSalazar 1:1ccf36276cd3 158 MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource");
MarceloSalazar 1:1ccf36276cd3 159 button->set_value("0");
MarceloSalazar 1:1ccf36276cd3 160 button->methods(M2MMethod::GET);
MarceloSalazar 1:1ccf36276cd3 161 button->observable(true);
bridadan 5:c18fab181ede 162 button->attach_notification_callback(button_callback);
maclobdell 8:ace9e5de6491 163 button_ptr = button;
maclobdell 8:ace9e5de6491 164
MarceloSalazar 1:1ccf36276cd3 165 MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource");
MarceloSalazar 1:1ccf36276cd3 166 pattern->set_value("500:500:500:500");
MarceloSalazar 1:1ccf36276cd3 167 pattern->methods(M2MMethod::GET | M2MMethod::PUT);
bridadan 5:c18fab181ede 168 pattern->attach_put_callback(pattern_updated);
MarceloSalazar 1:1ccf36276cd3 169 pattern_ptr = pattern;
MarceloSalazar 0:e13a8a944e25 170
MarceloSalazar 1:1ccf36276cd3 171 MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource");
MarceloSalazar 1:1ccf36276cd3 172 blink->methods(M2MMethod::POST);
bridadan 5:c18fab181ede 173 blink->attach_post_callback(blink_callback);
MarceloSalazar 0:e13a8a944e25 174
MarceloSalazar 0:e13a8a944e25 175 mbedClient.register_and_connect();
MarceloSalazar 0:e13a8a944e25 176
bridadan 6:254a7e7fbef1 177 // Wait for client to finish registering
bridadan 6:254a7e7fbef1 178 while (!mbedClient.is_client_registered()) {
bridadan 6:254a7e7fbef1 179 wait_ms(100);
bridadan 6:254a7e7fbef1 180 }
bridadan 6:254a7e7fbef1 181
bridadan 6:254a7e7fbef1 182 // Placeholder for callback to update local resource when GET comes.
maclobdell 8:ace9e5de6491 183 //timer.attach(&button_press, 5.0);
maclobdell 8:ace9e5de6491 184 sw2.mode(PullUp);
maclobdell 8:ace9e5de6491 185 sw2.fall(button_press);
maclobdell 8:ace9e5de6491 186 button_count = 0;
maclobdell 8:ace9e5de6491 187
MarceloSalazar 0:e13a8a944e25 188 // Check if client is registering or registered, if true sleep and repeat.
MarceloSalazar 0:e13a8a944e25 189 while (mbedClient.is_register_called()) {
maclobdell 8:ace9e5de6491 190 //static int button_count = 0;
maclobdell 8:ace9e5de6491 191
MarceloSalazar 1:1ccf36276cd3 192 wait_ms(100);
MarceloSalazar 1:1ccf36276cd3 193
MarceloSalazar 1:1ccf36276cd3 194 if (button_pressed) {
MarceloSalazar 1:1ccf36276cd3 195 button_pressed = false;
maclobdell 8:ace9e5de6491 196 //printf("button clicked %d times\r\n", ++button_count);
maclobdell 8:ace9e5de6491 197 //button->set_value(button_count);
maclobdell 8:ace9e5de6491 198 printf("button clicked %d times\r\n", button_count);
MarceloSalazar 0:e13a8a944e25 199 }
maclobdell 8:ace9e5de6491 200
MarceloSalazar 0:e13a8a944e25 201 }
MarceloSalazar 0:e13a8a944e25 202
MarceloSalazar 0:e13a8a944e25 203 // Client unregistered, exit program.
MarceloSalazar 0:e13a8a944e25 204 return 0;
MarceloSalazar 0:e13a8a944e25 205 }