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 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 #if defined(TARGET_WIO_3G) 00029 DigitalOut SD_POWER(PA_15, 1); 00030 #endif 00031 00032 // Default block device 00033 BlockDevice* bd = BlockDevice::get_default_instance(); 00034 FATFileSystem fs("sd", bd); 00035 00036 // Default network interface object 00037 CellularBase *net; 00038 00039 // Declaring pointers for access to Mbed Cloud Client resources outside of main() 00040 MbedCloudClientResource *button_res; 00041 MbedCloudClientResource *pattern_res; 00042 00043 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button 00044 void fake_button_press() { 00045 int v = button_res->get_value_int() + 1; 00046 00047 button_res->set_value(v); 00048 00049 printf("Simulated button clicked %d times\n", v); 00050 } 00051 00052 /** 00053 * PUT handler 00054 * @param resource The resource that triggered the callback 00055 * @param newValue Updated value for the resource 00056 */ 00057 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { 00058 printf("PUT received, new value: %s\n", newValue.c_str()); 00059 } 00060 00061 /** 00062 * POST handler 00063 * @param resource The resource that triggered the callback 00064 * @param buffer If a body was passed to the POST function, this contains the data. 00065 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00066 * @param size Size of the body 00067 */ 00068 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00069 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); 00070 00071 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern 00072 00073 // Parse the pattern string, and toggle the LED in that pattern 00074 string s = std::string(pattern_res->get_value().c_str()); 00075 size_t i = 0; 00076 size_t pos = s.find(':'); 00077 while (pos != string::npos) { 00078 wait_ms(atoi(s.substr(i, pos - i).c_str())); 00079 augmentedLed = !augmentedLed; 00080 00081 i = ++pos; 00082 pos = s.find(':', pos); 00083 00084 if (pos == string::npos) { 00085 wait_ms(atoi(s.substr(i, s.length()).c_str())); 00086 augmentedLed = !augmentedLed; 00087 } 00088 } 00089 } 00090 00091 /** 00092 * Notification callback handler 00093 * @param resource The resource that triggered the callback 00094 * @param status The delivery status of the notification 00095 */ 00096 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00097 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); 00098 } 00099 00100 /** 00101 * Registration callback handler 00102 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00103 */ 00104 void registered(const ConnectorClientEndpointInfo *endpoint) { 00105 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00106 } 00107 00108 int main(void) { 00109 printf("Starting Simple Pelion Device Management Client example\n"); 00110 printf("Connecting to the network...\n"); 00111 00112 net = CellularBase::get_default_instance(); 00113 00114 /* Set Pin code for SIM card */ 00115 net->set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE); 00116 00117 /* Set network credentials here, e.g., APN */ 00118 net->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD); 00119 00120 nsapi_error_t status = net->connect(); 00121 00122 if (status != NSAPI_ERROR_OK) { 00123 printf("Connecting to the network failed %d!\n", status); 00124 return -1; 00125 } 00126 00127 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00128 00129 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud 00130 SimpleMbedCloudClient client(net, bd, &fs); 00131 int client_status = client.init(); 00132 if (client_status != 0) { 00133 printf("Pelion Client initialization failed (%d)\n", client_status); 00134 return -1; 00135 } 00136 00137 // Creating resources, which can be written or read from the cloud 00138 button_res = client.create_resource("3200/0/5501", "button_count"); 00139 button_res->set_value(0); 00140 button_res->methods(M2MMethod::GET); 00141 button_res->observable(true); 00142 button_res->attach_notification_callback(button_callback); 00143 00144 pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); 00145 pattern_res->set_value("500:500:500:500:500:500:500:500"); 00146 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); 00147 pattern_res->attach_put_callback(pattern_updated); 00148 00149 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); 00150 blink_res->methods(M2MMethod::POST); 00151 blink_res->attach_post_callback(blink_callback); 00152 00153 printf("Initialized Pelion Client. Registering...\n"); 00154 00155 // Callback that fires when registering is complete 00156 client.on_registered(®istered); 00157 00158 // Register with Mbed Cloud 00159 client.register_and_connect(); 00160 00161 // Placeholder for callback to update local resource when GET comes. 00162 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00163 Ticker timer; 00164 timer.attach(eventQueue.event(&fake_button_press), 5.0); 00165 00166 // You can easily run the eventQueue in a separate thread if required 00167 eventQueue.dispatch_forever(); 00168 } 00169 #endif
Generated on Sun Jul 17 2022 02:04:38 by
