Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
adustm
Date:
Fri Jul 06 11:44:19 2018 +0200
Revision:
4:cf7342047b4d
Parent:
1:e86b1cffc402
Child:
6:e0e1e1b93099
Update for public cloud client version

Who changed what in which revision?

UserRevisionLine numberNew 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 // ----------------------------------------------------------------------------
adustm 1:e86b1cffc402 18
adustm 1:e86b1cffc402 19 #include "mbed.h"
adustm 1:e86b1cffc402 20 #include "simple-mbed-cloud-client.h"
adustm 1:e86b1cffc402 21 #include "SDBlockDevice.h"
adustm 1:e86b1cffc402 22 #include "FATFileSystem.h"
adustm 1:e86b1cffc402 23 #include "ISM43362Interface.h"
adustm 1:e86b1cffc402 24
adustm 4:cf7342047b4d 25 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
adustm 4:cf7342047b4d 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
adustm 4:cf7342047b4d 27 EventQueue eventQueue;
adustm 1:e86b1cffc402 28
adustm 4:cf7342047b4d 29 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
adustm 1:e86b1cffc402 30 SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
adustm 4:cf7342047b4d 31 FATFileSystem fs("sd", &sd);
adustm 4:cf7342047b4d 32
adustm 4:cf7342047b4d 33 // Declaring net interface as a global variable instead of local to avoid stack overflow
adustm 4:cf7342047b4d 34 ISM43362Interface net;
adustm 1:e86b1cffc402 35
adustm 4:cf7342047b4d 36 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
adustm 4:cf7342047b4d 37 MbedCloudClientResource *button_res;
adustm 4:cf7342047b4d 38 MbedCloudClientResource *pattern_res;
adustm 1:e86b1cffc402 39
adustm 4:cf7342047b4d 40 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
adustm 4:cf7342047b4d 41 void fake_button_press() {
adustm 4:cf7342047b4d 42 int v = button_res->get_value_int() + 1;
adustm 1:e86b1cffc402 43
adustm 4:cf7342047b4d 44 button_res->set_value(v);
adustm 1:e86b1cffc402 45
adustm 4:cf7342047b4d 46 printf("Simulated button clicked %d times\n", v);
adustm 1:e86b1cffc402 47 }
adustm 1:e86b1cffc402 48
adustm 4:cf7342047b4d 49 /**
adustm 4:cf7342047b4d 50 * PUT handler
adustm 4:cf7342047b4d 51 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 52 * @param newValue Updated value for the resource
adustm 4:cf7342047b4d 53 */
adustm 4:cf7342047b4d 54 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
adustm 4:cf7342047b4d 55 printf("PUT received, new value: %s\n", newValue.c_str());
adustm 1:e86b1cffc402 56 }
adustm 1:e86b1cffc402 57
adustm 4:cf7342047b4d 58 /**
adustm 4:cf7342047b4d 59 * POST handler
adustm 4:cf7342047b4d 60 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 61 * @param buffer If a body was passed to the POST function, this contains the data.
adustm 4:cf7342047b4d 62 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
adustm 4:cf7342047b4d 63 * @param size Size of the body
adustm 4:cf7342047b4d 64 */
adustm 4:cf7342047b4d 65 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
adustm 4:cf7342047b4d 66 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
adustm 4:cf7342047b4d 67
adustm 4:cf7342047b4d 68 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
adustm 1:e86b1cffc402 69
adustm 4:cf7342047b4d 70 // Parse the pattern string, and toggle the LED in that pattern
adustm 4:cf7342047b4d 71 string s = std::string(pattern_res->get_value().c_str());
adustm 4:cf7342047b4d 72 size_t i = 0;
adustm 4:cf7342047b4d 73 size_t pos = s.find(':');
adustm 4:cf7342047b4d 74 while (pos != string::npos) {
adustm 4:cf7342047b4d 75 wait_ms(atoi(s.substr(i, pos - i).c_str()));
adustm 4:cf7342047b4d 76 augmentedLed = !augmentedLed;
adustm 4:cf7342047b4d 77
adustm 4:cf7342047b4d 78 i = ++pos;
adustm 4:cf7342047b4d 79 pos = s.find(':', pos);
adustm 4:cf7342047b4d 80
adustm 4:cf7342047b4d 81 if (pos == string::npos) {
adustm 4:cf7342047b4d 82 wait_ms(atoi(s.substr(i, s.length()).c_str()));
adustm 4:cf7342047b4d 83 augmentedLed = !augmentedLed;
adustm 4:cf7342047b4d 84 }
adustm 4:cf7342047b4d 85 }
adustm 1:e86b1cffc402 86 }
adustm 1:e86b1cffc402 87
adustm 4:cf7342047b4d 88 /**
adustm 4:cf7342047b4d 89 * Notification callback handler
adustm 4:cf7342047b4d 90 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 91 * @param status The delivery status of the notification
adustm 4:cf7342047b4d 92 */
adustm 4:cf7342047b4d 93 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
adustm 4:cf7342047b4d 94 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
adustm 4:cf7342047b4d 95 }
adustm 1:e86b1cffc402 96
adustm 4:cf7342047b4d 97 /**
adustm 4:cf7342047b4d 98 * Registration callback handler
adustm 4:cf7342047b4d 99 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
adustm 4:cf7342047b4d 100 */
adustm 4:cf7342047b4d 101 void registered(const ConnectorClientEndpointInfo *endpoint) {
adustm 4:cf7342047b4d 102 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
adustm 4:cf7342047b4d 103 }
adustm 1:e86b1cffc402 104
adustm 4:cf7342047b4d 105 int main(void) {
adustm 4:cf7342047b4d 106 printf("Starting Simple Mbed Cloud Client example\n");
adustm 4:cf7342047b4d 107 printf("Connecting to the network using Wifi...\n");
adustm 4:cf7342047b4d 108
adustm 4:cf7342047b4d 109 // Connect to the internet (DHCP is expected to be on)
adustm 4:cf7342047b4d 110 nsapi_error_t status = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, (strlen(MBED_CONF_APP_WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE);
adustm 4:cf7342047b4d 111
adustm 4:cf7342047b4d 112 if (status != 0) {
adustm 4:cf7342047b4d 113 printf("Connecting to the network failed %d!\n", status);
adustm 1:e86b1cffc402 114 return -1;
adustm 1:e86b1cffc402 115 }
adustm 1:e86b1cffc402 116
adustm 4:cf7342047b4d 117 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
adustm 1:e86b1cffc402 118
adustm 4:cf7342047b4d 119 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
adustm 4:cf7342047b4d 120 SimpleMbedCloudClient client(&net, &sd, &fs);
adustm 4:cf7342047b4d 121 int client_status = client.init();
adustm 4:cf7342047b4d 122 if (client_status != 0) {
adustm 4:cf7342047b4d 123 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
adustm 1:e86b1cffc402 124 return -1;
adustm 1:e86b1cffc402 125 }
adustm 1:e86b1cffc402 126
adustm 4:cf7342047b4d 127 // Creating resources, which can be written or read from the cloud
adustm 4:cf7342047b4d 128 button_res = client.create_resource("3200/0/5501", "button_count");
adustm 4:cf7342047b4d 129 button_res->set_value(0);
adustm 4:cf7342047b4d 130 button_res->methods(M2MMethod::GET);
adustm 4:cf7342047b4d 131 button_res->observable(true);
adustm 4:cf7342047b4d 132 button_res->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 133
adustm 4:cf7342047b4d 134 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
adustm 4:cf7342047b4d 135 pattern_res->set_value("500:500:500:500:500:500:500:500");
adustm 4:cf7342047b4d 136 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
adustm 4:cf7342047b4d 137 pattern_res->attach_put_callback(pattern_updated);
adustm 1:e86b1cffc402 138
adustm 4:cf7342047b4d 139 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
adustm 4:cf7342047b4d 140 blink_res->methods(M2MMethod::POST);
adustm 4:cf7342047b4d 141 blink_res->attach_post_callback(blink_callback);
adustm 4:cf7342047b4d 142
adustm 4:cf7342047b4d 143 printf("Initialized Mbed Cloud Client. Registering...\n");
adustm 1:e86b1cffc402 144
adustm 4:cf7342047b4d 145 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 146 client.on_registered(&registered);
adustm 1:e86b1cffc402 147
adustm 4:cf7342047b4d 148 // Register with Mbed Cloud
adustm 4:cf7342047b4d 149 client.register_and_connect();
adustm 1:e86b1cffc402 150
adustm 1:e86b1cffc402 151 // Placeholder for callback to update local resource when GET comes.
adustm 4:cf7342047b4d 152 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
adustm 4:cf7342047b4d 153 Ticker timer;
adustm 4:cf7342047b4d 154 timer.attach(eventQueue.event(&fake_button_press), 5.0);
adustm 1:e86b1cffc402 155
adustm 4:cf7342047b4d 156 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 157 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 158 }