Workshop example
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
main.cpp@10:b27c962b3c3f, 2018-12-08 (annotated)
- Committer:
- screamer
- Date:
- Sat Dec 08 01:46:22 2018 +0000
- Revision:
- 10:b27c962b3c3f
- Parent:
- 9:265744785d33
- Child:
- 11:8df4529f060d
Update example to Pelion Client 2.0.1.1 and Mbed OS 5.10.4. Also prepare repo to expose sensors in the cloud
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adustm | 1:e86b1cffc402 | 1 | // ---------------------------------------------------------------------------- |
adustm | 4:cf7342047b4d | 2 | // Copyright 2016-2018 ARM Ltd. |
adustm | 1:e86b1cffc402 | 3 | // |
adustm | 1:e86b1cffc402 | 4 | // SPDX-License-Identifier: Apache-2.0 |
adustm | 1:e86b1cffc402 | 5 | // |
adustm | 1:e86b1cffc402 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
adustm | 1:e86b1cffc402 | 7 | // you may not use this file except in compliance with the License. |
adustm | 1:e86b1cffc402 | 8 | // You may obtain a copy of the License at |
adustm | 1:e86b1cffc402 | 9 | // |
adustm | 1:e86b1cffc402 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
adustm | 1:e86b1cffc402 | 11 | // |
adustm | 1:e86b1cffc402 | 12 | // Unless required by applicable law or agreed to in writing, software |
adustm | 1:e86b1cffc402 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
adustm | 1:e86b1cffc402 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
adustm | 1:e86b1cffc402 | 15 | // See the License for the specific language governing permissions and |
adustm | 1:e86b1cffc402 | 16 | // limitations under the License. |
adustm | 1:e86b1cffc402 | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 9:265744785d33 | 18 | #ifndef MBED_TEST_MODE |
adustm | 1:e86b1cffc402 | 19 | #include "mbed.h" |
adustm | 1:e86b1cffc402 | 20 | #include "simple-mbed-cloud-client.h" |
screamer | 10:b27c962b3c3f | 21 | #include "LittleFileSystem.h" |
screamer | 10:b27c962b3c3f | 22 | |
screamer | 10:b27c962b3c3f | 23 | //#define SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 24 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 25 | #include "HTS221Sensor.h" |
screamer | 10:b27c962b3c3f | 26 | #include "LPS22HBSensor.h" |
screamer | 10:b27c962b3c3f | 27 | #include "LSM6DSLSensor.h" |
screamer | 10:b27c962b3c3f | 28 | #include "lis3mdl_class.h" |
screamer | 10:b27c962b3c3f | 29 | #include "VL53L0X.h" |
screamer | 10:b27c962b3c3f | 30 | |
screamer | 10:b27c962b3c3f | 31 | static DevI2C devI2c(PB_11,PB_10); |
screamer | 10:b27c962b3c3f | 32 | static HTS221Sensor hum_temp(&devI2c); |
screamer | 10:b27c962b3c3f | 33 | static LPS22HBSensor press_temp(&devI2c); |
screamer | 10:b27c962b3c3f | 34 | static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address |
screamer | 10:b27c962b3c3f | 35 | static LIS3MDL magnetometer(&devI2c); |
screamer | 10:b27c962b3c3f | 36 | static DigitalOut shutdown_pin(PC_6); |
screamer | 10:b27c962b3c3f | 37 | static VL53L0X range(&devI2c, &shutdown_pin, PC_7); |
screamer | 10:b27c962b3c3f | 38 | |
screamer | 10:b27c962b3c3f | 39 | InterruptIn button(USER_BUTTON); |
screamer | 10:b27c962b3c3f | 40 | #endif /* SENSORS_AND_BUTTONS */ |
adustm | 1:e86b1cffc402 | 41 | |
adustm | 4:cf7342047b4d | 42 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
adustm | 4:cf7342047b4d | 43 | // 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 |
adustm | 4:cf7342047b4d | 44 | EventQueue eventQueue; |
adustm | 1:e86b1cffc402 | 45 | |
MarceloSalazar | 9:265744785d33 | 46 | // Default network interface object |
MarceloSalazar | 9:265744785d33 | 47 | NetworkInterface *net; |
adustm | 6:e0e1e1b93099 | 48 | |
MarceloSalazar | 9:265744785d33 | 49 | // Default block device |
MarceloSalazar | 9:265744785d33 | 50 | BlockDevice* bd = BlockDevice::get_default_instance(); |
screamer | 10:b27c962b3c3f | 51 | SlicingBlockDevice sd(bd, 0, 2*1024*1024); |
screamer | 10:b27c962b3c3f | 52 | LittleFileSystem fs("fs", &sd); |
adustm | 4:cf7342047b4d | 53 | |
MarceloSalazar | 9:265744785d33 | 54 | // Declaring pointers for access to Pelion Client resources outside of main() |
adustm | 4:cf7342047b4d | 55 | MbedCloudClientResource *button_res; |
adustm | 4:cf7342047b4d | 56 | MbedCloudClientResource *pattern_res; |
adustm | 1:e86b1cffc402 | 57 | |
screamer | 10:b27c962b3c3f | 58 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 59 | // Additional resources for sensor readings |
screamer | 10:b27c962b3c3f | 60 | MbedCloudClientResource *humidity_res; |
screamer | 10:b27c962b3c3f | 61 | MbedCloudClientResource *temperature_res; |
screamer | 10:b27c962b3c3f | 62 | MbedCloudClientResource *distance_res; |
screamer | 10:b27c962b3c3f | 63 | #endif /* SENSORS_AND_BUTTONS */ |
adustm | 1:e86b1cffc402 | 64 | |
screamer | 10:b27c962b3c3f | 65 | // When the device is registered, this variable will be used to access various useful information, like device ID etc. |
screamer | 10:b27c962b3c3f | 66 | static const ConnectorClientEndpointInfo* endpointInfo; |
adustm | 1:e86b1cffc402 | 67 | |
screamer | 10:b27c962b3c3f | 68 | /** |
screamer | 10:b27c962b3c3f | 69 | * Button function triggered by the physical button press or by timer depending on SENSORS_AND_BUTTONS macro. |
screamer | 10:b27c962b3c3f | 70 | */ |
screamer | 10:b27c962b3c3f | 71 | void button_press() { |
screamer | 10:b27c962b3c3f | 72 | int v = button_res->get_value_int() + 1; |
screamer | 10:b27c962b3c3f | 73 | button_res->set_value(v); |
screamer | 10:b27c962b3c3f | 74 | printf("Button clicked %d times\n", v); |
adustm | 1:e86b1cffc402 | 75 | } |
adustm | 1:e86b1cffc402 | 76 | |
adustm | 4:cf7342047b4d | 77 | /** |
adustm | 4:cf7342047b4d | 78 | * PUT handler |
adustm | 4:cf7342047b4d | 79 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 80 | * @param newValue Updated value for the resource |
adustm | 4:cf7342047b4d | 81 | */ |
adustm | 4:cf7342047b4d | 82 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
adustm | 4:cf7342047b4d | 83 | printf("PUT received, new value: %s\n", newValue.c_str()); |
adustm | 1:e86b1cffc402 | 84 | } |
adustm | 1:e86b1cffc402 | 85 | |
adustm | 4:cf7342047b4d | 86 | /** |
adustm | 4:cf7342047b4d | 87 | * POST handler |
adustm | 4:cf7342047b4d | 88 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 89 | * @param buffer If a body was passed to the POST function, this contains the data. |
adustm | 4:cf7342047b4d | 90 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
adustm | 4:cf7342047b4d | 91 | * @param size Size of the body |
adustm | 4:cf7342047b4d | 92 | */ |
adustm | 4:cf7342047b4d | 93 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
adustm | 4:cf7342047b4d | 94 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
adustm | 4:cf7342047b4d | 95 | |
adustm | 4:cf7342047b4d | 96 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
adustm | 1:e86b1cffc402 | 97 | |
adustm | 4:cf7342047b4d | 98 | // Parse the pattern string, and toggle the LED in that pattern |
adustm | 4:cf7342047b4d | 99 | string s = std::string(pattern_res->get_value().c_str()); |
adustm | 4:cf7342047b4d | 100 | size_t i = 0; |
adustm | 4:cf7342047b4d | 101 | size_t pos = s.find(':'); |
adustm | 4:cf7342047b4d | 102 | while (pos != string::npos) { |
adustm | 4:cf7342047b4d | 103 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
adustm | 4:cf7342047b4d | 104 | augmentedLed = !augmentedLed; |
adustm | 4:cf7342047b4d | 105 | |
adustm | 4:cf7342047b4d | 106 | i = ++pos; |
adustm | 4:cf7342047b4d | 107 | pos = s.find(':', pos); |
adustm | 4:cf7342047b4d | 108 | |
adustm | 4:cf7342047b4d | 109 | if (pos == string::npos) { |
adustm | 4:cf7342047b4d | 110 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
adustm | 4:cf7342047b4d | 111 | augmentedLed = !augmentedLed; |
adustm | 4:cf7342047b4d | 112 | } |
adustm | 4:cf7342047b4d | 113 | } |
adustm | 1:e86b1cffc402 | 114 | } |
adustm | 1:e86b1cffc402 | 115 | |
adustm | 4:cf7342047b4d | 116 | /** |
adustm | 4:cf7342047b4d | 117 | * Notification callback handler |
adustm | 4:cf7342047b4d | 118 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 119 | * @param status The delivery status of the notification |
adustm | 4:cf7342047b4d | 120 | */ |
adustm | 4:cf7342047b4d | 121 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
adustm | 4:cf7342047b4d | 122 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
adustm | 4:cf7342047b4d | 123 | } |
adustm | 1:e86b1cffc402 | 124 | |
adustm | 4:cf7342047b4d | 125 | /** |
adustm | 4:cf7342047b4d | 126 | * Registration callback handler |
adustm | 4:cf7342047b4d | 127 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
adustm | 4:cf7342047b4d | 128 | */ |
adustm | 4:cf7342047b4d | 129 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
MarceloSalazar | 9:265744785d33 | 130 | printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
screamer | 10:b27c962b3c3f | 131 | endpointInfo = endpoint; |
adustm | 4:cf7342047b4d | 132 | } |
adustm | 1:e86b1cffc402 | 133 | |
screamer | 10:b27c962b3c3f | 134 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 135 | /** |
screamer | 10:b27c962b3c3f | 136 | * Initialize sensors |
screamer | 10:b27c962b3c3f | 137 | */ |
screamer | 10:b27c962b3c3f | 138 | void sensors_init() { |
screamer | 10:b27c962b3c3f | 139 | uint8_t id; |
screamer | 10:b27c962b3c3f | 140 | |
screamer | 10:b27c962b3c3f | 141 | // Initialize sensors |
screamer | 10:b27c962b3c3f | 142 | hum_temp.init(NULL); |
screamer | 10:b27c962b3c3f | 143 | press_temp.init(NULL); |
screamer | 10:b27c962b3c3f | 144 | acc_gyro.init(NULL); |
screamer | 10:b27c962b3c3f | 145 | magnetometer.init(NULL); |
screamer | 10:b27c962b3c3f | 146 | range.init_sensor(VL53L0X_DEFAULT_ADDRESS); |
screamer | 10:b27c962b3c3f | 147 | |
screamer | 10:b27c962b3c3f | 148 | /// Call sensors enable routines |
screamer | 10:b27c962b3c3f | 149 | hum_temp.enable(); |
screamer | 10:b27c962b3c3f | 150 | press_temp.enable(); |
screamer | 10:b27c962b3c3f | 151 | //magnetometer.enable(); |
screamer | 10:b27c962b3c3f | 152 | acc_gyro.enable_x(); |
screamer | 10:b27c962b3c3f | 153 | acc_gyro.enable_g(); |
screamer | 10:b27c962b3c3f | 154 | |
screamer | 10:b27c962b3c3f | 155 | printf("\033[2J\033[20A"); |
screamer | 10:b27c962b3c3f | 156 | printf ("\r\n--- Sensors configuration ---\r\n\r\n"); |
screamer | 10:b27c962b3c3f | 157 | |
screamer | 10:b27c962b3c3f | 158 | hum_temp.read_id(&id); |
screamer | 10:b27c962b3c3f | 159 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
screamer | 10:b27c962b3c3f | 160 | press_temp.read_id(&id); |
screamer | 10:b27c962b3c3f | 161 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
screamer | 10:b27c962b3c3f | 162 | magnetometer.read_id(&id); |
screamer | 10:b27c962b3c3f | 163 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
screamer | 10:b27c962b3c3f | 164 | acc_gyro.read_id(&id); |
screamer | 10:b27c962b3c3f | 165 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
screamer | 10:b27c962b3c3f | 166 | |
screamer | 10:b27c962b3c3f | 167 | printf("\n\r--- Reading sensor values ---\n\r"); ; |
screamer | 10:b27c962b3c3f | 168 | } |
screamer | 10:b27c962b3c3f | 169 | |
screamer | 10:b27c962b3c3f | 170 | /** |
screamer | 10:b27c962b3c3f | 171 | * Update sensors and report their values. |
screamer | 10:b27c962b3c3f | 172 | * This function is called periodically. |
screamer | 10:b27c962b3c3f | 173 | */ |
screamer | 10:b27c962b3c3f | 174 | void sensors_update() { |
screamer | 10:b27c962b3c3f | 175 | float value1, value2; |
screamer | 10:b27c962b3c3f | 176 | int32_t axes[3]; |
screamer | 10:b27c962b3c3f | 177 | uint32_t distance; |
screamer | 10:b27c962b3c3f | 178 | |
screamer | 10:b27c962b3c3f | 179 | printf("\r\n"); |
screamer | 10:b27c962b3c3f | 180 | |
screamer | 10:b27c962b3c3f | 181 | value1 = value2 = 0.0; |
screamer | 10:b27c962b3c3f | 182 | hum_temp.get_temperature(&value1); |
screamer | 10:b27c962b3c3f | 183 | hum_temp.get_humidity(&value2); |
screamer | 10:b27c962b3c3f | 184 | if (endpointInfo) { |
screamer | 10:b27c962b3c3f | 185 | temperature_res->set_value(value1); |
screamer | 10:b27c962b3c3f | 186 | humidity_res->set_value(value2); |
screamer | 10:b27c962b3c3f | 187 | } |
screamer | 10:b27c962b3c3f | 188 | printf("HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", value1, value2); |
screamer | 10:b27c962b3c3f | 189 | |
screamer | 10:b27c962b3c3f | 190 | value1 = value2 = 0.0; |
screamer | 10:b27c962b3c3f | 191 | press_temp.get_temperature(&value1); |
screamer | 10:b27c962b3c3f | 192 | press_temp.get_pressure(&value2); |
screamer | 10:b27c962b3c3f | 193 | printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value1, value2); |
screamer | 10:b27c962b3c3f | 194 | printf("Mag/Acc/Gyro readings: x, y, z\r\n"); |
screamer | 10:b27c962b3c3f | 195 | |
screamer | 10:b27c962b3c3f | 196 | magnetometer.get_m_axes(axes); |
screamer | 10:b27c962b3c3f | 197 | printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
screamer | 10:b27c962b3c3f | 198 | acc_gyro.get_x_axes(axes); |
screamer | 10:b27c962b3c3f | 199 | printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
screamer | 10:b27c962b3c3f | 200 | acc_gyro.get_g_axes(axes); |
screamer | 10:b27c962b3c3f | 201 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
screamer | 10:b27c962b3c3f | 202 | |
screamer | 10:b27c962b3c3f | 203 | if (range.get_distance(&distance) == VL53L0X_ERROR_NONE) { |
screamer | 10:b27c962b3c3f | 204 | printf("VL53L0X [mm]: %6ld\r\n", distance); |
screamer | 10:b27c962b3c3f | 205 | if (endpointInfo) { |
screamer | 10:b27c962b3c3f | 206 | distance_res->set_value((int)distance); |
screamer | 10:b27c962b3c3f | 207 | } |
screamer | 10:b27c962b3c3f | 208 | } else { |
screamer | 10:b27c962b3c3f | 209 | printf("VL53L0X [mm]: --\r\n"); |
screamer | 10:b27c962b3c3f | 210 | } |
screamer | 10:b27c962b3c3f | 211 | |
screamer | 10:b27c962b3c3f | 212 | printf("\033[8A"); |
screamer | 10:b27c962b3c3f | 213 | } |
screamer | 10:b27c962b3c3f | 214 | #endif /* SENSORS_AND_BUTTONS */ |
screamer | 10:b27c962b3c3f | 215 | |
screamer | 10:b27c962b3c3f | 216 | |
adustm | 4:cf7342047b4d | 217 | int main(void) { |
MarceloSalazar | 9:265744785d33 | 218 | printf("Starting Simple Pelion Device Management Client example\n"); |
adustm | 4:cf7342047b4d | 219 | printf("Connecting to the network using Wifi...\n"); |
adustm | 4:cf7342047b4d | 220 | |
screamer | 10:b27c962b3c3f | 221 | // If the User button is pressed, then format storage. |
screamer | 10:b27c962b3c3f | 222 | const int PRESSED = 0; |
screamer | 10:b27c962b3c3f | 223 | DigitalIn *user_button = new DigitalIn(USER_BUTTON); |
screamer | 10:b27c962b3c3f | 224 | if (user_button->read() == PRESSED) { |
screamer | 10:b27c962b3c3f | 225 | printf("User button is pushed on start. Formatting the storage...\n"); |
screamer | 10:b27c962b3c3f | 226 | int storage_status = fs.reformat(&sd); |
screamer | 10:b27c962b3c3f | 227 | if (storage_status != 0) { |
screamer | 10:b27c962b3c3f | 228 | if (sd.erase(0, sd.size()) == 0) { |
screamer | 10:b27c962b3c3f | 229 | if (fs.format(&sd) == 0) { |
screamer | 10:b27c962b3c3f | 230 | storage_status = 0; |
screamer | 10:b27c962b3c3f | 231 | printf("The storage reformatted successfully.\n"); |
screamer | 10:b27c962b3c3f | 232 | } |
screamer | 10:b27c962b3c3f | 233 | } |
screamer | 10:b27c962b3c3f | 234 | } |
screamer | 10:b27c962b3c3f | 235 | if (storage_status != 0) { |
screamer | 10:b27c962b3c3f | 236 | printf("Failed to reformat the storage.\n"); |
screamer | 10:b27c962b3c3f | 237 | } |
screamer | 10:b27c962b3c3f | 238 | } |
screamer | 10:b27c962b3c3f | 239 | |
screamer | 10:b27c962b3c3f | 240 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 241 | sensors_init(); |
screamer | 10:b27c962b3c3f | 242 | #endif /* SENSORS_AND_BUTTONS */ |
screamer | 10:b27c962b3c3f | 243 | |
adustm | 4:cf7342047b4d | 244 | // Connect to the internet (DHCP is expected to be on) |
MarceloSalazar | 9:265744785d33 | 245 | net = NetworkInterface::get_default_instance(); |
adustm | 4:cf7342047b4d | 246 | |
screamer | 10:b27c962b3c3f | 247 | nsapi_error_t net_status = -1; |
screamer | 10:b27c962b3c3f | 248 | for (int tries = 0; tries < 3; tries++) { |
screamer | 10:b27c962b3c3f | 249 | net_status = net->connect(); |
screamer | 10:b27c962b3c3f | 250 | if (net_status == NSAPI_ERROR_OK) { |
screamer | 10:b27c962b3c3f | 251 | break; |
screamer | 10:b27c962b3c3f | 252 | } else { |
screamer | 10:b27c962b3c3f | 253 | printf("[WARN] Unable to connect to network. Retrying...\n"); |
screamer | 10:b27c962b3c3f | 254 | } |
screamer | 10:b27c962b3c3f | 255 | } |
MarceloSalazar | 9:265744785d33 | 256 | |
screamer | 10:b27c962b3c3f | 257 | if (net_status != NSAPI_ERROR_OK) { |
screamer | 10:b27c962b3c3f | 258 | printf("Connecting to the network failed %d!\n", net_status); |
adustm | 1:e86b1cffc402 | 259 | return -1; |
adustm | 1:e86b1cffc402 | 260 | } |
adustm | 1:e86b1cffc402 | 261 | |
MarceloSalazar | 9:265744785d33 | 262 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
adustm | 1:e86b1cffc402 | 263 | |
MarceloSalazar | 9:265744785d33 | 264 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM |
MarceloSalazar | 9:265744785d33 | 265 | SimpleMbedCloudClient client(net, bd, &fs); |
adustm | 4:cf7342047b4d | 266 | int client_status = client.init(); |
adustm | 4:cf7342047b4d | 267 | if (client_status != 0) { |
MarceloSalazar | 9:265744785d33 | 268 | printf("Pelion Client initialization failed (%d)\n", client_status); |
adustm | 1:e86b1cffc402 | 269 | return -1; |
adustm | 1:e86b1cffc402 | 270 | } |
adustm | 1:e86b1cffc402 | 271 | |
adustm | 4:cf7342047b4d | 272 | // Creating resources, which can be written or read from the cloud |
adustm | 4:cf7342047b4d | 273 | button_res = client.create_resource("3200/0/5501", "button_count"); |
adustm | 4:cf7342047b4d | 274 | button_res->set_value(0); |
adustm | 4:cf7342047b4d | 275 | button_res->methods(M2MMethod::GET); |
adustm | 4:cf7342047b4d | 276 | button_res->observable(true); |
adustm | 4:cf7342047b4d | 277 | button_res->attach_notification_callback(button_callback); |
adustm | 1:e86b1cffc402 | 278 | |
adustm | 4:cf7342047b4d | 279 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
adustm | 4:cf7342047b4d | 280 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
adustm | 4:cf7342047b4d | 281 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
adustm | 4:cf7342047b4d | 282 | pattern_res->attach_put_callback(pattern_updated); |
adustm | 1:e86b1cffc402 | 283 | |
adustm | 4:cf7342047b4d | 284 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
adustm | 4:cf7342047b4d | 285 | blink_res->methods(M2MMethod::POST); |
adustm | 4:cf7342047b4d | 286 | blink_res->attach_post_callback(blink_callback); |
adustm | 4:cf7342047b4d | 287 | |
screamer | 10:b27c962b3c3f | 288 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 289 | // Sensor resources |
screamer | 10:b27c962b3c3f | 290 | temperature_res = client.create_resource("3303/0/5700", "temperature"); |
screamer | 10:b27c962b3c3f | 291 | temperature_res->set_value(0); |
screamer | 10:b27c962b3c3f | 292 | temperature_res->methods(M2MMethod::GET); |
screamer | 10:b27c962b3c3f | 293 | temperature_res->observable(true); |
screamer | 10:b27c962b3c3f | 294 | |
screamer | 10:b27c962b3c3f | 295 | humidity_res = client.create_resource("3304/0/5700", "humidity"); |
screamer | 10:b27c962b3c3f | 296 | humidity_res->set_value(0); |
screamer | 10:b27c962b3c3f | 297 | humidity_res->methods(M2MMethod::GET); |
screamer | 10:b27c962b3c3f | 298 | humidity_res->observable(true); |
screamer | 10:b27c962b3c3f | 299 | |
screamer | 10:b27c962b3c3f | 300 | distance_res = client.create_resource("3330/0/5700", "distance"); |
screamer | 10:b27c962b3c3f | 301 | distance_res->set_value(0); |
screamer | 10:b27c962b3c3f | 302 | distance_res->methods(M2MMethod::GET); |
screamer | 10:b27c962b3c3f | 303 | distance_res->observable(true); |
screamer | 10:b27c962b3c3f | 304 | #endif /* SENSORS_AND_BUTTONS */ |
screamer | 10:b27c962b3c3f | 305 | |
MarceloSalazar | 9:265744785d33 | 306 | printf("Initialized Pelion Client. Registering...\n"); |
adustm | 1:e86b1cffc402 | 307 | |
adustm | 4:cf7342047b4d | 308 | // Callback that fires when registering is complete |
adustm | 4:cf7342047b4d | 309 | client.on_registered(®istered); |
adustm | 1:e86b1cffc402 | 310 | |
MarceloSalazar | 9:265744785d33 | 311 | // Register with Pelion DM |
adustm | 4:cf7342047b4d | 312 | client.register_and_connect(); |
adustm | 1:e86b1cffc402 | 313 | |
adustm | 1:e86b1cffc402 | 314 | // Placeholder for callback to update local resource when GET comes. |
adustm | 4:cf7342047b4d | 315 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
screamer | 10:b27c962b3c3f | 316 | #ifdef SENSORS_AND_BUTTONS |
screamer | 10:b27c962b3c3f | 317 | button.fall(eventQueue.event(button_press)); |
screamer | 10:b27c962b3c3f | 318 | |
adustm | 4:cf7342047b4d | 319 | Ticker timer; |
screamer | 10:b27c962b3c3f | 320 | timer.attach(eventQueue.event(sensors_update), 3.0); |
screamer | 10:b27c962b3c3f | 321 | #else /* SENSORS_AND_BUTTONS */ |
screamer | 10:b27c962b3c3f | 322 | Ticker timer; |
screamer | 10:b27c962b3c3f | 323 | timer.attach(eventQueue.event(&button_press), 5.0); |
screamer | 10:b27c962b3c3f | 324 | #endif /* SENSORS_AND_BUTTONS */ |
adustm | 1:e86b1cffc402 | 325 | |
adustm | 4:cf7342047b4d | 326 | // You can easily run the eventQueue in a separate thread if required |
adustm | 4:cf7342047b4d | 327 | eventQueue.dispatch_forever(); |
adustm | 1:e86b1cffc402 | 328 | } |
MarceloSalazar | 9:265744785d33 | 329 | #endif |