A simple interface to mbed Device Connector, where you just declare variables to push them to the cloud.
Dependents: Wifi_Get_Test_V1 simple-mbed-client-example simple-client-app-shield simple-sensor-client
Fork of simple-mbed-client by
TL;DR? See simple-mbed-client-example to get started immediately.
This library is a simpler interface to mbed Client, making it trivial to expose sensors, actuators and other variables to the cloud. It does not require you to change how you write your code. You can take any local variable, swap it out for a call to Simple Mbed Client, and the variable will automatically be synchronised with mbed Cloud.
For example, here's how you expose the value of a light sensor to the cloud:
SimpleMbedClient client; SimpleResourceInt light_value = client.define_resource("light/0/value", 0); // create the var AnalogIn light(A1); void read_light_sensor() { // light_value behaves just like a normal variable that you can read and write to! light_value = light.read_u16(); } // update every second Ticker t; t.attach(&read_light_sensor, 1.0f);
Setting up
First import this library to your project. As Simple Mbed Client also needs a way to talk to the outside world, you'll need a NetworkInterface
-object. The easiest way is by using the easy-connect library, so add that to your project as well. See the easy-connect docs on how to specify the connectivity method.
We also need a way of authenticating with mbed Cloud. For this we need a security certificate. Go to mbed Cloud, and select 'GET MY DEVICE SECURITY CREDENTIALS'. Save the certificate as security.h in your project folder.
Now we can initiate Simple Mbed Client and connect it to the internet.
#include "mbed.h" #include "security.h" #include "easy-connect.h" #include "simple-mbed-client.h" SimpleMbedClient client; DigitalOut led(LED1, 0); void registered() { led = 1; } int main() { NetworkInterface* network = connect_to_network(); // if connection failed, network will be NULL client.setup(network); // returns a bool, check if it's true client.on_registered(®istered); while (1) { wait_ms(25000); client.keep_alive(); } }
Defining variables
You can define a new variable by a call to client.define_resource
. This function takes five arguments:
path
- The URL on which your variable is exposed in mbed Cloud. Needs to be three (3) segments, split by a slash (/) in the form of 'sensor/0/value'. The second segment always needs to be numeric.defaultValue
- The default value of the variable. Needs to be either a string or an integer. Depending on the type that you pass in here the type of the variable is defined.operation
- Some variables might be read-only or write-only (seen from the cloud). Use the operation to define these constraints. It's of type M2MBase::Operation. Default is GET_PUT_ALLOWED.observable
- If set to false, cloud applications cannot subscribe to updates on this variable. Default is true.callback
- Function pointer which is called whenever the value of the variable is changed from the cloud.
The type returned by the function is either SimpleResourceInt
or SimpleResourceString
. You can assign and read from these variables like any normal local variable.
void name_updated(string new_value) { printf("Value is now %s\n", new_value.c_str()); } SimpleResourceString name = client.define_resource("device/0/name", "jan", M2MBase::GET_PUT_ALLOWED, true, &name_updated); // we can read and write to this variable, e.g.: stringstream ss; ss << name; // or name = "pietje"; // are all valid
Defining functions
You can define functions, which do not have a value, but can just be invoked from the cloud, by a call to client.define_function
. This function takes two arguments:
path
- The URL on which your variable is exposed in mbed Cloud. Needs to be three (3) segments, split by a slash (/) in the form of 'sensor/0/value'. The second segment always needs to be numeric.callback
- Function pointer which is invoked when the function is called. Takes in a pointer, which contains the data being passed in from the cloud.
void play(void* data) { if (data) { // data can be NULL! // cast it to something useful } } client.define_function("music/0/play", &play);
Accessing the underlying M2MResource
If you need access to the underlying M2MResource you can do so by calling get_resource
on a variable, or by calling client.get_resource
if it's a function.
SimpleResourceInt led = client.define_resource("led/0/value", true); client.define_function("led/0/toggle", &toggleLed); // now to get the resource M2MResource* ledResource = led.get_resource(); M2MResource* toggleResource = client.get_resource("led/0/toggle");
Printing variables
Unfortunately printf
is kind of dumb, and does not automatically cast the variables. If you want to print any of the Simple Mbed Client variables you'll need to cast yourself.
SimpleResourceInt led = client.define_resource("led/0/value", true); printf("Value is currently %d\n", static_cast<int>(led));
Event Queue
Simple Mbed Client uses an mbed-events EventQueue - running on a separate RTOS thread - to handle incoming events without blocking the main loop. Both the thread and event queue are created when initializing the library. You can override this behavior by providing your own event queue. In this case no thread is created.
EventQueue myQueue; SimpleMbedClient client(&myQueue);
You can also use the queue to process your own events, which is very useful when dealing with ISRs. The queue is accessible through the eventQueue()
function on the client object and returns a pointer to the queue.
SimpleMbedClient client; InterruptIn btn(D2); int main() { btn.fall(client.eventQueue()->event(&fall)); }
mbedtls_mbed_client_config.h@17:200624714d15, 2017-02-07 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Tue Feb 07 11:23:22 2017 +0100
- Revision:
- 17:200624714d15
Move mbedtls_mbed_client_config.h into simple-mbed-client, allow overriding of constructor and add ServerAddress macro to simple-mbed-client
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
17:200624714d15 | 1 | /** |
Jan Jongboom |
17:200624714d15 | 2 | * Minimal configuration for using mbedtls as part of mbed-client |
Jan Jongboom |
17:200624714d15 | 3 | * |
Jan Jongboom |
17:200624714d15 | 4 | * NOTE! This is an optimized, minimal configuration for mbed Client. |
Jan Jongboom |
17:200624714d15 | 5 | * We know it works with mbed Client but if you want to add more |
Jan Jongboom |
17:200624714d15 | 6 | * services/communications to the application yourself - please ensure |
Jan Jongboom |
17:200624714d15 | 7 | * you update this configuration accordingly. The default configuration |
Jan Jongboom |
17:200624714d15 | 8 | * can be found from mbedTLS Github: |
Jan Jongboom |
17:200624714d15 | 9 | * |
Jan Jongboom |
17:200624714d15 | 10 | * https://github.com/ARMmbed/mbedtls/blob/development/include/mbedtls/config.h |
Jan Jongboom |
17:200624714d15 | 11 | * |
Jan Jongboom |
17:200624714d15 | 12 | * |
Jan Jongboom |
17:200624714d15 | 13 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
Jan Jongboom |
17:200624714d15 | 14 | * SPDX-License-Identifier: Apache-2.0 |
Jan Jongboom |
17:200624714d15 | 15 | * |
Jan Jongboom |
17:200624714d15 | 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Jan Jongboom |
17:200624714d15 | 17 | * not use this file except in compliance with the License. |
Jan Jongboom |
17:200624714d15 | 18 | * You may obtain a copy of the License at |
Jan Jongboom |
17:200624714d15 | 19 | * |
Jan Jongboom |
17:200624714d15 | 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jan Jongboom |
17:200624714d15 | 21 | * |
Jan Jongboom |
17:200624714d15 | 22 | * Unless required by applicable law or agreed to in writing, software |
Jan Jongboom |
17:200624714d15 | 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Jan Jongboom |
17:200624714d15 | 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jan Jongboom |
17:200624714d15 | 25 | * See the License for the specific language governing permissions and |
Jan Jongboom |
17:200624714d15 | 26 | * limitations under the License. |
Jan Jongboom |
17:200624714d15 | 27 | * |
Jan Jongboom |
17:200624714d15 | 28 | * This file is part of mbed TLS (https://tls.mbed.org) |
Jan Jongboom |
17:200624714d15 | 29 | */ |
Jan Jongboom |
17:200624714d15 | 30 | |
Jan Jongboom |
17:200624714d15 | 31 | |
Jan Jongboom |
17:200624714d15 | 32 | #ifndef MBEDTLS_CUSTOM_CONFIG_H |
Jan Jongboom |
17:200624714d15 | 33 | #define MBEDTLS_CUSTOM_CONFIG_H |
Jan Jongboom |
17:200624714d15 | 34 | |
Jan Jongboom |
17:200624714d15 | 35 | /* Enable entropy for K64F and K22F. This means entropy is disabled for all other targets. */ |
Jan Jongboom |
17:200624714d15 | 36 | /* Do **NOT** deploy this code in production on other targets! */ |
Jan Jongboom |
17:200624714d15 | 37 | /* See https://tls.mbed.org/kb/how-to/add-entropy-sources-to-entropy-pool */ |
Jan Jongboom |
17:200624714d15 | 38 | #if defined(TARGET_K64F) || defined(TARGET_K22F) |
Jan Jongboom |
17:200624714d15 | 39 | #undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES |
Jan Jongboom |
17:200624714d15 | 40 | #undef MBEDTLS_TEST_NULL_ENTROPY |
Jan Jongboom |
17:200624714d15 | 41 | #endif |
Jan Jongboom |
17:200624714d15 | 42 | |
Jan Jongboom |
17:200624714d15 | 43 | /* System support */ |
Jan Jongboom |
17:200624714d15 | 44 | #define MBEDTLS_HAVE_ASM |
Jan Jongboom |
17:200624714d15 | 45 | |
Jan Jongboom |
17:200624714d15 | 46 | /* mbed TLS feature support */ |
Jan Jongboom |
17:200624714d15 | 47 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED |
Jan Jongboom |
17:200624714d15 | 48 | #define MBEDTLS_ECP_NIST_OPTIM |
Jan Jongboom |
17:200624714d15 | 49 | #define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH |
Jan Jongboom |
17:200624714d15 | 50 | #define MBEDTLS_SSL_PROTO_TLS1_2 |
Jan Jongboom |
17:200624714d15 | 51 | #define MBEDTLS_SSL_PROTO_DTLS |
Jan Jongboom |
17:200624714d15 | 52 | #define MBEDTLS_SSL_DTLS_ANTI_REPLAY |
Jan Jongboom |
17:200624714d15 | 53 | #define MBEDTLS_SSL_DTLS_HELLO_VERIFY |
Jan Jongboom |
17:200624714d15 | 54 | #define MBEDTLS_SSL_EXPORT_KEYS |
Jan Jongboom |
17:200624714d15 | 55 | |
Jan Jongboom |
17:200624714d15 | 56 | /* mbed TLS modules */ |
Jan Jongboom |
17:200624714d15 | 57 | #define MBEDTLS_AES_C |
Jan Jongboom |
17:200624714d15 | 58 | #define MBEDTLS_ASN1_PARSE_C |
Jan Jongboom |
17:200624714d15 | 59 | #define MBEDTLS_ASN1_WRITE_C |
Jan Jongboom |
17:200624714d15 | 60 | #define MBEDTLS_BIGNUM_C |
Jan Jongboom |
17:200624714d15 | 61 | #define MBEDTLS_CIPHER_C |
Jan Jongboom |
17:200624714d15 | 62 | #define MBEDTLS_CTR_DRBG_C |
Jan Jongboom |
17:200624714d15 | 63 | #define MBEDTLS_ECP_C |
Jan Jongboom |
17:200624714d15 | 64 | #define MBEDTLS_ENTROPY_C |
Jan Jongboom |
17:200624714d15 | 65 | #define MBEDTLS_MD_C |
Jan Jongboom |
17:200624714d15 | 66 | #define MBEDTLS_OID_C |
Jan Jongboom |
17:200624714d15 | 67 | #define MBEDTLS_PK_C |
Jan Jongboom |
17:200624714d15 | 68 | #define MBEDTLS_PK_PARSE_C |
Jan Jongboom |
17:200624714d15 | 69 | #define MBEDTLS_SHA256_C |
Jan Jongboom |
17:200624714d15 | 70 | #define MBEDTLS_SSL_COOKIE_C |
Jan Jongboom |
17:200624714d15 | 71 | #define MBEDTLS_SSL_CLI_C |
Jan Jongboom |
17:200624714d15 | 72 | #define MBEDTLS_SSL_SRV_C |
Jan Jongboom |
17:200624714d15 | 73 | #define MBEDTLS_SSL_TLS_C |
Jan Jongboom |
17:200624714d15 | 74 | |
Jan Jongboom |
17:200624714d15 | 75 | // XXX mbedclient needs these: mbedtls_x509_crt_free, mbedtls_x509_crt_init, mbedtls_x509_crt_parse |
Jan Jongboom |
17:200624714d15 | 76 | #define MBEDTLS_X509_USE_C |
Jan Jongboom |
17:200624714d15 | 77 | #define MBEDTLS_X509_CRT_PARSE_C |
Jan Jongboom |
17:200624714d15 | 78 | |
Jan Jongboom |
17:200624714d15 | 79 | // XXX: clean these up!! |
Jan Jongboom |
17:200624714d15 | 80 | #define MBEDTLS_SHA512_C |
Jan Jongboom |
17:200624714d15 | 81 | #define MBEDTLS_ECDH_C |
Jan Jongboom |
17:200624714d15 | 82 | #define MBEDTLS_GCM_C |
Jan Jongboom |
17:200624714d15 | 83 | |
Jan Jongboom |
17:200624714d15 | 84 | #define MBEDTLS_ECDH_C |
Jan Jongboom |
17:200624714d15 | 85 | #define MBEDTLS_ECDSA_C |
Jan Jongboom |
17:200624714d15 | 86 | #define MBEDTLS_X509_CRT_PARSE_C |
Jan Jongboom |
17:200624714d15 | 87 | |
Jan Jongboom |
17:200624714d15 | 88 | // Remove RSA, save 20KB at total |
Jan Jongboom |
17:200624714d15 | 89 | #undef MBEDTLS_RSA_C |
Jan Jongboom |
17:200624714d15 | 90 | #undef MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED |
Jan Jongboom |
17:200624714d15 | 91 | |
Jan Jongboom |
17:200624714d15 | 92 | // Remove error messages, save 10KB of ROM |
Jan Jongboom |
17:200624714d15 | 93 | #undef MBEDTLS_ERROR_C |
Jan Jongboom |
17:200624714d15 | 94 | |
Jan Jongboom |
17:200624714d15 | 95 | // Remove selftesting and save 11KB of ROM |
Jan Jongboom |
17:200624714d15 | 96 | #undef MBEDTLS_SELF_TEST |
Jan Jongboom |
17:200624714d15 | 97 | |
Jan Jongboom |
17:200624714d15 | 98 | // Reduces ROM size by 30 kB |
Jan Jongboom |
17:200624714d15 | 99 | #undef MBEDTLS_ERROR_STRERROR_DUMMY |
Jan Jongboom |
17:200624714d15 | 100 | #undef MBEDTLS_VERSION_FEATURES |
Jan Jongboom |
17:200624714d15 | 101 | #undef MBEDTLS_DEBUG_C |
Jan Jongboom |
17:200624714d15 | 102 | |
Jan Jongboom |
17:200624714d15 | 103 | // needed for parsing the certificates |
Jan Jongboom |
17:200624714d15 | 104 | #define MBEDTLS_PEM_PARSE_C |
Jan Jongboom |
17:200624714d15 | 105 | // dep of the previous |
Jan Jongboom |
17:200624714d15 | 106 | #define MBEDTLS_BASE64_C |
Jan Jongboom |
17:200624714d15 | 107 | |
Jan Jongboom |
17:200624714d15 | 108 | // Reduce IO buffer to save RAM, default is 16KB |
Jan Jongboom |
17:200624714d15 | 109 | #define MBEDTLS_SSL_MAX_CONTENT_LEN 2048 |
Jan Jongboom |
17:200624714d15 | 110 | |
Jan Jongboom |
17:200624714d15 | 111 | // define to save 8KB RAM at the expense of ROM |
Jan Jongboom |
17:200624714d15 | 112 | #undef MBEDTLS_AES_ROM_TABLES |
Jan Jongboom |
17:200624714d15 | 113 | |
Jan Jongboom |
17:200624714d15 | 114 | // Save ROM and a few bytes of RAM by specifying our own ciphersuite list |
Jan Jongboom |
17:200624714d15 | 115 | #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 |
Jan Jongboom |
17:200624714d15 | 116 | |
Jan Jongboom |
17:200624714d15 | 117 | #include "mbedtls/check_config.h" |
Jan Jongboom |
17:200624714d15 | 118 | |
Jan Jongboom |
17:200624714d15 | 119 | #endif /* MBEDTLS_CUSTOM_CONFIG_H */ |