Example program running mbedClient over UbloxATCellularInterface or OnboardCellularInterface for the C030 platform.

Dependencies:   ublox-cellular-base ublox-at-cellular-interface ublox-ppp-cellular-interface ublox-at-cellular-interface-n2xx ublox-cellular-base-n2xx

Committer:
RobMeades
Date:
Wed Feb 28 14:58:04 2018 +0000
Revision:
16:d4f4a9dfa938
Parent:
15:21e0bf60dd0c
Update to latest libraries and replace UbloxPPPCellularInterface with OnboardCellularInterface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rob.meades@u-blox.com 1:9f355da25904 1 /* mbed Microcontroller Library
rob.meades@u-blox.com 1:9f355da25904 2 * Copyright (c) 2017 u-blox
rob.meades@u-blox.com 1:9f355da25904 3 *
rob.meades@u-blox.com 1:9f355da25904 4 * Licensed under the Apache License, Version 2.0 (the "License");
rob.meades@u-blox.com 1:9f355da25904 5 * you may not use this file except in compliance with the License.
rob.meades@u-blox.com 1:9f355da25904 6 * You may obtain a copy of the License at
rob.meades@u-blox.com 1:9f355da25904 7 *
rob.meades@u-blox.com 1:9f355da25904 8 * http://www.apache.org/licenses/LICENSE-2.0
rob.meades@u-blox.com 1:9f355da25904 9 *
rob.meades@u-blox.com 1:9f355da25904 10 * Unless required by applicable law or agreed to in writing, software
rob.meades@u-blox.com 1:9f355da25904 11 * distributed under the License is distributed on an "AS IS" BASIS,
rob.meades@u-blox.com 1:9f355da25904 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rob.meades@u-blox.com 1:9f355da25904 13 * See the License for the specific language governing permissions and
rob.meades@u-blox.com 1:9f355da25904 14 * limitations under the License.
rob.meades@u-blox.com 1:9f355da25904 15 */
rob.meades@u-blox.com 1:9f355da25904 16
rob.meades@u-blox.com 1:9f355da25904 17 #define __STDC_FORMAT_MACROS
rob.meades@u-blox.com 1:9f355da25904 18 #include <inttypes.h>
rob.meades@u-blox.com 1:9f355da25904 19 #include <string>
rob.meades@u-blox.com 1:9f355da25904 20 #include <sstream>
rob.meades@u-blox.com 1:9f355da25904 21 #include <vector>
rob.meades@u-blox.com 1:9f355da25904 22
rob.meades@u-blox.com 1:9f355da25904 23 #include "mbed.h"
rob.meades@u-blox.com 1:9f355da25904 24 #include "mbedtls/entropy_poll.h"
rob.meades@u-blox.com 1:9f355da25904 25 #include "UbloxATCellularInterface.h"
RobMeades 16:d4f4a9dfa938 26 #include "OnboardCellularInterface.h"
rob.meades@u-blox.com 12:78e41b0a374f 27 #include "UbloxATCellularInterfaceN2xx.h"
rob.meades@u-blox.com 1:9f355da25904 28 #include "simpleclient.h"
rob.meades@u-blox.com 1:9f355da25904 29 #include "security.h"
rob.meades@u-blox.com 1:9f355da25904 30 #include "mbed_trace.h"
rob.meades@u-blox.com 1:9f355da25904 31 #include "mbed.h"
rob.meades@u-blox.com 1:9f355da25904 32
rob.meades@u-blox.com 13:c1f582d9ad94 33 // You must select the correct interface library for your board, by
rob.meades@u-blox.com 13:c1f582d9ad94 34 // uncommenting the correct line below. Supported combinations are
rob.meades@u-blox.com 13:c1f582d9ad94 35 // indicated with a "Y" in the table below.
rob.meades@u-blox.com 12:78e41b0a374f 36 //
rob.meades@u-blox.com 13:c1f582d9ad94 37 // C030_U201 C030_N211
rob.meades@u-blox.com 13:c1f582d9ad94 38 // UbloxATCellularInterface Y -
RobMeades 16:d4f4a9dfa938 39 // OnboardCellularInterface Y -
rob.meades@u-blox.com 13:c1f582d9ad94 40 // UbloxATCellularInterfaceN2xx - Y
rob.meades@u-blox.com 12:78e41b0a374f 41 // Note: the N211 module supports only UDP, not TCP
rob.meades@u-blox.com 12:78e41b0a374f 42
RobMeades 16:d4f4a9dfa938 43 // OnboardCellularInterface uses LWIP and the PPP cellular interface
rob.meades@u-blox.com 12:78e41b0a374f 44 // on the mbed MCU, while using UbloxATCellularInterface and
rob.meades@u-blox.com 12:78e41b0a374f 45 // UbloxATCellularInterfaceN2xx uses an IP stack on the cellular
rob.meades@u-blox.com 13:c1f582d9ad94 46 // module and hence uses less RAM. This also allows other AT command
rob.meades@u-blox.com 13:c1f582d9ad94 47 // operations (e.g. sending an SMS) to happen during a data transfer
rob.meades@u-blox.com 13:c1f582d9ad94 48 // (for which you should replace the UbloxATCellularInterface library with
rob.meades@u-blox.com 13:c1f582d9ad94 49 // the UbloxATCellularInterfaceExt library). However, it is slower than
rob.meades@u-blox.com 13:c1f582d9ad94 50 // using the LWIP/PPP on the mbed MCU interface since more string parsing
rob.meades@u-blox.com 13:c1f582d9ad94 51 // is required.
RobMeades 3:5b8623c17906 52 #define INTERFACE_CLASS UbloxATCellularInterface
RobMeades 16:d4f4a9dfa938 53 //#define INTERFACE_CLASS OnboardCellularInterface
rob.meades@u-blox.com 12:78e41b0a374f 54 //#define INTERFACE_CLASS UbloxATCellularInterfaceN2xx
RobMeades 3:5b8623c17906 55
rob.meades@u-blox.com 1:9f355da25904 56 // The credentials of the SIM in the board. If PIN checking is enabled
rob.meades@u-blox.com 1:9f355da25904 57 // for your SIM card you must set this to the required PIN.
rob.meades@u-blox.com 1:9f355da25904 58 #define PIN "0000"
rob.meades@u-blox.com 1:9f355da25904 59
rob.meades@u-blox.com 1:9f355da25904 60 // Network credentials. You should set this according to your
rob.meades@u-blox.com 12:78e41b0a374f 61 // network/SIM card. For C030 non-N2xx boards, leave the parameters as NULL
rob.meades@u-blox.com 1:9f355da25904 62 // otherwise, if you do not know the APN for your network, you may
rob.meades@u-blox.com 1:9f355da25904 63 // either try the fairly common "internet" for the APN (and leave the
rob.meades@u-blox.com 1:9f355da25904 64 // username and password NULL), or you may leave all three as NULL and then
rob.meades@u-blox.com 1:9f355da25904 65 // a lookup will be attempted for a small number of known networks
rob.meades@u-blox.com 1:9f355da25904 66 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
rob.meades@u-blox.com 1:9f355da25904 67 #define APN NULL
rob.meades@u-blox.com 1:9f355da25904 68 #define USERNAME NULL
rob.meades@u-blox.com 1:9f355da25904 69 #define PASSWORD NULL
rob.meades@u-blox.com 1:9f355da25904 70
rob.meades@u-blox.com 1:9f355da25904 71 // LEDs
rob.meades@u-blox.com 1:9f355da25904 72 DigitalOut ledRed(LED1, 1);
rob.meades@u-blox.com 1:9f355da25904 73 DigitalOut ledGreen(LED2, 1);
rob.meades@u-blox.com 1:9f355da25904 74 DigitalOut ledBlue(LED3, 1);
rob.meades@u-blox.com 1:9f355da25904 75
rob.meades@u-blox.com 1:9f355da25904 76 // The user button
rob.meades@u-blox.com 1:9f355da25904 77 volatile bool buttonPressed = false;
rob.meades@u-blox.com 1:9f355da25904 78
rob.meades@u-blox.com 1:9f355da25904 79 static void good() {
rob.meades@u-blox.com 1:9f355da25904 80 ledGreen = 0;
rob.meades@u-blox.com 1:9f355da25904 81 ledBlue = 1;
rob.meades@u-blox.com 1:9f355da25904 82 ledRed = 1;
rob.meades@u-blox.com 1:9f355da25904 83 }
rob.meades@u-blox.com 1:9f355da25904 84
rob.meades@u-blox.com 1:9f355da25904 85 static void bad() {
rob.meades@u-blox.com 1:9f355da25904 86 ledRed = 0;
rob.meades@u-blox.com 1:9f355da25904 87 ledGreen = 1;
rob.meades@u-blox.com 1:9f355da25904 88 ledBlue = 1;
rob.meades@u-blox.com 1:9f355da25904 89 }
rob.meades@u-blox.com 1:9f355da25904 90
rob.meades@u-blox.com 1:9f355da25904 91 static void event() {
rob.meades@u-blox.com 1:9f355da25904 92 ledBlue = 0;
rob.meades@u-blox.com 1:9f355da25904 93 ledRed = 1;
rob.meades@u-blox.com 1:9f355da25904 94 ledGreen = 1;
rob.meades@u-blox.com 1:9f355da25904 95 }
rob.meades@u-blox.com 1:9f355da25904 96
rob.meades@u-blox.com 1:9f355da25904 97 static void pulseEvent() {
rob.meades@u-blox.com 1:9f355da25904 98 event();
rob.meades@u-blox.com 1:9f355da25904 99 wait_ms(500);
rob.meades@u-blox.com 1:9f355da25904 100 good();
rob.meades@u-blox.com 1:9f355da25904 101 }
rob.meades@u-blox.com 1:9f355da25904 102
rob.meades@u-blox.com 1:9f355da25904 103 static void ledOff() {
rob.meades@u-blox.com 1:9f355da25904 104 ledBlue = 1;
rob.meades@u-blox.com 1:9f355da25904 105 ledRed = 1;
rob.meades@u-blox.com 1:9f355da25904 106 ledGreen = 1;
rob.meades@u-blox.com 1:9f355da25904 107 }
rob.meades@u-blox.com 1:9f355da25904 108
rob.meades@u-blox.com 1:9f355da25904 109 // Resource values for the Device Object
rob.meades@u-blox.com 1:9f355da25904 110 struct MbedClientDevice device = {
rob.meades@u-blox.com 1:9f355da25904 111 "Manufacturer", // Manufacturer
rob.meades@u-blox.com 1:9f355da25904 112 "Type", // Type
rob.meades@u-blox.com 1:9f355da25904 113 "ModelNumber", // ModelNumber
rob.meades@u-blox.com 1:9f355da25904 114 "SerialNumber" // SerialNumber
rob.meades@u-blox.com 1:9f355da25904 115 };
rob.meades@u-blox.com 1:9f355da25904 116
rob.meades@u-blox.com 1:9f355da25904 117 class LedResource {
rob.meades@u-blox.com 1:9f355da25904 118 public:
rob.meades@u-blox.com 1:9f355da25904 119 LedResource() {
rob.meades@u-blox.com 1:9f355da25904 120 ledObject = M2MInterfaceFactory::create_object("3311");
rob.meades@u-blox.com 1:9f355da25904 121 M2MObjectInstance *inst = ledObject->create_object_instance();
rob.meades@u-blox.com 1:9f355da25904 122
rob.meades@u-blox.com 1:9f355da25904 123 // An observable resource
rob.meades@u-blox.com 1:9f355da25904 124 M2MResource *onResource = inst->create_dynamic_resource("5850", "On/Off", M2MResourceInstance::BOOLEAN, true);
rob.meades@u-blox.com 1:9f355da25904 125 onResource->set_operation(M2MBase::GET_PUT_ALLOWED);
rob.meades@u-blox.com 1:9f355da25904 126 onResource->set_value(false);
rob.meades@u-blox.com 1:9f355da25904 127
rob.meades@u-blox.com 1:9f355da25904 128 // An multi-valued resource
rob.meades@u-blox.com 1:9f355da25904 129 M2MResource *dimmerResource = inst->create_dynamic_resource("5851", "Dimmer", M2MResourceInstance::BOOLEAN, false);
rob.meades@u-blox.com 1:9f355da25904 130 dimmerResource->set_operation(M2MBase::GET_PUT_ALLOWED);
rob.meades@u-blox.com 1:9f355da25904 131 dimmerResource->set_value(false);
rob.meades@u-blox.com 1:9f355da25904 132 }
rob.meades@u-blox.com 1:9f355da25904 133
rob.meades@u-blox.com 1:9f355da25904 134 ~LedResource() {
rob.meades@u-blox.com 1:9f355da25904 135 }
rob.meades@u-blox.com 1:9f355da25904 136
rob.meades@u-blox.com 1:9f355da25904 137 M2MObject *get_object() {
rob.meades@u-blox.com 1:9f355da25904 138 return ledObject;
rob.meades@u-blox.com 1:9f355da25904 139 }
rob.meades@u-blox.com 1:9f355da25904 140
rob.meades@u-blox.com 1:9f355da25904 141 private:
rob.meades@u-blox.com 1:9f355da25904 142 M2MObject *ledObject;
rob.meades@u-blox.com 1:9f355da25904 143 };
rob.meades@u-blox.com 1:9f355da25904 144
rob.meades@u-blox.com 1:9f355da25904 145 static void cbButton()
rob.meades@u-blox.com 1:9f355da25904 146 {
rob.meades@u-blox.com 1:9f355da25904 147 buttonPressed = true;
rob.meades@u-blox.com 1:9f355da25904 148 pulseEvent();
rob.meades@u-blox.com 1:9f355da25904 149 }
rob.meades@u-blox.com 1:9f355da25904 150
rob.meades@u-blox.com 5:d81fdd2c89f2 151 /* This example program for the u-blox C030 board instantiates mbedClient
RobMeades 16:d4f4a9dfa938 152 * and runs it over a UbloxATCellularInterface or a OnboardCellularInterface to
rob.meades@u-blox.com 1:9f355da25904 153 * the mbed connector.
rob.meades@u-blox.com 1:9f355da25904 154 * Progress may be monitored with a serial terminal running at 9600 baud.
rob.meades@u-blox.com 1:9f355da25904 155 * The LED on the C030 board will turn green when this program is
rob.meades@u-blox.com 1:9f355da25904 156 * operating correctly, pulse blue when an mbedClient operation is completed
rob.meades@u-blox.com 1:9f355da25904 157 * and turn red if there is a failure.
rob.meades@u-blox.com 1:9f355da25904 158 *
rob.meades@u-blox.com 5:d81fdd2c89f2 159 * Note: mbedClient malloc's around 34 kbytes of RAM, more than is available on
rob.meades@u-blox.com 5:d81fdd2c89f2 160 * the C027 platform, hence this example will not run on the C027 platform.
rob.meades@u-blox.com 5:d81fdd2c89f2 161 *
rob.meades@u-blox.com 1:9f355da25904 162 * IMPORTANT to use this example you must first register with the mbed Connector:
rob.meades@u-blox.com 1:9f355da25904 163 *
rob.meades@u-blox.com 1:9f355da25904 164 * https://connector.mbed.com/
rob.meades@u-blox.com 1:9f355da25904 165 *
rob.meades@u-blox.com 1:9f355da25904 166 * ...using your mbed developer credentials and generate your own copy of the file
rob.meades@u-blox.com 1:9f355da25904 167 * security.h, replacing the empty one in this directory with yours and
rob.meades@u-blox.com 1:9f355da25904 168 * recompiling/downloading the code to your board.
rob.meades@u-blox.com 1:9f355da25904 169 */
rob.meades@u-blox.com 1:9f355da25904 170
rob.meades@u-blox.com 1:9f355da25904 171 int main()
rob.meades@u-blox.com 1:9f355da25904 172 {
RobMeades 3:5b8623c17906 173 INTERFACE_CLASS *interface = new INTERFACE_CLASS();
rob.meades@u-blox.com 13:c1f582d9ad94 174 // If you need to debug the cellular interface, comment out the
rob.meades@u-blox.com 13:c1f582d9ad94 175 // instantiation above and uncomment the one below.
RobMeades 15:21e0bf60dd0c 176 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE,
RobMeades 15:21e0bf60dd0c 177 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_BAUD_RATE.
RobMeades 3:5b8623c17906 178 // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
rob.meades@u-blox.com 13:c1f582d9ad94 179 // xxx,
RobMeades 3:5b8623c17906 180 // true);
rob.meades@u-blox.com 1:9f355da25904 181 MbedClient *mbedClient = new MbedClient(device);
rob.meades@u-blox.com 1:9f355da25904 182 M2MObjectList objectList;
rob.meades@u-blox.com 1:9f355da25904 183 M2MSecurity *registerObject;
rob.meades@u-blox.com 1:9f355da25904 184 M2MDevice *deviceObject;
rob.meades@u-blox.com 1:9f355da25904 185 LedResource ledResource;
rob.meades@u-blox.com 1:9f355da25904 186 unsigned int seed;
rob.meades@u-blox.com 1:9f355da25904 187 size_t len;
rob.meades@u-blox.com 1:9f355da25904 188 InterruptIn userButton(SW0);
rob.meades@u-blox.com 1:9f355da25904 189
rob.meades@u-blox.com 1:9f355da25904 190 // Attach a function to the user button
rob.meades@u-blox.com 1:9f355da25904 191 userButton.rise(&cbButton);
rob.meades@u-blox.com 1:9f355da25904 192
rob.meades@u-blox.com 8:f341bfe37797 193 #if MBED_CONF_MBED_TRACE_ENABLE
rob.meades@u-blox.com 1:9f355da25904 194 mbed_trace_init();
rob.meades@u-blox.com 8:f341bfe37797 195 #endif
rob.meades@u-blox.com 1:9f355da25904 196 srand(seed);
rob.meades@u-blox.com 1:9f355da25904 197
rob.meades@u-blox.com 1:9f355da25904 198 // Randomize source port
RobMeades 3:5b8623c17906 199 mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof seed, &len);
rob.meades@u-blox.com 1:9f355da25904 200
rob.meades@u-blox.com 1:9f355da25904 201 good();
rob.meades@u-blox.com 1:9f355da25904 202 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
RobMeades 16:d4f4a9dfa938 203 pulseEvent();
rob.meades@u-blox.com 1:9f355da25904 204
RobMeades 16:d4f4a9dfa938 205 // Create endpoint interface to manage register and unregister
RobMeades 16:d4f4a9dfa938 206 mbedClient->create_interface("coap://api.connector.mbed.com:5684", interface);
rob.meades@u-blox.com 1:9f355da25904 207
RobMeades 16:d4f4a9dfa938 208 // Create objects of varying types, see simpleclient.h for more details on implementation.
RobMeades 16:d4f4a9dfa938 209 registerObject = mbedClient->create_register_object(); // Server object specifying connector info
RobMeades 16:d4f4a9dfa938 210 deviceObject = mbedClient->create_device_object(); // Device resources object
rob.meades@u-blox.com 1:9f355da25904 211
RobMeades 16:d4f4a9dfa938 212 // Add objects to list
RobMeades 16:d4f4a9dfa938 213 objectList.push_back(deviceObject);
RobMeades 16:d4f4a9dfa938 214 objectList.push_back(ledResource.get_object());
rob.meades@u-blox.com 1:9f355da25904 215
RobMeades 16:d4f4a9dfa938 216 // Set endpoint registration object
RobMeades 16:d4f4a9dfa938 217 mbedClient->set_register_object(registerObject);
rob.meades@u-blox.com 1:9f355da25904 218
RobMeades 16:d4f4a9dfa938 219 printf("Updating object registration in a loop (with a 30 second refresh period) until the user button is presed...\n");
RobMeades 16:d4f4a9dfa938 220 interface->set_credentials(APN, USERNAME, PASSWORD);
RobMeades 16:d4f4a9dfa938 221 while (!buttonPressed) {
RobMeades 16:d4f4a9dfa938 222 // Make sure cellular is connected
RobMeades 16:d4f4a9dfa938 223 if (interface->connect(PIN) == 0) {
RobMeades 16:d4f4a9dfa938 224 pulseEvent();
RobMeades 16:d4f4a9dfa938 225 printf("[Still] connected to packet network.\n");
RobMeades 16:d4f4a9dfa938 226 if (mbedClient->register_successful()) {
RobMeades 16:d4f4a9dfa938 227 printf("Updating registration (follow progress at https://connector.mbed.com/#home)...\n");
RobMeades 16:d4f4a9dfa938 228 mbedClient->test_update_register();
rob.meades@u-blox.com 1:9f355da25904 229 } else {
RobMeades 16:d4f4a9dfa938 230 printf("Registering with connector (follow progress at https://connector.mbed.com/#home)...\n");
RobMeades 16:d4f4a9dfa938 231 mbedClient->test_register(registerObject, objectList);
rob.meades@u-blox.com 1:9f355da25904 232 }
RobMeades 16:d4f4a9dfa938 233 } else {
RobMeades 16:d4f4a9dfa938 234 bad();
RobMeades 16:d4f4a9dfa938 235 printf("Failed to connect, will retry (have you checked that an antenna is plugged in and your APN is correct?)...\n");
rob.meades@u-blox.com 1:9f355da25904 236 }
RobMeades 16:d4f4a9dfa938 237 Thread::wait(30000);
RobMeades 16:d4f4a9dfa938 238 printf("[Checking if user button has been pressed]\n");
rob.meades@u-blox.com 1:9f355da25904 239 }
RobMeades 16:d4f4a9dfa938 240
RobMeades 16:d4f4a9dfa938 241 pulseEvent();
RobMeades 16:d4f4a9dfa938 242 printf("User button was pressed, stopping...\n");
RobMeades 16:d4f4a9dfa938 243 mbedClient->test_unregister();
RobMeades 16:d4f4a9dfa938 244 interface->disconnect();
RobMeades 16:d4f4a9dfa938 245 ledOff();
RobMeades 16:d4f4a9dfa938 246 printf("Stopped.\n");
RobMeades 16:d4f4a9dfa938 247 M2MDevice::delete_instance();
rob.meades@u-blox.com 1:9f355da25904 248 }
rob.meades@u-blox.com 1:9f355da25904 249
rob.meades@u-blox.com 8:f341bfe37797 250 // End Of File