Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
adustm
Date:
Tue Jul 10 14:52:15 2018 +0200
Revision:
6:e0e1e1b93099
Parent:
4:cf7342047b4d
Child:
9:265744785d33
Add DISCO_F413ZH

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