ST
/
example-IDW01M1-mbed-Cloud-connect
Generic example for any board with IDMW0xM1 shield
main.cpp@40:17cd01e5f1fa, 2018-05-07 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Mon May 07 09:51:15 2018 +0200
- Revision:
- 40:17cd01e5f1fa
- Parent:
- 31:11e00e0c285d
- Child:
- 42:cddcdf68386d
Align with `simple-mbed-cloud-client-example-restricted`
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarceloSalazar | 0:e13a8a944e25 | 1 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 2 | // Copyright 2016-2017 ARM Ltd. |
MarceloSalazar | 0:e13a8a944e25 | 3 | // |
MarceloSalazar | 0:e13a8a944e25 | 4 | // SPDX-License-Identifier: Apache-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 5 | // |
MarceloSalazar | 0:e13a8a944e25 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
MarceloSalazar | 0:e13a8a944e25 | 7 | // you may not use this file except in compliance with the License. |
MarceloSalazar | 0:e13a8a944e25 | 8 | // You may obtain a copy of the License at |
MarceloSalazar | 0:e13a8a944e25 | 9 | // |
MarceloSalazar | 0:e13a8a944e25 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 11 | // |
MarceloSalazar | 0:e13a8a944e25 | 12 | // Unless required by applicable law or agreed to in writing, software |
MarceloSalazar | 0:e13a8a944e25 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
MarceloSalazar | 0:e13a8a944e25 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MarceloSalazar | 0:e13a8a944e25 | 15 | // See the License for the specific language governing permissions and |
MarceloSalazar | 0:e13a8a944e25 | 16 | // limitations under the License. |
MarceloSalazar | 0:e13a8a944e25 | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 18 | |
MarceloSalazar | 1:1ccf36276cd3 | 19 | #include "mbed.h" |
MarceloSalazar | 0:e13a8a944e25 | 20 | #include "simple-mbed-cloud-client.h" |
MarceloSalazar | 1:1ccf36276cd3 | 21 | #include "SDBlockDevice.h" |
MarceloSalazar | 1:1ccf36276cd3 | 22 | #include "FATFileSystem.h" |
Wolfgang Betz |
17:ada41201c642 | 23 | #include "SpwfSAInterface.h" |
MarceloSalazar | 1:1ccf36276cd3 | 24 | |
Wolfgang Betz |
40:17cd01e5f1fa | 25 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
Wolfgang Betz |
40:17cd01e5f1fa | 26 | // 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 |
Wolfgang Betz |
40:17cd01e5f1fa | 27 | EventQueue eventQueue; |
MarceloSalazar | 0:e13a8a944e25 | 28 | |
Wolfgang Betz |
40:17cd01e5f1fa | 29 | // Instanziate Wi-Fi device |
Wolfgang Betz |
40:17cd01e5f1fa | 30 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
bridadan | 5:c18fab181ede | 31 | |
Wolfgang Betz |
40:17cd01e5f1fa | 32 | // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available) |
Wolfgang Betz |
19:5dd1ec7fdf0d | 33 | SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); |
Wolfgang Betz |
40:17cd01e5f1fa | 34 | FATFileSystem fs("sd", &sd); |
Wolfgang Betz |
19:5dd1ec7fdf0d | 35 | |
Wolfgang Betz |
40:17cd01e5f1fa | 36 | // Declaring pointers for access to Mbed Cloud Client resources outside of main() |
Wolfgang Betz |
40:17cd01e5f1fa | 37 | MbedCloudClientResource *button_res; |
Wolfgang Betz |
40:17cd01e5f1fa | 38 | MbedCloudClientResource *pattern_res; |
MarceloSalazar | 1:1ccf36276cd3 | 39 | |
Wolfgang Betz |
40:17cd01e5f1fa | 40 | // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button |
Wolfgang Betz |
40:17cd01e5f1fa | 41 | void fake_button_press() { |
Wolfgang Betz |
40:17cd01e5f1fa | 42 | int v = button_res->get_value_int() + 1; |
MarceloSalazar | 1:1ccf36276cd3 | 43 | |
Wolfgang Betz |
40:17cd01e5f1fa | 44 | button_res->set_value(v); |
Wolfgang Betz |
40:17cd01e5f1fa | 45 | |
Wolfgang Betz |
40:17cd01e5f1fa | 46 | printf("Simulated button clicked %d times\n", v); |
MarceloSalazar | 0:e13a8a944e25 | 47 | } |
MarceloSalazar | 0:e13a8a944e25 | 48 | |
Wolfgang Betz |
40:17cd01e5f1fa | 49 | /** |
Wolfgang Betz |
40:17cd01e5f1fa | 50 | * PUT handler |
Wolfgang Betz |
40:17cd01e5f1fa | 51 | * @param resource The resource that triggered the callback |
Wolfgang Betz |
40:17cd01e5f1fa | 52 | * @param newValue Updated value for the resource |
Wolfgang Betz |
40:17cd01e5f1fa | 53 | */ |
Wolfgang Betz |
40:17cd01e5f1fa | 54 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
Wolfgang Betz |
40:17cd01e5f1fa | 55 | printf("PUT received, new value: %s\n", newValue.c_str()); |
MarceloSalazar | 0:e13a8a944e25 | 56 | } |
MarceloSalazar | 0:e13a8a944e25 | 57 | |
Wolfgang Betz |
40:17cd01e5f1fa | 58 | /** |
Wolfgang Betz |
40:17cd01e5f1fa | 59 | * POST handler |
Wolfgang Betz |
40:17cd01e5f1fa | 60 | * @param resource The resource that triggered the callback |
Wolfgang Betz |
40:17cd01e5f1fa | 61 | * @param buffer If a body was passed to the POST function, this contains the data. |
Wolfgang Betz |
40:17cd01e5f1fa | 62 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
Wolfgang Betz |
40:17cd01e5f1fa | 63 | * @param size Size of the body |
Wolfgang Betz |
40:17cd01e5f1fa | 64 | */ |
Wolfgang Betz |
40:17cd01e5f1fa | 65 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
Wolfgang Betz |
40:17cd01e5f1fa | 66 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
Wolfgang Betz |
40:17cd01e5f1fa | 67 | |
Wolfgang Betz |
40:17cd01e5f1fa | 68 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
Wolfgang Betz |
40:17cd01e5f1fa | 69 | |
Wolfgang Betz |
40:17cd01e5f1fa | 70 | // Parse the pattern string, and toggle the LED in that pattern |
Wolfgang Betz |
40:17cd01e5f1fa | 71 | string s = std::string(pattern_res->get_value().c_str()); |
Wolfgang Betz |
40:17cd01e5f1fa | 72 | size_t i = 0; |
Wolfgang Betz |
40:17cd01e5f1fa | 73 | size_t pos = s.find(':'); |
Wolfgang Betz |
40:17cd01e5f1fa | 74 | while (pos != string::npos) { |
Wolfgang Betz |
40:17cd01e5f1fa | 75 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
Wolfgang Betz |
40:17cd01e5f1fa | 76 | augmentedLed = !augmentedLed; |
Wolfgang Betz |
40:17cd01e5f1fa | 77 | |
Wolfgang Betz |
40:17cd01e5f1fa | 78 | i = ++pos; |
Wolfgang Betz |
40:17cd01e5f1fa | 79 | pos = s.find(':', pos); |
Wolfgang Betz |
40:17cd01e5f1fa | 80 | |
Wolfgang Betz |
40:17cd01e5f1fa | 81 | if (pos == string::npos) { |
Wolfgang Betz |
40:17cd01e5f1fa | 82 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
Wolfgang Betz |
40:17cd01e5f1fa | 83 | augmentedLed = !augmentedLed; |
Wolfgang Betz |
40:17cd01e5f1fa | 84 | } |
Wolfgang Betz |
17:ada41201c642 | 85 | } |
MarceloSalazar | 0:e13a8a944e25 | 86 | } |
MarceloSalazar | 0:e13a8a944e25 | 87 | |
Wolfgang Betz |
40:17cd01e5f1fa | 88 | /** |
Wolfgang Betz |
40:17cd01e5f1fa | 89 | * Notification callback handler |
Wolfgang Betz |
40:17cd01e5f1fa | 90 | * @param resource The resource that triggered the callback |
Wolfgang Betz |
40:17cd01e5f1fa | 91 | * @param status The delivery status of the notification |
Wolfgang Betz |
40:17cd01e5f1fa | 92 | */ |
Wolfgang Betz |
40:17cd01e5f1fa | 93 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
Wolfgang Betz |
40:17cd01e5f1fa | 94 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
Wolfgang Betz |
17:ada41201c642 | 95 | } |
MarceloSalazar | 0:e13a8a944e25 | 96 | |
Wolfgang Betz |
40:17cd01e5f1fa | 97 | /** |
Wolfgang Betz |
40:17cd01e5f1fa | 98 | * Registration callback handler |
Wolfgang Betz |
40:17cd01e5f1fa | 99 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
Wolfgang Betz |
40:17cd01e5f1fa | 100 | */ |
Wolfgang Betz |
40:17cd01e5f1fa | 101 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
Wolfgang Betz |
40:17cd01e5f1fa | 102 | printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
Wolfgang Betz |
40:17cd01e5f1fa | 103 | } |
MarceloSalazar | 1:1ccf36276cd3 | 104 | |
Wolfgang Betz |
40:17cd01e5f1fa | 105 | int main(void) { |
Wolfgang Betz |
40:17cd01e5f1fa | 106 | printf("Starting Simple Mbed Cloud Client example\n"); |
Wolfgang Betz |
40:17cd01e5f1fa | 107 | printf("Connecting to the network using Ethernet...\n"); |
Wolfgang Betz |
17:ada41201c642 | 108 | |
Wolfgang Betz |
40:17cd01e5f1fa | 109 | // Connect to the internet (DHCP is expected to be on) |
Wolfgang Betz |
40:17cd01e5f1fa | 110 | nsapi_error_t status = wifi.connect(); |
Wolfgang Betz |
40:17cd01e5f1fa | 111 | |
Wolfgang Betz |
40:17cd01e5f1fa | 112 | if (status != 0) { |
Wolfgang Betz |
40:17cd01e5f1fa | 113 | printf("Connecting to the network failed %d!\n", status); |
MarceloSalazar | 1:1ccf36276cd3 | 114 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 115 | } |
MarceloSalazar | 0:e13a8a944e25 | 116 | |
Wolfgang Betz |
40:17cd01e5f1fa | 117 | printf("Connected to the network successfully. IP address: %s\n", wifi.get_ip_address()); |
MarceloSalazar | 1:1ccf36276cd3 | 118 | |
Wolfgang Betz |
40:17cd01e5f1fa | 119 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
Wolfgang Betz |
40:17cd01e5f1fa | 120 | SimpleMbedCloudClient client(&wifi, &sd, &fs); |
Wolfgang Betz |
40:17cd01e5f1fa | 121 | int client_status = client.init(); |
Wolfgang Betz |
40:17cd01e5f1fa | 122 | if (client_status != 0) { |
Wolfgang Betz |
40:17cd01e5f1fa | 123 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
MarceloSalazar | 1:1ccf36276cd3 | 124 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 125 | } |
MarceloSalazar | 1:1ccf36276cd3 | 126 | |
Wolfgang Betz |
40:17cd01e5f1fa | 127 | // Creating resources, which can be written or read from the cloud |
Wolfgang Betz |
40:17cd01e5f1fa | 128 | button_res = client.create_resource("3200/0/5501", "button_count"); |
Wolfgang Betz |
40:17cd01e5f1fa | 129 | button_res->set_value(0); |
Wolfgang Betz |
40:17cd01e5f1fa | 130 | button_res->methods(M2MMethod::GET); |
Wolfgang Betz |
40:17cd01e5f1fa | 131 | button_res->observable(true); |
Wolfgang Betz |
40:17cd01e5f1fa | 132 | button_res->attach_notification_callback(button_callback); |
MarceloSalazar | 0:e13a8a944e25 | 133 | |
Wolfgang Betz |
40:17cd01e5f1fa | 134 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
Wolfgang Betz |
40:17cd01e5f1fa | 135 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
Wolfgang Betz |
40:17cd01e5f1fa | 136 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
Wolfgang Betz |
40:17cd01e5f1fa | 137 | pattern_res->attach_put_callback(pattern_updated); |
MarceloSalazar | 0:e13a8a944e25 | 138 | |
Wolfgang Betz |
40:17cd01e5f1fa | 139 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
Wolfgang Betz |
40:17cd01e5f1fa | 140 | blink_res->methods(M2MMethod::POST); |
Wolfgang Betz |
40:17cd01e5f1fa | 141 | blink_res->attach_post_callback(blink_callback); |
Wolfgang Betz |
17:ada41201c642 | 142 | |
Wolfgang Betz |
40:17cd01e5f1fa | 143 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
Wolfgang Betz |
17:ada41201c642 | 144 | |
Wolfgang Betz |
40:17cd01e5f1fa | 145 | // Callback that fires when registering is complete |
Wolfgang Betz |
40:17cd01e5f1fa | 146 | client.on_registered(®istered); |
MarceloSalazar | 0:e13a8a944e25 | 147 | |
Wolfgang Betz |
40:17cd01e5f1fa | 148 | // Register with Mbed Cloud |
Wolfgang Betz |
40:17cd01e5f1fa | 149 | client.register_and_connect(); |
MarceloSalazar | 0:e13a8a944e25 | 150 | |
Wolfgang Betz |
40:17cd01e5f1fa | 151 | // Placeholder for callback to update local resource when GET comes. |
Wolfgang Betz |
40:17cd01e5f1fa | 152 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
Wolfgang Betz |
40:17cd01e5f1fa | 153 | Ticker timer; |
Wolfgang Betz |
40:17cd01e5f1fa | 154 | timer.attach(eventQueue.event(&fake_button_press), 5.0); |
MarceloSalazar | 1:1ccf36276cd3 | 155 | |
Wolfgang Betz |
40:17cd01e5f1fa | 156 | // You can easily run the eventQueue in a separate thread if required |
Wolfgang Betz |
40:17cd01e5f1fa | 157 | eventQueue.dispatch_forever(); |
MarceloSalazar | 0:e13a8a944e25 | 158 | } |