Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
MarceloSalazar
Date:
Sun Oct 14 19:01:53 2018 +0100
Revision:
9:265744785d33
Parent:
6:e0e1e1b93099
Child:
10:b27c962b3c3f
Add support for FW Update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 1:e86b1cffc402 1 // ----------------------------------------------------------------------------
adustm 4:cf7342047b4d 2 // Copyright 2016-2018 ARM Ltd.
adustm 1:e86b1cffc402 3 //
adustm 1:e86b1cffc402 4 // SPDX-License-Identifier: Apache-2.0
adustm 1:e86b1cffc402 5 //
adustm 1:e86b1cffc402 6 // Licensed under the Apache License, Version 2.0 (the "License");
adustm 1:e86b1cffc402 7 // you may not use this file except in compliance with the License.
adustm 1:e86b1cffc402 8 // You may obtain a copy of the License at
adustm 1:e86b1cffc402 9 //
adustm 1:e86b1cffc402 10 // http://www.apache.org/licenses/LICENSE-2.0
adustm 1:e86b1cffc402 11 //
adustm 1:e86b1cffc402 12 // Unless required by applicable law or agreed to in writing, software
adustm 1:e86b1cffc402 13 // distributed under the License is distributed on an "AS IS" BASIS,
adustm 1:e86b1cffc402 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
adustm 1:e86b1cffc402 15 // See the License for the specific language governing permissions and
adustm 1:e86b1cffc402 16 // limitations under the License.
adustm 1:e86b1cffc402 17 // ----------------------------------------------------------------------------
MarceloSalazar 9:265744785d33 18 #ifndef MBED_TEST_MODE
adustm 1:e86b1cffc402 19 #include "mbed.h"
adustm 1:e86b1cffc402 20 #include "simple-mbed-cloud-client.h"
adustm 1:e86b1cffc402 21 #include "FATFileSystem.h"
adustm 1:e86b1cffc402 22
adustm 4:cf7342047b4d 23 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
adustm 4:cf7342047b4d 24 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
adustm 4:cf7342047b4d 25 EventQueue eventQueue;
adustm 1:e86b1cffc402 26
MarceloSalazar 9:265744785d33 27 // Default network interface object
MarceloSalazar 9:265744785d33 28 NetworkInterface *net;
adustm 6:e0e1e1b93099 29
MarceloSalazar 9:265744785d33 30 // Default block device
MarceloSalazar 9:265744785d33 31 BlockDevice* bd = BlockDevice::get_default_instance();
MarceloSalazar 9:265744785d33 32 FATFileSystem fs("sd", bd);
adustm 4:cf7342047b4d 33
MarceloSalazar 9:265744785d33 34 // Declaring pointers for access to Pelion Client resources outside of main()
adustm 4:cf7342047b4d 35 MbedCloudClientResource *button_res;
adustm 4:cf7342047b4d 36 MbedCloudClientResource *pattern_res;
adustm 1:e86b1cffc402 37
adustm 4:cf7342047b4d 38 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
adustm 4:cf7342047b4d 39 void fake_button_press() {
adustm 4:cf7342047b4d 40 int v = button_res->get_value_int() + 1;
adustm 1:e86b1cffc402 41
adustm 4:cf7342047b4d 42 button_res->set_value(v);
adustm 1:e86b1cffc402 43
adustm 4:cf7342047b4d 44 printf("Simulated button clicked %d times\n", v);
adustm 1:e86b1cffc402 45 }
adustm 1:e86b1cffc402 46
adustm 4:cf7342047b4d 47 /**
adustm 4:cf7342047b4d 48 * PUT handler
adustm 4:cf7342047b4d 49 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 50 * @param newValue Updated value for the resource
adustm 4:cf7342047b4d 51 */
adustm 4:cf7342047b4d 52 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
adustm 4:cf7342047b4d 53 printf("PUT received, new value: %s\n", newValue.c_str());
adustm 1:e86b1cffc402 54 }
adustm 1:e86b1cffc402 55
adustm 4:cf7342047b4d 56 /**
adustm 4:cf7342047b4d 57 * POST handler
adustm 4:cf7342047b4d 58 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 59 * @param buffer If a body was passed to the POST function, this contains the data.
adustm 4:cf7342047b4d 60 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
adustm 4:cf7342047b4d 61 * @param size Size of the body
adustm 4:cf7342047b4d 62 */
adustm 4:cf7342047b4d 63 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
adustm 4:cf7342047b4d 64 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
adustm 4:cf7342047b4d 65
adustm 4:cf7342047b4d 66 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
adustm 1:e86b1cffc402 67
adustm 4:cf7342047b4d 68 // Parse the pattern string, and toggle the LED in that pattern
adustm 4:cf7342047b4d 69 string s = std::string(pattern_res->get_value().c_str());
adustm 4:cf7342047b4d 70 size_t i = 0;
adustm 4:cf7342047b4d 71 size_t pos = s.find(':');
adustm 4:cf7342047b4d 72 while (pos != string::npos) {
adustm 4:cf7342047b4d 73 wait_ms(atoi(s.substr(i, pos - i).c_str()));
adustm 4:cf7342047b4d 74 augmentedLed = !augmentedLed;
adustm 4:cf7342047b4d 75
adustm 4:cf7342047b4d 76 i = ++pos;
adustm 4:cf7342047b4d 77 pos = s.find(':', pos);
adustm 4:cf7342047b4d 78
adustm 4:cf7342047b4d 79 if (pos == string::npos) {
adustm 4:cf7342047b4d 80 wait_ms(atoi(s.substr(i, s.length()).c_str()));
adustm 4:cf7342047b4d 81 augmentedLed = !augmentedLed;
adustm 4:cf7342047b4d 82 }
adustm 4:cf7342047b4d 83 }
adustm 1:e86b1cffc402 84 }
adustm 1:e86b1cffc402 85
adustm 4:cf7342047b4d 86 /**
adustm 4:cf7342047b4d 87 * Notification callback handler
adustm 4:cf7342047b4d 88 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 89 * @param status The delivery status of the notification
adustm 4:cf7342047b4d 90 */
adustm 4:cf7342047b4d 91 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
adustm 4:cf7342047b4d 92 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
adustm 4:cf7342047b4d 93 }
adustm 1:e86b1cffc402 94
adustm 4:cf7342047b4d 95 /**
adustm 4:cf7342047b4d 96 * Registration callback handler
adustm 4:cf7342047b4d 97 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
adustm 4:cf7342047b4d 98 */
adustm 4:cf7342047b4d 99 void registered(const ConnectorClientEndpointInfo *endpoint) {
MarceloSalazar 9:265744785d33 100 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
adustm 4:cf7342047b4d 101 }
adustm 1:e86b1cffc402 102
adustm 4:cf7342047b4d 103 int main(void) {
MarceloSalazar 9:265744785d33 104 printf("Starting Simple Pelion Device Management Client example\n");
adustm 4:cf7342047b4d 105 printf("Connecting to the network using Wifi...\n");
adustm 4:cf7342047b4d 106
adustm 4:cf7342047b4d 107 // Connect to the internet (DHCP is expected to be on)
MarceloSalazar 9:265744785d33 108 net = NetworkInterface::get_default_instance();
adustm 4:cf7342047b4d 109
MarceloSalazar 9:265744785d33 110 nsapi_error_t status = net->connect();
MarceloSalazar 9:265744785d33 111
MarceloSalazar 9:265744785d33 112 if (status != NSAPI_ERROR_OK) {
adustm 4:cf7342047b4d 113 printf("Connecting to the network failed %d!\n", status);
adustm 1:e86b1cffc402 114 return -1;
adustm 1:e86b1cffc402 115 }
adustm 1:e86b1cffc402 116
MarceloSalazar 9:265744785d33 117 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 118
MarceloSalazar 9:265744785d33 119 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 120 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 121 int client_status = client.init();
adustm 4:cf7342047b4d 122 if (client_status != 0) {
MarceloSalazar 9:265744785d33 123 printf("Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 124 return -1;
adustm 1:e86b1cffc402 125 }
adustm 1:e86b1cffc402 126
adustm 4:cf7342047b4d 127 // Creating resources, which can be written or read from the cloud
adustm 4:cf7342047b4d 128 button_res = client.create_resource("3200/0/5501", "button_count");
adustm 4:cf7342047b4d 129 button_res->set_value(0);
adustm 4:cf7342047b4d 130 button_res->methods(M2MMethod::GET);
adustm 4:cf7342047b4d 131 button_res->observable(true);
adustm 4:cf7342047b4d 132 button_res->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 133
adustm 4:cf7342047b4d 134 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
adustm 4:cf7342047b4d 135 pattern_res->set_value("500:500:500:500:500:500:500:500");
adustm 4:cf7342047b4d 136 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
adustm 4:cf7342047b4d 137 pattern_res->attach_put_callback(pattern_updated);
adustm 1:e86b1cffc402 138
adustm 4:cf7342047b4d 139 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
adustm 4:cf7342047b4d 140 blink_res->methods(M2MMethod::POST);
adustm 4:cf7342047b4d 141 blink_res->attach_post_callback(blink_callback);
adustm 4:cf7342047b4d 142
MarceloSalazar 9:265744785d33 143 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 144
adustm 4:cf7342047b4d 145 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 146 client.on_registered(&registered);
adustm 1:e86b1cffc402 147
MarceloSalazar 9:265744785d33 148 // Register with Pelion DM
adustm 4:cf7342047b4d 149 client.register_and_connect();
adustm 1:e86b1cffc402 150
adustm 1:e86b1cffc402 151 // Placeholder for callback to update local resource when GET comes.
adustm 4:cf7342047b4d 152 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
adustm 4:cf7342047b4d 153 Ticker timer;
adustm 4:cf7342047b4d 154 timer.attach(eventQueue.event(&fake_button_press), 5.0);
adustm 1:e86b1cffc402 155
adustm 4:cf7342047b4d 156 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 157 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 158 }
MarceloSalazar 9:265744785d33 159 #endif