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.

Files at this revision

API Documentation at this revision

Comitter:
Jan Jongboom
Date:
Thu Jun 30 12:33:00 2016 +0200
Parent:
16:71ec73ea9b6d
Child:
18:9aadd6ce6f87
Commit message:
Update to latest mbed-os, latest simple-mbed-client, and add easy-connect

Changed in this revision

easy-connect.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
simple-mbed-client.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/easy-connect.lib	Thu Jun 30 12:33:00 2016 +0200
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/easy-connect/#fbcc0ac33e944eb71feb80a1c40bf1baf5b2b61f
--- a/main.cpp	Tue May 24 15:02:19 2016 -0500
+++ b/main.cpp	Thu Jun 30 12:33:00 2016 +0200
@@ -1,96 +1,65 @@
-/*
- * Copyright (c) 2015 ARM Limited. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0
- * 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.
- */
-#include <string>
 #include "mbed.h"
-#include "security.h"				// security file from connector.mbed.com
-#include "LWIPInterface.h"			// using Ethernet for connectivity
-#include "simple-mbed-client.h"		// simple-mbed-client
+#include "security.h"
+#include "easy-connect.h"
+#include "simple-mbed-client.h"
 
-// So these lines are unique to connecting etc... but then it's just a normal program
-
-Serial pc(USBTX, USBRX);			// talk back to the computer
-LWIPInterface lwip;					// define the Ethernet interface
+Serial pc(USBTX, USBRX);
 SimpleMbedClient client;
 
-// Declare some peripherals here
 DigitalOut led(LED_RED);
-DigitalOut registeredLed(LED_BLUE, 1);
+DigitalOut blinkLed(LED_GREEN);
 InterruptIn btn(SW3);
-
-// We need a semaphore because we cannot update value in interrupt context
 Semaphore updates(0);
 
-// play function, invoked thru mbed Client
-void play(void* args) {
-    printf("I'm gonna play a song!\n");
-}
-
-// patternUpdated is called when the value of led/0/pattern changes
-void patternUpdated(string newPattern) {
-    printf("Got a new pattern... %s\n", newPattern.c_str());
+void patternUpdated(string v) {
+    printf("New pattern: %s\n", v.c_str());
 }
 
-// Here we define some mbed Client resources
-SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED, true);
-// need a callback when a PUT request comes in? Just pass in a callback
-SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500", &patternUpdated);
+SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED);
+SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated);
 
-// Status callbacks if you're interested in that
-void registered() {
-    printf("registered\n");
-    registeredLed = 0;
-}
-void unregistered() {
-    printf("unregistered\n");
-    registeredLed = 1;
-}
-
-// we cannot do blocking stuff here, or device will crash
-// release semaphore and update btn_count in the while() loop...
 void fall() {
     updates.release();
 }
 
-// normal function
 void toggleLed() {
     led = !led;
 }
 
+void registered() {
+    printf("registered\n");
+}
+void unregistered() {
+    printf("unregistered\n");
+}
+
+void play(void* args) {
+    stringstream ss(pattern);
+    string item;
+    while(getline(ss, item, ':')) {
+        wait_ms(atoi((const char*)item.c_str()));
+        blinkLed = !blinkLed;
+    }
+}
+
 int main() {
     pc.baud(115200);
 
+    btn.fall(&fall);
+
     Ticker t;
     t.attach(&toggleLed, 1.0f);
 
-    btn.fall(&fall);
+    client.define_function("led/0/play", &play);
 
-    // here we define functions that should live in the cloud
-    client.define_function("song/0/play", &play);
-
-    int connect = lwip.connect(); // can swap out for other network iface
-    if (connect != 0) {
-        printf("Connect to eth failed...\n");
+    NetworkInterface* network = easy_connect();
+    if (!network) {
         return 1;
     }
 
-    printf("IP address %s\r\n", lwip.get_ip_address());
-
-    bool setup = client.setup(&lwip); // start mbed client!
+    bool setup = client.setup(network);
     if (!setup) {
-        printf("Setting up mbed_client failed...\n");
+        printf("Client setup failed\n");
         return 1;
     }
 
@@ -99,14 +68,10 @@
 
     while (1) {
         int v = updates.wait(25000);
-
         if (v == 1) {
             btn_count = btn_count + 1;
-            // printf is a bit inflexible, static_cast to get the right value
-            printf("btn_count is now %d\n", static_cast<int>(btn_count));
+            printf("Button count is now %d\n", static_cast<int>(btn_count));
         }
-
-        // call keep_alive at least every 30s.
         client.keep_alive();
     }
 }
--- a/mbed-os.lib	Tue May 24 15:02:19 2016 -0500
+++ b/mbed-os.lib	Thu Jun 30 12:33:00 2016 +0200
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#8a6e9c3a265ec0fa9ddf32343202c1e0ebeadd8f
+https://github.com/ARMmbed/mbed-os/#60b8656d29dbd5a9d7f619f3cace1a7a1da96c7d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Jun 30 12:33:00 2016 +0200
@@ -0,0 +1,23 @@
+{
+    "config": {
+        "network-interface":{
+            "help": "options are ETHERNET,WIFI,MESH_LOWPAN_ND,MESH_THREAD",
+            "value": "ETHERNET"
+        },
+        "wifi-ssid": {
+            "help": "WiFi SSID",
+            "value": "\"SSID\""
+        },
+        "wifi-password": {
+            "help": "WiFi Password",
+            "value": "\"Password\""
+        }
+    },
+    "target_overrides": {
+        "*": {
+            "target.features_add": ["IPV6", "CLIENT", "IPV4"],
+            "mbed-client.reconnection-count": 3,
+            "mbed-client.reconnection-interval": 5
+        }
+    }
+}
--- a/simple-mbed-client.lib	Tue May 24 15:02:19 2016 -0500
+++ b/simple-mbed-client.lib	Thu Jun 30 12:33:00 2016 +0200
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/sandbox/code/simple-mbed-client/#ce2322965a27
+https://developer.mbed.org/teams/sandbox/code/simple-mbed-client/#0f9eae5739ddc64493d66b1896f36ab351acda2a