Generic Pelion Device Management example for various U-blox-based boards.
Dependencies: ublox-at-cellular-interface ublox-cellular-base
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 #ifdef TARGET_UBLOX_C030_R41XM 00026 #include "UbloxATCellularInterface.h" 00027 #endif 00028 00029 #define PIN "0000" 00030 00031 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. 00032 NetworkInterface *net; 00033 00034 // Default block device available on the target board 00035 BlockDevice *bd = BlockDevice::get_default_instance(); 00036 00037 #if COMPONENT_SD || COMPONENT_NUSD 00038 // Use FATFileSystem for SD card type blockdevices 00039 FATFileSystem fs("fs"); 00040 #else 00041 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions 00042 LittleFileSystem fs("fs"); 00043 #endif 00044 00045 // Default User button for GET example 00046 InterruptIn button(BUTTON1); 00047 // Default LED to use for PUT/POST example 00048 DigitalOut led(LED1, 0); 00049 00050 // Declaring pointers for access to Pelion Device Management Client resources outside of main() 00051 MbedCloudClientResource *res_button; 00052 MbedCloudClientResource *res_led; 00053 MbedCloudClientResource *res_post; 00054 00055 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) 00056 // 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 00057 EventQueue eventQueue; 00058 00059 // When the device is registered, this variable will be used to access various useful information, like device ID etc. 00060 static const ConnectorClientEndpointInfo* endpointInfo; 00061 00062 /** 00063 * PUT handler - sets the value of the built-in LED 00064 * @param resource The resource that triggered the callback 00065 * @param newValue Updated value for the resource 00066 */ 00067 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { 00068 printf("*** PUT received, new value: %s \n", newValue.c_str()); 00069 led = atoi(newValue.c_str()); 00070 } 00071 00072 /** 00073 * POST handler - prints the content of the payload 00074 * @param resource The resource that triggered the callback 00075 * @param buffer If a body was passed to the POST function, this contains the data. 00076 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00077 * @param size Size of the body 00078 */ 00079 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00080 printf("*** POST received (length %u). Payload: ", size); 00081 for (size_t ix = 0; ix < size; ix++) { 00082 printf("%02x ", buffer[ix]); 00083 } 00084 printf("\n"); 00085 } 00086 00087 /** 00088 * Button handler 00089 * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) 00090 */ 00091 void button_press() { 00092 int v = res_button->get_value_int() + 1; 00093 res_button->set_value(v); 00094 printf("*** Button clicked %d times \n", v); 00095 } 00096 00097 /** 00098 * Notification callback handler 00099 * @param resource The resource that triggered the callback 00100 * @param status The delivery status of the notification 00101 */ 00102 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00103 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); 00104 } 00105 00106 /** 00107 * Registration callback handler 00108 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00109 */ 00110 void registered(const ConnectorClientEndpointInfo *endpoint) { 00111 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00112 endpointInfo = endpoint; 00113 } 00114 00115 int main(void) { 00116 printf("\nStarting Simple Pelion Device Management Client example\n"); 00117 00118 int storage_status = fs.mount(bd); 00119 if (storage_status != 0) { 00120 printf("Storage mounting failed.\n"); 00121 } 00122 // If the User button is pressed ons start, then format storage. 00123 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); 00124 if (btn_pressed) { 00125 printf("User button is pushed on start...\n"); 00126 } 00127 00128 if (storage_status || btn_pressed) { 00129 printf("Formatting the storage...\n"); 00130 int storage_status = StorageHelper::format(&fs, bd); 00131 if (storage_status != 0) { 00132 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); 00133 } 00134 } else { 00135 printf("You can hold the user button during boot to format the storage and change the device identity.\n"); 00136 } 00137 00138 // Connect to the Internet (DHCP is expected to be on) 00139 printf("Connecting to the network using the default network interface...\n"); 00140 #if !NSAPI_PPP_AVAILABLE 00141 net = new UbloxATCellularInterface(); 00142 00143 if (((UbloxATCellularInterface*)net)->init(PIN)) { 00144 00145 if ( (((UbloxATCellularInterface*)net)->is_registered_csd() || ((UbloxATCellularInterface*)net)->is_registered_psd() || ((UbloxATCellularInterface*)net)->is_registered_eps()) ) { 00146 printf("De-registering...\n\n"); 00147 ((UbloxATCellularInterface*)net)->nwk_deregistration(); 00148 } 00149 00150 00151 if (((UbloxATCellularInterface*)net)->set_modem_rat(UbloxATCellularInterface::GPRS_EGPRS)) { 00152 printf("GPRS_EGPRS RAT configured\n"); 00153 } 00154 00155 if (((UbloxATCellularInterface*)net)->reboot_modem()) { 00156 printf("Rebooting modem\n"); 00157 } 00158 } 00159 #else 00160 net = NetworkInterface::get_default_instance(); 00161 #endif 00162 00163 nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION; 00164 while ((net_status = net->connect()) != NSAPI_ERROR_OK) { 00165 printf("Unable to connect to network (%d). Retrying...\n", net_status); 00166 } 00167 00168 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00169 00170 printf("Initializing Pelion Device Management Client...\n"); 00171 00172 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management 00173 SimpleMbedCloudClient client(net, bd, &fs); 00174 int client_status = client.init(); 00175 if (client_status != 0) { 00176 printf("Pelion Client initialization failed (%d)\n", client_status); 00177 return -1; 00178 } 00179 00180 // Creating resources, which can be written or read from the cloud 00181 res_button = client.create_resource("3200/0/5501", "Button Count"); 00182 res_button->set_value(0); 00183 res_button->methods(M2MMethod::GET); 00184 res_button->observable(true); 00185 res_button->attach_notification_callback(button_callback); 00186 00187 res_led = client.create_resource("3201/0/5853", "LED State"); 00188 res_led->set_value(led.read()); 00189 res_led->methods(M2MMethod::GET | M2MMethod::PUT); 00190 res_led->attach_put_callback(put_callback); 00191 00192 res_post = client.create_resource("3300/0/5605", "Execute Function"); 00193 res_post->methods(M2MMethod::POST); 00194 res_post->attach_post_callback(post_callback); 00195 00196 printf("Initialized Pelion Device Management Client. Registering...\n"); 00197 00198 // Callback that fires when registering is complete 00199 client.on_registered(®istered); 00200 00201 // Register with Pelion DM 00202 client.register_and_connect(); 00203 00204 // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00205 button.fall(eventQueue.event(&button_press)); 00206 printf("Press the user button to increment the LwM2M resource value...\n"); 00207 00208 // You can easily run the eventQueue in a separate thread if required 00209 eventQueue.dispatch_forever(); 00210 } 00211 00212 #endif /* MBED_TEST_MODE */
Generated on Tue Jul 12 2022 22:05:13 by 1.7.2