Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #include "LittleFileSystem.h" 00024 00025 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. 00026 NetworkInterface *net = NetworkInterface::get_default_instance(); 00027 00028 // Default block device available on the target board 00029 BlockDevice *bd = BlockDevice::get_default_instance(); 00030 00031 #if COMPONENT_SD || COMPONENT_NUSD 00032 // Use FATFileSystem for SD card type blockdevices 00033 FATFileSystem fs("fs", bd); 00034 #else 00035 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions 00036 LittleFileSystem fs("fs", bd); 00037 #endif 00038 00039 #if USE_BUTTON == 1 00040 InterruptIn button(BUTTON1); 00041 #endif /* USE_BUTTON */ 00042 00043 // Default LED to use for PUT/POST example 00044 DigitalOut led(LED1); 00045 00046 // Declaring pointers for access to Pelion Device Management Client resources outside of main() 00047 MbedCloudClientResource *button_res; 00048 MbedCloudClientResource *led_res; 00049 MbedCloudClientResource *post_res; 00050 00051 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) 00052 // 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 00053 EventQueue eventQueue; 00054 00055 /** 00056 * PUT handler - sets the value of the built-in LED 00057 * @param resource The resource that triggered the callback 00058 * @param newValue Updated value for the resource 00059 */ 00060 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { 00061 printf("PUT received. New value: %s\n", newValue.c_str()); 00062 int tmp=atoi(newValue.c_str()); 00063 if(tmp!=0 && tmp!=1) { 00064 printf("Unexpected value : %d!!!\n", tmp); 00065 return; 00066 } 00067 led = atoi(newValue.c_str()); 00068 } 00069 00070 /** 00071 * POST handler - prints the content of the payload 00072 * @param resource The resource that triggered the callback 00073 * @param buffer If a body was passed to the POST function, this contains the data. 00074 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00075 * @param size Size of the body 00076 */ 00077 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00078 printf("POST received (length %u). Payload: ", size); 00079 for (size_t ix = 0; ix < size; ix++) { 00080 printf("%02x ", buffer[ix]); 00081 } 00082 printf("\n"); 00083 } 00084 00085 /** 00086 * Button handler 00087 * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) 00088 */ 00089 void button_press() { 00090 int v = button_res->get_value_int() + 1; 00091 button_res->set_value(v); 00092 printf("Button clicked %d times\n", v); 00093 } 00094 00095 /** 00096 * Notification callback handler 00097 * @param resource The resource that triggered the callback 00098 * @param status The delivery status of the notification 00099 */ 00100 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00101 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); 00102 } 00103 00104 /** 00105 * Registration callback handler 00106 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00107 */ 00108 void registered(const ConnectorClientEndpointInfo *endpoint) { 00109 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00110 } 00111 00112 int main(void) { 00113 printf("\nStarting Simple Pelion Device Management Client example\n"); 00114 00115 #if USE_BUTTON == 1 00116 // If the User button is pressed ons start, then format storage. 00117 if (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE) { 00118 printf("User button is pushed on start. Formatting the storage...\n"); 00119 int storage_status = StorageHelper::format(&fs, bd); 00120 if (storage_status != 0) { 00121 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); 00122 } 00123 } else { 00124 printf("You can hold the user button during boot to format the storage and change the device identity.\n"); 00125 } 00126 #endif /* USE_BUTTON */ 00127 00128 // Connect to the Internet (DHCP is expected to be on) 00129 printf("Connecting to the network using the default network interface...\n"); 00130 net = NetworkInterface::get_default_instance(); 00131 00132 nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION; 00133 while ((net_status = net->connect()) != NSAPI_ERROR_OK) { 00134 printf("Unable to connect to network (%d). Retrying...\n", net_status); 00135 } 00136 00137 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00138 00139 printf("Initializing Pelion Device Management Client...\n"); 00140 00141 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management 00142 SimpleMbedCloudClient client(net, bd, &fs); 00143 int client_status = client.init(); 00144 if (client_status != 0) { 00145 printf("Pelion Client initialization failed (%d)\n", client_status); 00146 return -1; 00147 } 00148 00149 // Creating resources, which can be written or read from the cloud 00150 button_res = client.create_resource("3200/0/5501", "button_count"); 00151 button_res->set_value(0); 00152 button_res->methods(M2MMethod::GET); 00153 button_res->observable(true); 00154 button_res->attach_notification_callback(button_callback); 00155 00156 led_res = client.create_resource("3201/0/5853", "led_state"); 00157 led_res->set_value(led.read()); 00158 led_res->methods(M2MMethod::GET | M2MMethod::PUT); 00159 led_res->attach_put_callback(put_callback); 00160 00161 post_res = client.create_resource("3300/0/5605", "execute_function"); 00162 post_res->methods(M2MMethod::POST); 00163 post_res->attach_post_callback(post_callback); 00164 00165 printf("Initialized Pelion Device Management Client. Registering...\n"); 00166 00167 // Callback that fires when registering is complete 00168 client.on_registered(®istered); 00169 00170 // Register with Pelion DM 00171 client.register_and_connect(); 00172 00173 #if USE_BUTTON == 1 00174 // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00175 button.fall(eventQueue.event(&button_press)); 00176 printf("Press the user button to increment the LwM2M resource value...\n"); 00177 #else 00178 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00179 Ticker timer; 00180 timer.attach(eventQueue.event(&button_press), 5.0); 00181 printf("Simulating button press every 5 seconds...\n"); 00182 #endif /* USE_BUTTON */ 00183 00184 // You can easily run the eventQueue in a separate thread if required 00185 eventQueue.dispatch_forever(); 00186 } 00187 00188 #endif /* MBED_TEST_MODE */
Generated on Mon Jul 18 2022 19:38:04 by
1.7.2