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.
source/main.cpp@38:fbbab77b54fb, 2017-02-07 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Tue Feb 07 13:02:40 2017 +0100
- Revision:
- 38:fbbab77b54fb
- Parent:
- 35:4b11d20d60e9
- Child:
- 40:6c039d073c0b
Bump mbed-os, easy-connect and simple-mbed-client. Get rid of stringstream. Work by default on devices without entropy sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
35:4b11d20d60e9 | 1 | #include "mbed.h" |
Jan Jongboom |
35:4b11d20d60e9 | 2 | #include "security.h" |
Jan Jongboom |
35:4b11d20d60e9 | 3 | #include "easy-connect.h" |
Jan Jongboom |
35:4b11d20d60e9 | 4 | #include "simple-mbed-client.h" |
Jan Jongboom |
35:4b11d20d60e9 | 5 | |
Jan Jongboom |
35:4b11d20d60e9 | 6 | SimpleMbedClient client; |
Jan Jongboom |
35:4b11d20d60e9 | 7 | |
Jan Jongboom |
35:4b11d20d60e9 | 8 | // Declare peripherals |
Jan Jongboom |
38:fbbab77b54fb | 9 | DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected |
Jan Jongboom |
38:fbbab77b54fb | 10 | DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector |
Jan Jongboom |
38:fbbab77b54fb | 11 | InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector |
Jan Jongboom |
35:4b11d20d60e9 | 12 | |
Jan Jongboom |
35:4b11d20d60e9 | 13 | Ticker connectivityTicker; |
Jan Jongboom |
35:4b11d20d60e9 | 14 | |
Jan Jongboom |
35:4b11d20d60e9 | 15 | // Callback function when the pattern resource is updated |
Jan Jongboom |
35:4b11d20d60e9 | 16 | void patternUpdated(string v) { |
Jan Jongboom |
35:4b11d20d60e9 | 17 | printf("New pattern: %s\n", v.c_str()); |
Jan Jongboom |
35:4b11d20d60e9 | 18 | } |
Jan Jongboom |
35:4b11d20d60e9 | 19 | |
Jan Jongboom |
35:4b11d20d60e9 | 20 | // Define resources here. They act as normal variables, but are exposed to the internet... |
Jan Jongboom |
35:4b11d20d60e9 | 21 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED); |
Jan Jongboom |
35:4b11d20d60e9 | 22 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated); |
Jan Jongboom |
35:4b11d20d60e9 | 23 | |
Jan Jongboom |
35:4b11d20d60e9 | 24 | void fall() { |
Jan Jongboom |
35:4b11d20d60e9 | 25 | btn_count = btn_count + 1; |
Jan Jongboom |
35:4b11d20d60e9 | 26 | printf("Button count is now %d\r\n", static_cast<int>(btn_count)); |
Jan Jongboom |
35:4b11d20d60e9 | 27 | } |
Jan Jongboom |
35:4b11d20d60e9 | 28 | |
Jan Jongboom |
35:4b11d20d60e9 | 29 | void toggleConnectivityLed() { |
Jan Jongboom |
35:4b11d20d60e9 | 30 | connectivityLed = !connectivityLed; |
Jan Jongboom |
35:4b11d20d60e9 | 31 | } |
Jan Jongboom |
35:4b11d20d60e9 | 32 | |
Jan Jongboom |
35:4b11d20d60e9 | 33 | void registered() { |
Jan Jongboom |
35:4b11d20d60e9 | 34 | printf("Registered\r\n"); |
Jan Jongboom |
35:4b11d20d60e9 | 35 | |
Jan Jongboom |
35:4b11d20d60e9 | 36 | connectivityTicker.detach(); |
Jan Jongboom |
35:4b11d20d60e9 | 37 | connectivityLed = BUILTIN_LED_ON; |
Jan Jongboom |
35:4b11d20d60e9 | 38 | } |
Jan Jongboom |
35:4b11d20d60e9 | 39 | |
Jan Jongboom |
35:4b11d20d60e9 | 40 | void play(void* args) { |
Jan Jongboom |
35:4b11d20d60e9 | 41 | connectivityLed = BUILTIN_LED_OFF; |
Jan Jongboom |
35:4b11d20d60e9 | 42 | |
Jan Jongboom |
35:4b11d20d60e9 | 43 | // Parse the pattern string, and toggle the LED in that pattern |
Jan Jongboom |
38:fbbab77b54fb | 44 | string s = static_cast<string>(pattern); |
Jan Jongboom |
38:fbbab77b54fb | 45 | size_t i = 0; |
Jan Jongboom |
38:fbbab77b54fb | 46 | size_t pos = s.find(':'); |
Jan Jongboom |
38:fbbab77b54fb | 47 | while (pos != string::npos) { |
Jan Jongboom |
38:fbbab77b54fb | 48 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
Jan Jongboom |
35:4b11d20d60e9 | 49 | augmentedLed = !augmentedLed; |
Jan Jongboom |
38:fbbab77b54fb | 50 | |
Jan Jongboom |
38:fbbab77b54fb | 51 | i = ++pos; |
Jan Jongboom |
38:fbbab77b54fb | 52 | pos = s.find(':', pos); |
Jan Jongboom |
38:fbbab77b54fb | 53 | |
Jan Jongboom |
38:fbbab77b54fb | 54 | if (pos == string::npos) { |
Jan Jongboom |
38:fbbab77b54fb | 55 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
Jan Jongboom |
38:fbbab77b54fb | 56 | augmentedLed = !augmentedLed; |
Jan Jongboom |
38:fbbab77b54fb | 57 | } |
Jan Jongboom |
35:4b11d20d60e9 | 58 | } |
Jan Jongboom |
35:4b11d20d60e9 | 59 | |
Jan Jongboom |
35:4b11d20d60e9 | 60 | augmentedLed = BUILTIN_LED_OFF; |
Jan Jongboom |
35:4b11d20d60e9 | 61 | connectivityLed = BUILTIN_LED_ON; |
Jan Jongboom |
35:4b11d20d60e9 | 62 | } |
Jan Jongboom |
35:4b11d20d60e9 | 63 | |
Jan Jongboom |
38:fbbab77b54fb | 64 | void play_from_cloud(void* args) { |
Jan Jongboom |
38:fbbab77b54fb | 65 | // play is a blocking function, so run this on the events thread instead of the main thread |
Jan Jongboom |
38:fbbab77b54fb | 66 | // (otherwise the device is not accessible while playing) |
Jan Jongboom |
38:fbbab77b54fb | 67 | client.eventQueue()->call(&play, args); |
Jan Jongboom |
38:fbbab77b54fb | 68 | } |
Jan Jongboom |
38:fbbab77b54fb | 69 | |
Jan Jongboom |
35:4b11d20d60e9 | 70 | int main() { |
Jan Jongboom |
38:fbbab77b54fb | 71 | // 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/) |
Jan Jongboom |
38:fbbab77b54fb | 72 | EventQueue* eventQueue = client.eventQueue(); |
Jan Jongboom |
35:4b11d20d60e9 | 73 | |
Jan Jongboom |
35:4b11d20d60e9 | 74 | // Handle interrupts on the eventQueue - so we don't do things in interrupt context |
Jan Jongboom |
38:fbbab77b54fb | 75 | btn.fall(eventQueue->event(&fall)); |
Jan Jongboom |
35:4b11d20d60e9 | 76 | |
Jan Jongboom |
35:4b11d20d60e9 | 77 | // Toggle the built-in LED every second (until we're connected, see `registered` function) |
Jan Jongboom |
38:fbbab77b54fb | 78 | connectivityTicker.attach(eventQueue->event(&toggleConnectivityLed), 1.0f); |
Jan Jongboom |
35:4b11d20d60e9 | 79 | |
Jan Jongboom |
35:4b11d20d60e9 | 80 | // Functions can be executed through mbed Device Connector via POST calls |
Jan Jongboom |
38:fbbab77b54fb | 81 | client.define_function("led/0/play", &play_from_cloud); |
Jan Jongboom |
35:4b11d20d60e9 | 82 | |
Jan Jongboom |
35:4b11d20d60e9 | 83 | // Connect to the internet (see mbed_app.json for the connectivity method) |
Jan Jongboom |
35:4b11d20d60e9 | 84 | NetworkInterface* network = easy_connect(true); |
Jan Jongboom |
35:4b11d20d60e9 | 85 | if (!network) { |
Jan Jongboom |
35:4b11d20d60e9 | 86 | return 1; |
Jan Jongboom |
35:4b11d20d60e9 | 87 | } |
Jan Jongboom |
35:4b11d20d60e9 | 88 | |
Jan Jongboom |
35:4b11d20d60e9 | 89 | // Connect to mbed Device Connector |
Jan Jongboom |
38:fbbab77b54fb | 90 | struct MbedClientOptions opts = client.get_default_options(); // opts contains information like the DeviceType |
Jan Jongboom |
35:4b11d20d60e9 | 91 | bool setup = client.setup(opts, network); |
Jan Jongboom |
35:4b11d20d60e9 | 92 | if (!setup) { |
Jan Jongboom |
35:4b11d20d60e9 | 93 | printf("Client setup failed\n"); |
Jan Jongboom |
35:4b11d20d60e9 | 94 | return 1; |
Jan Jongboom |
35:4b11d20d60e9 | 95 | } |
Jan Jongboom |
35:4b11d20d60e9 | 96 | client.on_registered(®istered); |
Jan Jongboom |
35:4b11d20d60e9 | 97 | } |