Example program for simple-mbed-client, connecting a device to mbed Device Connector.
Dependencies: simple-mbed-client
Fork of simple-mbed-client-example by
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 16:71ec73ea9b6d, committed 2016-05-24
- Comitter:
- Jan Jongboom
- Date:
- Tue May 24 15:02:19 2016 -0500
- Parent:
- 15:27e27ae4a6b0
- Child:
- 17:6d69aa1b393f
- Commit message:
- Move from mbed.bld to mbed-os.lib
Changed in this revision
--- a/LWIPInterface.lib Tue May 24 16:15:08 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/NetworkSocketAPI/code/LWIPInterface/#ebba105e893b
--- a/MACROS.txt Tue May 24 16:15:08 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -YOTTA_CFG -YOTTA_CONFIG -TARGET_LIKE_MBED -TARGET_LIKE_CORTEX_M4 \ No newline at end of file
--- a/NetworkSocketAPI.lib Tue May 24 16:15:08 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#543799d6472e
--- a/main.cpp Tue May 24 16:15:08 2016 +0000
+++ b/main.cpp Tue May 24 15:02:19 2016 -0500
@@ -13,6 +13,7 @@
* 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
@@ -34,12 +35,12 @@
// play function, invoked thru mbed Client
void play(void* args) {
- printf("I'm gonna play a song!\n");
+ 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());
+ printf("Got a new pattern... %s\n", newPattern.c_str());
}
// Here we define some mbed Client resources
@@ -49,65 +50,63 @@
// Status callbacks if you're interested in that
void registered() {
- printf("registered\n");
- registeredLed = 0;
+ printf("registered\n");
+ registeredLed = 0;
}
void unregistered() {
- printf("unregistered\n");
- registeredLed = 1;
+ 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();
+ updates.release();
}
// normal function
void toggleLed() {
- led = !led;
+ led = !led;
}
int main() {
pc.baud(115200);
-
- printf("Hello world\n");
-
+
Ticker t;
t.attach(&toggleLed, 1.0f);
-
+
btn.fall(&fall);
-
- // I want C++11 lambdas!
+
// here we define functions that should live in the cloud
- client.define_function("music/0/play", &play);
+ client.define_function("song/0/play", &play);
- if (lwip.connect() != 0) { // connect to the internet
- printf("Connect to eth failed...\n");
- return 1;
- }
+ int connect = lwip.connect(); // can swap out for other network iface
+ if (connect != 0) {
+ printf("Connect to eth failed...\n");
+ return 1;
+ }
- printf("IP address %s\r\n", lwip.getIPAddress());
-
- bool setup = client.setup(&lwip); // start mbed client!
+ printf("IP address %s\r\n", lwip.get_ip_address());
+
+ bool setup = client.setup(&lwip); // start mbed client!
if (!setup) {
- printf("Setting up mbed_client failed...\n");
- return 1;
+ printf("Setting up mbed_client failed...\n");
+ return 1;
}
client.on_registered(®istered);
client.on_unregistered(&unregistered);
-
+
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));
- }
-
- // call keep_alive every now and then
- client.keep_alive();
+ 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));
+ }
+
+ // call keep_alive at least every 30s.
+ client.keep_alive();
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue May 24 15:02:19 2016 -0500 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#8a6e9c3a265ec0fa9ddf32343202c1e0ebeadd8f
--- a/mbed-rtos.lib Tue May 24 16:15:08 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
--- a/mbed.bld Tue May 24 16:15:08 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96 \ No newline at end of file
--- a/simple-mbed-client.lib Tue May 24 16:15:08 2016 +0000 +++ b/simple-mbed-client.lib Tue May 24 15:02:19 2016 -0500 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/sandbox/code/simple-mbed-client/#0a015df677a4 +https://developer.mbed.org/teams/sandbox/code/simple-mbed-client/#ce2322965a27
