Example using the UBLOX_C030_U201 board to communicate over a cellular link with Mbed Cloud.

Committer:
RobMeades
Date:
Wed May 23 16:41:14 2018 +0100
Revision:
0:b6d427c3defa
Child:
1:eabbeaa37715
Initial revision.

Who changed what in which revision?

UserRevisionLine numberNew 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 // ----------------------------------------------------------------------------
RobMeades 0:b6d427c3defa 18
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 #include "OnboardCellularInterface.h"
RobMeades 0:b6d427c3defa 24
RobMeades 0:b6d427c3defa 25 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
RobMeades 0:b6d427c3defa 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
RobMeades 0:b6d427c3defa 27 EventQueue eventQueue;
RobMeades 0:b6d427c3defa 28
RobMeades 0:b6d427c3defa 29 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
RobMeades 0:b6d427c3defa 30 SDBlockDevice sd(D11, D12, D13, D10);
RobMeades 0:b6d427c3defa 31 FATFileSystem fs("sd", &sd);
RobMeades 0:b6d427c3defa 32
RobMeades 0:b6d427c3defa 33 // The cellular driver
RobMeades 0:b6d427c3defa 34 // If you want to examine AT command exchanges, change this to net(true) with mbed trace enabled
RobMeades 0:b6d427c3defa 35 OnboardCellularInterface net;
RobMeades 0:b6d427c3defa 36
RobMeades 0:b6d427c3defa 37 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
RobMeades 0:b6d427c3defa 38 MbedCloudClientResource *button_res;
RobMeades 0:b6d427c3defa 39 MbedCloudClientResource *pattern_res;
RobMeades 0:b6d427c3defa 40
RobMeades 0:b6d427c3defa 41 // 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 42 void fake_button_press() {
RobMeades 0:b6d427c3defa 43 int v = button_res->get_value_int() + 1;
RobMeades 0:b6d427c3defa 44
RobMeades 0:b6d427c3defa 45 button_res->set_value(v);
RobMeades 0:b6d427c3defa 46
RobMeades 0:b6d427c3defa 47 printf("Simulated button clicked %d times\n", v);
RobMeades 0:b6d427c3defa 48 }
RobMeades 0:b6d427c3defa 49
RobMeades 0:b6d427c3defa 50 /**
RobMeades 0:b6d427c3defa 51 * PUT handler
RobMeades 0:b6d427c3defa 52 * @param resource The resource that triggered the callback
RobMeades 0:b6d427c3defa 53 * @param newValue Updated value for the resource
RobMeades 0:b6d427c3defa 54 */
RobMeades 0:b6d427c3defa 55 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
RobMeades 0:b6d427c3defa 56 printf("PUT received, new value: %s\n", newValue.c_str());
RobMeades 0:b6d427c3defa 57 }
RobMeades 0:b6d427c3defa 58
RobMeades 0:b6d427c3defa 59 /**
RobMeades 0:b6d427c3defa 60 * POST handler
RobMeades 0:b6d427c3defa 61 * @param resource The resource that triggered the callback
RobMeades 0:b6d427c3defa 62 * @param buffer If a body was passed to the POST function, this contains the data.
RobMeades 0:b6d427c3defa 63 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
RobMeades 0:b6d427c3defa 64 * @param size Size of the body
RobMeades 0:b6d427c3defa 65 */
RobMeades 0:b6d427c3defa 66 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
RobMeades 0:b6d427c3defa 67 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
RobMeades 0:b6d427c3defa 68
RobMeades 0:b6d427c3defa 69 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
RobMeades 0:b6d427c3defa 70
RobMeades 0:b6d427c3defa 71 // Parse the pattern string, and toggle the LED in that pattern
RobMeades 0:b6d427c3defa 72 string s = std::string(pattern_res->get_value().c_str());
RobMeades 0:b6d427c3defa 73 size_t i = 0;
RobMeades 0:b6d427c3defa 74 size_t pos = s.find(':');
RobMeades 0:b6d427c3defa 75 while (pos != string::npos) {
RobMeades 0:b6d427c3defa 76 wait_ms(atoi(s.substr(i, pos - i).c_str()));
RobMeades 0:b6d427c3defa 77 augmentedLed = !augmentedLed;
RobMeades 0:b6d427c3defa 78
RobMeades 0:b6d427c3defa 79 i = ++pos;
RobMeades 0:b6d427c3defa 80 pos = s.find(':', pos);
RobMeades 0:b6d427c3defa 81
RobMeades 0:b6d427c3defa 82 if (pos == string::npos) {
RobMeades 0:b6d427c3defa 83 wait_ms(atoi(s.substr(i, s.length()).c_str()));
RobMeades 0:b6d427c3defa 84 augmentedLed = !augmentedLed;
RobMeades 0:b6d427c3defa 85 }
RobMeades 0:b6d427c3defa 86 }
RobMeades 0:b6d427c3defa 87 }
RobMeades 0:b6d427c3defa 88
RobMeades 0:b6d427c3defa 89 /**
RobMeades 0:b6d427c3defa 90 * Notification callback handler
RobMeades 0:b6d427c3defa 91 * @param resource The resource that triggered the callback
RobMeades 0:b6d427c3defa 92 * @param status The delivery status of the notification
RobMeades 0:b6d427c3defa 93 */
RobMeades 0:b6d427c3defa 94 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
RobMeades 0:b6d427c3defa 95 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
RobMeades 0:b6d427c3defa 96 }
RobMeades 0:b6d427c3defa 97
RobMeades 0:b6d427c3defa 98 /**
RobMeades 0:b6d427c3defa 99 * Registration callback handler
RobMeades 0:b6d427c3defa 100 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
RobMeades 0:b6d427c3defa 101 */
RobMeades 0:b6d427c3defa 102 void registered(const ConnectorClientEndpointInfo *endpoint) {
RobMeades 0:b6d427c3defa 103 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
RobMeades 0:b6d427c3defa 104 }
RobMeades 0:b6d427c3defa 105
RobMeades 0:b6d427c3defa 106 int main(void) {
RobMeades 0:b6d427c3defa 107 printf("Starting Simple Mbed Cloud Client example\n");
RobMeades 0:b6d427c3defa 108
RobMeades 0:b6d427c3defa 109 // Connect to the internet (DHCP is expected to be on)
RobMeades 0:b6d427c3defa 110 printf("Connecting to the network using Cellular, please wait up to 180 seconds for a connection...\n");
RobMeades 0:b6d427c3defa 111 nsapi_error_t status = net.connect();
RobMeades 0:b6d427c3defa 112
RobMeades 0:b6d427c3defa 113 if (status != 0) {
RobMeades 0:b6d427c3defa 114 printf("Connecting to the network failed (%d)!\n", status);
RobMeades 0:b6d427c3defa 115 printf("Make sure that an antenna is connected, the APN is correct for your SIM and\n");
RobMeades 0:b6d427c3defa 116 printf("either a rechargeable battery or a 3 Amp USB power supply is plugged in.\n");
RobMeades 0:b6d427c3defa 117 printf("For UBLOX_C030_U201 the APN should be left blank and you must have\n");
RobMeades 0:b6d427c3defa 118 printf("activated your board [once] at http://www.jtiotsims.com/ubxC030).\n");
RobMeades 0:b6d427c3defa 119 return -1;
RobMeades 0:b6d427c3defa 120 }
RobMeades 0:b6d427c3defa 121
RobMeades 0:b6d427c3defa 122 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
RobMeades 0:b6d427c3defa 123
RobMeades 0:b6d427c3defa 124 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
RobMeades 0:b6d427c3defa 125 SimpleMbedCloudClient client(&net, &sd, &fs);
RobMeades 0:b6d427c3defa 126 int client_status = client.init();
RobMeades 0:b6d427c3defa 127 if (client_status != 0) {
RobMeades 0:b6d427c3defa 128 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
RobMeades 0:b6d427c3defa 129 return -1;
RobMeades 0:b6d427c3defa 130 }
RobMeades 0:b6d427c3defa 131
RobMeades 0:b6d427c3defa 132 // Creating resources, which can be written or read from the cloud
RobMeades 0:b6d427c3defa 133 button_res = client.create_resource("3200/0/5501", "button_count");
RobMeades 0:b6d427c3defa 134 button_res->set_value(0);
RobMeades 0:b6d427c3defa 135 button_res->methods(M2MMethod::GET);
RobMeades 0:b6d427c3defa 136 button_res->observable(true);
RobMeades 0:b6d427c3defa 137 button_res->attach_notification_callback(button_callback);
RobMeades 0:b6d427c3defa 138
RobMeades 0:b6d427c3defa 139 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
RobMeades 0:b6d427c3defa 140 pattern_res->set_value("500:500:500:500:500:500:500:500");
RobMeades 0:b6d427c3defa 141 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
RobMeades 0:b6d427c3defa 142 pattern_res->attach_put_callback(pattern_updated);
RobMeades 0:b6d427c3defa 143
RobMeades 0:b6d427c3defa 144 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
RobMeades 0:b6d427c3defa 145 blink_res->methods(M2MMethod::POST);
RobMeades 0:b6d427c3defa 146 blink_res->attach_post_callback(blink_callback);
RobMeades 0:b6d427c3defa 147
RobMeades 0:b6d427c3defa 148 printf("Initialized Mbed Cloud Client. Registering...\n");
RobMeades 0:b6d427c3defa 149
RobMeades 0:b6d427c3defa 150 // Callback that fires when registering is complete
RobMeades 0:b6d427c3defa 151 client.on_registered(&registered);
RobMeades 0:b6d427c3defa 152
RobMeades 0:b6d427c3defa 153 // Register with Mbed Cloud
RobMeades 0:b6d427c3defa 154 client.register_and_connect();
RobMeades 0:b6d427c3defa 155
RobMeades 0:b6d427c3defa 156 // Placeholder for callback to update local resource when GET comes.
RobMeades 0:b6d427c3defa 157 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
RobMeades 0:b6d427c3defa 158 Ticker timer;
RobMeades 0:b6d427c3defa 159 timer.attach(eventQueue.event(&fake_button_press), 5.0);
RobMeades 0:b6d427c3defa 160
RobMeades 0:b6d427c3defa 161 // You can easily run the eventQueue in a separate thread if required
RobMeades 0:b6d427c3defa 162 eventQueue.dispatch_forever();
RobMeades 0:b6d427c3defa 163 }