Workshop example
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
main.cpp@35:42b3fba640b1, 2019-05-21 (annotated)
- Committer:
- JimCarver
- Date:
- Tue May 21 21:16:24 2019 +0000
- Revision:
- 35:42b3fba640b1
- Parent:
- 34:a5724eeaaf9d
Simple version for workshop
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 |
screamer | 28:0e774865873d | 19 | |
JimCarver | 35:42b3fba640b1 | 20 | |
adustm | 1:e86b1cffc402 | 21 | #include "mbed.h" |
adustm | 1:e86b1cffc402 | 22 | #include "simple-mbed-cloud-client.h" |
screamer | 10:b27c962b3c3f | 23 | #include "LittleFileSystem.h" |
screamer | 33:cfd9430e7d1e | 24 | |
screamer | 33:cfd9430e7d1e | 25 | // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. |
screamer | 33:cfd9430e7d1e | 26 | NetworkInterface *net; |
screamer | 33:cfd9430e7d1e | 27 | |
screamer | 33:cfd9430e7d1e | 28 | // Default block device available on the target board |
screamer | 33:cfd9430e7d1e | 29 | BlockDevice* bd = BlockDevice::get_default_instance(); |
screamer | 33:cfd9430e7d1e | 30 | SlicingBlockDevice sd(bd, 0, 2*1024*1024); |
screamer | 33:cfd9430e7d1e | 31 | |
screamer | 33:cfd9430e7d1e | 32 | #if COMPONENT_SD || COMPONENT_NUSD |
screamer | 33:cfd9430e7d1e | 33 | // Use FATFileSystem for SD card type blockdevices |
screamer | 33:cfd9430e7d1e | 34 | FATFileSystem fs("fs"); |
screamer | 33:cfd9430e7d1e | 35 | #else |
screamer | 33:cfd9430e7d1e | 36 | // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions |
screamer | 33:cfd9430e7d1e | 37 | LittleFileSystem fs("fs"); |
screamer | 33:cfd9430e7d1e | 38 | #endif |
screamer | 33:cfd9430e7d1e | 39 | |
screamer | 33:cfd9430e7d1e | 40 | // Default User button for GET example and for resetting the storage |
screamer | 33:cfd9430e7d1e | 41 | InterruptIn button(BUTTON1); |
screamer | 33:cfd9430e7d1e | 42 | // Default LED to use for PUT/POST example |
screamer | 33:cfd9430e7d1e | 43 | DigitalOut led(LED1, 1); |
screamer | 33:cfd9430e7d1e | 44 | |
screamer | 33:cfd9430e7d1e | 45 | // How often to fetch sensor data (in seconds) |
screamer | 33:cfd9430e7d1e | 46 | #define SENSORS_POLL_INTERVAL 3.0 |
screamer | 33:cfd9430e7d1e | 47 | |
JimCarver | 35:42b3fba640b1 | 48 | |
screamer | 33:cfd9430e7d1e | 49 | |
screamer | 33:cfd9430e7d1e | 50 | // Sensors related includes and initialization |
screamer | 10:b27c962b3c3f | 51 | |
JimCarver | 35:42b3fba640b1 | 52 | |
screamer | 33:cfd9430e7d1e | 53 | // Temperature reading from microcontroller |
screamer | 33:cfd9430e7d1e | 54 | AnalogIn adc_temp(ADC_TEMP); |
screamer | 33:cfd9430e7d1e | 55 | // Voltage reference reading from microcontroller |
screamer | 33:cfd9430e7d1e | 56 | AnalogIn adc_vref(ADC_VREF); |
screamer | 11:8df4529f060d | 57 | |
MarceloSalazar | 9:265744785d33 | 58 | // Declaring pointers for access to Pelion Client resources outside of main() |
screamer | 12:1f1a50e973db | 59 | MbedCloudClientResource *res_button; |
screamer | 12:1f1a50e973db | 60 | MbedCloudClientResource *res_led; |
adustm | 1:e86b1cffc402 | 61 | |
JimCarver | 35:42b3fba640b1 | 62 | |
screamer | 28:0e774865873d | 63 | MbedCloudClientResource *res_adc_temp; |
screamer | 28:0e774865873d | 64 | MbedCloudClientResource *res_adc_voltage; |
JimCarver | 35:42b3fba640b1 | 65 | |
adustm | 1:e86b1cffc402 | 66 | |
screamer | 33:cfd9430e7d1e | 67 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
screamer | 33:cfd9430e7d1e | 68 | // 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 | 33:cfd9430e7d1e | 69 | EventQueue eventQueue; |
screamer | 33:cfd9430e7d1e | 70 | |
screamer | 10:b27c962b3c3f | 71 | // When the device is registered, this variable will be used to access various useful information, like device ID etc. |
screamer | 10:b27c962b3c3f | 72 | static const ConnectorClientEndpointInfo* endpointInfo; |
adustm | 1:e86b1cffc402 | 73 | |
screamer | 10:b27c962b3c3f | 74 | /** |
adustm | 4:cf7342047b4d | 75 | * PUT handler |
adustm | 4:cf7342047b4d | 76 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 77 | * @param newValue Updated value for the resource |
adustm | 4:cf7342047b4d | 78 | */ |
screamer | 32:2871fbeb627d | 79 | void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { |
screamer | 29:6ff737b67e7d | 80 | printf("*** PUT received, new value: %s \n", newValue.c_str()); |
screamer | 11:8df4529f060d | 81 | led = atoi(newValue.c_str()); |
adustm | 1:e86b1cffc402 | 82 | } |
adustm | 1:e86b1cffc402 | 83 | |
adustm | 4:cf7342047b4d | 84 | /** |
adustm | 4:cf7342047b4d | 85 | * POST handler |
adustm | 4:cf7342047b4d | 86 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 87 | * @param buffer If a body was passed to the POST function, this contains the data. |
adustm | 4:cf7342047b4d | 88 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
adustm | 4:cf7342047b4d | 89 | * @param size Size of the body |
adustm | 4:cf7342047b4d | 90 | */ |
screamer | 32:2871fbeb627d | 91 | void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
screamer | 32:2871fbeb627d | 92 | printf("*** POST received (length %u). Payload: ", size); |
screamer | 32:2871fbeb627d | 93 | for (size_t ix = 0; ix < size; ix++) { |
screamer | 32:2871fbeb627d | 94 | printf("%02x ", buffer[ix]); |
screamer | 32:2871fbeb627d | 95 | } |
screamer | 32:2871fbeb627d | 96 | printf("\n"); |
screamer | 11:8df4529f060d | 97 | } |
adustm | 1:e86b1cffc402 | 98 | |
screamer | 11:8df4529f060d | 99 | /** |
screamer | 13:42b49a0caade | 100 | * Button function triggered by the physical button press. |
screamer | 11:8df4529f060d | 101 | */ |
screamer | 11:8df4529f060d | 102 | void button_press() { |
screamer | 12:1f1a50e973db | 103 | int v = res_button->get_value_int() + 1; |
screamer | 12:1f1a50e973db | 104 | res_button->set_value(v); |
screamer | 29:6ff737b67e7d | 105 | printf("*** Button clicked %d times \n", v); |
adustm | 1:e86b1cffc402 | 106 | } |
adustm | 1:e86b1cffc402 | 107 | |
adustm | 4:cf7342047b4d | 108 | /** |
adustm | 4:cf7342047b4d | 109 | * Notification callback handler |
adustm | 4:cf7342047b4d | 110 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 111 | * @param status The delivery status of the notification |
adustm | 4:cf7342047b4d | 112 | */ |
adustm | 4:cf7342047b4d | 113 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
screamer | 29:6ff737b67e7d | 114 | printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); |
adustm | 4:cf7342047b4d | 115 | } |
adustm | 1:e86b1cffc402 | 116 | |
adustm | 4:cf7342047b4d | 117 | /** |
adustm | 4:cf7342047b4d | 118 | * Registration callback handler |
adustm | 4:cf7342047b4d | 119 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
adustm | 4:cf7342047b4d | 120 | */ |
adustm | 4:cf7342047b4d | 121 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
screamer | 17:fc98adcf835a | 122 | printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
screamer | 10:b27c962b3c3f | 123 | endpointInfo = endpoint; |
adustm | 4:cf7342047b4d | 124 | } |
adustm | 1:e86b1cffc402 | 125 | |
screamer | 10:b27c962b3c3f | 126 | /** |
screamer | 10:b27c962b3c3f | 127 | * Initialize sensors |
screamer | 10:b27c962b3c3f | 128 | */ |
screamer | 10:b27c962b3c3f | 129 | void sensors_init() { |
screamer | 10:b27c962b3c3f | 130 | |
screamer | 29:6ff737b67e7d | 131 | printf ("\nSensors configuration:\n"); |
screamer | 10:b27c962b3c3f | 132 | |
screamer | 17:fc98adcf835a | 133 | printf("\n"); ; |
screamer | 10:b27c962b3c3f | 134 | } |
screamer | 10:b27c962b3c3f | 135 | |
screamer | 10:b27c962b3c3f | 136 | /** |
screamer | 10:b27c962b3c3f | 137 | * Update sensors and report their values. |
screamer | 10:b27c962b3c3f | 138 | * This function is called periodically. |
screamer | 10:b27c962b3c3f | 139 | */ |
screamer | 10:b27c962b3c3f | 140 | void sensors_update() { |
JimCarver | 35:42b3fba640b1 | 141 | float temp3_value, volt_value = 0.0; |
screamer | 10:b27c962b3c3f | 142 | |
screamer | 32:2871fbeb627d | 143 | temp3_value = adc_temp.read()*100; |
screamer | 32:2871fbeb627d | 144 | volt_value = adc_vref.read(); |
screamer | 32:2871fbeb627d | 145 | |
screamer | 28:0e774865873d | 146 | |
screamer | 28:0e774865873d | 147 | |
screamer | 13:42b49a0caade | 148 | if (endpointInfo) { |
JimCarver | 35:42b3fba640b1 | 149 | |
screamer | 32:2871fbeb627d | 150 | res_adc_temp->set_value(temp3_value); |
screamer | 32:2871fbeb627d | 151 | res_adc_voltage->set_value(volt_value); |
JimCarver | 35:42b3fba640b1 | 152 | |
screamer | 28:0e774865873d | 153 | } |
screamer | 10:b27c962b3c3f | 154 | } |
screamer | 10:b27c962b3c3f | 155 | |
adustm | 4:cf7342047b4d | 156 | int main(void) { |
screamer | 17:fc98adcf835a | 157 | printf("\nStarting Simple Pelion Device Management Client example\n"); |
adustm | 4:cf7342047b4d | 158 | |
screamer | 29:6ff737b67e7d | 159 | int storage_status = fs.mount(&sd); |
screamer | 29:6ff737b67e7d | 160 | if (storage_status != 0) { |
screamer | 29:6ff737b67e7d | 161 | printf("Storage mounting failed.\n"); |
screamer | 29:6ff737b67e7d | 162 | } |
screamer | 30:15743b79c6cb | 163 | // If the User button is pressed ons start, then format storage. |
screamer | 29:6ff737b67e7d | 164 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
screamer | 29:6ff737b67e7d | 165 | if (btn_pressed) { |
screamer | 29:6ff737b67e7d | 166 | printf("User button is pushed on start...\n"); |
screamer | 29:6ff737b67e7d | 167 | } |
screamer | 30:15743b79c6cb | 168 | |
screamer | 29:6ff737b67e7d | 169 | if (storage_status || btn_pressed) { |
screamer | 29:6ff737b67e7d | 170 | printf("Formatting the storage...\n"); |
screamer | 30:15743b79c6cb | 171 | int storage_status = StorageHelper::format(&fs, &sd); |
screamer | 10:b27c962b3c3f | 172 | if (storage_status != 0) { |
screamer | 13:42b49a0caade | 173 | printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); |
screamer | 10:b27c962b3c3f | 174 | } |
screamer | 28:0e774865873d | 175 | } else { |
screamer | 28:0e774865873d | 176 | printf("You can hold the user button during boot to format the storage and change the device identity.\n"); |
screamer | 10:b27c962b3c3f | 177 | } |
screamer | 10:b27c962b3c3f | 178 | |
screamer | 10:b27c962b3c3f | 179 | sensors_init(); |
screamer | 10:b27c962b3c3f | 180 | |
adustm | 4:cf7342047b4d | 181 | // Connect to the internet (DHCP is expected to be on) |
screamer | 13:42b49a0caade | 182 | printf("Connecting to the network using Wifi...\n"); |
MarceloSalazar | 9:265744785d33 | 183 | net = NetworkInterface::get_default_instance(); |
adustm | 4:cf7342047b4d | 184 | |
screamer | 10:b27c962b3c3f | 185 | nsapi_error_t net_status = -1; |
screamer | 10:b27c962b3c3f | 186 | for (int tries = 0; tries < 3; tries++) { |
screamer | 10:b27c962b3c3f | 187 | net_status = net->connect(); |
screamer | 10:b27c962b3c3f | 188 | if (net_status == NSAPI_ERROR_OK) { |
screamer | 10:b27c962b3c3f | 189 | break; |
screamer | 10:b27c962b3c3f | 190 | } else { |
screamer | 13:42b49a0caade | 191 | printf("Unable to connect to network. Retrying...\n"); |
screamer | 10:b27c962b3c3f | 192 | } |
screamer | 10:b27c962b3c3f | 193 | } |
MarceloSalazar | 9:265744785d33 | 194 | |
screamer | 10:b27c962b3c3f | 195 | if (net_status != NSAPI_ERROR_OK) { |
screamer | 13:42b49a0caade | 196 | printf("ERROR: Connecting to the network failed (%d)!\n", net_status); |
adustm | 1:e86b1cffc402 | 197 | return -1; |
adustm | 1:e86b1cffc402 | 198 | } |
adustm | 1:e86b1cffc402 | 199 | |
MarceloSalazar | 9:265744785d33 | 200 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
adustm | 1:e86b1cffc402 | 201 | |
screamer | 17:fc98adcf835a | 202 | printf("Initializing Pelion Device Management Client...\n"); |
screamer | 17:fc98adcf835a | 203 | |
MarceloSalazar | 9:265744785d33 | 204 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM |
MarceloSalazar | 9:265744785d33 | 205 | SimpleMbedCloudClient client(net, bd, &fs); |
adustm | 4:cf7342047b4d | 206 | int client_status = client.init(); |
adustm | 4:cf7342047b4d | 207 | if (client_status != 0) { |
screamer | 13:42b49a0caade | 208 | printf("ERROR: Pelion Client initialization failed (%d)\n", client_status); |
adustm | 1:e86b1cffc402 | 209 | return -1; |
adustm | 1:e86b1cffc402 | 210 | } |
adustm | 1:e86b1cffc402 | 211 | |
adustm | 4:cf7342047b4d | 212 | // Creating resources, which can be written or read from the cloud |
screamer | 32:2871fbeb627d | 213 | res_button = client.create_resource("3200/0/5501", "Button Count"); |
screamer | 12:1f1a50e973db | 214 | res_button->set_value(0); |
screamer | 12:1f1a50e973db | 215 | res_button->methods(M2MMethod::GET); |
screamer | 12:1f1a50e973db | 216 | res_button->observable(true); |
screamer | 12:1f1a50e973db | 217 | res_button->attach_notification_callback(button_callback); |
adustm | 1:e86b1cffc402 | 218 | |
screamer | 33:cfd9430e7d1e | 219 | res_led = client.create_resource("3201/0/5853", "LED State"); |
screamer | 33:cfd9430e7d1e | 220 | res_led->set_value(1); |
screamer | 33:cfd9430e7d1e | 221 | res_led->methods(M2MMethod::GET | M2MMethod::PUT); |
screamer | 33:cfd9430e7d1e | 222 | res_led->attach_put_callback(put_callback); |
screamer | 33:cfd9430e7d1e | 223 | |
screamer | 32:2871fbeb627d | 224 | res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)"); |
screamer | 28:0e774865873d | 225 | res_adc_temp->set_value(0); |
screamer | 28:0e774865873d | 226 | res_adc_temp->methods(M2MMethod::GET); |
screamer | 28:0e774865873d | 227 | res_adc_temp->observable(true); |
screamer | 28:0e774865873d | 228 | |
screamer | 32:2871fbeb627d | 229 | res_adc_voltage = client.create_resource("3316/0/5700", "Voltage"); |
screamer | 28:0e774865873d | 230 | res_adc_voltage->set_value(0); |
screamer | 28:0e774865873d | 231 | res_adc_voltage->methods(M2MMethod::GET); |
screamer | 28:0e774865873d | 232 | res_adc_voltage->observable(true); |
screamer | 28:0e774865873d | 233 | |
MarceloSalazar | 9:265744785d33 | 234 | printf("Initialized Pelion Client. Registering...\n"); |
adustm | 1:e86b1cffc402 | 235 | |
adustm | 4:cf7342047b4d | 236 | // Callback that fires when registering is complete |
adustm | 4:cf7342047b4d | 237 | client.on_registered(®istered); |
adustm | 1:e86b1cffc402 | 238 | |
MarceloSalazar | 9:265744785d33 | 239 | // Register with Pelion DM |
adustm | 4:cf7342047b4d | 240 | client.register_and_connect(); |
adustm | 1:e86b1cffc402 | 241 | |
screamer | 17:fc98adcf835a | 242 | int i = 600; // wait up 60 seconds before attaching sensors and button events |
screamer | 12:1f1a50e973db | 243 | while (i-- > 0 && !client.is_client_registered()) { |
screamer | 12:1f1a50e973db | 244 | wait_ms(100); |
screamer | 12:1f1a50e973db | 245 | } |
screamer | 12:1f1a50e973db | 246 | |
screamer | 11:8df4529f060d | 247 | button.fall(eventQueue.event(&button_press)); |
screamer | 10:b27c962b3c3f | 248 | |
screamer | 15:a0430d40a918 | 249 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
adustm | 4:cf7342047b4d | 250 | Ticker timer; |
screamer | 11:8df4529f060d | 251 | timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); |
adustm | 1:e86b1cffc402 | 252 | |
adustm | 4:cf7342047b4d | 253 | // You can easily run the eventQueue in a separate thread if required |
adustm | 4:cf7342047b4d | 254 | eventQueue.dispatch_forever(); |
adustm | 1:e86b1cffc402 | 255 | } |
screamer | 28:0e774865873d | 256 | |
MarceloSalazar | 9:265744785d33 | 257 | #endif |