Jim Carver
/
mbed-cloud-workshop-connect
Fork for workshops
main.cpp@0:6b753f761943, 2018-10-12 (annotated)
- Committer:
- JimCarver
- Date:
- Fri Oct 12 21:22:49 2018 +0000
- Revision:
- 0:6b753f761943
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 0:6b753f761943 | 1 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 2 | // Copyright 2016-2018 ARM Ltd. |
JimCarver | 0:6b753f761943 | 3 | // |
JimCarver | 0:6b753f761943 | 4 | // SPDX-License-Identifier: Apache-2.0 |
JimCarver | 0:6b753f761943 | 5 | // |
JimCarver | 0:6b753f761943 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
JimCarver | 0:6b753f761943 | 7 | // you may not use this file except in compliance with the License. |
JimCarver | 0:6b753f761943 | 8 | // You may obtain a copy of the License at |
JimCarver | 0:6b753f761943 | 9 | // |
JimCarver | 0:6b753f761943 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
JimCarver | 0:6b753f761943 | 11 | // |
JimCarver | 0:6b753f761943 | 12 | // Unless required by applicable law or agreed to in writing, software |
JimCarver | 0:6b753f761943 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
JimCarver | 0:6b753f761943 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JimCarver | 0:6b753f761943 | 15 | // See the License for the specific language governing permissions and |
JimCarver | 0:6b753f761943 | 16 | // limitations under the License. |
JimCarver | 0:6b753f761943 | 17 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 18 | |
JimCarver | 0:6b753f761943 | 19 | #include "mbed.h" |
JimCarver | 0:6b753f761943 | 20 | #include <stdio.h> |
JimCarver | 0:6b753f761943 | 21 | #include <errno.h> |
JimCarver | 0:6b753f761943 | 22 | |
JimCarver | 0:6b753f761943 | 23 | #include "qspi_api.h" |
JimCarver | 0:6b753f761943 | 24 | #include "QSPI.h" |
JimCarver | 0:6b753f761943 | 25 | #include "qspi-blockdevice/QSPIFBlockDevice.h" |
JimCarver | 0:6b753f761943 | 26 | #define DEVICE_QSPI |
JimCarver | 0:6b753f761943 | 27 | |
JimCarver | 0:6b753f761943 | 28 | #include "FATFileSystem.h" |
JimCarver | 0:6b753f761943 | 29 | #include "LittleFileSystem.h" |
JimCarver | 0:6b753f761943 | 30 | #include "simple-mbed-cloud-client.h" |
JimCarver | 0:6b753f761943 | 31 | // #include "SDBlockDevice.h" |
JimCarver | 0:6b753f761943 | 32 | #include "ISM43362Interface.h" |
JimCarver | 0:6b753f761943 | 33 | // #include "SPIFBlockDevice.h" // to use onboard spi flash |
JimCarver | 0:6b753f761943 | 34 | |
JimCarver | 0:6b753f761943 | 35 | #define WIFI_SSID "mbed" |
JimCarver | 0:6b753f761943 | 36 | #define WIFI_PASSWORD "workshop-password" |
JimCarver | 0:6b753f761943 | 37 | |
JimCarver | 0:6b753f761943 | 38 | |
JimCarver | 0:6b753f761943 | 39 | |
JimCarver | 0:6b753f761943 | 40 | |
JimCarver | 0:6b753f761943 | 41 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
JimCarver | 0:6b753f761943 | 42 | // 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 |
JimCarver | 0:6b753f761943 | 43 | EventQueue eventQueue; |
JimCarver | 0:6b753f761943 | 44 | |
JimCarver | 0:6b753f761943 | 45 | // Declaring net interface as a global variable instead of local to avoid stack overflow |
JimCarver | 0:6b753f761943 | 46 | ISM43362Interface net; |
JimCarver | 0:6b753f761943 | 47 | |
JimCarver | 0:6b753f761943 | 48 | // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available) |
JimCarver | 0:6b753f761943 | 49 | // SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); |
JimCarver | 0:6b753f761943 | 50 | QSPIFBlockDevice sd(PE_12, PE_13, PE_14, PE_15,PE_10,PE_11,0,8000000); |
JimCarver | 0:6b753f761943 | 51 | |
JimCarver | 0:6b753f761943 | 52 | // FATFileSystem fs("sd", &bd); |
JimCarver | 0:6b753f761943 | 53 | |
JimCarver | 0:6b753f761943 | 54 | LittleFileSystem fs("sd"); |
JimCarver | 0:6b753f761943 | 55 | |
JimCarver | 0:6b753f761943 | 56 | // Declaring pointers for access to Mbed Cloud Client resources outside of main() |
JimCarver | 0:6b753f761943 | 57 | MbedCloudClientResource *button_res; |
JimCarver | 0:6b753f761943 | 58 | MbedCloudClientResource *pattern_res; |
JimCarver | 0:6b753f761943 | 59 | |
JimCarver | 0:6b753f761943 | 60 | // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button |
JimCarver | 0:6b753f761943 | 61 | void fake_button_press() { |
JimCarver | 0:6b753f761943 | 62 | int v = button_res->get_value_int() + 1; |
JimCarver | 0:6b753f761943 | 63 | |
JimCarver | 0:6b753f761943 | 64 | button_res->set_value(v); |
JimCarver | 0:6b753f761943 | 65 | |
JimCarver | 0:6b753f761943 | 66 | printf("Simulated button clicked %d times\n", v); |
JimCarver | 0:6b753f761943 | 67 | } |
JimCarver | 0:6b753f761943 | 68 | |
JimCarver | 0:6b753f761943 | 69 | /** |
JimCarver | 0:6b753f761943 | 70 | * PUT handler |
JimCarver | 0:6b753f761943 | 71 | * @param resource The resource that triggered the callback |
JimCarver | 0:6b753f761943 | 72 | * @param newValue Updated value for the resource |
JimCarver | 0:6b753f761943 | 73 | */ |
JimCarver | 0:6b753f761943 | 74 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
JimCarver | 0:6b753f761943 | 75 | printf("PUT received, new value: %s\n", newValue.c_str()); |
JimCarver | 0:6b753f761943 | 76 | } |
JimCarver | 0:6b753f761943 | 77 | |
JimCarver | 0:6b753f761943 | 78 | /** |
JimCarver | 0:6b753f761943 | 79 | * POST handler |
JimCarver | 0:6b753f761943 | 80 | * @param resource The resource that triggered the callback |
JimCarver | 0:6b753f761943 | 81 | * @param buffer If a body was passed to the POST function, this contains the data. |
JimCarver | 0:6b753f761943 | 82 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
JimCarver | 0:6b753f761943 | 83 | * @param size Size of the body |
JimCarver | 0:6b753f761943 | 84 | */ |
JimCarver | 0:6b753f761943 | 85 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
JimCarver | 0:6b753f761943 | 86 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
JimCarver | 0:6b753f761943 | 87 | |
JimCarver | 0:6b753f761943 | 88 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
JimCarver | 0:6b753f761943 | 89 | |
JimCarver | 0:6b753f761943 | 90 | // Parse the pattern string, and toggle the LED in that pattern |
JimCarver | 0:6b753f761943 | 91 | string s = std::string(pattern_res->get_value().c_str()); |
JimCarver | 0:6b753f761943 | 92 | size_t i = 0; |
JimCarver | 0:6b753f761943 | 93 | size_t pos = s.find(':'); |
JimCarver | 0:6b753f761943 | 94 | while (pos != string::npos) { |
JimCarver | 0:6b753f761943 | 95 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
JimCarver | 0:6b753f761943 | 96 | augmentedLed = !augmentedLed; |
JimCarver | 0:6b753f761943 | 97 | |
JimCarver | 0:6b753f761943 | 98 | i = ++pos; |
JimCarver | 0:6b753f761943 | 99 | pos = s.find(':', pos); |
JimCarver | 0:6b753f761943 | 100 | |
JimCarver | 0:6b753f761943 | 101 | if (pos == string::npos) { |
JimCarver | 0:6b753f761943 | 102 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
JimCarver | 0:6b753f761943 | 103 | augmentedLed = !augmentedLed; |
JimCarver | 0:6b753f761943 | 104 | } |
JimCarver | 0:6b753f761943 | 105 | } |
JimCarver | 0:6b753f761943 | 106 | } |
JimCarver | 0:6b753f761943 | 107 | |
JimCarver | 0:6b753f761943 | 108 | /** |
JimCarver | 0:6b753f761943 | 109 | * Notification callback handler |
JimCarver | 0:6b753f761943 | 110 | * @param resource The resource that triggered the callback |
JimCarver | 0:6b753f761943 | 111 | * @param status The delivery status of the notification |
JimCarver | 0:6b753f761943 | 112 | */ |
JimCarver | 0:6b753f761943 | 113 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
JimCarver | 0:6b753f761943 | 114 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
JimCarver | 0:6b753f761943 | 115 | } |
JimCarver | 0:6b753f761943 | 116 | |
JimCarver | 0:6b753f761943 | 117 | /** |
JimCarver | 0:6b753f761943 | 118 | * Registration callback handler |
JimCarver | 0:6b753f761943 | 119 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
JimCarver | 0:6b753f761943 | 120 | */ |
JimCarver | 0:6b753f761943 | 121 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
JimCarver | 0:6b753f761943 | 122 | printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
JimCarver | 0:6b753f761943 | 123 | } |
JimCarver | 0:6b753f761943 | 124 | |
JimCarver | 0:6b753f761943 | 125 | int main(void) { |
JimCarver | 0:6b753f761943 | 126 | printf("Starting Simple Mbed Cloud Client example\n"); |
JimCarver | 0:6b753f761943 | 127 | |
JimCarver | 0:6b753f761943 | 128 | printf("Checking SDCard is Formatted\r\n"); |
JimCarver | 0:6b753f761943 | 129 | int err = fs.mount(&sd); |
JimCarver | 0:6b753f761943 | 130 | printf("%s\n", (err ? "Fail :(" : "OK")); |
JimCarver | 0:6b753f761943 | 131 | if (err) { |
JimCarver | 0:6b753f761943 | 132 | // Reformat if we can't mount the filesystem |
JimCarver | 0:6b753f761943 | 133 | // this should only happen on the first boot |
JimCarver | 0:6b753f761943 | 134 | printf("No filesystem found, formatting... "); |
JimCarver | 0:6b753f761943 | 135 | fflush(stdout); |
JimCarver | 0:6b753f761943 | 136 | err = fs.reformat(&sd); |
JimCarver | 0:6b753f761943 | 137 | printf("%s\n", (err ? "Fail :(" : "OK")); |
JimCarver | 0:6b753f761943 | 138 | if (err) { |
JimCarver | 0:6b753f761943 | 139 | error("error: %s (%d)\n", strerror(-err), err); |
JimCarver | 0:6b753f761943 | 140 | } |
JimCarver | 0:6b753f761943 | 141 | } |
JimCarver | 0:6b753f761943 | 142 | // err = fs.unmount(); |
JimCarver | 0:6b753f761943 | 143 | // printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
JimCarver | 0:6b753f761943 | 144 | // if (err < 0) { |
JimCarver | 0:6b753f761943 | 145 | // error("error: %s (%d)\n", strerror(-err), err); |
JimCarver | 0:6b753f761943 | 146 | // } |
JimCarver | 0:6b753f761943 | 147 | |
JimCarver | 0:6b753f761943 | 148 | printf("Connecting to the network using Wifi...\n"); |
JimCarver | 0:6b753f761943 | 149 | |
JimCarver | 0:6b753f761943 | 150 | // Connect to the internet (DHCP is expected to be on) |
JimCarver | 0:6b753f761943 | 151 | nsapi_error_t status = net.connect(WIFI_SSID, WIFI_PASSWORD, (strlen(WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE); |
JimCarver | 0:6b753f761943 | 152 | |
JimCarver | 0:6b753f761943 | 153 | if (status != 0) { |
JimCarver | 0:6b753f761943 | 154 | printf("Connecting to the network failed %d!\n", status); |
JimCarver | 0:6b753f761943 | 155 | return -1; |
JimCarver | 0:6b753f761943 | 156 | } |
JimCarver | 0:6b753f761943 | 157 | |
JimCarver | 0:6b753f761943 | 158 | printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address()); |
JimCarver | 0:6b753f761943 | 159 | |
JimCarver | 0:6b753f761943 | 160 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
JimCarver | 0:6b753f761943 | 161 | SimpleMbedCloudClient client(&net, &sd, &fs); |
JimCarver | 0:6b753f761943 | 162 | int client_status = client.init(); |
JimCarver | 0:6b753f761943 | 163 | if (client_status != 0) { |
JimCarver | 0:6b753f761943 | 164 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
JimCarver | 0:6b753f761943 | 165 | return -1; |
JimCarver | 0:6b753f761943 | 166 | } |
JimCarver | 0:6b753f761943 | 167 | |
JimCarver | 0:6b753f761943 | 168 | // Creating resources, which can be written or read from the cloud |
JimCarver | 0:6b753f761943 | 169 | button_res = client.create_resource("3200/0/5501", "button_count"); |
JimCarver | 0:6b753f761943 | 170 | button_res->set_value(0); |
JimCarver | 0:6b753f761943 | 171 | button_res->methods(M2MMethod::GET); |
JimCarver | 0:6b753f761943 | 172 | button_res->observable(true); |
JimCarver | 0:6b753f761943 | 173 | button_res->attach_notification_callback(button_callback); |
JimCarver | 0:6b753f761943 | 174 | |
JimCarver | 0:6b753f761943 | 175 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
JimCarver | 0:6b753f761943 | 176 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
JimCarver | 0:6b753f761943 | 177 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
JimCarver | 0:6b753f761943 | 178 | pattern_res->attach_put_callback(pattern_updated); |
JimCarver | 0:6b753f761943 | 179 | |
JimCarver | 0:6b753f761943 | 180 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
JimCarver | 0:6b753f761943 | 181 | blink_res->methods(M2MMethod::POST); |
JimCarver | 0:6b753f761943 | 182 | blink_res->attach_post_callback(blink_callback); |
JimCarver | 0:6b753f761943 | 183 | |
JimCarver | 0:6b753f761943 | 184 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
JimCarver | 0:6b753f761943 | 185 | |
JimCarver | 0:6b753f761943 | 186 | // Callback that fires when registering is complete |
JimCarver | 0:6b753f761943 | 187 | client.on_registered(®istered); |
JimCarver | 0:6b753f761943 | 188 | |
JimCarver | 0:6b753f761943 | 189 | // Register with Mbed Cloud |
JimCarver | 0:6b753f761943 | 190 | client.register_and_connect(); |
JimCarver | 0:6b753f761943 | 191 | |
JimCarver | 0:6b753f761943 | 192 | // Placeholder for callback to update local resource when GET comes. |
JimCarver | 0:6b753f761943 | 193 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
JimCarver | 0:6b753f761943 | 194 | Ticker timer; |
JimCarver | 0:6b753f761943 | 195 | timer.attach(eventQueue.event(&fake_button_press), 5.0); |
JimCarver | 0:6b753f761943 | 196 | |
JimCarver | 0:6b753f761943 | 197 | // You can easily run the eventQueue in a separate thread if required |
JimCarver | 0:6b753f761943 | 198 | eventQueue.dispatch_forever(); |
JimCarver | 0:6b753f761943 | 199 | } |