![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example
Dependencies: FXAS21002 FXOS8700Q
main.cpp@0:11cc2b7889af, 2019-11-19 (annotated)
- Committer:
- maygup01
- Date:
- Tue Nov 19 09:49:38 2019 +0000
- Revision:
- 0:11cc2b7889af
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maygup01 | 0:11cc2b7889af | 1 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 2 | // Copyright 2016-2018 ARM Ltd. |
maygup01 | 0:11cc2b7889af | 3 | // |
maygup01 | 0:11cc2b7889af | 4 | // SPDX-License-Identifier: Apache-2.0 |
maygup01 | 0:11cc2b7889af | 5 | // |
maygup01 | 0:11cc2b7889af | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
maygup01 | 0:11cc2b7889af | 7 | // you may not use this file except in compliance with the License. |
maygup01 | 0:11cc2b7889af | 8 | // You may obtain a copy of the License at |
maygup01 | 0:11cc2b7889af | 9 | // |
maygup01 | 0:11cc2b7889af | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
maygup01 | 0:11cc2b7889af | 11 | // |
maygup01 | 0:11cc2b7889af | 12 | // Unless required by applicable law or agreed to in writing, software |
maygup01 | 0:11cc2b7889af | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
maygup01 | 0:11cc2b7889af | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maygup01 | 0:11cc2b7889af | 15 | // See the License for the specific language governing permissions and |
maygup01 | 0:11cc2b7889af | 16 | // limitations under the License. |
maygup01 | 0:11cc2b7889af | 17 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 18 | #ifndef MBED_TEST_MODE |
maygup01 | 0:11cc2b7889af | 19 | |
maygup01 | 0:11cc2b7889af | 20 | #include "mbed.h" |
maygup01 | 0:11cc2b7889af | 21 | #include "simple-mbed-cloud-client.h" |
maygup01 | 0:11cc2b7889af | 22 | #include "FATFileSystem.h" |
maygup01 | 0:11cc2b7889af | 23 | #include "LittleFileSystem.h" |
maygup01 | 0:11cc2b7889af | 24 | |
maygup01 | 0:11cc2b7889af | 25 | // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. |
maygup01 | 0:11cc2b7889af | 26 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
maygup01 | 0:11cc2b7889af | 27 | |
maygup01 | 0:11cc2b7889af | 28 | // Default block device available on the target board |
maygup01 | 0:11cc2b7889af | 29 | BlockDevice *bd = BlockDevice::get_default_instance(); |
maygup01 | 0:11cc2b7889af | 30 | |
maygup01 | 0:11cc2b7889af | 31 | #if COMPONENT_SD || COMPONENT_NUSD |
maygup01 | 0:11cc2b7889af | 32 | // Use FATFileSystem for SD card type blockdevices |
maygup01 | 0:11cc2b7889af | 33 | FATFileSystem fs("fs"); |
maygup01 | 0:11cc2b7889af | 34 | #else |
maygup01 | 0:11cc2b7889af | 35 | // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions |
maygup01 | 0:11cc2b7889af | 36 | LittleFileSystem fs("fs"); |
maygup01 | 0:11cc2b7889af | 37 | #endif |
maygup01 | 0:11cc2b7889af | 38 | |
maygup01 | 0:11cc2b7889af | 39 | // Default User button for GET example and for resetting the storage |
maygup01 | 0:11cc2b7889af | 40 | InterruptIn button(BUTTON1); |
maygup01 | 0:11cc2b7889af | 41 | // Default LED to use for PUT/POST example |
maygup01 | 0:11cc2b7889af | 42 | DigitalOut led(LED1, 0); |
maygup01 | 0:11cc2b7889af | 43 | |
maygup01 | 0:11cc2b7889af | 44 | // How often to fetch sensor data (in seconds) |
maygup01 | 0:11cc2b7889af | 45 | #define SENSORS_POLL_INTERVAL 3.0 |
maygup01 | 0:11cc2b7889af | 46 | |
maygup01 | 0:11cc2b7889af | 47 | // Send all sensor data or just limited (useful for when running out of memory) |
maygup01 | 0:11cc2b7889af | 48 | //#define SEND_ALL_SENSORS |
maygup01 | 0:11cc2b7889af | 49 | |
maygup01 | 0:11cc2b7889af | 50 | // Sensors related includes and initialization |
maygup01 | 0:11cc2b7889af | 51 | #include "FXOS8700Q.h" |
maygup01 | 0:11cc2b7889af | 52 | #include "FXAS21002.h" |
maygup01 | 0:11cc2b7889af | 53 | |
maygup01 | 0:11cc2b7889af | 54 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 55 | I2C sens_i2c(PTD9, PTD8); |
maygup01 | 0:11cc2b7889af | 56 | #else |
maygup01 | 0:11cc2b7889af | 57 | I2C sens_i2c(PTE25, PTE24); |
maygup01 | 0:11cc2b7889af | 58 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 59 | |
maygup01 | 0:11cc2b7889af | 60 | FXOS8700QAccelerometer sens_acc(sens_i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
maygup01 | 0:11cc2b7889af | 61 | FXOS8700QMagnetometer sens_mag(sens_i2c, FXOS8700CQ_SLAVE_ADDR1); |
maygup01 | 0:11cc2b7889af | 62 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 63 | FXAS21002 sens_gyro(PTD9, PTD8, 0x20); |
maygup01 | 0:11cc2b7889af | 64 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 65 | |
maygup01 | 0:11cc2b7889af | 66 | // Declaring pointers for access to Pelion Device Management Client resources outside of main() |
maygup01 | 0:11cc2b7889af | 67 | MbedCloudClientResource *res_button; |
maygup01 | 0:11cc2b7889af | 68 | MbedCloudClientResource *res_led; |
maygup01 | 0:11cc2b7889af | 69 | MbedCloudClientResource *res_post; |
maygup01 | 0:11cc2b7889af | 70 | |
maygup01 | 0:11cc2b7889af | 71 | // Additional resources for sensor readings |
maygup01 | 0:11cc2b7889af | 72 | #ifdef SEND_ALL_SENSORS |
maygup01 | 0:11cc2b7889af | 73 | MbedCloudClientResource *res_magnometer_x; |
maygup01 | 0:11cc2b7889af | 74 | MbedCloudClientResource *res_magnometer_y; |
maygup01 | 0:11cc2b7889af | 75 | MbedCloudClientResource *res_magnometer_z; |
maygup01 | 0:11cc2b7889af | 76 | MbedCloudClientResource *res_accelerometer_x; |
maygup01 | 0:11cc2b7889af | 77 | MbedCloudClientResource *res_accelerometer_y; |
maygup01 | 0:11cc2b7889af | 78 | MbedCloudClientResource *res_accelerometer_z; |
maygup01 | 0:11cc2b7889af | 79 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 80 | MbedCloudClientResource *res_gyroscope_x; |
maygup01 | 0:11cc2b7889af | 81 | MbedCloudClientResource *res_gyroscope_y; |
maygup01 | 0:11cc2b7889af | 82 | MbedCloudClientResource *res_gyroscope_z; |
maygup01 | 0:11cc2b7889af | 83 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 84 | #endif /* SEND_ALL_SENSORS */ |
maygup01 | 0:11cc2b7889af | 85 | |
maygup01 | 0:11cc2b7889af | 86 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
maygup01 | 0:11cc2b7889af | 87 | // 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 |
maygup01 | 0:11cc2b7889af | 88 | EventQueue eventQueue; |
maygup01 | 0:11cc2b7889af | 89 | |
maygup01 | 0:11cc2b7889af | 90 | // When the device is registered, this variable will be used to access various useful information, like device ID etc. |
maygup01 | 0:11cc2b7889af | 91 | static const ConnectorClientEndpointInfo* endpointInfo; |
maygup01 | 0:11cc2b7889af | 92 | |
maygup01 | 0:11cc2b7889af | 93 | /** |
maygup01 | 0:11cc2b7889af | 94 | * PUT handler - sets the value of the built-in LED |
maygup01 | 0:11cc2b7889af | 95 | * @param resource The resource that triggered the callback |
maygup01 | 0:11cc2b7889af | 96 | * @param newValue Updated value for the resource |
maygup01 | 0:11cc2b7889af | 97 | */ |
maygup01 | 0:11cc2b7889af | 98 | void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { |
maygup01 | 0:11cc2b7889af | 99 | printf("*** PUT received, new value: %s \n", newValue.c_str()); |
maygup01 | 0:11cc2b7889af | 100 | led = atoi(newValue.c_str()); |
maygup01 | 0:11cc2b7889af | 101 | } |
maygup01 | 0:11cc2b7889af | 102 | |
maygup01 | 0:11cc2b7889af | 103 | /** |
maygup01 | 0:11cc2b7889af | 104 | * POST handler - prints the content of the payload |
maygup01 | 0:11cc2b7889af | 105 | * @param resource The resource that triggered the callback |
maygup01 | 0:11cc2b7889af | 106 | * @param buffer If a body was passed to the POST function, this contains the data. |
maygup01 | 0:11cc2b7889af | 107 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
maygup01 | 0:11cc2b7889af | 108 | * @param size Size of the body |
maygup01 | 0:11cc2b7889af | 109 | */ |
maygup01 | 0:11cc2b7889af | 110 | void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
maygup01 | 0:11cc2b7889af | 111 | printf("*** POST received (length %u). Payload: ", size); |
maygup01 | 0:11cc2b7889af | 112 | for (size_t ix = 0; ix < size; ix++) { |
maygup01 | 0:11cc2b7889af | 113 | printf("%02x ", buffer[ix]); |
maygup01 | 0:11cc2b7889af | 114 | } |
maygup01 | 0:11cc2b7889af | 115 | printf("\n"); |
maygup01 | 0:11cc2b7889af | 116 | } |
maygup01 | 0:11cc2b7889af | 117 | |
maygup01 | 0:11cc2b7889af | 118 | /** |
maygup01 | 0:11cc2b7889af | 119 | * Button handler |
maygup01 | 0:11cc2b7889af | 120 | * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) |
maygup01 | 0:11cc2b7889af | 121 | */ |
maygup01 | 0:11cc2b7889af | 122 | void button_press() { |
maygup01 | 0:11cc2b7889af | 123 | int v = res_button->get_value_int() + 1; |
maygup01 | 0:11cc2b7889af | 124 | res_button->set_value(v); |
maygup01 | 0:11cc2b7889af | 125 | printf("*** Button clicked %d times \n", v); |
maygup01 | 0:11cc2b7889af | 126 | } |
maygup01 | 0:11cc2b7889af | 127 | |
maygup01 | 0:11cc2b7889af | 128 | /** |
maygup01 | 0:11cc2b7889af | 129 | * Notification callback handler |
maygup01 | 0:11cc2b7889af | 130 | * @param resource The resource that triggered the callback |
maygup01 | 0:11cc2b7889af | 131 | * @param status The delivery status of the notification |
maygup01 | 0:11cc2b7889af | 132 | */ |
maygup01 | 0:11cc2b7889af | 133 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
maygup01 | 0:11cc2b7889af | 134 | printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); |
maygup01 | 0:11cc2b7889af | 135 | } |
maygup01 | 0:11cc2b7889af | 136 | |
maygup01 | 0:11cc2b7889af | 137 | /** |
maygup01 | 0:11cc2b7889af | 138 | * Registration callback handler |
maygup01 | 0:11cc2b7889af | 139 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
maygup01 | 0:11cc2b7889af | 140 | */ |
maygup01 | 0:11cc2b7889af | 141 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
maygup01 | 0:11cc2b7889af | 142 | printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
maygup01 | 0:11cc2b7889af | 143 | endpointInfo = endpoint; |
maygup01 | 0:11cc2b7889af | 144 | } |
maygup01 | 0:11cc2b7889af | 145 | |
maygup01 | 0:11cc2b7889af | 146 | /** |
maygup01 | 0:11cc2b7889af | 147 | * Initialize sensors |
maygup01 | 0:11cc2b7889af | 148 | */ |
maygup01 | 0:11cc2b7889af | 149 | void sensors_init() { |
maygup01 | 0:11cc2b7889af | 150 | printf ("\nSensors configuration:\n"); |
maygup01 | 0:11cc2b7889af | 151 | |
maygup01 | 0:11cc2b7889af | 152 | sens_acc.enable(); |
maygup01 | 0:11cc2b7889af | 153 | sens_mag.enable(); |
maygup01 | 0:11cc2b7889af | 154 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 155 | sens_gyro.activate(true); |
maygup01 | 0:11cc2b7889af | 156 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 157 | |
maygup01 | 0:11cc2b7889af | 158 | printf("FXOS8700Q accelerometer = 0x%X\n", sens_acc.whoAmI()); |
maygup01 | 0:11cc2b7889af | 159 | printf("FXOS8700Q magnetometer = 0x%X\n", sens_mag.whoAmI()); |
maygup01 | 0:11cc2b7889af | 160 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 161 | printf("FXAS21002 gyroscope = 0x%X\n", sens_gyro.getStatus()); |
maygup01 | 0:11cc2b7889af | 162 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 163 | |
maygup01 | 0:11cc2b7889af | 164 | printf("\n"); ; |
maygup01 | 0:11cc2b7889af | 165 | } |
maygup01 | 0:11cc2b7889af | 166 | |
maygup01 | 0:11cc2b7889af | 167 | /** |
maygup01 | 0:11cc2b7889af | 168 | * Update sensors and report their values. |
maygup01 | 0:11cc2b7889af | 169 | * This function is called periodically. |
maygup01 | 0:11cc2b7889af | 170 | */ |
maygup01 | 0:11cc2b7889af | 171 | void sensors_update() { |
maygup01 | 0:11cc2b7889af | 172 | motion_data_counts_t acc_raw, mag_raw; |
maygup01 | 0:11cc2b7889af | 173 | |
maygup01 | 0:11cc2b7889af | 174 | sens_acc.getAxis(acc_raw); |
maygup01 | 0:11cc2b7889af | 175 | sens_mag.getAxis(mag_raw); |
maygup01 | 0:11cc2b7889af | 176 | |
maygup01 | 0:11cc2b7889af | 177 | float mag_x = (double)mag_raw.x / 1000.0, mag_y = (double)mag_raw.y / 1000.0, mag_z = (double)mag_raw.z / 1000.0; |
maygup01 | 0:11cc2b7889af | 178 | float acc_x = (double)acc_raw.x / 1000.0, acc_y = (double)acc_raw.y / 1000.0, acc_z = (double)acc_raw.z / 1000.0; |
maygup01 | 0:11cc2b7889af | 179 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 180 | float gyro_x = (double)sens_gyro.getX() / 1000.0, gyro_y = (double)sens_gyro.getY() / 1000.0, gyro_z = (double)sens_gyro.getZ() / 1000.0; |
maygup01 | 0:11cc2b7889af | 181 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 182 | |
maygup01 | 0:11cc2b7889af | 183 | printf(" \n"); |
maygup01 | 0:11cc2b7889af | 184 | printf("FXOS8700Q mag: %7.3f x, %7.3f y, %7.3f z [gauss] \n", mag_x, mag_y, mag_z); |
maygup01 | 0:11cc2b7889af | 185 | printf("FXOS8700Q acc: %7.3f x, %7.3f y, %7.3f z [g] \n", acc_x, acc_y, acc_z); |
maygup01 | 0:11cc2b7889af | 186 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 187 | printf("FXAS21002 gryo: %7.3f x, %7.3f y, %7.3f z [dps] \n", gyro_x, gyro_y, gyro_z); |
maygup01 | 0:11cc2b7889af | 188 | printf("\r\033[4A"); |
maygup01 | 0:11cc2b7889af | 189 | #else |
maygup01 | 0:11cc2b7889af | 190 | printf("\r\033[3A"); |
maygup01 | 0:11cc2b7889af | 191 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 192 | |
maygup01 | 0:11cc2b7889af | 193 | if (endpointInfo) { |
maygup01 | 0:11cc2b7889af | 194 | #ifdef SEND_ALL_SENSORS |
maygup01 | 0:11cc2b7889af | 195 | res_accelerometer_x->set_value(acc_x); |
maygup01 | 0:11cc2b7889af | 196 | res_accelerometer_y->set_value(acc_y); |
maygup01 | 0:11cc2b7889af | 197 | res_accelerometer_z->set_value(acc_z); |
maygup01 | 0:11cc2b7889af | 198 | res_magnometer_x->set_value(mag_x); |
maygup01 | 0:11cc2b7889af | 199 | res_magnometer_y->set_value(mag_y); |
maygup01 | 0:11cc2b7889af | 200 | res_magnometer_z->set_value(mag_z); |
maygup01 | 0:11cc2b7889af | 201 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 202 | res_gyroscope_x->set_value(gyro_x); |
maygup01 | 0:11cc2b7889af | 203 | res_gyroscope_y->set_value(gyro_y); |
maygup01 | 0:11cc2b7889af | 204 | res_gyroscope_z->set_value(gyro_z); |
maygup01 | 0:11cc2b7889af | 205 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 206 | #endif /* SEND_ALL_SENSORS */ |
maygup01 | 0:11cc2b7889af | 207 | } |
maygup01 | 0:11cc2b7889af | 208 | } |
maygup01 | 0:11cc2b7889af | 209 | |
maygup01 | 0:11cc2b7889af | 210 | int main(void) { |
maygup01 | 0:11cc2b7889af | 211 | printf("\nStarting Simple Pelion Device Management Client example\n"); |
maygup01 | 0:11cc2b7889af | 212 | |
maygup01 | 0:11cc2b7889af | 213 | int storage_status = fs.mount(bd); |
maygup01 | 0:11cc2b7889af | 214 | if (storage_status != 0) { |
maygup01 | 0:11cc2b7889af | 215 | printf("Storage mounting failed.\n"); |
maygup01 | 0:11cc2b7889af | 216 | } |
maygup01 | 0:11cc2b7889af | 217 | #if USE_BUTTON == 1 |
maygup01 | 0:11cc2b7889af | 218 | // If the User button is pressed ons start, then format storage. |
maygup01 | 0:11cc2b7889af | 219 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
maygup01 | 0:11cc2b7889af | 220 | if (btn_pressed) { |
maygup01 | 0:11cc2b7889af | 221 | printf("User button is pushed on start...\n"); |
maygup01 | 0:11cc2b7889af | 222 | } |
maygup01 | 0:11cc2b7889af | 223 | #else |
maygup01 | 0:11cc2b7889af | 224 | bool btn_pressed = FALSE; |
maygup01 | 0:11cc2b7889af | 225 | #endif /* USE_BUTTON */ |
maygup01 | 0:11cc2b7889af | 226 | |
maygup01 | 0:11cc2b7889af | 227 | if (storage_status || btn_pressed) { |
maygup01 | 0:11cc2b7889af | 228 | printf("Formatting the storage...\n"); |
maygup01 | 0:11cc2b7889af | 229 | int storage_status = StorageHelper::format(&fs, bd); |
maygup01 | 0:11cc2b7889af | 230 | if (storage_status != 0) { |
maygup01 | 0:11cc2b7889af | 231 | printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); |
maygup01 | 0:11cc2b7889af | 232 | } |
maygup01 | 0:11cc2b7889af | 233 | } else { |
maygup01 | 0:11cc2b7889af | 234 | printf("You can hold the user button during boot to format the storage and change the device identity.\n"); |
maygup01 | 0:11cc2b7889af | 235 | } |
maygup01 | 0:11cc2b7889af | 236 | |
maygup01 | 0:11cc2b7889af | 237 | sensors_init(); |
maygup01 | 0:11cc2b7889af | 238 | |
maygup01 | 0:11cc2b7889af | 239 | // Connect to the Internet (DHCP is expected to be on) |
maygup01 | 0:11cc2b7889af | 240 | printf("Connecting to the network using the default network interface...\n"); |
maygup01 | 0:11cc2b7889af | 241 | net = NetworkInterface::get_default_instance(); |
maygup01 | 0:11cc2b7889af | 242 | |
maygup01 | 0:11cc2b7889af | 243 | nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION; |
maygup01 | 0:11cc2b7889af | 244 | while ((net_status = net->connect()) != NSAPI_ERROR_OK) { |
maygup01 | 0:11cc2b7889af | 245 | printf("Unable to connect to network (%d). Retrying...\n", net_status); |
maygup01 | 0:11cc2b7889af | 246 | } |
maygup01 | 0:11cc2b7889af | 247 | |
maygup01 | 0:11cc2b7889af | 248 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
maygup01 | 0:11cc2b7889af | 249 | |
maygup01 | 0:11cc2b7889af | 250 | printf("Initializing Pelion Device Management Client...\n"); |
maygup01 | 0:11cc2b7889af | 251 | |
maygup01 | 0:11cc2b7889af | 252 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management |
maygup01 | 0:11cc2b7889af | 253 | SimpleMbedCloudClient client(net, bd, &fs); |
maygup01 | 0:11cc2b7889af | 254 | int client_status = client.init(); |
maygup01 | 0:11cc2b7889af | 255 | if (client_status != 0) { |
maygup01 | 0:11cc2b7889af | 256 | printf("Pelion Client initialization failed (%d)\n", client_status); |
maygup01 | 0:11cc2b7889af | 257 | return -1; |
maygup01 | 0:11cc2b7889af | 258 | } |
maygup01 | 0:11cc2b7889af | 259 | |
maygup01 | 0:11cc2b7889af | 260 | // Creating resources, which can be written or read from the cloud |
maygup01 | 0:11cc2b7889af | 261 | res_button = client.create_resource("3200/0/5501", "button_count"); |
maygup01 | 0:11cc2b7889af | 262 | res_button->set_value(0); |
maygup01 | 0:11cc2b7889af | 263 | res_button->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 264 | res_button->observable(true); |
maygup01 | 0:11cc2b7889af | 265 | res_button->attach_notification_callback(button_callback); |
maygup01 | 0:11cc2b7889af | 266 | |
maygup01 | 0:11cc2b7889af | 267 | res_led = client.create_resource("3201/0/5853", "led_state"); |
maygup01 | 0:11cc2b7889af | 268 | res_led->set_value(led.read()); |
maygup01 | 0:11cc2b7889af | 269 | res_led->methods(M2MMethod::GET | M2MMethod::PUT); |
maygup01 | 0:11cc2b7889af | 270 | res_led->attach_put_callback(put_callback); |
maygup01 | 0:11cc2b7889af | 271 | |
maygup01 | 0:11cc2b7889af | 272 | res_post = client.create_resource("3300/0/5605", "execute_function"); |
maygup01 | 0:11cc2b7889af | 273 | res_post->methods(M2MMethod::POST); |
maygup01 | 0:11cc2b7889af | 274 | res_post->attach_post_callback(post_callback); |
maygup01 | 0:11cc2b7889af | 275 | |
maygup01 | 0:11cc2b7889af | 276 | #ifdef SEND_ALL_SENSORS |
maygup01 | 0:11cc2b7889af | 277 | res_accelerometer_x = client.create_resource("3313/0/5702", "Accelerometer X"); |
maygup01 | 0:11cc2b7889af | 278 | res_accelerometer_x->set_value(0); |
maygup01 | 0:11cc2b7889af | 279 | res_accelerometer_x->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 280 | res_accelerometer_x->observable(true); |
maygup01 | 0:11cc2b7889af | 281 | |
maygup01 | 0:11cc2b7889af | 282 | res_accelerometer_y = client.create_resource("3313/0/5703", "Accelerometer Y"); |
maygup01 | 0:11cc2b7889af | 283 | res_accelerometer_y->set_value(0); |
maygup01 | 0:11cc2b7889af | 284 | res_accelerometer_y->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 285 | res_accelerometer_y->observable(true); |
maygup01 | 0:11cc2b7889af | 286 | |
maygup01 | 0:11cc2b7889af | 287 | res_accelerometer_z = client.create_resource("3313/0/5704", "Accelerometer Z"); |
maygup01 | 0:11cc2b7889af | 288 | res_accelerometer_z->set_value(0); |
maygup01 | 0:11cc2b7889af | 289 | res_accelerometer_z->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 290 | res_accelerometer_z->observable(true); |
maygup01 | 0:11cc2b7889af | 291 | |
maygup01 | 0:11cc2b7889af | 292 | res_magnometer_x = client.create_resource("3314/0/5702", "Magnometer X"); |
maygup01 | 0:11cc2b7889af | 293 | res_magnometer_x->set_value(0); |
maygup01 | 0:11cc2b7889af | 294 | res_magnometer_x->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 295 | res_magnometer_x->observable(true); |
maygup01 | 0:11cc2b7889af | 296 | |
maygup01 | 0:11cc2b7889af | 297 | res_magnometer_y = client.create_resource("3314/0/5703", "Magnometer Y"); |
maygup01 | 0:11cc2b7889af | 298 | res_magnometer_y->set_value(0); |
maygup01 | 0:11cc2b7889af | 299 | res_magnometer_y->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 300 | res_magnometer_y->observable(true); |
maygup01 | 0:11cc2b7889af | 301 | |
maygup01 | 0:11cc2b7889af | 302 | res_magnometer_z = client.create_resource("3314/0/5704", "Magnometer Z"); |
maygup01 | 0:11cc2b7889af | 303 | res_magnometer_z->set_value(0); |
maygup01 | 0:11cc2b7889af | 304 | res_magnometer_z->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 305 | res_magnometer_z->observable(true); |
maygup01 | 0:11cc2b7889af | 306 | |
maygup01 | 0:11cc2b7889af | 307 | #ifdef TARGET_K66F |
maygup01 | 0:11cc2b7889af | 308 | res_gyroscope_x = client.create_resource("3334/0/5702", "Gyroscope X"); |
maygup01 | 0:11cc2b7889af | 309 | res_gyroscope_x->set_value(0); |
maygup01 | 0:11cc2b7889af | 310 | res_gyroscope_x->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 311 | res_gyroscope_x->observable(true); |
maygup01 | 0:11cc2b7889af | 312 | |
maygup01 | 0:11cc2b7889af | 313 | res_gyroscope_y = client.create_resource("3334/0/5703", "Gyroscope Y"); |
maygup01 | 0:11cc2b7889af | 314 | res_gyroscope_y->set_value(0); |
maygup01 | 0:11cc2b7889af | 315 | res_gyroscope_y->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 316 | res_gyroscope_y->observable(true); |
maygup01 | 0:11cc2b7889af | 317 | |
maygup01 | 0:11cc2b7889af | 318 | res_gyroscope_z = client.create_resource("3334/0/5704", "Gyroscope Z"); |
maygup01 | 0:11cc2b7889af | 319 | res_gyroscope_z->set_value(0); |
maygup01 | 0:11cc2b7889af | 320 | res_gyroscope_z->methods(M2MMethod::GET); |
maygup01 | 0:11cc2b7889af | 321 | res_gyroscope_z->observable(true); |
maygup01 | 0:11cc2b7889af | 322 | #endif /* TARGET_K66F */ |
maygup01 | 0:11cc2b7889af | 323 | #endif /* SEND_ALL_SENSORS */ |
maygup01 | 0:11cc2b7889af | 324 | |
maygup01 | 0:11cc2b7889af | 325 | printf("Initialized Pelion Device Management Client. Registering...\n"); |
maygup01 | 0:11cc2b7889af | 326 | |
maygup01 | 0:11cc2b7889af | 327 | // Callback that fires when registering is complete |
maygup01 | 0:11cc2b7889af | 328 | client.on_registered(®istered); |
maygup01 | 0:11cc2b7889af | 329 | |
maygup01 | 0:11cc2b7889af | 330 | // Register with Pelion DM |
maygup01 | 0:11cc2b7889af | 331 | client.register_and_connect(); |
maygup01 | 0:11cc2b7889af | 332 | |
maygup01 | 0:11cc2b7889af | 333 | // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
maygup01 | 0:11cc2b7889af | 334 | button.fall(eventQueue.event(&button_press)); |
maygup01 | 0:11cc2b7889af | 335 | printf("Press the user button to increment the LwM2M resource value...\n"); |
maygup01 | 0:11cc2b7889af | 336 | |
maygup01 | 0:11cc2b7889af | 337 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
maygup01 | 0:11cc2b7889af | 338 | Ticker timer; |
maygup01 | 0:11cc2b7889af | 339 | timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); |
maygup01 | 0:11cc2b7889af | 340 | |
maygup01 | 0:11cc2b7889af | 341 | // You can easily run the eventQueue in a separate thread if required |
maygup01 | 0:11cc2b7889af | 342 | eventQueue.dispatch_forever(); |
maygup01 | 0:11cc2b7889af | 343 | } |
maygup01 | 0:11cc2b7889af | 344 | |
maygup01 | 0:11cc2b7889af | 345 | #endif /* MBED_TEST_MODE */ |