Pelion workshop with Grove sensors.

Committer:
MarceloSalazar
Date:
Wed Feb 14 10:43:56 2018 +0000
Revision:
1:1ccf36276cd3
Parent:
0:e13a8a944e25
Child:
2:6ed27f413b30
Simplified version of application

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
MarceloSalazar 1:1ccf36276cd3 29 #define LED_OFF 1
MarceloSalazar 1:1ccf36276cd3 30
MarceloSalazar 1:1ccf36276cd3 31 DigitalOut led(LED_RED, LED_OFF);
MarceloSalazar 1:1ccf36276cd3 32 InterruptIn button(BUTTON1);
MarceloSalazar 0:e13a8a944e25 33
MarceloSalazar 0:e13a8a944e25 34 // Pointers to the resources that will be created in main_application().
MarceloSalazar 1:1ccf36276cd3 35 static MbedCloudClientResource* pattern_ptr;
MarceloSalazar 0:e13a8a944e25 36
MarceloSalazar 0:e13a8a944e25 37 // Pointer to mbedClient, used for calling close function.
MarceloSalazar 0:e13a8a944e25 38 static SimpleMbedCloudClient *client;
MarceloSalazar 0:e13a8a944e25 39
MarceloSalazar 1:1ccf36276cd3 40 static bool button_pressed = false;
MarceloSalazar 1:1ccf36276cd3 41
MarceloSalazar 1:1ccf36276cd3 42 void button_press() {
MarceloSalazar 1:1ccf36276cd3 43 button_pressed = true;
MarceloSalazar 1:1ccf36276cd3 44 }
MarceloSalazar 1:1ccf36276cd3 45
MarceloSalazar 1:1ccf36276cd3 46 void pattern_updated(const char *) {
MarceloSalazar 1:1ccf36276cd3 47 printf("PUT received, new value: %s\n", pattern_ptr->get_value());
MarceloSalazar 0:e13a8a944e25 48 }
MarceloSalazar 0:e13a8a944e25 49
MarceloSalazar 0:e13a8a944e25 50 void blink_callback(void *) {
MarceloSalazar 1:1ccf36276cd3 51 const char *pattern = pattern_ptr->get_value();
MarceloSalazar 0:e13a8a944e25 52 printf("LED pattern = %s\n", pattern);
MarceloSalazar 0:e13a8a944e25 53 // The pattern is something like 500:200:500, so parse that.
MarceloSalazar 0:e13a8a944e25 54 // LED blinking is done while parsing.
MarceloSalazar 1:1ccf36276cd3 55 led = !led;
MarceloSalazar 0:e13a8a944e25 56 while (*pattern != '\0') {
MarceloSalazar 0:e13a8a944e25 57 // Wait for requested time.
MarceloSalazar 1:1ccf36276cd3 58 wait_ms(atoi(pattern));
MarceloSalazar 1:1ccf36276cd3 59 led = !led;
MarceloSalazar 0:e13a8a944e25 60 // Search for next value.
MarceloSalazar 0:e13a8a944e25 61 pattern = strchr(pattern, ':');
MarceloSalazar 0:e13a8a944e25 62 if(!pattern) {
MarceloSalazar 0:e13a8a944e25 63 break; // while
MarceloSalazar 0:e13a8a944e25 64 }
MarceloSalazar 0:e13a8a944e25 65 pattern++;
MarceloSalazar 0:e13a8a944e25 66 }
MarceloSalazar 1:1ccf36276cd3 67
MarceloSalazar 1:1ccf36276cd3 68 led = LED_OFF;
MarceloSalazar 0:e13a8a944e25 69 }
MarceloSalazar 0:e13a8a944e25 70
MarceloSalazar 0:e13a8a944e25 71 void button_notification_status_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MarceloSalazar 0:e13a8a944e25 72 {
MarceloSalazar 0:e13a8a944e25 73 switch(status) {
MarceloSalazar 0:e13a8a944e25 74 case NOTIFICATION_STATUS_BUILD_ERROR:
MarceloSalazar 0:e13a8a944e25 75 printf("Notification callback: (%s) error when building CoAP message\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 76 break;
MarceloSalazar 0:e13a8a944e25 77 case NOTIFICATION_STATUS_RESEND_QUEUE_FULL:
MarceloSalazar 0:e13a8a944e25 78 printf("Notification callback: (%s) CoAP resend queue full\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 79 break;
MarceloSalazar 0:e13a8a944e25 80 case NOTIFICATION_STATUS_SENT:
MarceloSalazar 0:e13a8a944e25 81 printf("Notification callback: (%s) Notification sent to server\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 82 break;
MarceloSalazar 0:e13a8a944e25 83 case NOTIFICATION_STATUS_DELIVERED:
MarceloSalazar 0:e13a8a944e25 84 printf("Notification callback: (%s) Notification delivered\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 85 break;
MarceloSalazar 0:e13a8a944e25 86 case NOTIFICATION_STATUS_SEND_FAILED:
MarceloSalazar 0:e13a8a944e25 87 printf("Notification callback: (%s) Notification sending failed\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 88 break;
MarceloSalazar 0:e13a8a944e25 89 case NOTIFICATION_STATUS_SUBSCRIBED:
MarceloSalazar 0:e13a8a944e25 90 printf("Notification callback: (%s) subscribed\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 91 break;
MarceloSalazar 0:e13a8a944e25 92 case NOTIFICATION_STATUS_UNSUBSCRIBED:
MarceloSalazar 0:e13a8a944e25 93 printf("Notification callback: (%s) subscription removed\n", object.uri_path());
MarceloSalazar 0:e13a8a944e25 94 break;
MarceloSalazar 0:e13a8a944e25 95 default:
MarceloSalazar 0:e13a8a944e25 96 break;
MarceloSalazar 0:e13a8a944e25 97 }
MarceloSalazar 0:e13a8a944e25 98 }
MarceloSalazar 0:e13a8a944e25 99
MarceloSalazar 0:e13a8a944e25 100 // This function is called when a POST request is received for resource 5000/0/1.
MarceloSalazar 1:1ccf36276cd3 101 void unregister_cb(void *)
MarceloSalazar 0:e13a8a944e25 102 {
MarceloSalazar 0:e13a8a944e25 103 printf("Unregister resource executed\n");
MarceloSalazar 0:e13a8a944e25 104 client->close();
MarceloSalazar 0:e13a8a944e25 105 }
MarceloSalazar 0:e13a8a944e25 106
MarceloSalazar 0:e13a8a944e25 107 // This function is called when a POST request is received for resource 5000/0/2.
MarceloSalazar 1:1ccf36276cd3 108 void factory_reset_cb(void *)
MarceloSalazar 0:e13a8a944e25 109 {
MarceloSalazar 0:e13a8a944e25 110 printf("Factory reset resource executed\n");
MarceloSalazar 0:e13a8a944e25 111 client->close();
MarceloSalazar 0:e13a8a944e25 112 kcm_status_e kcm_status = kcm_factory_reset();
MarceloSalazar 0:e13a8a944e25 113 if (kcm_status != KCM_STATUS_SUCCESS) {
MarceloSalazar 0:e13a8a944e25 114 printf("Failed to do factory reset - %d\n", kcm_status);
MarceloSalazar 0:e13a8a944e25 115 } else {
MarceloSalazar 0:e13a8a944e25 116 printf("Factory reset completed. Now restart the device\n");
MarceloSalazar 0:e13a8a944e25 117 }
MarceloSalazar 0:e13a8a944e25 118 }
MarceloSalazar 0:e13a8a944e25 119
MarceloSalazar 0:e13a8a944e25 120 int main(void)
MarceloSalazar 0:e13a8a944e25 121 {
MarceloSalazar 0:e13a8a944e25 122 // IOTMORF-1712: DAPLINK starts the previous application during flashing a new binary
MarceloSalazar 0:e13a8a944e25 123 // This is workaround to prevent possible deletion of credentials or storage corruption
MarceloSalazar 0:e13a8a944e25 124 // while replacing the application binary.
MarceloSalazar 0:e13a8a944e25 125 wait(2);
MarceloSalazar 0:e13a8a944e25 126
MarceloSalazar 1:1ccf36276cd3 127 // Misc OS setup
MarceloSalazar 1:1ccf36276cd3 128 srand(time(NULL));
MarceloSalazar 1:1ccf36276cd3 129
MarceloSalazar 1:1ccf36276cd3 130 EthernetInterface net;
MarceloSalazar 1:1ccf36276cd3 131 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
MarceloSalazar 1:1ccf36276cd3 132 FATFileSystem fs("sd");
MarceloSalazar 1:1ccf36276cd3 133
MarceloSalazar 1:1ccf36276cd3 134 printf("Start Simple Mbed Cloud Client\n");
MarceloSalazar 1:1ccf36276cd3 135
MarceloSalazar 1:1ccf36276cd3 136 // Initialize button interrupt
MarceloSalazar 1:1ccf36276cd3 137 button.fall(&button_press);
MarceloSalazar 1:1ccf36276cd3 138
MarceloSalazar 1:1ccf36276cd3 139 // Initialize SD card
MarceloSalazar 1:1ccf36276cd3 140 int status = sd.init();
MarceloSalazar 1:1ccf36276cd3 141 if (status != BD_ERROR_OK) {
MarceloSalazar 1:1ccf36276cd3 142 printf("Failed to init SD card\r\n");
MarceloSalazar 1:1ccf36276cd3 143 return -1;
MarceloSalazar 1:1ccf36276cd3 144 }
MarceloSalazar 0:e13a8a944e25 145
MarceloSalazar 1:1ccf36276cd3 146 // Mount the file system (reformatting on failure)
MarceloSalazar 1:1ccf36276cd3 147 status = fs.mount(&sd);
MarceloSalazar 1:1ccf36276cd3 148 if (status) {
MarceloSalazar 1:1ccf36276cd3 149 printf("Failed to mount FAT file system, reformatting...\r\n");
MarceloSalazar 1:1ccf36276cd3 150 status = fs.reformat(&sd);
MarceloSalazar 1:1ccf36276cd3 151
MarceloSalazar 1:1ccf36276cd3 152 if (status) {
MarceloSalazar 1:1ccf36276cd3 153 printf("Failed to reformat FAT file system\r\n");
MarceloSalazar 1:1ccf36276cd3 154 return -1;
MarceloSalazar 1:1ccf36276cd3 155 } else {
MarceloSalazar 1:1ccf36276cd3 156 printf("Reformat and mount complete\r\n");
MarceloSalazar 1:1ccf36276cd3 157 }
MarceloSalazar 1:1ccf36276cd3 158 }
MarceloSalazar 1:1ccf36276cd3 159
MarceloSalazar 1:1ccf36276cd3 160 printf("Connecting to the network using Ethernet...\n");
MarceloSalazar 1:1ccf36276cd3 161
MarceloSalazar 1:1ccf36276cd3 162 status = net.connect();
MarceloSalazar 1:1ccf36276cd3 163 if (status) {
MarceloSalazar 1:1ccf36276cd3 164 printf("Connection to Network Failed %d!\n", status);
MarceloSalazar 1:1ccf36276cd3 165 return -1;
MarceloSalazar 1:1ccf36276cd3 166 } else {
MarceloSalazar 1:1ccf36276cd3 167 const char *ip_addr = net.get_ip_address();
MarceloSalazar 1:1ccf36276cd3 168 printf("Connected successfully\n");
MarceloSalazar 1:1ccf36276cd3 169 printf("IP address %s\n", ip_addr);
MarceloSalazar 1:1ccf36276cd3 170 }
MarceloSalazar 1:1ccf36276cd3 171
MarceloSalazar 1:1ccf36276cd3 172 SimpleMbedCloudClient mbedClient(&net);
MarceloSalazar 0:e13a8a944e25 173 // Save pointer to mbedClient so that other functions can access it.
MarceloSalazar 0:e13a8a944e25 174 client = &mbedClient;
MarceloSalazar 0:e13a8a944e25 175
MarceloSalazar 1:1ccf36276cd3 176 status = mbedClient.init();
MarceloSalazar 1:1ccf36276cd3 177 if (status) {
MarceloSalazar 1:1ccf36276cd3 178 return -1;
MarceloSalazar 1:1ccf36276cd3 179 }
MarceloSalazar 1:1ccf36276cd3 180
MarceloSalazar 0:e13a8a944e25 181 printf("Client initialized\r\n");
MarceloSalazar 0:e13a8a944e25 182
MarceloSalazar 1:1ccf36276cd3 183 // Mbed Cloud Client resource setup
MarceloSalazar 1:1ccf36276cd3 184 MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource");
MarceloSalazar 1:1ccf36276cd3 185 button->set_value("0");
MarceloSalazar 1:1ccf36276cd3 186 button->methods(M2MMethod::GET);
MarceloSalazar 1:1ccf36276cd3 187 button->observable(true);
MarceloSalazar 1:1ccf36276cd3 188 button->attach_notification(M2MMethod::GET, (void*)button_notification_status_callback);
MarceloSalazar 0:e13a8a944e25 189
MarceloSalazar 1:1ccf36276cd3 190 MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource");
MarceloSalazar 1:1ccf36276cd3 191 pattern->set_value("500:500:500:500");
MarceloSalazar 1:1ccf36276cd3 192 pattern->methods(M2MMethod::GET | M2MMethod::PUT);
MarceloSalazar 1:1ccf36276cd3 193 pattern->attach(M2MMethod::PUT, (void*)pattern_updated);
MarceloSalazar 1:1ccf36276cd3 194 pattern_ptr = pattern;
MarceloSalazar 0:e13a8a944e25 195
MarceloSalazar 1:1ccf36276cd3 196 MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource");
MarceloSalazar 1:1ccf36276cd3 197 blink->methods(M2MMethod::POST);
MarceloSalazar 1:1ccf36276cd3 198 blink->attach(M2MMethod::POST, (void*)blink_callback);
MarceloSalazar 0:e13a8a944e25 199
MarceloSalazar 1:1ccf36276cd3 200 MbedCloudClientResource *unregister = mbedClient.create_resource("5000/0/1", "unregister");
MarceloSalazar 1:1ccf36276cd3 201 unregister->methods(M2MMethod::POST);
MarceloSalazar 1:1ccf36276cd3 202 unregister->attach(M2MMethod::POST, (void*)unregister_cb);
MarceloSalazar 1:1ccf36276cd3 203
MarceloSalazar 1:1ccf36276cd3 204 MbedCloudClientResource *factoryReset = mbedClient.create_resource("5000/0/2", "factory_reset");
MarceloSalazar 1:1ccf36276cd3 205 factoryReset->methods(M2MMethod::POST);
MarceloSalazar 1:1ccf36276cd3 206 factoryReset->attach(M2MMethod::POST, (void*)factory_reset_cb);
MarceloSalazar 0:e13a8a944e25 207
MarceloSalazar 0:e13a8a944e25 208 mbedClient.register_and_connect();
MarceloSalazar 0:e13a8a944e25 209
MarceloSalazar 0:e13a8a944e25 210 // Check if client is registering or registered, if true sleep and repeat.
MarceloSalazar 0:e13a8a944e25 211 while (mbedClient.is_register_called()) {
MarceloSalazar 0:e13a8a944e25 212 static int button_count = 0;
MarceloSalazar 1:1ccf36276cd3 213 wait_ms(100);
MarceloSalazar 1:1ccf36276cd3 214
MarceloSalazar 1:1ccf36276cd3 215 if (button_pressed) {
MarceloSalazar 1:1ccf36276cd3 216 button_pressed = false;
MarceloSalazar 1:1ccf36276cd3 217 printf("Button clicked %d times\r\n", ++button_count);
MarceloSalazar 1:1ccf36276cd3 218 button->set_value(button_count);
MarceloSalazar 0:e13a8a944e25 219 }
MarceloSalazar 0:e13a8a944e25 220 }
MarceloSalazar 0:e13a8a944e25 221
MarceloSalazar 0:e13a8a944e25 222 // Client unregistered, exit program.
MarceloSalazar 0:e13a8a944e25 223 return 0;
MarceloSalazar 0:e13a8a944e25 224 }