Simple Mbed Cloud Client application using features of K64 & K66

Connect to Mbed Cloud!

This example was customized a bit for FRDM-K66 and FRDM-K64F.

It depends on having an SD Card plugged in for storage of credentials. It could be changed later to use a SPI flash or other storage on a shield or wired in.

The app keeps track of how many times switch 2 (SW2) is pressed. The value can be retrieved via a GET request to Mbed Cloud.

Also, it will blink a pattern based on milisecond (ms) timing values that can be sent from Mbed Cloud. The pattern can be sent with a PUT request and the blinking sequence can be triggered by a POST request.

Committer:
maclobdell
Date:
Tue Oct 09 22:08:16 2018 -0500
Revision:
15:df35a45db1cc
Parent:
13:49062a0d117e
remove sd driver.  its in mbed-os now.

Who changed what in which revision?

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