Specific Pelion Device Management connect example over WI-FI for DISCO_L475VG_IOT01 board

Fork of example-DISCO_L475_IOT-mbed-Cloud-connect by ST

Committer:
MACRUM
Date:
Tue Nov 06 07:16:56 2018 +0000
Revision:
11:7a66d5cc6a87
Parent:
10:0b85f5df5d99
Fix default blockdevice

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