pelion-example-common

Dependencies:   ublox-at-cellular-interface ublox-cellular-base

Committer:
fahimalavi
Date:
Mon Jun 24 17:29:58 2019 +0500
Revision:
10:4c2ed226f5a8
Parent:
7:230af466efc4
Child:
11:7cdc7397d270
Updating example to use ublox-at-cellular-interface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:a076a1bbe630 1 // ----------------------------------------------------------------------------
screamer 0:a076a1bbe630 2 // Copyright 2016-2018 ARM Ltd.
screamer 0:a076a1bbe630 3 //
screamer 0:a076a1bbe630 4 // SPDX-License-Identifier: Apache-2.0
screamer 0:a076a1bbe630 5 //
screamer 0:a076a1bbe630 6 // Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:a076a1bbe630 7 // you may not use this file except in compliance with the License.
screamer 0:a076a1bbe630 8 // You may obtain a copy of the License at
screamer 0:a076a1bbe630 9 //
screamer 0:a076a1bbe630 10 // http://www.apache.org/licenses/LICENSE-2.0
screamer 0:a076a1bbe630 11 //
screamer 0:a076a1bbe630 12 // Unless required by applicable law or agreed to in writing, software
screamer 0:a076a1bbe630 13 // distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:a076a1bbe630 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:a076a1bbe630 15 // See the License for the specific language governing permissions and
screamer 0:a076a1bbe630 16 // limitations under the License.
screamer 0:a076a1bbe630 17 // ----------------------------------------------------------------------------
screamer 0:a076a1bbe630 18 #ifndef MBED_TEST_MODE
screamer 0:a076a1bbe630 19
screamer 0:a076a1bbe630 20 #include "mbed.h"
screamer 0:a076a1bbe630 21 #include "simple-mbed-cloud-client.h"
screamer 0:a076a1bbe630 22 #include "FATFileSystem.h"
screamer 0:a076a1bbe630 23 #include "LittleFileSystem.h"
fahimalavi 10:4c2ed226f5a8 24 #include "UbloxATCellularInterface.h"
fahimalavi 10:4c2ed226f5a8 25
fahimalavi 10:4c2ed226f5a8 26 #define PIN "0000"
screamer 0:a076a1bbe630 27
screamer 3:3b2db67b206e 28 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
screamer 0:a076a1bbe630 29 NetworkInterface *net = NetworkInterface::get_default_instance();
screamer 0:a076a1bbe630 30
screamer 3:3b2db67b206e 31 // Default block device available on the target board
screamer 0:a076a1bbe630 32 BlockDevice *bd = BlockDevice::get_default_instance();
screamer 3:3b2db67b206e 33
screamer 3:3b2db67b206e 34 #if COMPONENT_SD || COMPONENT_NUSD
screamer 3:3b2db67b206e 35 // Use FATFileSystem for SD card type blockdevices
screamer 7:230af466efc4 36 FATFileSystem fs("fs");
screamer 3:3b2db67b206e 37 #else
screamer 3:3b2db67b206e 38 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
screamer 7:230af466efc4 39 LittleFileSystem fs("fs");
screamer 3:3b2db67b206e 40 #endif
screamer 3:3b2db67b206e 41
screamer 7:230af466efc4 42 // Default User button for GET example
screamer 6:2fb5057c0e42 43 InterruptIn button(BUTTON1);
screamer 0:a076a1bbe630 44 // Default LED to use for PUT/POST example
screamer 7:230af466efc4 45 DigitalOut led(LED1, 0);
screamer 1:a50c1e691ff1 46
screamer 6:2fb5057c0e42 47 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
screamer 7:230af466efc4 48 MbedCloudClientResource *res_button;
screamer 7:230af466efc4 49 MbedCloudClientResource *res_led;
screamer 7:230af466efc4 50 MbedCloudClientResource *res_post;
screamer 0:a076a1bbe630 51
screamer 6:2fb5057c0e42 52 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
screamer 6:2fb5057c0e42 53 // 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
screamer 6:2fb5057c0e42 54 EventQueue eventQueue;
screamer 0:a076a1bbe630 55
screamer 7:230af466efc4 56 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
screamer 7:230af466efc4 57 static const ConnectorClientEndpointInfo* endpointInfo;
screamer 7:230af466efc4 58
screamer 0:a076a1bbe630 59 /**
screamer 6:2fb5057c0e42 60 * PUT handler - sets the value of the built-in LED
screamer 0:a076a1bbe630 61 * @param resource The resource that triggered the callback
screamer 0:a076a1bbe630 62 * @param newValue Updated value for the resource
screamer 0:a076a1bbe630 63 */
screamer 6:2fb5057c0e42 64 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
screamer 7:230af466efc4 65 printf("*** PUT received, new value: %s \n", newValue.c_str());
screamer 0:a076a1bbe630 66 led = atoi(newValue.c_str());
screamer 0:a076a1bbe630 67 }
screamer 0:a076a1bbe630 68
screamer 0:a076a1bbe630 69 /**
screamer 6:2fb5057c0e42 70 * POST handler - prints the content of the payload
screamer 0:a076a1bbe630 71 * @param resource The resource that triggered the callback
screamer 0:a076a1bbe630 72 * @param buffer If a body was passed to the POST function, this contains the data.
screamer 0:a076a1bbe630 73 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
screamer 0:a076a1bbe630 74 * @param size Size of the body
screamer 0:a076a1bbe630 75 */
screamer 6:2fb5057c0e42 76 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
screamer 7:230af466efc4 77 printf("*** POST received (length %u). Payload: ", size);
screamer 6:2fb5057c0e42 78 for (size_t ix = 0; ix < size; ix++) {
screamer 6:2fb5057c0e42 79 printf("%02x ", buffer[ix]);
screamer 6:2fb5057c0e42 80 }
screamer 6:2fb5057c0e42 81 printf("\n");
screamer 0:a076a1bbe630 82 }
screamer 0:a076a1bbe630 83
screamer 0:a076a1bbe630 84 /**
screamer 6:2fb5057c0e42 85 * Button handler
screamer 6:2fb5057c0e42 86 * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below)
screamer 0:a076a1bbe630 87 */
screamer 0:a076a1bbe630 88 void button_press() {
screamer 7:230af466efc4 89 int v = res_button->get_value_int() + 1;
screamer 7:230af466efc4 90 res_button->set_value(v);
screamer 7:230af466efc4 91 printf("*** Button clicked %d times \n", v);
screamer 0:a076a1bbe630 92 }
screamer 0:a076a1bbe630 93
screamer 0:a076a1bbe630 94 /**
screamer 0:a076a1bbe630 95 * Notification callback handler
screamer 0:a076a1bbe630 96 * @param resource The resource that triggered the callback
screamer 0:a076a1bbe630 97 * @param status The delivery status of the notification
screamer 0:a076a1bbe630 98 */
screamer 0:a076a1bbe630 99 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
screamer 7:230af466efc4 100 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status);
screamer 0:a076a1bbe630 101 }
screamer 0:a076a1bbe630 102
screamer 0:a076a1bbe630 103 /**
screamer 0:a076a1bbe630 104 * Registration callback handler
screamer 0:a076a1bbe630 105 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
screamer 0:a076a1bbe630 106 */
screamer 0:a076a1bbe630 107 void registered(const ConnectorClientEndpointInfo *endpoint) {
screamer 3:3b2db67b206e 108 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
screamer 7:230af466efc4 109 endpointInfo = endpoint;
screamer 0:a076a1bbe630 110 }
screamer 0:a076a1bbe630 111
screamer 0:a076a1bbe630 112 int main(void) {
screamer 3:3b2db67b206e 113 printf("\nStarting Simple Pelion Device Management Client example\n");
screamer 0:a076a1bbe630 114
screamer 7:230af466efc4 115 int storage_status = fs.mount(bd);
screamer 7:230af466efc4 116 if (storage_status != 0) {
screamer 7:230af466efc4 117 printf("Storage mounting failed.\n");
screamer 7:230af466efc4 118 }
screamer 0:a076a1bbe630 119 // If the User button is pressed ons start, then format storage.
screamer 7:230af466efc4 120 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
screamer 7:230af466efc4 121 if (btn_pressed) {
screamer 7:230af466efc4 122 printf("User button is pushed on start...\n");
screamer 7:230af466efc4 123 }
screamer 7:230af466efc4 124
screamer 7:230af466efc4 125 if (storage_status || btn_pressed) {
screamer 7:230af466efc4 126 printf("Formatting the storage...\n");
screamer 6:2fb5057c0e42 127 int storage_status = StorageHelper::format(&fs, bd);
screamer 0:a076a1bbe630 128 if (storage_status != 0) {
screamer 0:a076a1bbe630 129 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
screamer 0:a076a1bbe630 130 }
screamer 3:3b2db67b206e 131 } else {
screamer 3:3b2db67b206e 132 printf("You can hold the user button during boot to format the storage and change the device identity.\n");
screamer 0:a076a1bbe630 133 }
screamer 0:a076a1bbe630 134
screamer 6:2fb5057c0e42 135 // Connect to the Internet (DHCP is expected to be on)
screamer 6:2fb5057c0e42 136 printf("Connecting to the network using the default network interface...\n");
fahimalavi 10:4c2ed226f5a8 137 #ifdef TARGET_UBLOX_C030_R41XM
fahimalavi 10:4c2ed226f5a8 138 net = new UbloxATCellularInterface();
fahimalavi 10:4c2ed226f5a8 139
fahimalavi 10:4c2ed226f5a8 140 if (((UbloxATCellularInterface*)net)->init(PIN)) {
fahimalavi 10:4c2ed226f5a8 141
fahimalavi 10:4c2ed226f5a8 142 if ( (((UbloxATCellularInterface*)net)->is_registered_csd() || ((UbloxATCellularInterface*)net)->is_registered_psd() || ((UbloxATCellularInterface*)net)->is_registered_eps()) ) {
fahimalavi 10:4c2ed226f5a8 143 printf("De-registering...\n\n");
fahimalavi 10:4c2ed226f5a8 144 ((UbloxATCellularInterface*)net)->nwk_deregistration();
fahimalavi 10:4c2ed226f5a8 145 }
fahimalavi 10:4c2ed226f5a8 146
fahimalavi 10:4c2ed226f5a8 147
fahimalavi 10:4c2ed226f5a8 148 if (((UbloxATCellularInterface*)net)->set_modem_rat(UbloxATCellularInterface::GPRS_EGPRS)) {
fahimalavi 10:4c2ed226f5a8 149 printf("GPRS_EGPRS RAT configured\n");
fahimalavi 10:4c2ed226f5a8 150 }
fahimalavi 10:4c2ed226f5a8 151
fahimalavi 10:4c2ed226f5a8 152 if (((UbloxATCellularInterface*)net)->reboot_modem()) {
fahimalavi 10:4c2ed226f5a8 153 printf("Rebooting modem\n");
fahimalavi 10:4c2ed226f5a8 154 }
fahimalavi 10:4c2ed226f5a8 155 }
fahimalavi 10:4c2ed226f5a8 156 #else
screamer 0:a076a1bbe630 157 net = NetworkInterface::get_default_instance();
fahimalavi 10:4c2ed226f5a8 158 #endif
screamer 0:a076a1bbe630 159
screamer 6:2fb5057c0e42 160 nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
screamer 6:2fb5057c0e42 161 while ((net_status = net->connect()) != NSAPI_ERROR_OK) {
screamer 6:2fb5057c0e42 162 printf("Unable to connect to network (%d). Retrying...\n", net_status);
screamer 0:a076a1bbe630 163 }
screamer 0:a076a1bbe630 164
screamer 0:a076a1bbe630 165 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
screamer 0:a076a1bbe630 166
screamer 6:2fb5057c0e42 167 printf("Initializing Pelion Device Management Client...\n");
screamer 6:2fb5057c0e42 168
screamer 6:2fb5057c0e42 169 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
screamer 0:a076a1bbe630 170 SimpleMbedCloudClient client(net, bd, &fs);
screamer 0:a076a1bbe630 171 int client_status = client.init();
screamer 0:a076a1bbe630 172 if (client_status != 0) {
screamer 6:2fb5057c0e42 173 printf("Pelion Client initialization failed (%d)\n", client_status);
screamer 0:a076a1bbe630 174 return -1;
screamer 0:a076a1bbe630 175 }
screamer 0:a076a1bbe630 176
screamer 0:a076a1bbe630 177 // Creating resources, which can be written or read from the cloud
screamer 7:230af466efc4 178 res_button = client.create_resource("3200/0/5501", "Button Count");
screamer 7:230af466efc4 179 res_button->set_value(0);
screamer 7:230af466efc4 180 res_button->methods(M2MMethod::GET);
screamer 7:230af466efc4 181 res_button->observable(true);
screamer 7:230af466efc4 182 res_button->attach_notification_callback(button_callback);
screamer 0:a076a1bbe630 183
screamer 7:230af466efc4 184 res_led = client.create_resource("3201/0/5853", "LED State");
screamer 7:230af466efc4 185 res_led->set_value(led.read());
screamer 7:230af466efc4 186 res_led->methods(M2MMethod::GET | M2MMethod::PUT);
screamer 7:230af466efc4 187 res_led->attach_put_callback(put_callback);
screamer 0:a076a1bbe630 188
screamer 7:230af466efc4 189 res_post = client.create_resource("3300/0/5605", "Execute Function");
screamer 7:230af466efc4 190 res_post->methods(M2MMethod::POST);
screamer 7:230af466efc4 191 res_post->attach_post_callback(post_callback);
screamer 0:a076a1bbe630 192
screamer 6:2fb5057c0e42 193 printf("Initialized Pelion Device Management Client. Registering...\n");
screamer 0:a076a1bbe630 194
screamer 0:a076a1bbe630 195 // Callback that fires when registering is complete
screamer 0:a076a1bbe630 196 client.on_registered(&registered);
screamer 0:a076a1bbe630 197
screamer 0:a076a1bbe630 198 // Register with Pelion DM
screamer 0:a076a1bbe630 199 client.register_and_connect();
screamer 0:a076a1bbe630 200
screamer 6:2fb5057c0e42 201 // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
screamer 0:a076a1bbe630 202 button.fall(eventQueue.event(&button_press));
screamer 3:3b2db67b206e 203 printf("Press the user button to increment the LwM2M resource value...\n");
screamer 0:a076a1bbe630 204
screamer 0:a076a1bbe630 205 // You can easily run the eventQueue in a separate thread if required
screamer 0:a076a1bbe630 206 eventQueue.dispatch_forever();
screamer 0:a076a1bbe630 207 }
screamer 6:2fb5057c0e42 208
screamer 6:2fb5057c0e42 209 #endif /* MBED_TEST_MODE */