Generic example for any board with IDMW0xM1 shield

Committer:
Wolfgang Betz
Date:
Thu May 10 09:50:02 2018 +0200
Revision:
52:0dd6b2df9805
Parent:
44:1d5e5eb2d2fb
Merge branch 'betzw_wb' into arm_deliv_wb

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MarceloSalazar 0:e13a8a944e25 1 // ----------------------------------------------------------------------------
Wolfgang Betz 44:1d5e5eb2d2fb 2 // Copyright 2016-2018 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 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
Wolfgang Betz 19:5dd1ec7fdf0d 30 SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
Wolfgang Betz 40:17cd01e5f1fa 31 FATFileSystem fs("sd", &sd);
Wolfgang Betz 19:5dd1ec7fdf0d 32
Wolfgang Betz 40:17cd01e5f1fa 33 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
Wolfgang Betz 40:17cd01e5f1fa 34 MbedCloudClientResource *button_res;
Wolfgang Betz 40:17cd01e5f1fa 35 MbedCloudClientResource *pattern_res;
MarceloSalazar 1:1ccf36276cd3 36
Wolfgang Betz 40:17cd01e5f1fa 37 // 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 38 void fake_button_press() {
Wolfgang Betz 40:17cd01e5f1fa 39 int v = button_res->get_value_int() + 1;
MarceloSalazar 1:1ccf36276cd3 40
Wolfgang Betz 40:17cd01e5f1fa 41 button_res->set_value(v);
Wolfgang Betz 40:17cd01e5f1fa 42
Wolfgang Betz 40:17cd01e5f1fa 43 printf("Simulated button clicked %d times\n", v);
MarceloSalazar 0:e13a8a944e25 44 }
MarceloSalazar 0:e13a8a944e25 45
Wolfgang Betz 40:17cd01e5f1fa 46 /**
Wolfgang Betz 40:17cd01e5f1fa 47 * PUT handler
Wolfgang Betz 40:17cd01e5f1fa 48 * @param resource The resource that triggered the callback
Wolfgang Betz 40:17cd01e5f1fa 49 * @param newValue Updated value for the resource
Wolfgang Betz 40:17cd01e5f1fa 50 */
Wolfgang Betz 40:17cd01e5f1fa 51 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
Wolfgang Betz 40:17cd01e5f1fa 52 printf("PUT received, new value: %s\n", newValue.c_str());
MarceloSalazar 0:e13a8a944e25 53 }
MarceloSalazar 0:e13a8a944e25 54
Wolfgang Betz 40:17cd01e5f1fa 55 /**
Wolfgang Betz 40:17cd01e5f1fa 56 * POST handler
Wolfgang Betz 40:17cd01e5f1fa 57 * @param resource The resource that triggered the callback
Wolfgang Betz 40:17cd01e5f1fa 58 * @param buffer If a body was passed to the POST function, this contains the data.
Wolfgang Betz 40:17cd01e5f1fa 59 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
Wolfgang Betz 40:17cd01e5f1fa 60 * @param size Size of the body
Wolfgang Betz 40:17cd01e5f1fa 61 */
Wolfgang Betz 40:17cd01e5f1fa 62 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
Wolfgang Betz 40:17cd01e5f1fa 63 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
Wolfgang Betz 40:17cd01e5f1fa 64
Wolfgang Betz 42:cddcdf68386d 65 static DigitalOut augmentedLed(NC); // LED that is used for blinking the pattern
Wolfgang Betz 42:cddcdf68386d 66 // betzw - NOTE: `LED1` conflicts with SPI clock,
Wolfgang Betz 42:cddcdf68386d 67 // therefore switching of blinking using `NC`
Wolfgang Betz 40:17cd01e5f1fa 68
Wolfgang Betz 40:17cd01e5f1fa 69 // Parse the pattern string, and toggle the LED in that pattern
Wolfgang Betz 40:17cd01e5f1fa 70 string s = std::string(pattern_res->get_value().c_str());
Wolfgang Betz 40:17cd01e5f1fa 71 size_t i = 0;
Wolfgang Betz 40:17cd01e5f1fa 72 size_t pos = s.find(':');
Wolfgang Betz 40:17cd01e5f1fa 73 while (pos != string::npos) {
Wolfgang Betz 40:17cd01e5f1fa 74 wait_ms(atoi(s.substr(i, pos - i).c_str()));
Wolfgang Betz 40:17cd01e5f1fa 75 augmentedLed = !augmentedLed;
Wolfgang Betz 40:17cd01e5f1fa 76
Wolfgang Betz 40:17cd01e5f1fa 77 i = ++pos;
Wolfgang Betz 40:17cd01e5f1fa 78 pos = s.find(':', pos);
Wolfgang Betz 40:17cd01e5f1fa 79
Wolfgang Betz 40:17cd01e5f1fa 80 if (pos == string::npos) {
Wolfgang Betz 40:17cd01e5f1fa 81 wait_ms(atoi(s.substr(i, s.length()).c_str()));
Wolfgang Betz 40:17cd01e5f1fa 82 augmentedLed = !augmentedLed;
Wolfgang Betz 40:17cd01e5f1fa 83 }
Wolfgang Betz 17:ada41201c642 84 }
MarceloSalazar 0:e13a8a944e25 85 }
MarceloSalazar 0:e13a8a944e25 86
Wolfgang Betz 40:17cd01e5f1fa 87 /**
Wolfgang Betz 40:17cd01e5f1fa 88 * Notification callback handler
Wolfgang Betz 40:17cd01e5f1fa 89 * @param resource The resource that triggered the callback
Wolfgang Betz 40:17cd01e5f1fa 90 * @param status The delivery status of the notification
Wolfgang Betz 40:17cd01e5f1fa 91 */
Wolfgang Betz 40:17cd01e5f1fa 92 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
Wolfgang Betz 40:17cd01e5f1fa 93 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
Wolfgang Betz 17:ada41201c642 94 }
MarceloSalazar 0:e13a8a944e25 95
Wolfgang Betz 40:17cd01e5f1fa 96 /**
Wolfgang Betz 40:17cd01e5f1fa 97 * Registration callback handler
Wolfgang Betz 40:17cd01e5f1fa 98 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
Wolfgang Betz 40:17cd01e5f1fa 99 */
Wolfgang Betz 40:17cd01e5f1fa 100 void registered(const ConnectorClientEndpointInfo *endpoint) {
Wolfgang Betz 40:17cd01e5f1fa 101 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
Wolfgang Betz 40:17cd01e5f1fa 102 }
MarceloSalazar 1:1ccf36276cd3 103
Wolfgang Betz 40:17cd01e5f1fa 104 int main(void) {
Wolfgang Betz 40:17cd01e5f1fa 105 printf("Starting Simple Mbed Cloud Client example\n");
Wolfgang Betz 42:cddcdf68386d 106 printf("Connecting to the network using WiFi...\n");
Wolfgang Betz 17:ada41201c642 107
Wolfgang Betz 40:17cd01e5f1fa 108 // Connect to the internet (DHCP is expected to be on)
Wolfgang Betz 42:cddcdf68386d 109 static SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
Wolfgang Betz 42:cddcdf68386d 110 nsapi_error_t status = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
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(&registered);
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 }