![](/media/cache/profiles/mypic_AeCA35r.jpg.50x50_q85.jpg)
Pelion example for WIZnet IoT Shield with BG96 Cat.M1, K6xF board (SK telecom network in Korea)
Dependencies: FXAS21002 FXOS8700Q
Overview
This document is based on https://os.mbed.com/teams/NXP/code/pelion-example-frdm/ and the code forked Daniel_Lee's BG96_K6xF_pelion-example-frdm repository and added some features. Need a WIZnet IoT Shield BG96 board and development board.
This example is known to work great on the following platforms:
- FRDM-K64F - onboard Ethernet and onboard SD card holder.
- FRDM-K66F - onboard Ethernet and onboard SD card holder.
Requirement
- FRDM-K64F or FRDM-K66F
- WIZnet IoT Shield BG96 board
- USIM card
Example functionality
This example showcases the following device functionality:
Read onboard FXOS8700Q accelerometer and magnetometer, and report the values as Pelion LWM2M resources (see image below). (FRDM-K66F only) Read onboard FXAS21002 gyroscope and report the values as Pelion LWM2M resources. On user button click, increment Pelion LWM2M button resource. Allow the user to change the state of the board LED from Pelion LWM2M led_state resource and PUT request.
1. Import the application into your desktop:
mbed import https://os.mbed.com/users/hkjung/code/BG96_K6xF_pelion-example-frdm/ cd BG96_K6xF_pelion-example-frdm
2. Install the CLOUD_SDK_API_KEY
mbed config -G CLOUD_SDK_API_KEY <PELION_DM_API_KEY>
For instructions on how to generate your API key, please see the documentation.
3. Initialize firmware credentials (done once per repository). You can use the following command:
mbed dm init -d "<your company name in Pelion DM>" --model-name "<product model identifier>" -q --force
If the above command does not work for your Mbed CLI, please consider upgrading Mbed CLI to version 1.9.x or above.
4. Compile and program:
mbed compile -t <toolchain> -m <TARGET_BOARD>
(supported toolchains : GCC_ARM / ARM / IAR)
5. You can connect on a virtual terminal/COM port to the platform using:
mbed sterm -b 115200
6. If successfully connect to cellular networks(SK Telecom) then you can get below message
[BOOT] Mbed Bootloader [BOOT] ARM: 00000000000000000000 [BOOT] OEM: 00000000000000000000 [BOOT] Layout: 0 83A8 [BOOT] Active firmware integrity check: [BOOT] SHA256: 76F082FDFF2C0E6C64A76DCF8957773A14B4146629424A0C7F71858788F24521 [BOOT] Version: 1556777151 [BOOT] Slot 0 is empty [BOOT] Active firmware up-to-date [BOOT] Application's start address: 0x10400 [BOOT] Application's jump address: 0x10B11 [BOOT] Application's stack address: 0x20030000 [BOOT] Forwarding to application... Starting Simple Pelion Device Management Client example You can hold the user button during boot to format the storage and change the device identity. Sensors configuration: FXOS8700Q accelerometer = 0xC7 FXOS8700Q magnetometer = 0xC7 Connecting to the network using the default network interface... Connected to the network successfully. IP address: 000:000:000:000 Initializing Pelion Device Management Client... Initialized Pelion Device Management Client. Registering... Press the user button to increment the LwM2M resource value... Registered to Pelion Device Management. Endpoint Name: xx Your Endpoint Name xx FXOS8700Q mag: 0.903 x, -0.073 y, 0.477 z [gauss] FXOS8700Q acc: -1.143 x, -2.459 y, 3.316 z [g]
main.cpp@8:28b898a501b4, 2019-05-02 (annotated)
- Committer:
- hkjung
- Date:
- Thu May 02 06:12:45 2019 +0000
- Revision:
- 8:28b898a501b4
- Parent:
- 5:f2123dc31d30
Added hardware reset function for WIZnet IoT Shield BG96
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:a9d53048f0b6 | 1 | // ---------------------------------------------------------------------------- |
screamer | 0:a9d53048f0b6 | 2 | // Copyright 2016-2018 ARM Ltd. |
screamer | 0:a9d53048f0b6 | 3 | // |
screamer | 0:a9d53048f0b6 | 4 | // SPDX-License-Identifier: Apache-2.0 |
screamer | 0:a9d53048f0b6 | 5 | // |
screamer | 0:a9d53048f0b6 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:a9d53048f0b6 | 7 | // you may not use this file except in compliance with the License. |
screamer | 0:a9d53048f0b6 | 8 | // You may obtain a copy of the License at |
screamer | 0:a9d53048f0b6 | 9 | // |
screamer | 0:a9d53048f0b6 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:a9d53048f0b6 | 11 | // |
screamer | 0:a9d53048f0b6 | 12 | // Unless required by applicable law or agreed to in writing, software |
screamer | 0:a9d53048f0b6 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:a9d53048f0b6 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:a9d53048f0b6 | 15 | // See the License for the specific language governing permissions and |
screamer | 0:a9d53048f0b6 | 16 | // limitations under the License. |
screamer | 0:a9d53048f0b6 | 17 | // ---------------------------------------------------------------------------- |
screamer | 0:a9d53048f0b6 | 18 | #ifndef MBED_TEST_MODE |
screamer | 0:a9d53048f0b6 | 19 | |
screamer | 0:a9d53048f0b6 | 20 | #include "mbed.h" |
screamer | 0:a9d53048f0b6 | 21 | #include "simple-mbed-cloud-client.h" |
screamer | 0:a9d53048f0b6 | 22 | #include "FATFileSystem.h" |
screamer | 0:a9d53048f0b6 | 23 | #include "LittleFileSystem.h" |
screamer | 5:f2123dc31d30 | 24 | |
hkjung | 8:28b898a501b4 | 25 | /* Pin configuraiton */ |
hkjung | 8:28b898a501b4 | 26 | // Cat.M1 |
hkjung | 8:28b898a501b4 | 27 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 8:28b898a501b4 | 28 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 8:28b898a501b4 | 29 | // On-board sensors |
hkjung | 8:28b898a501b4 | 30 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 8:28b898a501b4 | 31 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 8:28b898a501b4 | 32 | |
screamer | 5:f2123dc31d30 | 33 | // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. |
screamer | 5:f2123dc31d30 | 34 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
screamer | 5:f2123dc31d30 | 35 | |
screamer | 5:f2123dc31d30 | 36 | // Default block device available on the target board |
screamer | 5:f2123dc31d30 | 37 | BlockDevice *bd = BlockDevice::get_default_instance(); |
screamer | 5:f2123dc31d30 | 38 | |
screamer | 5:f2123dc31d30 | 39 | #if COMPONENT_SD || COMPONENT_NUSD |
screamer | 5:f2123dc31d30 | 40 | // Use FATFileSystem for SD card type blockdevices |
screamer | 5:f2123dc31d30 | 41 | FATFileSystem fs("fs"); |
screamer | 5:f2123dc31d30 | 42 | #else |
screamer | 5:f2123dc31d30 | 43 | // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions |
screamer | 5:f2123dc31d30 | 44 | LittleFileSystem fs("fs"); |
screamer | 5:f2123dc31d30 | 45 | #endif |
screamer | 5:f2123dc31d30 | 46 | |
screamer | 5:f2123dc31d30 | 47 | // Default User button for GET example and for resetting the storage |
screamer | 5:f2123dc31d30 | 48 | InterruptIn button(BUTTON1); |
screamer | 5:f2123dc31d30 | 49 | // Default LED to use for PUT/POST example |
screamer | 5:f2123dc31d30 | 50 | DigitalOut led(LED1, 0); |
screamer | 5:f2123dc31d30 | 51 | |
screamer | 5:f2123dc31d30 | 52 | // How often to fetch sensor data (in seconds) |
screamer | 5:f2123dc31d30 | 53 | #define SENSORS_POLL_INTERVAL 3.0 |
screamer | 5:f2123dc31d30 | 54 | |
screamer | 5:f2123dc31d30 | 55 | // Send all sensor data or just limited (useful for when running out of memory) |
screamer | 5:f2123dc31d30 | 56 | //#define SEND_ALL_SENSORS |
screamer | 5:f2123dc31d30 | 57 | |
screamer | 5:f2123dc31d30 | 58 | // Sensors related includes and initialization |
screamer | 1:42d51cf7cebe | 59 | #include "FXOS8700Q.h" |
screamer | 2:fba2c7066355 | 60 | #include "FXAS21002.h" |
screamer | 1:42d51cf7cebe | 61 | |
screamer | 2:fba2c7066355 | 62 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 63 | I2C sens_i2c(PTD9, PTD8); |
screamer | 2:fba2c7066355 | 64 | #else |
screamer | 2:fba2c7066355 | 65 | I2C sens_i2c(PTE25, PTE24); |
screamer | 2:fba2c7066355 | 66 | #endif /* TARGET_K66F */ |
screamer | 2:fba2c7066355 | 67 | |
screamer | 2:fba2c7066355 | 68 | FXOS8700QAccelerometer sens_acc(sens_i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
screamer | 2:fba2c7066355 | 69 | FXOS8700QMagnetometer sens_mag(sens_i2c, FXOS8700CQ_SLAVE_ADDR1); |
screamer | 2:fba2c7066355 | 70 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 71 | FXAS21002 sens_gyro(PTD9, PTD8, 0x20); |
screamer | 2:fba2c7066355 | 72 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 73 | |
screamer | 0:a9d53048f0b6 | 74 | // Declaring pointers for access to Pelion Device Management Client resources outside of main() |
screamer | 2:fba2c7066355 | 75 | MbedCloudClientResource *res_button; |
screamer | 2:fba2c7066355 | 76 | MbedCloudClientResource *res_led; |
screamer | 2:fba2c7066355 | 77 | MbedCloudClientResource *res_post; |
screamer | 5:f2123dc31d30 | 78 | |
screamer | 5:f2123dc31d30 | 79 | // Additional resources for sensor readings |
screamer | 1:42d51cf7cebe | 80 | #ifdef SEND_ALL_SENSORS |
screamer | 1:42d51cf7cebe | 81 | MbedCloudClientResource *res_magnometer_x; |
screamer | 1:42d51cf7cebe | 82 | MbedCloudClientResource *res_magnometer_y; |
screamer | 1:42d51cf7cebe | 83 | MbedCloudClientResource *res_magnometer_z; |
screamer | 1:42d51cf7cebe | 84 | MbedCloudClientResource *res_accelerometer_x; |
screamer | 1:42d51cf7cebe | 85 | MbedCloudClientResource *res_accelerometer_y; |
screamer | 1:42d51cf7cebe | 86 | MbedCloudClientResource *res_accelerometer_z; |
screamer | 2:fba2c7066355 | 87 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 88 | MbedCloudClientResource *res_gyroscope_x; |
screamer | 2:fba2c7066355 | 89 | MbedCloudClientResource *res_gyroscope_y; |
screamer | 2:fba2c7066355 | 90 | MbedCloudClientResource *res_gyroscope_z; |
screamer | 2:fba2c7066355 | 91 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 92 | #endif /* SEND_ALL_SENSORS */ |
screamer | 0:a9d53048f0b6 | 93 | |
screamer | 0:a9d53048f0b6 | 94 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
screamer | 0:a9d53048f0b6 | 95 | // 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 | 0:a9d53048f0b6 | 96 | EventQueue eventQueue; |
screamer | 0:a9d53048f0b6 | 97 | |
screamer | 1:42d51cf7cebe | 98 | // When the device is registered, this variable will be used to access various useful information, like device ID etc. |
screamer | 1:42d51cf7cebe | 99 | static const ConnectorClientEndpointInfo* endpointInfo; |
screamer | 1:42d51cf7cebe | 100 | |
hkjung | 8:28b898a501b4 | 101 | void iotshield_modem_init(void) |
hkjung | 8:28b898a501b4 | 102 | { |
hkjung | 8:28b898a501b4 | 103 | DigitalOut _RESET_IOTSHIELD(MBED_CONF_IOTSHIELD_CATM1_RESET); |
hkjung | 8:28b898a501b4 | 104 | DigitalOut _PWRKEY_IOTSHIELD(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
hkjung | 8:28b898a501b4 | 105 | |
hkjung | 8:28b898a501b4 | 106 | _RESET_IOTSHIELD = 1; |
hkjung | 8:28b898a501b4 | 107 | _PWRKEY_IOTSHIELD = 1; |
hkjung | 8:28b898a501b4 | 108 | wait_ms(300); |
hkjung | 8:28b898a501b4 | 109 | |
hkjung | 8:28b898a501b4 | 110 | _RESET_IOTSHIELD = 0; |
hkjung | 8:28b898a501b4 | 111 | _PWRKEY_IOTSHIELD = 0; |
hkjung | 8:28b898a501b4 | 112 | wait_ms(400); |
hkjung | 8:28b898a501b4 | 113 | |
hkjung | 8:28b898a501b4 | 114 | _RESET_IOTSHIELD = 1; |
hkjung | 8:28b898a501b4 | 115 | wait_ms(1000); |
hkjung | 8:28b898a501b4 | 116 | } |
hkjung | 8:28b898a501b4 | 117 | |
screamer | 0:a9d53048f0b6 | 118 | /** |
screamer | 0:a9d53048f0b6 | 119 | * PUT handler - sets the value of the built-in LED |
screamer | 0:a9d53048f0b6 | 120 | * @param resource The resource that triggered the callback |
screamer | 0:a9d53048f0b6 | 121 | * @param newValue Updated value for the resource |
screamer | 0:a9d53048f0b6 | 122 | */ |
screamer | 0:a9d53048f0b6 | 123 | void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { |
screamer | 2:fba2c7066355 | 124 | printf("*** PUT received, new value: %s \n", newValue.c_str()); |
screamer | 0:a9d53048f0b6 | 125 | led = atoi(newValue.c_str()); |
screamer | 0:a9d53048f0b6 | 126 | } |
screamer | 0:a9d53048f0b6 | 127 | |
screamer | 0:a9d53048f0b6 | 128 | /** |
screamer | 0:a9d53048f0b6 | 129 | * POST handler - prints the content of the payload |
screamer | 0:a9d53048f0b6 | 130 | * @param resource The resource that triggered the callback |
screamer | 0:a9d53048f0b6 | 131 | * @param buffer If a body was passed to the POST function, this contains the data. |
screamer | 0:a9d53048f0b6 | 132 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
screamer | 0:a9d53048f0b6 | 133 | * @param size Size of the body |
screamer | 0:a9d53048f0b6 | 134 | */ |
screamer | 0:a9d53048f0b6 | 135 | void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
screamer | 2:fba2c7066355 | 136 | printf("*** POST received (length %u). Payload: ", size); |
screamer | 0:a9d53048f0b6 | 137 | for (size_t ix = 0; ix < size; ix++) { |
screamer | 0:a9d53048f0b6 | 138 | printf("%02x ", buffer[ix]); |
screamer | 0:a9d53048f0b6 | 139 | } |
screamer | 0:a9d53048f0b6 | 140 | printf("\n"); |
screamer | 0:a9d53048f0b6 | 141 | } |
screamer | 0:a9d53048f0b6 | 142 | |
screamer | 0:a9d53048f0b6 | 143 | /** |
screamer | 0:a9d53048f0b6 | 144 | * Button handler |
screamer | 0:a9d53048f0b6 | 145 | * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) |
screamer | 0:a9d53048f0b6 | 146 | */ |
screamer | 0:a9d53048f0b6 | 147 | void button_press() { |
screamer | 2:fba2c7066355 | 148 | int v = res_button->get_value_int() + 1; |
screamer | 2:fba2c7066355 | 149 | res_button->set_value(v); |
screamer | 2:fba2c7066355 | 150 | printf("*** Button clicked %d times \n", v); |
screamer | 0:a9d53048f0b6 | 151 | } |
screamer | 0:a9d53048f0b6 | 152 | |
screamer | 0:a9d53048f0b6 | 153 | /** |
screamer | 0:a9d53048f0b6 | 154 | * Notification callback handler |
screamer | 0:a9d53048f0b6 | 155 | * @param resource The resource that triggered the callback |
screamer | 0:a9d53048f0b6 | 156 | * @param status The delivery status of the notification |
screamer | 0:a9d53048f0b6 | 157 | */ |
screamer | 0:a9d53048f0b6 | 158 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
screamer | 2:fba2c7066355 | 159 | printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); |
screamer | 0:a9d53048f0b6 | 160 | } |
screamer | 0:a9d53048f0b6 | 161 | |
screamer | 0:a9d53048f0b6 | 162 | /** |
screamer | 0:a9d53048f0b6 | 163 | * Registration callback handler |
screamer | 0:a9d53048f0b6 | 164 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
screamer | 0:a9d53048f0b6 | 165 | */ |
screamer | 0:a9d53048f0b6 | 166 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
screamer | 0:a9d53048f0b6 | 167 | printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
screamer | 3:2231338197f2 | 168 | endpointInfo = endpoint; |
screamer | 0:a9d53048f0b6 | 169 | } |
screamer | 0:a9d53048f0b6 | 170 | |
screamer | 1:42d51cf7cebe | 171 | /** |
screamer | 1:42d51cf7cebe | 172 | * Initialize sensors |
screamer | 1:42d51cf7cebe | 173 | */ |
screamer | 1:42d51cf7cebe | 174 | void sensors_init() { |
screamer | 1:42d51cf7cebe | 175 | printf ("\nSensors configuration:\n"); |
screamer | 1:42d51cf7cebe | 176 | |
screamer | 2:fba2c7066355 | 177 | sens_acc.enable(); |
screamer | 2:fba2c7066355 | 178 | sens_mag.enable(); |
screamer | 2:fba2c7066355 | 179 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 180 | sens_gyro.activate(true); |
screamer | 2:fba2c7066355 | 181 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 182 | |
screamer | 2:fba2c7066355 | 183 | printf("FXOS8700Q accelerometer = 0x%X\n", sens_acc.whoAmI()); |
screamer | 2:fba2c7066355 | 184 | printf("FXOS8700Q magnetometer = 0x%X\n", sens_mag.whoAmI()); |
screamer | 2:fba2c7066355 | 185 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 186 | printf("FXAS21002 gyroscope = 0x%X\n", sens_gyro.getStatus()); |
screamer | 2:fba2c7066355 | 187 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 188 | |
screamer | 1:42d51cf7cebe | 189 | printf("\n"); ; |
screamer | 1:42d51cf7cebe | 190 | } |
screamer | 1:42d51cf7cebe | 191 | |
screamer | 1:42d51cf7cebe | 192 | /** |
screamer | 1:42d51cf7cebe | 193 | * Update sensors and report their values. |
screamer | 1:42d51cf7cebe | 194 | * This function is called periodically. |
screamer | 1:42d51cf7cebe | 195 | */ |
screamer | 1:42d51cf7cebe | 196 | void sensors_update() { |
screamer | 1:42d51cf7cebe | 197 | motion_data_counts_t acc_raw, mag_raw; |
screamer | 1:42d51cf7cebe | 198 | |
screamer | 2:fba2c7066355 | 199 | sens_acc.getAxis(acc_raw); |
screamer | 2:fba2c7066355 | 200 | sens_mag.getAxis(mag_raw); |
screamer | 1:42d51cf7cebe | 201 | |
screamer | 2:fba2c7066355 | 202 | 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; |
screamer | 2:fba2c7066355 | 203 | 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; |
screamer | 2:fba2c7066355 | 204 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 205 | 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; |
screamer | 2:fba2c7066355 | 206 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 207 | |
screamer | 2:fba2c7066355 | 208 | printf(" \n"); |
screamer | 2:fba2c7066355 | 209 | printf("FXOS8700Q mag: %7.3f x, %7.3f y, %7.3f z [gauss] \n", mag_x, mag_y, mag_z); |
screamer | 2:fba2c7066355 | 210 | printf("FXOS8700Q acc: %7.3f x, %7.3f y, %7.3f z [g] \n", acc_x, acc_y, acc_z); |
screamer | 2:fba2c7066355 | 211 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 212 | printf("FXAS21002 gryo: %7.3f x, %7.3f y, %7.3f z [dps] \n", gyro_x, gyro_y, gyro_z); |
screamer | 2:fba2c7066355 | 213 | printf("\r\033[4A"); |
screamer | 2:fba2c7066355 | 214 | #else |
screamer | 1:42d51cf7cebe | 215 | printf("\r\033[3A"); |
screamer | 2:fba2c7066355 | 216 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 217 | |
screamer | 1:42d51cf7cebe | 218 | if (endpointInfo) { |
screamer | 1:42d51cf7cebe | 219 | #ifdef SEND_ALL_SENSORS |
screamer | 3:2231338197f2 | 220 | res_accelerometer_x->set_value(acc_x); |
screamer | 3:2231338197f2 | 221 | res_accelerometer_y->set_value(acc_y); |
screamer | 3:2231338197f2 | 222 | res_accelerometer_z->set_value(acc_z); |
screamer | 2:fba2c7066355 | 223 | res_magnometer_x->set_value(mag_x); |
screamer | 2:fba2c7066355 | 224 | res_magnometer_y->set_value(mag_y); |
screamer | 2:fba2c7066355 | 225 | res_magnometer_z->set_value(mag_z); |
screamer | 2:fba2c7066355 | 226 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 227 | res_gyroscope_x->set_value(gyro_x); |
screamer | 2:fba2c7066355 | 228 | res_gyroscope_y->set_value(gyro_y); |
screamer | 2:fba2c7066355 | 229 | res_gyroscope_z->set_value(gyro_z); |
screamer | 2:fba2c7066355 | 230 | #endif /* TARGET_K66F */ |
screamer | 1:42d51cf7cebe | 231 | #endif /* SEND_ALL_SENSORS */ |
screamer | 1:42d51cf7cebe | 232 | } |
screamer | 1:42d51cf7cebe | 233 | } |
screamer | 1:42d51cf7cebe | 234 | |
screamer | 0:a9d53048f0b6 | 235 | int main(void) { |
screamer | 0:a9d53048f0b6 | 236 | printf("\nStarting Simple Pelion Device Management Client example\n"); |
screamer | 0:a9d53048f0b6 | 237 | |
screamer | 2:fba2c7066355 | 238 | int storage_status = fs.mount(bd); |
screamer | 2:fba2c7066355 | 239 | if (storage_status != 0) { |
screamer | 2:fba2c7066355 | 240 | printf("Storage mounting failed.\n"); |
screamer | 2:fba2c7066355 | 241 | } |
screamer | 0:a9d53048f0b6 | 242 | #if USE_BUTTON == 1 |
screamer | 0:a9d53048f0b6 | 243 | // If the User button is pressed ons start, then format storage. |
screamer | 2:fba2c7066355 | 244 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
screamer | 2:fba2c7066355 | 245 | if (btn_pressed) { |
screamer | 2:fba2c7066355 | 246 | printf("User button is pushed on start...\n"); |
screamer | 2:fba2c7066355 | 247 | } |
screamer | 2:fba2c7066355 | 248 | #else |
screamer | 2:fba2c7066355 | 249 | bool btn_pressed = FALSE; |
screamer | 2:fba2c7066355 | 250 | #endif /* USE_BUTTON */ |
screamer | 2:fba2c7066355 | 251 | |
screamer | 2:fba2c7066355 | 252 | if (storage_status || btn_pressed) { |
screamer | 2:fba2c7066355 | 253 | printf("Formatting the storage...\n"); |
screamer | 0:a9d53048f0b6 | 254 | int storage_status = StorageHelper::format(&fs, bd); |
screamer | 0:a9d53048f0b6 | 255 | if (storage_status != 0) { |
screamer | 0:a9d53048f0b6 | 256 | printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); |
screamer | 0:a9d53048f0b6 | 257 | } |
screamer | 0:a9d53048f0b6 | 258 | } else { |
screamer | 0:a9d53048f0b6 | 259 | printf("You can hold the user button during boot to format the storage and change the device identity.\n"); |
screamer | 0:a9d53048f0b6 | 260 | } |
screamer | 0:a9d53048f0b6 | 261 | |
screamer | 1:42d51cf7cebe | 262 | sensors_init(); |
hkjung | 8:28b898a501b4 | 263 | |
hkjung | 8:28b898a501b4 | 264 | // Cat.M1 module init: Hardware reset and power on |
hkjung | 8:28b898a501b4 | 265 | iotshield_modem_init(); |
screamer | 1:42d51cf7cebe | 266 | |
screamer | 0:a9d53048f0b6 | 267 | // Connect to the Internet (DHCP is expected to be on) |
screamer | 0:a9d53048f0b6 | 268 | printf("Connecting to the network using the default network interface...\n"); |
screamer | 0:a9d53048f0b6 | 269 | net = NetworkInterface::get_default_instance(); |
screamer | 0:a9d53048f0b6 | 270 | |
screamer | 0:a9d53048f0b6 | 271 | nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION; |
screamer | 0:a9d53048f0b6 | 272 | while ((net_status = net->connect()) != NSAPI_ERROR_OK) { |
screamer | 0:a9d53048f0b6 | 273 | printf("Unable to connect to network (%d). Retrying...\n", net_status); |
screamer | 0:a9d53048f0b6 | 274 | } |
screamer | 0:a9d53048f0b6 | 275 | |
screamer | 0:a9d53048f0b6 | 276 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
screamer | 0:a9d53048f0b6 | 277 | |
screamer | 0:a9d53048f0b6 | 278 | printf("Initializing Pelion Device Management Client...\n"); |
screamer | 0:a9d53048f0b6 | 279 | |
screamer | 0:a9d53048f0b6 | 280 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management |
screamer | 0:a9d53048f0b6 | 281 | SimpleMbedCloudClient client(net, bd, &fs); |
screamer | 0:a9d53048f0b6 | 282 | int client_status = client.init(); |
screamer | 0:a9d53048f0b6 | 283 | if (client_status != 0) { |
screamer | 0:a9d53048f0b6 | 284 | printf("Pelion Client initialization failed (%d)\n", client_status); |
screamer | 0:a9d53048f0b6 | 285 | return -1; |
screamer | 0:a9d53048f0b6 | 286 | } |
screamer | 0:a9d53048f0b6 | 287 | |
screamer | 0:a9d53048f0b6 | 288 | // Creating resources, which can be written or read from the cloud |
screamer | 2:fba2c7066355 | 289 | res_button = client.create_resource("3200/0/5501", "button_count"); |
screamer | 2:fba2c7066355 | 290 | res_button->set_value(0); |
screamer | 2:fba2c7066355 | 291 | res_button->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 292 | res_button->observable(true); |
screamer | 2:fba2c7066355 | 293 | res_button->attach_notification_callback(button_callback); |
screamer | 2:fba2c7066355 | 294 | |
screamer | 2:fba2c7066355 | 295 | res_led = client.create_resource("3201/0/5853", "led_state"); |
screamer | 2:fba2c7066355 | 296 | res_led->set_value(led.read()); |
screamer | 2:fba2c7066355 | 297 | res_led->methods(M2MMethod::GET | M2MMethod::PUT); |
screamer | 2:fba2c7066355 | 298 | res_led->attach_put_callback(put_callback); |
screamer | 2:fba2c7066355 | 299 | |
screamer | 2:fba2c7066355 | 300 | res_post = client.create_resource("3300/0/5605", "execute_function"); |
screamer | 2:fba2c7066355 | 301 | res_post->methods(M2MMethod::POST); |
screamer | 2:fba2c7066355 | 302 | res_post->attach_post_callback(post_callback); |
screamer | 2:fba2c7066355 | 303 | |
screamer | 2:fba2c7066355 | 304 | #ifdef SEND_ALL_SENSORS |
screamer | 2:fba2c7066355 | 305 | res_accelerometer_x = client.create_resource("3313/0/5702", "Accelerometer X"); |
screamer | 2:fba2c7066355 | 306 | res_accelerometer_x->set_value(0); |
screamer | 2:fba2c7066355 | 307 | res_accelerometer_x->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 308 | res_accelerometer_x->observable(true); |
screamer | 2:fba2c7066355 | 309 | |
screamer | 2:fba2c7066355 | 310 | res_accelerometer_y = client.create_resource("3313/0/5703", "Accelerometer Y"); |
screamer | 2:fba2c7066355 | 311 | res_accelerometer_y->set_value(0); |
screamer | 2:fba2c7066355 | 312 | res_accelerometer_y->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 313 | res_accelerometer_y->observable(true); |
screamer | 2:fba2c7066355 | 314 | |
screamer | 2:fba2c7066355 | 315 | res_accelerometer_z = client.create_resource("3313/0/5704", "Accelerometer Z"); |
screamer | 2:fba2c7066355 | 316 | res_accelerometer_z->set_value(0); |
screamer | 2:fba2c7066355 | 317 | res_accelerometer_z->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 318 | res_accelerometer_z->observable(true); |
screamer | 0:a9d53048f0b6 | 319 | |
screamer | 2:fba2c7066355 | 320 | res_magnometer_x = client.create_resource("3314/0/5702", "Magnometer X"); |
screamer | 2:fba2c7066355 | 321 | res_magnometer_x->set_value(0); |
screamer | 2:fba2c7066355 | 322 | res_magnometer_x->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 323 | res_magnometer_x->observable(true); |
screamer | 2:fba2c7066355 | 324 | |
screamer | 2:fba2c7066355 | 325 | res_magnometer_y = client.create_resource("3314/0/5703", "Magnometer Y"); |
screamer | 2:fba2c7066355 | 326 | res_magnometer_y->set_value(0); |
screamer | 2:fba2c7066355 | 327 | res_magnometer_y->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 328 | res_magnometer_y->observable(true); |
screamer | 2:fba2c7066355 | 329 | |
screamer | 2:fba2c7066355 | 330 | res_magnometer_z = client.create_resource("3314/0/5704", "Magnometer Z"); |
screamer | 2:fba2c7066355 | 331 | res_magnometer_z->set_value(0); |
screamer | 2:fba2c7066355 | 332 | res_magnometer_z->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 333 | res_magnometer_z->observable(true); |
screamer | 0:a9d53048f0b6 | 334 | |
screamer | 2:fba2c7066355 | 335 | #ifdef TARGET_K66F |
screamer | 2:fba2c7066355 | 336 | res_gyroscope_x = client.create_resource("3334/0/5702", "Gyroscope X"); |
screamer | 2:fba2c7066355 | 337 | res_gyroscope_x->set_value(0); |
screamer | 2:fba2c7066355 | 338 | res_gyroscope_x->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 339 | res_gyroscope_x->observable(true); |
screamer | 2:fba2c7066355 | 340 | |
screamer | 2:fba2c7066355 | 341 | res_gyroscope_y = client.create_resource("3334/0/5703", "Gyroscope Y"); |
screamer | 2:fba2c7066355 | 342 | res_gyroscope_y->set_value(0); |
screamer | 2:fba2c7066355 | 343 | res_gyroscope_y->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 344 | res_gyroscope_y->observable(true); |
screamer | 2:fba2c7066355 | 345 | |
screamer | 2:fba2c7066355 | 346 | res_gyroscope_z = client.create_resource("3334/0/5704", "Gyroscope Z"); |
screamer | 2:fba2c7066355 | 347 | res_gyroscope_z->set_value(0); |
screamer | 2:fba2c7066355 | 348 | res_gyroscope_z->methods(M2MMethod::GET); |
screamer | 2:fba2c7066355 | 349 | res_gyroscope_z->observable(true); |
screamer | 2:fba2c7066355 | 350 | #endif /* TARGET_K66F */ |
screamer | 2:fba2c7066355 | 351 | #endif /* SEND_ALL_SENSORS */ |
screamer | 0:a9d53048f0b6 | 352 | |
screamer | 0:a9d53048f0b6 | 353 | printf("Initialized Pelion Device Management Client. Registering...\n"); |
screamer | 0:a9d53048f0b6 | 354 | |
screamer | 0:a9d53048f0b6 | 355 | // Callback that fires when registering is complete |
screamer | 0:a9d53048f0b6 | 356 | client.on_registered(®istered); |
screamer | 0:a9d53048f0b6 | 357 | |
screamer | 0:a9d53048f0b6 | 358 | // Register with Pelion DM |
screamer | 0:a9d53048f0b6 | 359 | client.register_and_connect(); |
screamer | 0:a9d53048f0b6 | 360 | |
screamer | 0:a9d53048f0b6 | 361 | // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
screamer | 0:a9d53048f0b6 | 362 | button.fall(eventQueue.event(&button_press)); |
screamer | 0:a9d53048f0b6 | 363 | printf("Press the user button to increment the LwM2M resource value...\n"); |
screamer | 1:42d51cf7cebe | 364 | |
screamer | 0:a9d53048f0b6 | 365 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
screamer | 0:a9d53048f0b6 | 366 | Ticker timer; |
screamer | 1:42d51cf7cebe | 367 | timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); |
screamer | 0:a9d53048f0b6 | 368 | |
screamer | 0:a9d53048f0b6 | 369 | // You can easily run the eventQueue in a separate thread if required |
screamer | 0:a9d53048f0b6 | 370 | eventQueue.dispatch_forever(); |
screamer | 0:a9d53048f0b6 | 371 | } |
screamer | 0:a9d53048f0b6 | 372 | |
screamer | 0:a9d53048f0b6 | 373 | #endif /* MBED_TEST_MODE */ |