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 14:ddc258abaaac, committed 2016-05-17
- Comitter:
- janjongboom
- Date:
- Tue May 17 00:15:40 2016 +0000
- Parent:
- 13:d4da5e6aa952
- Child:
- 15:27e27ae4a6b0
- Commit message:
- Use semaphore in interrupt context, and up value in main thread.
Changed in this revision
--- a/main.cpp Sun May 15 20:28:52 2016 +0000
+++ b/main.cpp Tue May 17 00:15:40 2016 +0000
@@ -29,14 +29,17 @@
DigitalOut registeredLed(LED_BLUE, 1);
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) {
- pc.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) {
- pc.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
@@ -46,17 +49,18 @@
// Status callbacks if you're interested in that
void registered() {
- pc.printf("registered\n");
+ printf("registered\n");
registeredLed = 0;
}
void unregistered() {
- pc.printf("unregistered\n");
+ printf("unregistered\n");
registeredLed = 1;
}
-// btn_count now lives in the cloud, so just manipulate it...
+// we cannot do blocking stuff here, or device will crash
+// release semaphore and update btn_count in the while() loop...
void fall() {
- btn_count = btn_count + 1;
+ updates.release();
}
// normal function
@@ -67,6 +71,8 @@
int main() {
pc.baud(115200);
+ printf("Hello world\n");
+
Ticker t;
t.attach(&toggleLed, 1.0f);
@@ -77,21 +83,31 @@
client.define_function("music/0/play", &play);
if (lwip.connect() != 0) { // connect to the internet
- pc.printf("Connect to eth failed...\n");
+ printf("Connect to eth failed...\n");
return 1;
}
- //lwipv4_socket_init(); // do I need this?
- pc.printf("IP address %s\r\n", lwip.getIPAddress());
+ printf("IP address %s\r\n", lwip.getIPAddress());
bool setup = client.setup(&lwip); // start mbed client!
if (!setup) {
printf("Setting up mbed_client failed...\n");
return 1;
}
-
+
client.on_registered(®istered);
client.on_unregistered(&unregistered);
- while (1) {}
+ 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();
+ }
}
--- a/security.h Sun May 15 20:28:52 2016 +0000 +++ b/security.h Tue May 17 00:15:40 2016 +0000 @@ -1,1 +1,55 @@ -#error "You need to get credentials from connector.mbed.com and paste them in this security.h file" +/* + * 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. + */ +#ifndef __SECURITY_H__ +#define __SECURITY_H__ + +#include <inttypes.h> + +#define MBED_DOMAIN "fc5bae18-72d8-4d16-a0d0-00608fa76464" +#define MBED_ENDPOINT_NAME "1f277937-4e63-4076-92af-7eb1f49e88f2" + +const uint8_t SER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+"tYmVkIHVzZXIxDTALBgNVBAcTBE91bHUxDTALBgNVBAgTBE91bHUxCzAJBgNVBA\r\n" +"YTAkZJMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqr0Uwojt8S8GhbkP/V1V7\r\n" +"60IJyscM9SC+B3TpYPIcolIwmp9IstxIJfNN5nbO3vS9lEfaut2AU6UvvBLUy6a\r\n" +"ADAMBggqhkjOPQQDAgUAA0gAMEUCIQC1RDl26vi2uMJjpoism0MRVLALW37Rs5O\r\n" +"pJCh4Wt4EOgIgKUUwryF9Pu4ve8MlydBnUnd9o2UQFe9P+i0/KFPZgHg=\r\n" +"-----END CERTIFICATE-----\r\n"; + +const uint8_t KEY[] = "-----BEGIN PRIVATE KEY-----\r\n" +"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgZr8pU60dkrbILqd9\r\n" +"dBy/Ov0f/0sBmVKZoCDrUhqv2CehRANCAASqvRTCiO3xLwaFuQ/9XVXvrQgnKxwz\r\n" +"1IL4HdOlg8hyiUjCan0iy3Egl803mds7e9L2UR9q63YBTpS+8EtTLpoA\r\n" +"-----END PRIVATE KEY-----\r\n"; + +#endif //__SECURITY_H__
--- a/simple-mbed-client.lib Sun May 15 20:28:52 2016 +0000 +++ b/simple-mbed-client.lib Tue May 17 00:15:40 2016 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/janjongboom/code/simple-mbed-client/#75015f627e89 +https://developer.mbed.org/users/janjongboom/code/simple-mbed-client/#0a015df677a4
