u-blox
/
mbed-cloud-example-c030
Example using the UBLOX_C030_U201 board to communicate over a cellular link with Mbed Cloud.
main.cpp@2:a34e80e223c2, 2018-10-14 (annotated)
- Committer:
- MarceloSalazar
- Date:
- Sun Oct 14 17:06:16 2018 +0100
- Revision:
- 2:a34e80e223c2
- Parent:
- 1:eabbeaa37715
Add support for FW Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:b6d427c3defa | 1 | // ---------------------------------------------------------------------------- |
RobMeades | 0:b6d427c3defa | 2 | // Copyright 2016-2018 ARM Ltd. |
RobMeades | 0:b6d427c3defa | 3 | // |
RobMeades | 0:b6d427c3defa | 4 | // SPDX-License-Identifier: Apache-2.0 |
RobMeades | 0:b6d427c3defa | 5 | // |
RobMeades | 0:b6d427c3defa | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
RobMeades | 0:b6d427c3defa | 7 | // you may not use this file except in compliance with the License. |
RobMeades | 0:b6d427c3defa | 8 | // You may obtain a copy of the License at |
RobMeades | 0:b6d427c3defa | 9 | // |
RobMeades | 0:b6d427c3defa | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
RobMeades | 0:b6d427c3defa | 11 | // |
RobMeades | 0:b6d427c3defa | 12 | // Unless required by applicable law or agreed to in writing, software |
RobMeades | 0:b6d427c3defa | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
RobMeades | 0:b6d427c3defa | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
RobMeades | 0:b6d427c3defa | 15 | // See the License for the specific language governing permissions and |
RobMeades | 0:b6d427c3defa | 16 | // limitations under the License. |
RobMeades | 0:b6d427c3defa | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 1:eabbeaa37715 | 18 | #ifndef MBED_TEST_MODE |
RobMeades | 0:b6d427c3defa | 19 | #include "mbed.h" |
RobMeades | 0:b6d427c3defa | 20 | #include "simple-mbed-cloud-client.h" |
RobMeades | 0:b6d427c3defa | 21 | #include "SDBlockDevice.h" |
RobMeades | 0:b6d427c3defa | 22 | #include "FATFileSystem.h" |
RobMeades | 0:b6d427c3defa | 23 | |
RobMeades | 0:b6d427c3defa | 24 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
RobMeades | 0:b6d427c3defa | 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 |
RobMeades | 0:b6d427c3defa | 26 | EventQueue eventQueue; |
RobMeades | 0:b6d427c3defa | 27 | |
MarceloSalazar | 1:eabbeaa37715 | 28 | // Default block device |
MarceloSalazar | 1:eabbeaa37715 | 29 | BlockDevice* bd = BlockDevice::get_default_instance(); |
MarceloSalazar | 1:eabbeaa37715 | 30 | FATFileSystem fs("sd", bd); |
RobMeades | 0:b6d427c3defa | 31 | |
MarceloSalazar | 1:eabbeaa37715 | 32 | // Default network interface object |
MarceloSalazar | 1:eabbeaa37715 | 33 | NetworkInterface *net; |
RobMeades | 0:b6d427c3defa | 34 | |
RobMeades | 0:b6d427c3defa | 35 | // Declaring pointers for access to Mbed Cloud Client resources outside of main() |
RobMeades | 0:b6d427c3defa | 36 | MbedCloudClientResource *button_res; |
RobMeades | 0:b6d427c3defa | 37 | MbedCloudClientResource *pattern_res; |
RobMeades | 0:b6d427c3defa | 38 | |
RobMeades | 0:b6d427c3defa | 39 | // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button |
RobMeades | 0:b6d427c3defa | 40 | void fake_button_press() { |
RobMeades | 0:b6d427c3defa | 41 | int v = button_res->get_value_int() + 1; |
RobMeades | 0:b6d427c3defa | 42 | |
RobMeades | 0:b6d427c3defa | 43 | button_res->set_value(v); |
RobMeades | 0:b6d427c3defa | 44 | |
RobMeades | 0:b6d427c3defa | 45 | printf("Simulated button clicked %d times\n", v); |
RobMeades | 0:b6d427c3defa | 46 | } |
RobMeades | 0:b6d427c3defa | 47 | |
RobMeades | 0:b6d427c3defa | 48 | /** |
RobMeades | 0:b6d427c3defa | 49 | * PUT handler |
RobMeades | 0:b6d427c3defa | 50 | * @param resource The resource that triggered the callback |
RobMeades | 0:b6d427c3defa | 51 | * @param newValue Updated value for the resource |
RobMeades | 0:b6d427c3defa | 52 | */ |
RobMeades | 0:b6d427c3defa | 53 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
RobMeades | 0:b6d427c3defa | 54 | printf("PUT received, new value: %s\n", newValue.c_str()); |
RobMeades | 0:b6d427c3defa | 55 | } |
RobMeades | 0:b6d427c3defa | 56 | |
RobMeades | 0:b6d427c3defa | 57 | /** |
RobMeades | 0:b6d427c3defa | 58 | * POST handler |
RobMeades | 0:b6d427c3defa | 59 | * @param resource The resource that triggered the callback |
RobMeades | 0:b6d427c3defa | 60 | * @param buffer If a body was passed to the POST function, this contains the data. |
RobMeades | 0:b6d427c3defa | 61 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
RobMeades | 0:b6d427c3defa | 62 | * @param size Size of the body |
RobMeades | 0:b6d427c3defa | 63 | */ |
RobMeades | 0:b6d427c3defa | 64 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
RobMeades | 0:b6d427c3defa | 65 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
RobMeades | 0:b6d427c3defa | 66 | |
RobMeades | 0:b6d427c3defa | 67 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
RobMeades | 0:b6d427c3defa | 68 | |
RobMeades | 0:b6d427c3defa | 69 | // Parse the pattern string, and toggle the LED in that pattern |
RobMeades | 0:b6d427c3defa | 70 | string s = std::string(pattern_res->get_value().c_str()); |
RobMeades | 0:b6d427c3defa | 71 | size_t i = 0; |
RobMeades | 0:b6d427c3defa | 72 | size_t pos = s.find(':'); |
RobMeades | 0:b6d427c3defa | 73 | while (pos != string::npos) { |
RobMeades | 0:b6d427c3defa | 74 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
RobMeades | 0:b6d427c3defa | 75 | augmentedLed = !augmentedLed; |
RobMeades | 0:b6d427c3defa | 76 | |
RobMeades | 0:b6d427c3defa | 77 | i = ++pos; |
RobMeades | 0:b6d427c3defa | 78 | pos = s.find(':', pos); |
RobMeades | 0:b6d427c3defa | 79 | |
RobMeades | 0:b6d427c3defa | 80 | if (pos == string::npos) { |
RobMeades | 0:b6d427c3defa | 81 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
RobMeades | 0:b6d427c3defa | 82 | augmentedLed = !augmentedLed; |
RobMeades | 0:b6d427c3defa | 83 | } |
RobMeades | 0:b6d427c3defa | 84 | } |
RobMeades | 0:b6d427c3defa | 85 | } |
RobMeades | 0:b6d427c3defa | 86 | |
RobMeades | 0:b6d427c3defa | 87 | /** |
RobMeades | 0:b6d427c3defa | 88 | * Notification callback handler |
RobMeades | 0:b6d427c3defa | 89 | * @param resource The resource that triggered the callback |
RobMeades | 0:b6d427c3defa | 90 | * @param status The delivery status of the notification |
RobMeades | 0:b6d427c3defa | 91 | */ |
RobMeades | 0:b6d427c3defa | 92 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
RobMeades | 0:b6d427c3defa | 93 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
RobMeades | 0:b6d427c3defa | 94 | } |
RobMeades | 0:b6d427c3defa | 95 | |
RobMeades | 0:b6d427c3defa | 96 | /** |
RobMeades | 0:b6d427c3defa | 97 | * Registration callback handler |
RobMeades | 0:b6d427c3defa | 98 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
RobMeades | 0:b6d427c3defa | 99 | */ |
RobMeades | 0:b6d427c3defa | 100 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
RobMeades | 0:b6d427c3defa | 101 | printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
RobMeades | 0:b6d427c3defa | 102 | } |
RobMeades | 0:b6d427c3defa | 103 | |
RobMeades | 0:b6d427c3defa | 104 | int main(void) { |
RobMeades | 0:b6d427c3defa | 105 | printf("Starting Simple Mbed Cloud Client example\n"); |
RobMeades | 0:b6d427c3defa | 106 | |
RobMeades | 0:b6d427c3defa | 107 | // Connect to the internet (DHCP is expected to be on) |
RobMeades | 0:b6d427c3defa | 108 | printf("Connecting to the network using Cellular, please wait up to 180 seconds for a connection...\n"); |
MarceloSalazar | 2:a34e80e223c2 | 109 | |
MarceloSalazar | 1:eabbeaa37715 | 110 | // Connect to the internet (DHCP is expected to be on) |
MarceloSalazar | 1:eabbeaa37715 | 111 | net = NetworkInterface::get_default_instance(); |
MarceloSalazar | 2:a34e80e223c2 | 112 | |
MarceloSalazar | 1:eabbeaa37715 | 113 | nsapi_error_t status = net->connect(); |
RobMeades | 0:b6d427c3defa | 114 | |
MarceloSalazar | 1:eabbeaa37715 | 115 | if (status != NSAPI_ERROR_OK) { |
RobMeades | 0:b6d427c3defa | 116 | printf("Connecting to the network failed (%d)!\n", status); |
RobMeades | 0:b6d427c3defa | 117 | printf("Make sure that an antenna is connected, the APN is correct for your SIM and\n"); |
RobMeades | 0:b6d427c3defa | 118 | printf("either a rechargeable battery or a 3 Amp USB power supply is plugged in.\n"); |
RobMeades | 0:b6d427c3defa | 119 | printf("For UBLOX_C030_U201 the APN should be left blank and you must have\n"); |
RobMeades | 0:b6d427c3defa | 120 | printf("activated your board [once] at http://www.jtiotsims.com/ubxC030).\n"); |
RobMeades | 0:b6d427c3defa | 121 | return -1; |
RobMeades | 0:b6d427c3defa | 122 | } |
MarceloSalazar | 2:a34e80e223c2 | 123 | |
MarceloSalazar | 1:eabbeaa37715 | 124 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
RobMeades | 0:b6d427c3defa | 125 | |
RobMeades | 0:b6d427c3defa | 126 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
MarceloSalazar | 1:eabbeaa37715 | 127 | SimpleMbedCloudClient client(net, bd, &fs); |
RobMeades | 0:b6d427c3defa | 128 | int client_status = client.init(); |
RobMeades | 0:b6d427c3defa | 129 | if (client_status != 0) { |
RobMeades | 0:b6d427c3defa | 130 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
RobMeades | 0:b6d427c3defa | 131 | return -1; |
RobMeades | 0:b6d427c3defa | 132 | } |
RobMeades | 0:b6d427c3defa | 133 | |
RobMeades | 0:b6d427c3defa | 134 | // Creating resources, which can be written or read from the cloud |
RobMeades | 0:b6d427c3defa | 135 | button_res = client.create_resource("3200/0/5501", "button_count"); |
RobMeades | 0:b6d427c3defa | 136 | button_res->set_value(0); |
RobMeades | 0:b6d427c3defa | 137 | button_res->methods(M2MMethod::GET); |
RobMeades | 0:b6d427c3defa | 138 | button_res->observable(true); |
RobMeades | 0:b6d427c3defa | 139 | button_res->attach_notification_callback(button_callback); |
RobMeades | 0:b6d427c3defa | 140 | |
RobMeades | 0:b6d427c3defa | 141 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
RobMeades | 0:b6d427c3defa | 142 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
RobMeades | 0:b6d427c3defa | 143 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
RobMeades | 0:b6d427c3defa | 144 | pattern_res->attach_put_callback(pattern_updated); |
RobMeades | 0:b6d427c3defa | 145 | |
RobMeades | 0:b6d427c3defa | 146 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
RobMeades | 0:b6d427c3defa | 147 | blink_res->methods(M2MMethod::POST); |
RobMeades | 0:b6d427c3defa | 148 | blink_res->attach_post_callback(blink_callback); |
RobMeades | 0:b6d427c3defa | 149 | |
RobMeades | 0:b6d427c3defa | 150 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
RobMeades | 0:b6d427c3defa | 151 | |
RobMeades | 0:b6d427c3defa | 152 | // Callback that fires when registering is complete |
RobMeades | 0:b6d427c3defa | 153 | client.on_registered(®istered); |
RobMeades | 0:b6d427c3defa | 154 | |
RobMeades | 0:b6d427c3defa | 155 | // Register with Mbed Cloud |
RobMeades | 0:b6d427c3defa | 156 | client.register_and_connect(); |
RobMeades | 0:b6d427c3defa | 157 | |
RobMeades | 0:b6d427c3defa | 158 | // Placeholder for callback to update local resource when GET comes. |
RobMeades | 0:b6d427c3defa | 159 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
RobMeades | 0:b6d427c3defa | 160 | Ticker timer; |
RobMeades | 0:b6d427c3defa | 161 | timer.attach(eventQueue.event(&fake_button_press), 5.0); |
RobMeades | 0:b6d427c3defa | 162 | |
RobMeades | 0:b6d427c3defa | 163 | // You can easily run the eventQueue in a separate thread if required |
RobMeades | 0:b6d427c3defa | 164 | eventQueue.dispatch_forever(); |
RobMeades | 0:b6d427c3defa | 165 | } |
MarceloSalazar | 2:a34e80e223c2 | 166 | #endif |