Pelion workshop with Grove sensors.

Committer:
MarceloSalazar
Date:
Wed Feb 14 15:09:00 2018 +0000
Revision:
4:a107dae867fb
Parent:
2:6ed27f413b30
Child:
5:c18fab181ede
Remove useless README.md

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 4:a107dae867fb 29 // Placeholder to hardware that trigger events (timer, button, etc)
MarceloSalazar 4:a107dae867fb 30 Ticker timer;
MarceloSalazar 0:e13a8a944e25 31
MarceloSalazar 0:e13a8a944e25 32 // Pointers to the resources that will be created in main_application().
MarceloSalazar 1:1ccf36276cd3 33 static MbedCloudClientResource* pattern_ptr;
MarceloSalazar 0:e13a8a944e25 34
MarceloSalazar 0:e13a8a944e25 35 // Pointer to mbedClient, used for calling close function.
MarceloSalazar 0:e13a8a944e25 36 static SimpleMbedCloudClient *client;
MarceloSalazar 0:e13a8a944e25 37
MarceloSalazar 1:1ccf36276cd3 38 static bool button_pressed = false;
MarceloSalazar 1:1ccf36276cd3 39
MarceloSalazar 1:1ccf36276cd3 40 void button_press() {
MarceloSalazar 1:1ccf36276cd3 41 button_pressed = true;
MarceloSalazar 1:1ccf36276cd3 42 }
MarceloSalazar 1:1ccf36276cd3 43
MarceloSalazar 1:1ccf36276cd3 44 void pattern_updated(const char *) {
MarceloSalazar 1:1ccf36276cd3 45 printf("PUT received, new value: %s\n", pattern_ptr->get_value());
MarceloSalazar 2:6ed27f413b30 46 // Placeholder for PUT action
MarceloSalazar 0:e13a8a944e25 47 }
MarceloSalazar 0:e13a8a944e25 48
MarceloSalazar 0:e13a8a944e25 49 void blink_callback(void *) {
MarceloSalazar 1:1ccf36276cd3 50 const char *pattern = pattern_ptr->get_value();
MarceloSalazar 2:6ed27f413b30 51 printf("POST received. LED pattern = %s\n", pattern);
MarceloSalazar 2:6ed27f413b30 52 // Placeholder for POST action
MarceloSalazar 0:e13a8a944e25 53 // The pattern is something like 500:200:500, so parse that.
MarceloSalazar 0:e13a8a944e25 54 }
MarceloSalazar 0:e13a8a944e25 55
MarceloSalazar 2:6ed27f413b30 56 void button_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MarceloSalazar 0:e13a8a944e25 57 {
MarceloSalazar 2:6ed27f413b30 58 printf("Button notification. Callback: (%s)\n", object.uri_path());
MarceloSalazar 2:6ed27f413b30 59 // Placeholder for GET
MarceloSalazar 0:e13a8a944e25 60 }
MarceloSalazar 0:e13a8a944e25 61
MarceloSalazar 0:e13a8a944e25 62
MarceloSalazar 0:e13a8a944e25 63 int main(void)
MarceloSalazar 0:e13a8a944e25 64 {
MarceloSalazar 2:6ed27f413b30 65 // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364)
MarceloSalazar 2:6ed27f413b30 66 // Older versions: workaround to prevent possible deletion of credentials:
MarceloSalazar 0:e13a8a944e25 67 wait(2);
MarceloSalazar 0:e13a8a944e25 68
MarceloSalazar 1:1ccf36276cd3 69 // Misc OS setup
MarceloSalazar 1:1ccf36276cd3 70 srand(time(NULL));
MarceloSalazar 1:1ccf36276cd3 71
MarceloSalazar 2:6ed27f413b30 72 // Placeholder for network
MarceloSalazar 1:1ccf36276cd3 73 EthernetInterface net;
MarceloSalazar 2:6ed27f413b30 74
MarceloSalazar 2:6ed27f413b30 75 // Placeholder for storage
MarceloSalazar 1:1ccf36276cd3 76 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
MarceloSalazar 1:1ccf36276cd3 77 FATFileSystem fs("sd");
MarceloSalazar 1:1ccf36276cd3 78
MarceloSalazar 1:1ccf36276cd3 79 printf("Start Simple Mbed Cloud Client\n");
MarceloSalazar 1:1ccf36276cd3 80
MarceloSalazar 1:1ccf36276cd3 81 // Initialize SD card
MarceloSalazar 1:1ccf36276cd3 82 int status = sd.init();
MarceloSalazar 1:1ccf36276cd3 83 if (status != BD_ERROR_OK) {
MarceloSalazar 1:1ccf36276cd3 84 printf("Failed to init SD card\r\n");
MarceloSalazar 1:1ccf36276cd3 85 return -1;
MarceloSalazar 1:1ccf36276cd3 86 }
MarceloSalazar 0:e13a8a944e25 87
MarceloSalazar 1:1ccf36276cd3 88 // Mount the file system (reformatting on failure)
MarceloSalazar 1:1ccf36276cd3 89 status = fs.mount(&sd);
MarceloSalazar 1:1ccf36276cd3 90 if (status) {
MarceloSalazar 1:1ccf36276cd3 91 printf("Failed to mount FAT file system, reformatting...\r\n");
MarceloSalazar 1:1ccf36276cd3 92 status = fs.reformat(&sd);
MarceloSalazar 1:1ccf36276cd3 93 if (status) {
MarceloSalazar 1:1ccf36276cd3 94 printf("Failed to reformat FAT file system\r\n");
MarceloSalazar 1:1ccf36276cd3 95 return -1;
MarceloSalazar 1:1ccf36276cd3 96 } else {
MarceloSalazar 1:1ccf36276cd3 97 printf("Reformat and mount complete\r\n");
MarceloSalazar 1:1ccf36276cd3 98 }
MarceloSalazar 1:1ccf36276cd3 99 }
MarceloSalazar 1:1ccf36276cd3 100
MarceloSalazar 1:1ccf36276cd3 101 printf("Connecting to the network using Ethernet...\n");
MarceloSalazar 1:1ccf36276cd3 102
MarceloSalazar 1:1ccf36276cd3 103 status = net.connect();
MarceloSalazar 1:1ccf36276cd3 104 if (status) {
MarceloSalazar 1:1ccf36276cd3 105 printf("Connection to Network Failed %d!\n", status);
MarceloSalazar 1:1ccf36276cd3 106 return -1;
MarceloSalazar 1:1ccf36276cd3 107 } else {
MarceloSalazar 1:1ccf36276cd3 108 const char *ip_addr = net.get_ip_address();
MarceloSalazar 1:1ccf36276cd3 109 printf("Connected successfully\n");
MarceloSalazar 1:1ccf36276cd3 110 printf("IP address %s\n", ip_addr);
MarceloSalazar 1:1ccf36276cd3 111 }
MarceloSalazar 1:1ccf36276cd3 112
MarceloSalazar 4:a107dae867fb 113 // Placeholder for callback to update local resource when GET comes.
MarceloSalazar 4:a107dae867fb 114 timer.attach(&button_press, 5.0);
MarceloSalazar 2:6ed27f413b30 115
MarceloSalazar 1:1ccf36276cd3 116 SimpleMbedCloudClient mbedClient(&net);
MarceloSalazar 0:e13a8a944e25 117 // Save pointer to mbedClient so that other functions can access it.
MarceloSalazar 0:e13a8a944e25 118 client = &mbedClient;
MarceloSalazar 0:e13a8a944e25 119
MarceloSalazar 1:1ccf36276cd3 120 status = mbedClient.init();
MarceloSalazar 1:1ccf36276cd3 121 if (status) {
MarceloSalazar 1:1ccf36276cd3 122 return -1;
MarceloSalazar 1:1ccf36276cd3 123 }
MarceloSalazar 1:1ccf36276cd3 124
MarceloSalazar 0:e13a8a944e25 125 printf("Client initialized\r\n");
MarceloSalazar 0:e13a8a944e25 126
MarceloSalazar 1:1ccf36276cd3 127 // Mbed Cloud Client resource setup
MarceloSalazar 1:1ccf36276cd3 128 MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource");
MarceloSalazar 1:1ccf36276cd3 129 button->set_value("0");
MarceloSalazar 1:1ccf36276cd3 130 button->methods(M2MMethod::GET);
MarceloSalazar 1:1ccf36276cd3 131 button->observable(true);
MarceloSalazar 2:6ed27f413b30 132 button->attach_notification(M2MMethod::GET, (void*)button_callback);
MarceloSalazar 0:e13a8a944e25 133
MarceloSalazar 1:1ccf36276cd3 134 MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource");
MarceloSalazar 1:1ccf36276cd3 135 pattern->set_value("500:500:500:500");
MarceloSalazar 1:1ccf36276cd3 136 pattern->methods(M2MMethod::GET | M2MMethod::PUT);
MarceloSalazar 1:1ccf36276cd3 137 pattern->attach(M2MMethod::PUT, (void*)pattern_updated);
MarceloSalazar 1:1ccf36276cd3 138 pattern_ptr = pattern;
MarceloSalazar 0:e13a8a944e25 139
MarceloSalazar 1:1ccf36276cd3 140 MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource");
MarceloSalazar 1:1ccf36276cd3 141 blink->methods(M2MMethod::POST);
MarceloSalazar 1:1ccf36276cd3 142 blink->attach(M2MMethod::POST, (void*)blink_callback);
MarceloSalazar 0:e13a8a944e25 143
MarceloSalazar 0:e13a8a944e25 144 mbedClient.register_and_connect();
MarceloSalazar 0:e13a8a944e25 145
MarceloSalazar 0:e13a8a944e25 146 // Check if client is registering or registered, if true sleep and repeat.
MarceloSalazar 0:e13a8a944e25 147 while (mbedClient.is_register_called()) {
MarceloSalazar 0:e13a8a944e25 148 static int button_count = 0;
MarceloSalazar 1:1ccf36276cd3 149 wait_ms(100);
MarceloSalazar 1:1ccf36276cd3 150
MarceloSalazar 1:1ccf36276cd3 151 if (button_pressed) {
MarceloSalazar 1:1ccf36276cd3 152 button_pressed = false;
MarceloSalazar 4:a107dae867fb 153 printf("Simulated button clicked %d times\r\n", ++button_count);
MarceloSalazar 1:1ccf36276cd3 154 button->set_value(button_count);
MarceloSalazar 0:e13a8a944e25 155 }
MarceloSalazar 0:e13a8a944e25 156 }
MarceloSalazar 0:e13a8a944e25 157
MarceloSalazar 0:e13a8a944e25 158 // Client unregistered, exit program.
MarceloSalazar 0:e13a8a944e25 159 return 0;
MarceloSalazar 0:e13a8a944e25 160 }