
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
main.cpp@1:9f355da25904, 2017-06-09 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Fri Jun 09 15:28:40 2017 +0100
- Revision:
- 1:9f355da25904
- Child:
- 3:5b8623c17906
Flesh out example.
Who changed what in which revision?
User | Revision | Line number | New 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" |
rob.meades@u-blox.com | 1:9f355da25904 | 26 | #include "simpleclient.h" |
rob.meades@u-blox.com | 1:9f355da25904 | 27 | #include "security.h" |
rob.meades@u-blox.com | 1:9f355da25904 | 28 | #include "mbed_trace.h" |
rob.meades@u-blox.com | 1:9f355da25904 | 29 | #include "mbed.h" |
rob.meades@u-blox.com | 1:9f355da25904 | 30 | |
rob.meades@u-blox.com | 1:9f355da25904 | 31 | // The credentials of the SIM in the board. If PIN checking is enabled |
rob.meades@u-blox.com | 1:9f355da25904 | 32 | // for your SIM card you must set this to the required PIN. |
rob.meades@u-blox.com | 1:9f355da25904 | 33 | #define PIN "0000" |
rob.meades@u-blox.com | 1:9f355da25904 | 34 | |
rob.meades@u-blox.com | 1:9f355da25904 | 35 | // Network credentials. You should set this according to your |
rob.meades@u-blox.com | 1:9f355da25904 | 36 | // network/SIM card. For C030 boards, leave the parameters as NULL |
rob.meades@u-blox.com | 1:9f355da25904 | 37 | // otherwise, if you do not know the APN for your network, you may |
rob.meades@u-blox.com | 1:9f355da25904 | 38 | // either try the fairly common "internet" for the APN (and leave the |
rob.meades@u-blox.com | 1:9f355da25904 | 39 | // username and password NULL), or you may leave all three as NULL and then |
rob.meades@u-blox.com | 1:9f355da25904 | 40 | // a lookup will be attempted for a small number of known networks |
rob.meades@u-blox.com | 1:9f355da25904 | 41 | // (see APN_db.h in mbed-os/features/netsocket/cellular/utils). |
rob.meades@u-blox.com | 1:9f355da25904 | 42 | #define APN NULL |
rob.meades@u-blox.com | 1:9f355da25904 | 43 | #define USERNAME NULL |
rob.meades@u-blox.com | 1:9f355da25904 | 44 | #define PASSWORD NULL |
rob.meades@u-blox.com | 1:9f355da25904 | 45 | |
rob.meades@u-blox.com | 1:9f355da25904 | 46 | // LEDs |
rob.meades@u-blox.com | 1:9f355da25904 | 47 | DigitalOut ledRed(LED1, 1); |
rob.meades@u-blox.com | 1:9f355da25904 | 48 | DigitalOut ledGreen(LED2, 1); |
rob.meades@u-blox.com | 1:9f355da25904 | 49 | DigitalOut ledBlue(LED3, 1); |
rob.meades@u-blox.com | 1:9f355da25904 | 50 | |
rob.meades@u-blox.com | 1:9f355da25904 | 51 | // The user button |
rob.meades@u-blox.com | 1:9f355da25904 | 52 | volatile bool buttonPressed = false; |
rob.meades@u-blox.com | 1:9f355da25904 | 53 | |
rob.meades@u-blox.com | 1:9f355da25904 | 54 | static void good() { |
rob.meades@u-blox.com | 1:9f355da25904 | 55 | ledGreen = 0; |
rob.meades@u-blox.com | 1:9f355da25904 | 56 | ledBlue = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 57 | ledRed = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 58 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 59 | |
rob.meades@u-blox.com | 1:9f355da25904 | 60 | static void bad() { |
rob.meades@u-blox.com | 1:9f355da25904 | 61 | ledRed = 0; |
rob.meades@u-blox.com | 1:9f355da25904 | 62 | ledGreen = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 63 | ledBlue = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 64 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 65 | |
rob.meades@u-blox.com | 1:9f355da25904 | 66 | static void event() { |
rob.meades@u-blox.com | 1:9f355da25904 | 67 | ledBlue = 0; |
rob.meades@u-blox.com | 1:9f355da25904 | 68 | ledRed = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 69 | ledGreen = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 70 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 71 | |
rob.meades@u-blox.com | 1:9f355da25904 | 72 | static void pulseEvent() { |
rob.meades@u-blox.com | 1:9f355da25904 | 73 | event(); |
rob.meades@u-blox.com | 1:9f355da25904 | 74 | wait_ms(500); |
rob.meades@u-blox.com | 1:9f355da25904 | 75 | good(); |
rob.meades@u-blox.com | 1:9f355da25904 | 76 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 77 | |
rob.meades@u-blox.com | 1:9f355da25904 | 78 | static void ledOff() { |
rob.meades@u-blox.com | 1:9f355da25904 | 79 | ledBlue = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 80 | ledRed = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 81 | ledGreen = 1; |
rob.meades@u-blox.com | 1:9f355da25904 | 82 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 83 | |
rob.meades@u-blox.com | 1:9f355da25904 | 84 | // Resource values for the Device Object |
rob.meades@u-blox.com | 1:9f355da25904 | 85 | struct MbedClientDevice device = { |
rob.meades@u-blox.com | 1:9f355da25904 | 86 | "Manufacturer", // Manufacturer |
rob.meades@u-blox.com | 1:9f355da25904 | 87 | "Type", // Type |
rob.meades@u-blox.com | 1:9f355da25904 | 88 | "ModelNumber", // ModelNumber |
rob.meades@u-blox.com | 1:9f355da25904 | 89 | "SerialNumber" // SerialNumber |
rob.meades@u-blox.com | 1:9f355da25904 | 90 | }; |
rob.meades@u-blox.com | 1:9f355da25904 | 91 | |
rob.meades@u-blox.com | 1:9f355da25904 | 92 | class LedResource { |
rob.meades@u-blox.com | 1:9f355da25904 | 93 | public: |
rob.meades@u-blox.com | 1:9f355da25904 | 94 | LedResource() { |
rob.meades@u-blox.com | 1:9f355da25904 | 95 | ledObject = M2MInterfaceFactory::create_object("3311"); |
rob.meades@u-blox.com | 1:9f355da25904 | 96 | M2MObjectInstance *inst = ledObject->create_object_instance(); |
rob.meades@u-blox.com | 1:9f355da25904 | 97 | |
rob.meades@u-blox.com | 1:9f355da25904 | 98 | // An observable resource |
rob.meades@u-blox.com | 1:9f355da25904 | 99 | M2MResource *onResource = inst->create_dynamic_resource("5850", "On/Off", M2MResourceInstance::BOOLEAN, true); |
rob.meades@u-blox.com | 1:9f355da25904 | 100 | onResource->set_operation(M2MBase::GET_PUT_ALLOWED); |
rob.meades@u-blox.com | 1:9f355da25904 | 101 | onResource->set_value(false); |
rob.meades@u-blox.com | 1:9f355da25904 | 102 | |
rob.meades@u-blox.com | 1:9f355da25904 | 103 | // An multi-valued resource |
rob.meades@u-blox.com | 1:9f355da25904 | 104 | M2MResource *dimmerResource = inst->create_dynamic_resource("5851", "Dimmer", M2MResourceInstance::BOOLEAN, false); |
rob.meades@u-blox.com | 1:9f355da25904 | 105 | dimmerResource->set_operation(M2MBase::GET_PUT_ALLOWED); |
rob.meades@u-blox.com | 1:9f355da25904 | 106 | dimmerResource->set_value(false); |
rob.meades@u-blox.com | 1:9f355da25904 | 107 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 108 | |
rob.meades@u-blox.com | 1:9f355da25904 | 109 | ~LedResource() { |
rob.meades@u-blox.com | 1:9f355da25904 | 110 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 111 | |
rob.meades@u-blox.com | 1:9f355da25904 | 112 | M2MObject *get_object() { |
rob.meades@u-blox.com | 1:9f355da25904 | 113 | return ledObject; |
rob.meades@u-blox.com | 1:9f355da25904 | 114 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 115 | |
rob.meades@u-blox.com | 1:9f355da25904 | 116 | private: |
rob.meades@u-blox.com | 1:9f355da25904 | 117 | M2MObject *ledObject; |
rob.meades@u-blox.com | 1:9f355da25904 | 118 | }; |
rob.meades@u-blox.com | 1:9f355da25904 | 119 | |
rob.meades@u-blox.com | 1:9f355da25904 | 120 | static void cbButton() |
rob.meades@u-blox.com | 1:9f355da25904 | 121 | { |
rob.meades@u-blox.com | 1:9f355da25904 | 122 | buttonPressed = true; |
rob.meades@u-blox.com | 1:9f355da25904 | 123 | pulseEvent(); |
rob.meades@u-blox.com | 1:9f355da25904 | 124 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 125 | |
rob.meades@u-blox.com | 1:9f355da25904 | 126 | /* This example program for the u-blox C030 and C027 boards instantiates |
rob.meades@u-blox.com | 1:9f355da25904 | 127 | * the mbedClient and runs it over a UbloxATCellularInterface connection to |
rob.meades@u-blox.com | 1:9f355da25904 | 128 | * the mbed connector. |
rob.meades@u-blox.com | 1:9f355da25904 | 129 | * Progress may be monitored with a serial terminal running at 9600 baud. |
rob.meades@u-blox.com | 1:9f355da25904 | 130 | * The LED on the C030 board will turn green when this program is |
rob.meades@u-blox.com | 1:9f355da25904 | 131 | * operating correctly, pulse blue when an mbedClient operation is completed |
rob.meades@u-blox.com | 1:9f355da25904 | 132 | * and turn red if there is a failure. |
rob.meades@u-blox.com | 1:9f355da25904 | 133 | * |
rob.meades@u-blox.com | 1:9f355da25904 | 134 | * IMPORTANT to use this example you must first register with the mbed Connector: |
rob.meades@u-blox.com | 1:9f355da25904 | 135 | * |
rob.meades@u-blox.com | 1:9f355da25904 | 136 | * https://connector.mbed.com/ |
rob.meades@u-blox.com | 1:9f355da25904 | 137 | * |
rob.meades@u-blox.com | 1:9f355da25904 | 138 | * ...using your mbed developer credentials and generate your own copy of the file |
rob.meades@u-blox.com | 1:9f355da25904 | 139 | * security.h, replacing the empty one in this directory with yours and |
rob.meades@u-blox.com | 1:9f355da25904 | 140 | * recompiling/downloading the code to your board. |
rob.meades@u-blox.com | 1:9f355da25904 | 141 | */ |
rob.meades@u-blox.com | 1:9f355da25904 | 142 | |
rob.meades@u-blox.com | 1:9f355da25904 | 143 | int main() |
rob.meades@u-blox.com | 1:9f355da25904 | 144 | { |
rob.meades@u-blox.com | 1:9f355da25904 | 145 | UbloxATCellularInterface *interface = new UbloxATCellularInterface(); |
rob.meades@u-blox.com | 1:9f355da25904 | 146 | MbedClient *mbedClient = new MbedClient(device); |
rob.meades@u-blox.com | 1:9f355da25904 | 147 | M2MObjectList objectList; |
rob.meades@u-blox.com | 1:9f355da25904 | 148 | M2MSecurity *registerObject; |
rob.meades@u-blox.com | 1:9f355da25904 | 149 | M2MDevice *deviceObject; |
rob.meades@u-blox.com | 1:9f355da25904 | 150 | LedResource ledResource; |
rob.meades@u-blox.com | 1:9f355da25904 | 151 | unsigned int seed; |
rob.meades@u-blox.com | 1:9f355da25904 | 152 | size_t len; |
rob.meades@u-blox.com | 1:9f355da25904 | 153 | InterruptIn userButton(SW0); |
rob.meades@u-blox.com | 1:9f355da25904 | 154 | |
rob.meades@u-blox.com | 1:9f355da25904 | 155 | // Attach a function to the user button |
rob.meades@u-blox.com | 1:9f355da25904 | 156 | userButton.rise(&cbButton); |
rob.meades@u-blox.com | 1:9f355da25904 | 157 | |
rob.meades@u-blox.com | 1:9f355da25904 | 158 | mbed_trace_init(); |
rob.meades@u-blox.com | 1:9f355da25904 | 159 | srand(seed); |
rob.meades@u-blox.com | 1:9f355da25904 | 160 | |
rob.meades@u-blox.com | 1:9f355da25904 | 161 | // Randomize source port |
rob.meades@u-blox.com | 1:9f355da25904 | 162 | #ifdef TARGET_UBLOX_C030 |
rob.meades@u-blox.com | 1:9f355da25904 | 163 | mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof(seed), &len); |
rob.meades@u-blox.com | 1:9f355da25904 | 164 | #else |
rob.meades@u-blox.com | 1:9f355da25904 | 165 | // NULL entropy, since C027 does not have a true random number generator |
rob.meades@u-blox.com | 1:9f355da25904 | 166 | mbedtls_null_entropy_poll(NULL, (unsigned char *) &seed, sizeof(seed), &len); |
rob.meades@u-blox.com | 1:9f355da25904 | 167 | #endif |
rob.meades@u-blox.com | 1:9f355da25904 | 168 | |
rob.meades@u-blox.com | 1:9f355da25904 | 169 | good(); |
rob.meades@u-blox.com | 1:9f355da25904 | 170 | printf("Starting up, please wait up to 180 seconds for network registration to complete...\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 171 | if (interface->init(PIN)) { |
rob.meades@u-blox.com | 1:9f355da25904 | 172 | pulseEvent(); |
rob.meades@u-blox.com | 1:9f355da25904 | 173 | printf("Registered with network.\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 174 | |
rob.meades@u-blox.com | 1:9f355da25904 | 175 | // Create endpoint interface to manage register and unregister |
rob.meades@u-blox.com | 1:9f355da25904 | 176 | mbedClient->create_interface("coap://api.connector.mbed.com:5684", interface); |
rob.meades@u-blox.com | 1:9f355da25904 | 177 | |
rob.meades@u-blox.com | 1:9f355da25904 | 178 | // Create objects of varying types, see simpleclient.h for more details on implementation. |
rob.meades@u-blox.com | 1:9f355da25904 | 179 | registerObject = mbedClient->create_register_object(); // Server object specifying connector info |
rob.meades@u-blox.com | 1:9f355da25904 | 180 | deviceObject = mbedClient->create_device_object(); // Device resources object |
rob.meades@u-blox.com | 1:9f355da25904 | 181 | |
rob.meades@u-blox.com | 1:9f355da25904 | 182 | // Add objects to list |
rob.meades@u-blox.com | 1:9f355da25904 | 183 | objectList.push_back(deviceObject); |
rob.meades@u-blox.com | 1:9f355da25904 | 184 | objectList.push_back(ledResource.get_object()); |
rob.meades@u-blox.com | 1:9f355da25904 | 185 | |
rob.meades@u-blox.com | 1:9f355da25904 | 186 | // Set endpoint registration object |
rob.meades@u-blox.com | 1:9f355da25904 | 187 | mbedClient->set_register_object(registerObject); |
rob.meades@u-blox.com | 1:9f355da25904 | 188 | |
rob.meades@u-blox.com | 1:9f355da25904 | 189 | printf("Updating object registration in a loop (with a 30 second refresh period) until the user button is presed...\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 190 | interface->set_credentials(APN, USERNAME, PASSWORD); |
rob.meades@u-blox.com | 1:9f355da25904 | 191 | while (!buttonPressed) { |
rob.meades@u-blox.com | 1:9f355da25904 | 192 | // Make sure cellular is connected |
rob.meades@u-blox.com | 1:9f355da25904 | 193 | if (interface->connect() == 0) { |
rob.meades@u-blox.com | 1:9f355da25904 | 194 | pulseEvent(); |
rob.meades@u-blox.com | 1:9f355da25904 | 195 | printf("[Still] connected to packet network.\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 196 | if (mbedClient->register_successful()) { |
rob.meades@u-blox.com | 1:9f355da25904 | 197 | printf("Updating registration (follow progress at https://connector.mbed.com/#home)...\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 198 | mbedClient->test_update_register(); |
rob.meades@u-blox.com | 1:9f355da25904 | 199 | } else { |
rob.meades@u-blox.com | 1:9f355da25904 | 200 | printf("Registering with connector (follow progress at https://connector.mbed.com/#home)...\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 201 | mbedClient->test_register(registerObject, objectList); |
rob.meades@u-blox.com | 1:9f355da25904 | 202 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 203 | } else { |
rob.meades@u-blox.com | 1:9f355da25904 | 204 | bad(); |
rob.meades@u-blox.com | 1:9f355da25904 | 205 | 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 | 206 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 207 | Thread::wait(30000); |
rob.meades@u-blox.com | 1:9f355da25904 | 208 | printf("[Checking if user button has been pressed]\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 209 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 210 | |
rob.meades@u-blox.com | 1:9f355da25904 | 211 | pulseEvent(); |
rob.meades@u-blox.com | 1:9f355da25904 | 212 | printf("User button was pressed, stopping...\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 213 | mbedClient->test_unregister(); |
rob.meades@u-blox.com | 1:9f355da25904 | 214 | interface->disconnect(); |
rob.meades@u-blox.com | 1:9f355da25904 | 215 | interface->deinit(); |
rob.meades@u-blox.com | 1:9f355da25904 | 216 | ledOff(); |
rob.meades@u-blox.com | 1:9f355da25904 | 217 | printf("Stopped.\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 218 | M2MDevice::delete_instance(); |
rob.meades@u-blox.com | 1:9f355da25904 | 219 | } else { |
rob.meades@u-blox.com | 1:9f355da25904 | 220 | bad(); |
rob.meades@u-blox.com | 1:9f355da25904 | 221 | printf("Unable to initialise the interface.\n"); |
rob.meades@u-blox.com | 1:9f355da25904 | 222 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 223 | } |
rob.meades@u-blox.com | 1:9f355da25904 | 224 | |
rob.meades@u-blox.com | 1:9f355da25904 | 225 | // End Of File |