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