Example program for simple-mbed-client, connecting a device to mbed Device Connector.
Dependencies: BM1383AGLV simple-mbed-client RegisterWriter kionix-kx123-driver
Fork of simple-client-app-shield by
source/main.cpp
- Committer:
- MACRUM
- Date:
- 2018-01-04
- Revision:
- 46:06913c7703ef
- Parent:
- 45:cc5199df4b43
- Child:
- 47:9c2cb9e43446
File content as of revision 46:06913c7703ef:
#include "mbed.h" #include "security.h" #include "easy-connect.h" #include "simple-mbed-client.h" #include "KX022.h" Serial pc(USBTX, USBRX); KX022 acc(I2C_SDA, I2C_SCL); SimpleMbedClient client; // Declare peripherals DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector Ticker connectivityTicker; Ticker readSensorTicker; // Callback function when the pattern resource is updated void patternUpdated(string v) { pc.printf("New pattern: %s\n", v.c_str()); } // Define resources here. They act as normal variables, but are exposed to the internet... 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); //SimpleResourceString lcd_text = client.define_resource("lcd/0/text", // "Hello from the cloud", M2MBase::GET_PUT_ALLOWED, true, &lcdTextUpdated); SimpleResourceInt aX = client.define_resource("accel/0/x", 0, M2MBase::GET_ALLOWED); SimpleResourceInt aY = client.define_resource("accel/0/y", 0, M2MBase::GET_ALLOWED); SimpleResourceInt aZ = client.define_resource("accel/0/z", 0, M2MBase::GET_ALLOWED); void readSensor() { int x, y, z; aX = x = (int)(acc.getAccX() * 10000); aY = y = (int)(acc.getAccY() * 10000); aZ = z = (int)(acc.getAccZ() * 10000); pc.printf("ACC: X=%ld Y=%ld Z=%ld\r\n", x, y, z); } void fall() { btn_count = btn_count + 1; pc.printf("Button count is now %d\r\n", static_cast<int>(btn_count)); } void toggleConnectivityLed() { connectivityLed = !connectivityLed; } void registered() { pc.printf("Registered\r\n"); connectivityTicker.detach(); connectivityLed = BUILTIN_LED_ON; } void play(void* args) { connectivityLed = BUILTIN_LED_OFF; // Parse the pattern string, and toggle the LED in that pattern string s = static_cast<string>(pattern); size_t i = 0; size_t pos = s.find(':'); while (pos != string::npos) { wait_ms(atoi(s.substr(i, pos - i).c_str())); augmentedLed = !augmentedLed; i = ++pos; pos = s.find(':', pos); if (pos == string::npos) { wait_ms(atoi(s.substr(i, s.length()).c_str())); augmentedLed = !augmentedLed; } } augmentedLed = BUILTIN_LED_OFF; connectivityLed = BUILTIN_LED_ON; } int main() { pc.baud(115200); // 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/) EventQueue* eventQueue = client.eventQueue(); // Handle interrupts on the eventQueue - so we don't do things in interrupt context btn.fall(eventQueue->event(&fall)); // Toggle the built-in LED every second (until we're connected, see `registered` function) connectivityTicker.attach(eventQueue->event(&toggleConnectivityLed), 1.0f); readSensorTicker.attach(eventQueue->event(&readSensor), 1.0f); // Connect to the internet (see mbed_app.json for the connectivity method) NetworkInterface* network = easy_connect(true); if (!network) { return 1; } // Connect to mbed Device Connector struct MbedClientOptions opts = client.get_default_options(); // opts contains information like the DeviceType bool setup = client.setup(opts, network); if (!setup) { pc.printf("Client setup failed\n"); return 1; } client.on_registered(®istered); }