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.
main.cpp@8:ed66653e5a22, 2018-10-09 (annotated)
- Committer:
- ccli8
- Date:
- Tue Oct 09 13:07:10 2018 +0800
- Revision:
- 8:ed66653e5a22
- Parent:
- 7:6aee86899520
- Child:
- 11:beac893830b4
Remove support for WiFi
Who changed what in which revision?
User | Revision | Line number | New 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 |
7:6aee86899520 | 18 | |
ccli8 |
7:6aee86899520 | 19 | #include "mbed.h" |
ccli8 |
7:6aee86899520 | 20 | #include "simple-mbed-cloud-client.h" |
ccli8 |
7:6aee86899520 | 21 | #include "FATFileSystem.h" |
ccli8 |
7:6aee86899520 | 22 | |
ccli8 |
7:6aee86899520 | 23 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
ccli8 |
7:6aee86899520 | 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 |
ccli8 |
7:6aee86899520 | 25 | EventQueue eventQueue; |
ccli8 |
7:6aee86899520 | 26 | |
ccli8 |
7:6aee86899520 | 27 | // Default block device |
ccli8 |
7:6aee86899520 | 28 | BlockDevice* bd = BlockDevice::get_default_instance(); |
ccli8 |
7:6aee86899520 | 29 | FATFileSystem fs("sd", bd); |
ccli8 |
7:6aee86899520 | 30 | |
ccli8 |
7:6aee86899520 | 31 | // Default network interface object |
ccli8 |
7:6aee86899520 | 32 | NetworkInterface *net; |
ccli8 |
7:6aee86899520 | 33 | |
ccli8 |
7:6aee86899520 | 34 | // Declaring pointers for access to Mbed Cloud Client resources outside of main() |
ccli8 |
7:6aee86899520 | 35 | MbedCloudClientResource *button_res; |
ccli8 |
7:6aee86899520 | 36 | MbedCloudClientResource *pattern_res; |
ccli8 |
7:6aee86899520 | 37 | |
ccli8 |
7:6aee86899520 | 38 | // 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 | 39 | void fake_button_press() { |
ccli8 |
7:6aee86899520 | 40 | int v = button_res->get_value_int() + 1; |
ccli8 |
7:6aee86899520 | 41 | |
ccli8 |
7:6aee86899520 | 42 | button_res->set_value(v); |
ccli8 |
7:6aee86899520 | 43 | |
ccli8 |
7:6aee86899520 | 44 | printf("Simulated button clicked %d times\n", v); |
ccli8 |
7:6aee86899520 | 45 | } |
ccli8 |
7:6aee86899520 | 46 | |
ccli8 |
7:6aee86899520 | 47 | /** |
ccli8 |
7:6aee86899520 | 48 | * PUT handler |
ccli8 |
7:6aee86899520 | 49 | * @param resource The resource that triggered the callback |
ccli8 |
7:6aee86899520 | 50 | * @param newValue Updated value for the resource |
ccli8 |
7:6aee86899520 | 51 | */ |
ccli8 |
7:6aee86899520 | 52 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
ccli8 |
7:6aee86899520 | 53 | printf("PUT received, new value: %s\n", newValue.c_str()); |
ccli8 |
7:6aee86899520 | 54 | } |
ccli8 |
7:6aee86899520 | 55 | |
ccli8 |
7:6aee86899520 | 56 | /** |
ccli8 |
7:6aee86899520 | 57 | * POST handler |
ccli8 |
7:6aee86899520 | 58 | * @param resource The resource that triggered the callback |
ccli8 |
7:6aee86899520 | 59 | * @param buffer If a body was passed to the POST function, this contains the data. |
ccli8 |
7:6aee86899520 | 60 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
ccli8 |
7:6aee86899520 | 61 | * @param size Size of the body |
ccli8 |
7:6aee86899520 | 62 | */ |
ccli8 |
7:6aee86899520 | 63 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
ccli8 |
7:6aee86899520 | 64 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
ccli8 |
7:6aee86899520 | 65 | |
ccli8 |
7:6aee86899520 | 66 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
ccli8 |
7:6aee86899520 | 67 | |
ccli8 |
7:6aee86899520 | 68 | // Parse the pattern string, and toggle the LED in that pattern |
ccli8 |
7:6aee86899520 | 69 | string s = std::string(pattern_res->get_value().c_str()); |
ccli8 |
7:6aee86899520 | 70 | size_t i = 0; |
ccli8 |
7:6aee86899520 | 71 | size_t pos = s.find(':'); |
ccli8 |
7:6aee86899520 | 72 | while (pos != string::npos) { |
ccli8 |
7:6aee86899520 | 73 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
ccli8 |
7:6aee86899520 | 74 | augmentedLed = !augmentedLed; |
ccli8 |
7:6aee86899520 | 75 | |
ccli8 |
7:6aee86899520 | 76 | i = ++pos; |
ccli8 |
7:6aee86899520 | 77 | pos = s.find(':', pos); |
ccli8 |
7:6aee86899520 | 78 | |
ccli8 |
7:6aee86899520 | 79 | if (pos == string::npos) { |
ccli8 |
7:6aee86899520 | 80 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
ccli8 |
7:6aee86899520 | 81 | augmentedLed = !augmentedLed; |
ccli8 |
7:6aee86899520 | 82 | } |
ccli8 |
7:6aee86899520 | 83 | } |
ccli8 |
7:6aee86899520 | 84 | } |
ccli8 |
7:6aee86899520 | 85 | |
ccli8 |
7:6aee86899520 | 86 | /** |
ccli8 |
7:6aee86899520 | 87 | * Notification callback handler |
ccli8 |
7:6aee86899520 | 88 | * @param resource The resource that triggered the callback |
ccli8 |
7:6aee86899520 | 89 | * @param status The delivery status of the notification |
ccli8 |
7:6aee86899520 | 90 | */ |
ccli8 |
7:6aee86899520 | 91 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
ccli8 |
7:6aee86899520 | 92 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
ccli8 |
7:6aee86899520 | 93 | } |
ccli8 |
7:6aee86899520 | 94 | |
ccli8 |
7:6aee86899520 | 95 | /** |
ccli8 |
7:6aee86899520 | 96 | * Registration callback handler |
ccli8 |
7:6aee86899520 | 97 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
ccli8 |
7:6aee86899520 | 98 | */ |
ccli8 |
7:6aee86899520 | 99 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
ccli8 |
7:6aee86899520 | 100 | printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
ccli8 |
7:6aee86899520 | 101 | } |
ccli8 |
7:6aee86899520 | 102 | |
ccli8 |
7:6aee86899520 | 103 | int main(void) { |
ccli8 |
7:6aee86899520 | 104 | printf("Starting Simple Mbed Cloud Client example\n"); |
ccli8 |
7:6aee86899520 | 105 | printf("Connecting to the network using Ethernet...\n"); |
ccli8 |
7:6aee86899520 | 106 | |
ccli8 |
7:6aee86899520 | 107 | // Connect to the internet (DHCP is expected to be on) |
ccli8 |
7:6aee86899520 | 108 | net = NetworkInterface::get_default_instance(); |
ccli8 |
7:6aee86899520 | 109 | |
ccli8 |
7:6aee86899520 | 110 | nsapi_error_t status = net->connect(); |
ccli8 |
7:6aee86899520 | 111 | |
ccli8 |
7:6aee86899520 | 112 | if (status != NSAPI_ERROR_OK) { |
ccli8 |
7:6aee86899520 | 113 | printf("Connecting to the network failed %d!\n", status); |
ccli8 |
7:6aee86899520 | 114 | return -1; |
ccli8 |
7:6aee86899520 | 115 | } |
ccli8 |
7:6aee86899520 | 116 | |
ccli8 |
7:6aee86899520 | 117 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
ccli8 |
7:6aee86899520 | 118 | |
ccli8 |
7:6aee86899520 | 119 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
ccli8 |
7:6aee86899520 | 120 | SimpleMbedCloudClient client(net, bd, &fs); |
ccli8 |
7:6aee86899520 | 121 | int client_status = client.init(); |
ccli8 |
7:6aee86899520 | 122 | if (client_status != 0) { |
ccli8 |
7:6aee86899520 | 123 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
ccli8 |
7:6aee86899520 | 124 | return -1; |
ccli8 |
7:6aee86899520 | 125 | } |
ccli8 |
7:6aee86899520 | 126 | |
ccli8 |
7:6aee86899520 | 127 | // Creating resources, which can be written or read from the cloud |
ccli8 |
7:6aee86899520 | 128 | button_res = client.create_resource("3200/0/5501", "button_count"); |
ccli8 |
7:6aee86899520 | 129 | button_res->set_value(0); |
ccli8 |
7:6aee86899520 | 130 | button_res->methods(M2MMethod::GET); |
ccli8 |
7:6aee86899520 | 131 | button_res->observable(true); |
ccli8 |
7:6aee86899520 | 132 | button_res->attach_notification_callback(button_callback); |
ccli8 |
7:6aee86899520 | 133 | |
ccli8 |
7:6aee86899520 | 134 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
ccli8 |
7:6aee86899520 | 135 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
ccli8 |
7:6aee86899520 | 136 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
ccli8 |
7:6aee86899520 | 137 | pattern_res->attach_put_callback(pattern_updated); |
ccli8 |
7:6aee86899520 | 138 | |
ccli8 |
7:6aee86899520 | 139 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
ccli8 |
7:6aee86899520 | 140 | blink_res->methods(M2MMethod::POST); |
ccli8 |
7:6aee86899520 | 141 | blink_res->attach_post_callback(blink_callback); |
ccli8 |
7:6aee86899520 | 142 | |
ccli8 |
7:6aee86899520 | 143 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
ccli8 |
7:6aee86899520 | 144 | |
ccli8 |
7:6aee86899520 | 145 | // Callback that fires when registering is complete |
ccli8 |
7:6aee86899520 | 146 | client.on_registered(®istered); |
ccli8 |
7:6aee86899520 | 147 | |
ccli8 |
7:6aee86899520 | 148 | // Register with Mbed Cloud |
ccli8 |
7:6aee86899520 | 149 | client.register_and_connect(); |
ccli8 |
7:6aee86899520 | 150 | |
ccli8 |
7:6aee86899520 | 151 | // Placeholder for callback to update local resource when GET comes. |
ccli8 |
7:6aee86899520 | 152 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
ccli8 |
7:6aee86899520 | 153 | Ticker timer; |
ccli8 |
7:6aee86899520 | 154 | timer.attach(eventQueue.event(&fake_button_press), 5.0); |
ccli8 |
7:6aee86899520 | 155 | |
ccli8 |
7:6aee86899520 | 156 | // You can easily run the eventQueue in a separate thread if required |
ccli8 |
7:6aee86899520 | 157 | eventQueue.dispatch_forever(); |
ccli8 |
7:6aee86899520 | 158 | } |