Simple Mbed Cloud client application using features of K64 & K66 including Ethernet and SD Card
Fork of mbed-cloud-example_K64_K66 by
DEPRECATED
This example application is not maintained and not recommended. It uses an old version of Mbed OS, Pelion DM, and Arm toolchain. It doesn't work with Mbed Studio.
Please use: https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-pelion/
main.cpp@18:49062a0d117e, 2018-10-05 (annotated)
- Committer:
- maclobdell
- Date:
- Fri Oct 05 13:11:30 2018 -0500
- Revision:
- 18:49062a0d117e
- Parent:
- 11:ae1f6fe932dc
Update to template version 1.4.1
Who changed what in which revision?
User | Revision | Line number | New 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 "FATFileSystem.h" |
maclobdell | 11:ae1f6fe932dc | 22 | |
maclobdell | 11:ae1f6fe932dc | 23 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
maclobdell | 11:ae1f6fe932dc | 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 |
maclobdell | 11:ae1f6fe932dc | 25 | EventQueue eventQueue; |
maclobdell | 11:ae1f6fe932dc | 26 | Thread thread1; |
maclobdell | 11:ae1f6fe932dc | 27 | |
maclobdell | 18:49062a0d117e | 28 | // Default block device |
maclobdell | 18:49062a0d117e | 29 | BlockDevice* bd = BlockDevice::get_default_instance(); |
maclobdell | 18:49062a0d117e | 30 | FATFileSystem fs("sd", bd); |
maclobdell | 18:49062a0d117e | 31 | |
maclobdell | 18:49062a0d117e | 32 | // Default network interface object |
maclobdell | 18:49062a0d117e | 33 | NetworkInterface *net; |
maclobdell | 18:49062a0d117e | 34 | |
maclobdell | 11:ae1f6fe932dc | 35 | InterruptIn sw2(SW2); |
maclobdell | 11:ae1f6fe932dc | 36 | DigitalOut led2(LED2); |
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 | 18:49062a0d117e | 112 | net = NetworkInterface::get_default_instance(); |
maclobdell | 11:ae1f6fe932dc | 113 | |
maclobdell | 18:49062a0d117e | 114 | nsapi_error_t status = net->connect(); |
maclobdell | 18:49062a0d117e | 115 | |
maclobdell | 18:49062a0d117e | 116 | if (status != NSAPI_ERROR_OK) { |
maclobdell | 11:ae1f6fe932dc | 117 | printf("Connecting to the network failed %d!\n", status); |
maclobdell | 11:ae1f6fe932dc | 118 | return -1; |
maclobdell | 11:ae1f6fe932dc | 119 | } |
maclobdell | 11:ae1f6fe932dc | 120 | |
maclobdell | 18:49062a0d117e | 121 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
maclobdell | 11:ae1f6fe932dc | 122 | |
maclobdell | 11:ae1f6fe932dc | 123 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
maclobdell | 18:49062a0d117e | 124 | SimpleMbedCloudClient client(net, bd, &fs); |
maclobdell | 11:ae1f6fe932dc | 125 | int client_status = client.init(); |
maclobdell | 11:ae1f6fe932dc | 126 | if (client_status != 0) { |
maclobdell | 11:ae1f6fe932dc | 127 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
maclobdell | 11:ae1f6fe932dc | 128 | return -1; |
maclobdell | 11:ae1f6fe932dc | 129 | } |
maclobdell | 11:ae1f6fe932dc | 130 | |
maclobdell | 11:ae1f6fe932dc | 131 | // Creating resources, which can be written or read from the cloud |
maclobdell | 11:ae1f6fe932dc | 132 | button_res = client.create_resource("3200/0/5501", "button_count"); |
maclobdell | 11:ae1f6fe932dc | 133 | button_res->set_value(0); |
maclobdell | 11:ae1f6fe932dc | 134 | button_res->methods(M2MMethod::GET); |
maclobdell | 11:ae1f6fe932dc | 135 | button_res->observable(true); |
maclobdell | 11:ae1f6fe932dc | 136 | button_res->attach_notification_callback(button_callback); |
maclobdell | 11:ae1f6fe932dc | 137 | |
maclobdell | 11:ae1f6fe932dc | 138 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
maclobdell | 11:ae1f6fe932dc | 139 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
maclobdell | 11:ae1f6fe932dc | 140 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
maclobdell | 11:ae1f6fe932dc | 141 | pattern_res->attach_put_callback(pattern_updated); |
maclobdell | 11:ae1f6fe932dc | 142 | |
maclobdell | 11:ae1f6fe932dc | 143 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
maclobdell | 11:ae1f6fe932dc | 144 | blink_res->methods(M2MMethod::POST); |
maclobdell | 11:ae1f6fe932dc | 145 | blink_res->attach_post_callback(blink_callback); |
maclobdell | 11:ae1f6fe932dc | 146 | |
maclobdell | 11:ae1f6fe932dc | 147 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
maclobdell | 11:ae1f6fe932dc | 148 | |
maclobdell | 11:ae1f6fe932dc | 149 | // Callback that fires when registering is complete |
maclobdell | 11:ae1f6fe932dc | 150 | client.on_registered(®istered); |
maclobdell | 11:ae1f6fe932dc | 151 | |
maclobdell | 11:ae1f6fe932dc | 152 | // Register with Mbed Cloud |
maclobdell | 11:ae1f6fe932dc | 153 | client.register_and_connect(); |
maclobdell | 11:ae1f6fe932dc | 154 | |
maclobdell | 11:ae1f6fe932dc | 155 | // Setup the button |
maclobdell | 11:ae1f6fe932dc | 156 | sw2.mode(PullUp); |
maclobdell | 11:ae1f6fe932dc | 157 | |
maclobdell | 11:ae1f6fe932dc | 158 | // The button fall handler is placed in the event queue so it will run in |
maclobdell | 11:ae1f6fe932dc | 159 | // thread context instead of ISR context, which allows safely updating the cloud resource |
maclobdell | 11:ae1f6fe932dc | 160 | sw2.fall(eventQueue.event(&button_press)); |
maclobdell | 11:ae1f6fe932dc | 161 | button_count = 0; |
maclobdell | 11:ae1f6fe932dc | 162 | |
maclobdell | 11:ae1f6fe932dc | 163 | // Start the event queue in a separate thread so the main thread continues |
maclobdell | 11:ae1f6fe932dc | 164 | thread1.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
maclobdell | 11:ae1f6fe932dc | 165 | |
maclobdell | 11:ae1f6fe932dc | 166 | while(1) |
maclobdell | 11:ae1f6fe932dc | 167 | { |
maclobdell | 11:ae1f6fe932dc | 168 | wait_ms(100); |
maclobdell | 11:ae1f6fe932dc | 169 | |
maclobdell | 11:ae1f6fe932dc | 170 | if (button_pressed) { |
maclobdell | 11:ae1f6fe932dc | 171 | button_pressed = false; |
maclobdell | 11:ae1f6fe932dc | 172 | printf("button clicked %d times\r\n", button_count); |
maclobdell | 11:ae1f6fe932dc | 173 | } |
maclobdell | 11:ae1f6fe932dc | 174 | |
maclobdell | 11:ae1f6fe932dc | 175 | } |
maclobdell | 11:ae1f6fe932dc | 176 | } |