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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2018 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 #ifndef MBED_TEST_MODE
00019 
00020 #include "mbed.h"
00021 #include "simple-mbed-cloud-client.h"
00022 #include "FATFileSystem.h"
00023 
00024 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
00025 // 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
00026 EventQueue eventQueue;
00027 
00028 // Default block device
00029 BlockDevice *bd = BlockDevice::get_default_instance();
00030 FATFileSystem fs("fs");
00031 
00032 // Default network interface object
00033 NetworkInterface *net = NetworkInterface::get_default_instance();
00034 
00035 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
00036 MbedCloudClientResource *button_res;
00037 MbedCloudClientResource *pattern_res;
00038 
00039 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
00040 void fake_button_press() {
00041     int v = button_res->get_value_int() + 1;
00042 
00043     button_res->set_value(v);
00044 
00045     printf("Simulated button clicked %d times\n", v);
00046 }
00047 
00048 /**
00049  * PUT handler
00050  * @param resource The resource that triggered the callback
00051  * @param newValue Updated value for the resource
00052  */
00053 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
00054     printf("PUT received, new value: %s\n", newValue.c_str());
00055 }
00056 
00057 /**
00058  * POST handler
00059  * @param resource The resource that triggered the callback
00060  * @param buffer If a body was passed to the POST function, this contains the data.
00061  *               Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
00062  * @param size Size of the body
00063  */
00064 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
00065     printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
00066 
00067     static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
00068 
00069     // Parse the pattern string, and toggle the LED in that pattern
00070     string s = std::string(pattern_res->get_value().c_str());
00071     size_t i = 0;
00072     size_t pos = s.find(':');
00073     while (pos != string::npos) {
00074         wait_ms(atoi(s.substr(i, pos - i).c_str()));
00075         augmentedLed = !augmentedLed;
00076 
00077         i = ++pos;
00078         pos = s.find(':', pos);
00079 
00080         if (pos == string::npos) {
00081             wait_ms(atoi(s.substr(i, s.length()).c_str()));
00082             augmentedLed = !augmentedLed;
00083         }
00084     }
00085 }
00086 
00087 /**
00088  * Notification callback handler
00089  * @param resource The resource that triggered the callback
00090  * @param status The delivery status of the notification
00091  */
00092 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
00093     printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
00094 }
00095 
00096 /**
00097  * Registration callback handler
00098  * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
00099  */
00100 void registered(const ConnectorClientEndpointInfo *endpoint) {
00101     printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
00102 }
00103 
00104 int main(void) {
00105     printf("Starting Simple Pelion Device Management Client example\n");
00106     printf("Connecting to the network...\n");
00107 
00108     // Connect to the internet (DHCP is expected to be on)
00109     nsapi_error_t status = net->connect();
00110 
00111     if (status != NSAPI_ERROR_OK) {
00112         printf("Connecting to the network failed %d!\n", status);
00113         return -1;
00114     }
00115 
00116     printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
00117 
00118     // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
00119     SimpleMbedCloudClient client(net, bd, &fs);
00120     int client_status = client.init();
00121     if (client_status != 0) {
00122         printf("Pelion Client initialization failed (%d)\n", client_status);
00123         return -1;
00124     }
00125 
00126     // Creating resources, which can be written or read from the cloud
00127     button_res = client.create_resource("3200/0/5501", "button_count");
00128     button_res->set_value(0);
00129     button_res->methods(M2MMethod::GET);
00130     button_res->observable(true);
00131     button_res->attach_notification_callback(button_callback);
00132 
00133     pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
00134     pattern_res->set_value("500:500:500:500:500:500:500:500");
00135     pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
00136     pattern_res->attach_put_callback(pattern_updated);
00137 
00138     MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
00139     blink_res->methods(M2MMethod::POST);
00140     blink_res->attach_post_callback(blink_callback);
00141 
00142     printf("Initialized Pelion Client. Registering...\n");
00143 
00144     // Callback that fires when registering is complete
00145     client.on_registered(&registered);
00146 
00147     // Register with Pelion Device Management
00148     client.register_and_connect();
00149 
00150     // Placeholder for callback to update local resource when GET comes.
00151     // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
00152     Ticker timer;
00153     timer.attach(eventQueue.event(&fake_button_press), 5.0);
00154 
00155     // You can easily run the eventQueue in a separate thread if required
00156     eventQueue.dispatch_forever();
00157 }
00158 #endif