Updated version to work with K64 and FW Update

Fork of mbed-cloud-connect-example-ethernet by NXP

Committer:
MarceloSalazar
Date:
Thu Sep 06 14:22:28 2018 +0100
Revision:
22:ca861d3f7a6c
Parent:
11:ae1f6fe932dc
Restore update_default_resources.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 11:ae1f6fe932dc 1 // ----------------------------------------------------------------------------
maclobdell 11:ae1f6fe932dc 2 // Copyright 2016-2018 ARM Ltd.
maclobdell 11:ae1f6fe932dc 3 //
maclobdell 11:ae1f6fe932dc 4 // SPDX-License-Identifier: Apache-2.0
maclobdell 11:ae1f6fe932dc 5 //
maclobdell 11:ae1f6fe932dc 6 // Licensed under the Apache License, Version 2.0 (the "License");
maclobdell 11:ae1f6fe932dc 7 // you may not use this file except in compliance with the License.
maclobdell 11:ae1f6fe932dc 8 // You may obtain a copy of the License at
maclobdell 11:ae1f6fe932dc 9 //
maclobdell 11:ae1f6fe932dc 10 // http://www.apache.org/licenses/LICENSE-2.0
maclobdell 11:ae1f6fe932dc 11 //
maclobdell 11:ae1f6fe932dc 12 // Unless required by applicable law or agreed to in writing, software
maclobdell 11:ae1f6fe932dc 13 // distributed under the License is distributed on an "AS IS" BASIS,
maclobdell 11:ae1f6fe932dc 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maclobdell 11:ae1f6fe932dc 15 // See the License for the specific language governing permissions and
maclobdell 11:ae1f6fe932dc 16 // limitations under the License.
maclobdell 11:ae1f6fe932dc 17 // ----------------------------------------------------------------------------
maclobdell 11:ae1f6fe932dc 18
maclobdell 11:ae1f6fe932dc 19 #include "mbed.h"
maclobdell 11:ae1f6fe932dc 20 #include "simple-mbed-cloud-client.h"
maclobdell 11:ae1f6fe932dc 21 #include "SDBlockDevice.h"
maclobdell 11:ae1f6fe932dc 22 #include "FATFileSystem.h"
maclobdell 11:ae1f6fe932dc 23 #include "EthernetInterface.h"
maclobdell 11:ae1f6fe932dc 24
maclobdell 11:ae1f6fe932dc 25 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
maclobdell 11:ae1f6fe932dc 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
maclobdell 11:ae1f6fe932dc 27 EventQueue eventQueue;
maclobdell 11:ae1f6fe932dc 28 Thread thread1;
maclobdell 11:ae1f6fe932dc 29
maclobdell 11:ae1f6fe932dc 30 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
maclobdell 11:ae1f6fe932dc 31 /* K64 & K66 */
maclobdell 11:ae1f6fe932dc 32 InterruptIn sw2(SW2);
maclobdell 11:ae1f6fe932dc 33 DigitalOut led2(LED2);
maclobdell 11:ae1f6fe932dc 34 /* K64 & K66 */
maclobdell 11:ae1f6fe932dc 35 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
maclobdell 11:ae1f6fe932dc 36 FATFileSystem fs("sd", &sd);
maclobdell 11:ae1f6fe932dc 37
maclobdell 11:ae1f6fe932dc 38 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
maclobdell 11:ae1f6fe932dc 39 MbedCloudClientResource *button_res;
maclobdell 11:ae1f6fe932dc 40 MbedCloudClientResource *pattern_res;
maclobdell 11:ae1f6fe932dc 41
maclobdell 11:ae1f6fe932dc 42 static bool button_pressed = false;
maclobdell 11:ae1f6fe932dc 43 static int button_count = 0;
maclobdell 11:ae1f6fe932dc 44
maclobdell 11:ae1f6fe932dc 45 void button_press() {
maclobdell 11:ae1f6fe932dc 46 button_pressed = true;
maclobdell 11:ae1f6fe932dc 47 ++button_count;
maclobdell 11:ae1f6fe932dc 48 button_res->set_value(button_count);
maclobdell 11:ae1f6fe932dc 49 }
maclobdell 11:ae1f6fe932dc 50
maclobdell 11:ae1f6fe932dc 51 /**
maclobdell 11:ae1f6fe932dc 52 * PUT handler
maclobdell 11:ae1f6fe932dc 53 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 54 * @param newValue Updated value for the resource
maclobdell 11:ae1f6fe932dc 55 */
maclobdell 11:ae1f6fe932dc 56 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
maclobdell 11:ae1f6fe932dc 57 printf("PUT received, new value: %s\n", newValue.c_str());
maclobdell 11:ae1f6fe932dc 58 }
maclobdell 11:ae1f6fe932dc 59
maclobdell 11:ae1f6fe932dc 60 /**
maclobdell 11:ae1f6fe932dc 61 * POST handler
maclobdell 11:ae1f6fe932dc 62 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 63 * @param buffer If a body was passed to the POST function, this contains the data.
maclobdell 11:ae1f6fe932dc 64 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
maclobdell 11:ae1f6fe932dc 65 * @param size Size of the body
maclobdell 11:ae1f6fe932dc 66 */
maclobdell 11:ae1f6fe932dc 67 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
maclobdell 11:ae1f6fe932dc 68 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
maclobdell 11:ae1f6fe932dc 69
maclobdell 11:ae1f6fe932dc 70 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
maclobdell 11:ae1f6fe932dc 71
maclobdell 11:ae1f6fe932dc 72 // Parse the pattern string, and toggle the LED in that pattern
maclobdell 11:ae1f6fe932dc 73 string s = std::string(pattern_res->get_value().c_str());
maclobdell 11:ae1f6fe932dc 74 size_t i = 0;
maclobdell 11:ae1f6fe932dc 75 size_t pos = s.find(':');
maclobdell 11:ae1f6fe932dc 76 while (pos != string::npos) {
maclobdell 11:ae1f6fe932dc 77 wait_ms(atoi(s.substr(i, pos - i).c_str()));
maclobdell 11:ae1f6fe932dc 78 augmentedLed = !augmentedLed;
maclobdell 11:ae1f6fe932dc 79
maclobdell 11:ae1f6fe932dc 80 i = ++pos;
maclobdell 11:ae1f6fe932dc 81 pos = s.find(':', pos);
maclobdell 11:ae1f6fe932dc 82
maclobdell 11:ae1f6fe932dc 83 if (pos == string::npos) {
maclobdell 11:ae1f6fe932dc 84 wait_ms(atoi(s.substr(i, s.length()).c_str()));
maclobdell 11:ae1f6fe932dc 85 augmentedLed = !augmentedLed;
maclobdell 11:ae1f6fe932dc 86 }
maclobdell 11:ae1f6fe932dc 87 }
maclobdell 11:ae1f6fe932dc 88 }
maclobdell 11:ae1f6fe932dc 89
maclobdell 11:ae1f6fe932dc 90 /**
maclobdell 11:ae1f6fe932dc 91 * Notification callback handler
maclobdell 11:ae1f6fe932dc 92 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 93 * @param status The delivery status of the notification
maclobdell 11:ae1f6fe932dc 94 */
maclobdell 11:ae1f6fe932dc 95 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
maclobdell 11:ae1f6fe932dc 96 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
maclobdell 11:ae1f6fe932dc 97 }
maclobdell 11:ae1f6fe932dc 98
maclobdell 11:ae1f6fe932dc 99 /**
maclobdell 11:ae1f6fe932dc 100 * Registration callback handler
maclobdell 11:ae1f6fe932dc 101 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
maclobdell 11:ae1f6fe932dc 102 */
maclobdell 11:ae1f6fe932dc 103 void registered(const ConnectorClientEndpointInfo *endpoint) {
maclobdell 11:ae1f6fe932dc 104 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
maclobdell 11:ae1f6fe932dc 105 }
maclobdell 11:ae1f6fe932dc 106
maclobdell 11:ae1f6fe932dc 107 int main(void) {
maclobdell 11:ae1f6fe932dc 108 printf("Starting Simple Mbed Cloud Client example\n");
maclobdell 11:ae1f6fe932dc 109 printf("Connecting to the network using Ethernet...\n");
maclobdell 11:ae1f6fe932dc 110
maclobdell 11:ae1f6fe932dc 111 // Connect to the internet (DHCP is expected to be on)
maclobdell 11:ae1f6fe932dc 112 EthernetInterface net;
maclobdell 11:ae1f6fe932dc 113 nsapi_error_t status = net.connect();
maclobdell 11:ae1f6fe932dc 114
maclobdell 11:ae1f6fe932dc 115 if (status != 0) {
maclobdell 11:ae1f6fe932dc 116 printf("Connecting to the network failed %d!\n", status);
maclobdell 11:ae1f6fe932dc 117 return -1;
maclobdell 11:ae1f6fe932dc 118 }
maclobdell 11:ae1f6fe932dc 119
maclobdell 11:ae1f6fe932dc 120 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
maclobdell 11:ae1f6fe932dc 121
maclobdell 11:ae1f6fe932dc 122 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
maclobdell 11:ae1f6fe932dc 123 SimpleMbedCloudClient client(&net, &sd, &fs);
maclobdell 11:ae1f6fe932dc 124 int client_status = client.init();
maclobdell 11:ae1f6fe932dc 125 if (client_status != 0) {
maclobdell 11:ae1f6fe932dc 126 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
maclobdell 11:ae1f6fe932dc 127 return -1;
maclobdell 11:ae1f6fe932dc 128 }
maclobdell 11:ae1f6fe932dc 129
maclobdell 11:ae1f6fe932dc 130 // Creating resources, which can be written or read from the cloud
maclobdell 11:ae1f6fe932dc 131 button_res = client.create_resource("3200/0/5501", "button_count");
maclobdell 11:ae1f6fe932dc 132 button_res->set_value(0);
maclobdell 11:ae1f6fe932dc 133 button_res->methods(M2MMethod::GET);
maclobdell 11:ae1f6fe932dc 134 button_res->observable(true);
maclobdell 11:ae1f6fe932dc 135 button_res->attach_notification_callback(button_callback);
maclobdell 11:ae1f6fe932dc 136
maclobdell 11:ae1f6fe932dc 137 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
maclobdell 11:ae1f6fe932dc 138 pattern_res->set_value("500:500:500:500:500:500:500:500");
maclobdell 11:ae1f6fe932dc 139 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
maclobdell 11:ae1f6fe932dc 140 pattern_res->attach_put_callback(pattern_updated);
maclobdell 11:ae1f6fe932dc 141
maclobdell 11:ae1f6fe932dc 142 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
maclobdell 11:ae1f6fe932dc 143 blink_res->methods(M2MMethod::POST);
maclobdell 11:ae1f6fe932dc 144 blink_res->attach_post_callback(blink_callback);
maclobdell 11:ae1f6fe932dc 145
maclobdell 11:ae1f6fe932dc 146 printf("Initialized Mbed Cloud Client. Registering...\n");
maclobdell 11:ae1f6fe932dc 147
maclobdell 11:ae1f6fe932dc 148 // Callback that fires when registering is complete
maclobdell 11:ae1f6fe932dc 149 client.on_registered(&registered);
maclobdell 11:ae1f6fe932dc 150
maclobdell 11:ae1f6fe932dc 151 // Register with Mbed Cloud
maclobdell 11:ae1f6fe932dc 152 client.register_and_connect();
maclobdell 11:ae1f6fe932dc 153
maclobdell 11:ae1f6fe932dc 154 // Setup the button
maclobdell 11:ae1f6fe932dc 155 sw2.mode(PullUp);
maclobdell 11:ae1f6fe932dc 156
maclobdell 11:ae1f6fe932dc 157 // The button fall handler is placed in the event queue so it will run in
maclobdell 11:ae1f6fe932dc 158 // thread context instead of ISR context, which allows safely updating the cloud resource
maclobdell 11:ae1f6fe932dc 159 sw2.fall(eventQueue.event(&button_press));
maclobdell 11:ae1f6fe932dc 160 button_count = 0;
maclobdell 11:ae1f6fe932dc 161
maclobdell 11:ae1f6fe932dc 162 // Start the event queue in a separate thread so the main thread continues
maclobdell 11:ae1f6fe932dc 163 thread1.start(callback(&eventQueue, &EventQueue::dispatch_forever));
maclobdell 11:ae1f6fe932dc 164
maclobdell 11:ae1f6fe932dc 165 while(1)
maclobdell 11:ae1f6fe932dc 166 {
maclobdell 11:ae1f6fe932dc 167 wait_ms(100);
maclobdell 11:ae1f6fe932dc 168
maclobdell 11:ae1f6fe932dc 169 if (button_pressed) {
maclobdell 11:ae1f6fe932dc 170 button_pressed = false;
maclobdell 11:ae1f6fe932dc 171 printf("button clicked %d times\r\n", button_count);
maclobdell 11:ae1f6fe932dc 172 }
maclobdell 11:ae1f6fe932dc 173
maclobdell 11:ae1f6fe932dc 174 }
maclobdell 11:ae1f6fe932dc 175 }