Toyomasa Watarai / Mbed OS Pelion-dm-example-DISCO_L475_IOT

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

Committer:
MACRUM
Date:
Mon Nov 05 13:57:40 2018 +0000
Revision:
10:0b85f5df5d99
Parent:
9:265744785d33
Child:
11:7a66d5cc6a87
Add QSPIF support

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"
adustm 1:e86b1cffc402 22
adustm 4:cf7342047b4d 23 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
adustm 4:cf7342047b4d 24 // 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 25 EventQueue eventQueue;
adustm 1:e86b1cffc402 26
MarceloSalazar 9:265744785d33 27 // Default network interface object
MarceloSalazar 9:265744785d33 28 NetworkInterface *net;
adustm 6:e0e1e1b93099 29
MarceloSalazar 9:265744785d33 30 // Default block device
MarceloSalazar 9:265744785d33 31 BlockDevice* bd = BlockDevice::get_default_instance();
MarceloSalazar 9:265744785d33 32 FATFileSystem fs("sd", bd);
adustm 4:cf7342047b4d 33
MarceloSalazar 9:265744785d33 34 // Declaring pointers for access to Pelion Client resources outside of main()
adustm 4:cf7342047b4d 35 MbedCloudClientResource *button_res;
adustm 4:cf7342047b4d 36 MbedCloudClientResource *pattern_res;
adustm 1:e86b1cffc402 37
MACRUM 10:0b85f5df5d99 38 DigitalIn button(USER_BUTTON);
MACRUM 10:0b85f5df5d99 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) {
MarceloSalazar 9:265744785d33 102 printf("Connected to Pelion Device Management. 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) {
MACRUM 10:0b85f5df5d99 106 button.mode(PullUp);
MACRUM 10:0b85f5df5d99 107 if (button.read() == 0) {
MACRUM 10:0b85f5df5d99 108 printf("Formatting QSPI Flash storage...\n\n");
MACRUM 10:0b85f5df5d99 109 fs.format(bd);
MACRUM 10:0b85f5df5d99 110 }
MACRUM 10:0b85f5df5d99 111
MarceloSalazar 9:265744785d33 112 printf("Starting Simple Pelion Device Management Client example\n");
adustm 4:cf7342047b4d 113 printf("Connecting to the network using Wifi...\n");
adustm 4:cf7342047b4d 114
adustm 4:cf7342047b4d 115 // Connect to the internet (DHCP is expected to be on)
MarceloSalazar 9:265744785d33 116 net = NetworkInterface::get_default_instance();
adustm 4:cf7342047b4d 117
MarceloSalazar 9:265744785d33 118 nsapi_error_t status = net->connect();
MarceloSalazar 9:265744785d33 119
MarceloSalazar 9:265744785d33 120 if (status != NSAPI_ERROR_OK) {
adustm 4:cf7342047b4d 121 printf("Connecting to the network failed %d!\n", status);
adustm 1:e86b1cffc402 122 return -1;
adustm 1:e86b1cffc402 123 }
adustm 1:e86b1cffc402 124
MarceloSalazar 9:265744785d33 125 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 126
MarceloSalazar 9:265744785d33 127 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 128 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 129 int client_status = client.init();
adustm 4:cf7342047b4d 130 if (client_status != 0) {
MarceloSalazar 9:265744785d33 131 printf("Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 132 return -1;
adustm 1:e86b1cffc402 133 }
adustm 1:e86b1cffc402 134
adustm 4:cf7342047b4d 135 // Creating resources, which can be written or read from the cloud
adustm 4:cf7342047b4d 136 button_res = client.create_resource("3200/0/5501", "button_count");
adustm 4:cf7342047b4d 137 button_res->set_value(0);
adustm 4:cf7342047b4d 138 button_res->methods(M2MMethod::GET);
adustm 4:cf7342047b4d 139 button_res->observable(true);
adustm 4:cf7342047b4d 140 button_res->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 141
adustm 4:cf7342047b4d 142 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
adustm 4:cf7342047b4d 143 pattern_res->set_value("500:500:500:500:500:500:500:500");
adustm 4:cf7342047b4d 144 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
adustm 4:cf7342047b4d 145 pattern_res->attach_put_callback(pattern_updated);
adustm 1:e86b1cffc402 146
adustm 4:cf7342047b4d 147 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
adustm 4:cf7342047b4d 148 blink_res->methods(M2MMethod::POST);
adustm 4:cf7342047b4d 149 blink_res->attach_post_callback(blink_callback);
adustm 4:cf7342047b4d 150
MarceloSalazar 9:265744785d33 151 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 152
adustm 4:cf7342047b4d 153 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 154 client.on_registered(&registered);
adustm 1:e86b1cffc402 155
MarceloSalazar 9:265744785d33 156 // Register with Pelion DM
adustm 4:cf7342047b4d 157 client.register_and_connect();
adustm 1:e86b1cffc402 158
adustm 1:e86b1cffc402 159 // Placeholder for callback to update local resource when GET comes.
adustm 4:cf7342047b4d 160 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
adustm 4:cf7342047b4d 161 Ticker timer;
adustm 4:cf7342047b4d 162 timer.attach(eventQueue.event(&fake_button_press), 5.0);
adustm 1:e86b1cffc402 163
adustm 4:cf7342047b4d 164 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 165 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 166 }
MarceloSalazar 9:265744785d33 167 #endif