Simple Mbed Cloud client application for NuMaker-PFM-M487 and NuMaker-PFM-NUC472 over Ethernet.

This application contains the example codes to:

1) Connect PFM-M487/PFM-NUC472 boards to Pelion

2) Enable Firmware update

For storage, PFM-M487/PFM-NUC472 support both SPI interface SD and built-in SD bus SD.

For connectivity, PFM-M487/PFM-NUC472 support Ethernet (on-board) by default.

This example supports Ethernet and built-in SD by default.

https://os.mbed.com/media/cache/platforms/NuMaker-PFM-M487.png.170x170_q85.png https://os.mbed.com/media/cache/platforms/NuMaker-PFM-NUC472Small.png.170x170_q85.png

Committer:
ccli8
Date:
Wed Dec 05 18:21:52 2018 +0800
Revision:
15:867695969724
Parent:
11:beac893830b4
Update NuMaker-mbed-SD-driver.lib

Fix user buffer may not be SD DMA-compatible

Who changed what in which revision?

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