Example program for simple-mbed-client, connecting a device to mbed Device Connector.

Dependencies:   simple-mbed-client

Fork of simple-mbed-client-example by Jan Jongboom

This is an example on how to connect to mbed Device Connector using simple-mbed-client. It can connect over Ethernet, WiFi (using an ESP8266 module or an ODIN board), Thread and 6LoWPAN.

After cloning this repository update `mbed_app.json` to reflect your connectivity method. For docs on configuring connectivity, see easy-connect.

End to end example

For an end-to-end example of using Simple mbed Client to connect devices to mbed Device Connector see Building an internet connected lighting system.

Entropy (or lack thereof)

On all platforms except the K64F and K22F the library is compiled without TLS entropy sources. This means that your code is inherently unsafe and should not be deployed to any production systems. To enable entropy, remove the MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY macros from mbed_app.json.

Revision:
38:fbbab77b54fb
Parent:
35:4b11d20d60e9
Child:
40:6c039d073c0b
--- a/source/main.cpp	Fri Dec 02 18:22:46 2016 +0800
+++ b/source/main.cpp	Tue Feb 07 13:02:40 2017 +0100
@@ -3,14 +3,12 @@
 #include "easy-connect.h"
 #include "simple-mbed-client.h"
 
-EventQueue eventQueue(32 * EVENTS_EVENT_SIZE);  // Event queue!
-
 SimpleMbedClient client;
 
 // Declare peripherals
-DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected
-DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF);  // LED that can be controlled through Connector
-InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector
+DigitalOut connectivityLed(LED2);                   // Blinks while connecting, turns solid when connected
+DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF);     // LED that can be controlled through Connector
+InterruptIn btn(MBED_CONF_APP_BUTTON);              // Button that sends it's count to Connector
 
 Ticker connectivityTicker;
 
@@ -43,33 +41,44 @@
     connectivityLed = BUILTIN_LED_OFF;
 
     // Parse the pattern string, and toggle the LED in that pattern
-    stringstream ss(pattern);
-    string item;
-    while(getline(ss, item, ':')) {
-        wait_ms(atoi((const char*)item.c_str()));
+    string s = static_cast<string>(pattern);
+    size_t i = 0;
+    size_t pos = s.find(':');
+    while (pos != string::npos) {
+        wait_ms(atoi(s.substr(i, pos - i).c_str()));
         augmentedLed = !augmentedLed;
+
+        i = ++pos;
+        pos = s.find(':', pos);
+
+        if (pos == string::npos) {
+            wait_ms(atoi(s.substr(i, s.length()).c_str()));
+            augmentedLed = !augmentedLed;
+        }
     }
 
     augmentedLed = BUILTIN_LED_OFF;
     connectivityLed = BUILTIN_LED_ON;
 }
 
+void play_from_cloud(void* args) {
+    // play is a blocking function, so run this on the events thread instead of the main thread
+    // (otherwise the device is not accessible while playing)
+    client.eventQueue()->call(&play, args);
+}
+
 int main() {
-    // Run a separate Thread to process events in
-    rtos::Thread et;
-    et.start(callback(&eventQueue, &EventQueue::dispatch_forever));
+    // SimpleClient gives us an event queue which we can use, running on a separate thread (see https://docs.mbed.com/docs/mbed-os-handbook/en/5.1/concepts/events/)
+    EventQueue* eventQueue = client.eventQueue();
 
     // Handle interrupts on the eventQueue - so we don't do things in interrupt context
-    btn.fall(eventQueue.event(&fall));
+    btn.fall(eventQueue->event(&fall));
 
     // Toggle the built-in LED every second (until we're connected, see `registered` function)
-    connectivityTicker.attach(eventQueue.event(toggleConnectivityLed), 1.0f);
-
-    // Keep alive ticker (every 25 seconds)
-    eventQueue.call_every(25000, callback(&client, &SimpleMbedClient::keep_alive));
+    connectivityTicker.attach(eventQueue->event(&toggleConnectivityLed), 1.0f);
 
     // Functions can be executed through mbed Device Connector via POST calls
-    client.define_function("led/0/play", &play);
+    client.define_function("led/0/play", &play_from_cloud);
 
     // Connect to the internet (see mbed_app.json for the connectivity method)
     NetworkInterface* network = easy_connect(true);
@@ -78,16 +87,11 @@
     }
 
     // Connect to mbed Device Connector
-    struct MbedClientOptions opts = client.get_default_options();
-    opts.ServerAddress = MBED_SERVER_ADDRESS;
-
+    struct MbedClientOptions opts = client.get_default_options(); // opts contains information like the DeviceType
     bool setup = client.setup(opts, network);
     if (!setup) {
         printf("Client setup failed\n");
         return 1;
     }
-
     client.on_registered(&registered);
-
-    while (1) {}
 }