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.

diff -r 5ee408a26231 -r 6c039d073c0b source/main.cpp
--- a/source/main.cpp	Tue Feb 07 13:03:04 2017 +0100
+++ b/source/main.cpp	Tue Feb 07 17:22:37 2017 +0100
@@ -61,12 +61,6 @@
     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() {
     // 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();
@@ -77,8 +71,8 @@
     // Toggle the built-in LED every second (until we're connected, see `registered` function)
     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_from_cloud);
+    // Functions can be executed through mbed Device Connector via POST calls (also defer to the event thread, so we never block)
+    client.define_function("led/0/play", eventQueue->event(&play));
     // Connect to the internet (see mbed_app.json for the connectivity method)
     NetworkInterface* network = easy_connect(true);