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

Revision:
1:9f355da25904
Child:
3:5b8623c17906
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 09 15:28:40 2017 +0100
@@ -0,0 +1,225 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2017 u-blox
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#include <string>
+#include <sstream>
+#include <vector>
+
+#include "mbed.h"
+#include "mbedtls/entropy_poll.h"
+#include "UbloxATCellularInterface.h"
+#include "simpleclient.h"
+#include "security.h"
+#include "mbed_trace.h"
+#include "mbed.h"
+
+// The credentials of the SIM in the board.  If PIN checking is enabled
+// for your SIM card you must set this to the required PIN.
+#define PIN "0000"
+
+// Network credentials.  You should set this according to your
+// network/SIM card.  For C030 boards, leave the parameters as NULL
+// otherwise, if you do not know the APN for your network, you may
+// either try the fairly common "internet" for the APN (and leave the
+// username and password NULL), or you may leave all three as NULL and then
+// a lookup will be attempted for a small number of known networks
+// (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
+#define APN         NULL
+#define USERNAME    NULL
+#define PASSWORD    NULL
+
+// LEDs
+DigitalOut ledRed(LED1, 1);
+DigitalOut ledGreen(LED2, 1);
+DigitalOut ledBlue(LED3, 1);
+
+// The user button
+volatile bool buttonPressed = false;
+
+static void good() {
+    ledGreen = 0;
+    ledBlue = 1;
+    ledRed = 1;
+}
+
+static void bad() {
+    ledRed = 0;
+    ledGreen = 1;
+    ledBlue = 1;
+}
+
+static void event() {
+    ledBlue = 0;
+    ledRed = 1;
+    ledGreen = 1;
+}
+
+static void pulseEvent() {
+    event();
+    wait_ms(500);
+    good();
+}
+
+static void ledOff() {
+    ledBlue = 1;
+    ledRed = 1;
+    ledGreen = 1;
+}
+
+// Resource values for the Device Object
+struct MbedClientDevice device = {
+        "Manufacturer", // Manufacturer
+        "Type",         // Type
+        "ModelNumber",  // ModelNumber
+        "SerialNumber"  // SerialNumber
+        };
+
+class LedResource {
+public:
+    LedResource() {
+        ledObject = M2MInterfaceFactory::create_object("3311");
+        M2MObjectInstance *inst = ledObject->create_object_instance();
+
+        // An observable resource
+        M2MResource *onResource = inst->create_dynamic_resource("5850", "On/Off", M2MResourceInstance::BOOLEAN, true);
+        onResource->set_operation(M2MBase::GET_PUT_ALLOWED);
+        onResource->set_value(false);
+
+        // An multi-valued resource
+        M2MResource *dimmerResource = inst->create_dynamic_resource("5851", "Dimmer", M2MResourceInstance::BOOLEAN, false);
+        dimmerResource->set_operation(M2MBase::GET_PUT_ALLOWED);
+        dimmerResource->set_value(false);
+    }
+
+    ~LedResource() {
+    }
+
+    M2MObject *get_object() {
+        return ledObject;
+    }
+
+private:
+    M2MObject *ledObject;
+};
+
+static void cbButton()
+{
+    buttonPressed = true;
+    pulseEvent();
+}
+
+/* This example program for the u-blox C030 and C027 boards instantiates
+ * the mbedClient and runs it over a UbloxATCellularInterface connection to
+ * the mbed connector.
+ * Progress may be monitored with a serial terminal running at 9600 baud.
+ * The LED on the C030 board will turn green when this program is
+ * operating correctly, pulse blue when an mbedClient operation is completed
+ * and turn red if there is a failure.
+ *
+ * IMPORTANT to use this example you must first register with the mbed Connector:
+ *
+ * https://connector.mbed.com/
+ *
+ * ...using your mbed developer credentials and generate your own copy of the file
+ * security.h, replacing the empty one in this directory with yours and
+ * recompiling/downloading the code to your board.
+ */
+
+int main()
+{
+    UbloxATCellularInterface *interface = new UbloxATCellularInterface();
+    MbedClient *mbedClient = new MbedClient(device);
+    M2MObjectList objectList;
+    M2MSecurity *registerObject;
+    M2MDevice *deviceObject;
+    LedResource ledResource;
+    unsigned int seed;
+    size_t len;
+    InterruptIn userButton(SW0);
+
+    // Attach a function to the user button
+    userButton.rise(&cbButton);
+    
+    mbed_trace_init();
+    srand(seed);
+
+    // Randomize source port
+#ifdef TARGET_UBLOX_C030
+    mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof(seed), &len);
+#else
+    // NULL entropy, since C027 does not have a true random number generator
+    mbedtls_null_entropy_poll(NULL, (unsigned char *) &seed, sizeof(seed), &len);
+#endif
+
+    good();
+    printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
+    if (interface->init(PIN)) {
+        pulseEvent();
+        printf("Registered with network.\n");
+        
+        // Create endpoint interface to manage register and unregister
+        mbedClient->create_interface("coap://api.connector.mbed.com:5684", interface);
+
+        // Create objects of varying types, see simpleclient.h for more details on implementation.
+        registerObject = mbedClient->create_register_object(); // Server object specifying connector info
+        deviceObject = mbedClient->create_device_object();     // Device resources object
+
+        // Add objects to list
+        objectList.push_back(deviceObject);
+        objectList.push_back(ledResource.get_object());
+
+        // Set endpoint registration object
+        mbedClient->set_register_object(registerObject);
+
+        printf("Updating object registration in a loop (with a 30 second refresh period) until the user button is presed...\n");
+        interface->set_credentials(APN, USERNAME, PASSWORD);
+        while (!buttonPressed) {
+            // Make sure cellular is connected
+            if (interface->connect() == 0) {
+                pulseEvent();
+                printf("[Still] connected to packet network.\n");
+                if (mbedClient->register_successful()) {
+                    printf("Updating registration (follow progress at https://connector.mbed.com/#home)...\n");
+                    mbedClient->test_update_register();
+                } else {
+                    printf("Registering with connector (follow progress at https://connector.mbed.com/#home)...\n");
+                    mbedClient->test_register(registerObject, objectList);
+                }
+            } else {
+                bad();
+                printf("Failed to connect, will retry (have you checked that an antenna is plugged in and your APN is correct?)...\n");
+            }
+            Thread::wait(30000);
+            printf("[Checking if user button has been pressed]\n");
+        }
+        
+        pulseEvent();
+        printf("User button was pressed, stopping...\n");
+        mbedClient->test_unregister();
+        interface->disconnect();
+        interface->deinit();
+        ledOff();
+        printf("Stopped.\n");
+        M2MDevice::delete_instance();
+    } else {
+        bad();
+        printf("Unable to initialise the interface.\n");
+    }
+}
+
+// End Of File
\ No newline at end of file