Example program for simple-mbed-client, connecting a device to mbed Device Connector.

Dependencies:   C12832 FXOS8700Q simple-mbed-client

Fork of simple-mbed-client-example by sandbox

Committer:
MACRUM
Date:
Sun Apr 16 01:53:48 2017 +0000
Revision:
45:cc5199df4b43
Parent:
44:5a1db9f98f5f
Use event API for sensor instead of thread

Who changed what in which revision?

UserRevisionLine numberNew 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"
MACRUM 44:5a1db9f98f5f 5 #include "C12832.h"
MACRUM 44:5a1db9f98f5f 6 #include "FXOS8700Q.h"
Jan Jongboom 35:4b11d20d60e9 7
MACRUM 45:cc5199df4b43 8 Serial pc(USBTX, USBRX);
MACRUM 44:5a1db9f98f5f 9 C12832 lcd(D11, D13, D12, D7, D10);
MACRUM 44:5a1db9f98f5f 10 I2C i2c(PTE25, PTE24);
MACRUM 44:5a1db9f98f5f 11 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
MACRUM 44:5a1db9f98f5f 12 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
MACRUM 44:5a1db9f98f5f 13
Jan Jongboom 35:4b11d20d60e9 14 SimpleMbedClient client;
Jan Jongboom 35:4b11d20d60e9 15
MACRUM 45:cc5199df4b43 16 // Declare peripherals
MACRUM 45:cc5199df4b43 17 DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected
MACRUM 45:cc5199df4b43 18 DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector
MACRUM 45:cc5199df4b43 19 InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector
Jan Jongboom 35:4b11d20d60e9 20
MACRUM 45:cc5199df4b43 21 Ticker connectivityTicker;
MACRUM 45:cc5199df4b43 22 Ticker readAccelTicker;
MACRUM 45:cc5199df4b43 23
MACRUM 45:cc5199df4b43 24 // Callback function when the pattern resource is updated
MACRUM 45:cc5199df4b43 25 void patternUpdated(string v)
MACRUM 45:cc5199df4b43 26 {
MACRUM 45:cc5199df4b43 27 pc.printf("New pattern: %s\n", v.c_str());
Jan Jongboom 35:4b11d20d60e9 28 }
Jan Jongboom 35:4b11d20d60e9 29
MACRUM 45:cc5199df4b43 30 void lcdTextUpdated(string v)
MACRUM 45:cc5199df4b43 31 {
MACRUM 44:5a1db9f98f5f 32 if (v.length() > 30) {
MACRUM 44:5a1db9f98f5f 33 v.erase(v.begin() + 30, v.end());
MACRUM 44:5a1db9f98f5f 34 }
MACRUM 45:cc5199df4b43 35 pc.printf("New text is %s\r\n", v.c_str());
MACRUM 44:5a1db9f98f5f 36
MACRUM 44:5a1db9f98f5f 37 lcd.cls();
MACRUM 44:5a1db9f98f5f 38 lcd.locate(0, 3);
MACRUM 44:5a1db9f98f5f 39 lcd.printf(v.c_str());
MACRUM 44:5a1db9f98f5f 40 }
MACRUM 44:5a1db9f98f5f 41
MACRUM 45:cc5199df4b43 42 // Define resources here. They act as normal variables, but are exposed to the internet...
Jan Jongboom 35:4b11d20d60e9 43 SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED);
Jan Jongboom 35:4b11d20d60e9 44 SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated);
MACRUM 44:5a1db9f98f5f 45 SimpleResourceString lcd_text = client.define_resource("lcd/0/text",
MACRUM 45:cc5199df4b43 46 "Hello from the cloud", M2MBase::GET_PUT_ALLOWED, true, &lcdTextUpdated);
Jan Jongboom 35:4b11d20d60e9 47
MACRUM 44:5a1db9f98f5f 48 SimpleResourceInt aX = client.define_resource("accel/0/x", 0, M2MBase::GET_ALLOWED);
MACRUM 44:5a1db9f98f5f 49 SimpleResourceInt aY = client.define_resource("accel/0/y", 0, M2MBase::GET_ALLOWED);
MACRUM 44:5a1db9f98f5f 50 SimpleResourceInt aZ = client.define_resource("accel/0/z", 0, M2MBase::GET_ALLOWED);
Jan Jongboom 35:4b11d20d60e9 51
MACRUM 45:cc5199df4b43 52 void readAccel()
MACRUM 45:cc5199df4b43 53 {
MACRUM 44:5a1db9f98f5f 54 motion_data_counts_t acc_data;
Jan Jongboom 38:fbbab77b54fb 55
MACRUM 45:cc5199df4b43 56 acc.getAxis(acc_data);
MACRUM 45:cc5199df4b43 57 aX = acc_data.x;
MACRUM 45:cc5199df4b43 58 aY = acc_data.y;
MACRUM 45:cc5199df4b43 59 aZ = acc_data.z;
MACRUM 45:cc5199df4b43 60
MACRUM 45:cc5199df4b43 61 pc.printf("ACC: X=%d Y=%d Z=%d\r\n", acc_data.x, acc_data.y, acc_data.z);
MACRUM 45:cc5199df4b43 62 }
Jan Jongboom 38:fbbab77b54fb 63
MACRUM 45:cc5199df4b43 64 void fall()
MACRUM 45:cc5199df4b43 65 {
MACRUM 45:cc5199df4b43 66 btn_count = btn_count + 1;
MACRUM 45:cc5199df4b43 67 pc.printf("Button count is now %d\r\n", static_cast<int>(btn_count));
MACRUM 45:cc5199df4b43 68 }
MACRUM 44:5a1db9f98f5f 69
MACRUM 45:cc5199df4b43 70 void toggleConnectivityLed()
MACRUM 45:cc5199df4b43 71 {
MACRUM 45:cc5199df4b43 72 connectivityLed = !connectivityLed;
MACRUM 45:cc5199df4b43 73 }
MACRUM 45:cc5199df4b43 74
MACRUM 45:cc5199df4b43 75 void registered()
MACRUM 45:cc5199df4b43 76 {
MACRUM 45:cc5199df4b43 77 pc.printf("Registered\r\n");
MACRUM 45:cc5199df4b43 78
MACRUM 45:cc5199df4b43 79 connectivityTicker.detach();
MACRUM 45:cc5199df4b43 80 connectivityLed = BUILTIN_LED_ON;
Jan Jongboom 35:4b11d20d60e9 81 }
Jan Jongboom 35:4b11d20d60e9 82
MACRUM 45:cc5199df4b43 83 void play(void* args)
MACRUM 45:cc5199df4b43 84 {
MACRUM 45:cc5199df4b43 85 connectivityLed = BUILTIN_LED_OFF;
Jan Jongboom 35:4b11d20d60e9 86
MACRUM 45:cc5199df4b43 87 // Parse the pattern string, and toggle the LED in that pattern
MACRUM 45:cc5199df4b43 88 string s = static_cast<string>(pattern);
MACRUM 45:cc5199df4b43 89 size_t i = 0;
MACRUM 45:cc5199df4b43 90 size_t pos = s.find(':');
MACRUM 45:cc5199df4b43 91 while (pos != string::npos) {
MACRUM 45:cc5199df4b43 92 wait_ms(atoi(s.substr(i, pos - i).c_str()));
MACRUM 45:cc5199df4b43 93 augmentedLed = !augmentedLed;
MACRUM 45:cc5199df4b43 94
MACRUM 45:cc5199df4b43 95 i = ++pos;
MACRUM 45:cc5199df4b43 96 pos = s.find(':', pos);
MACRUM 45:cc5199df4b43 97
MACRUM 45:cc5199df4b43 98 if (pos == string::npos) {
MACRUM 45:cc5199df4b43 99 wait_ms(atoi(s.substr(i, s.length()).c_str()));
MACRUM 45:cc5199df4b43 100 augmentedLed = !augmentedLed;
MACRUM 45:cc5199df4b43 101 }
MACRUM 45:cc5199df4b43 102 }
Jan Jongboom 35:4b11d20d60e9 103
MACRUM 45:cc5199df4b43 104 augmentedLed = BUILTIN_LED_OFF;
MACRUM 45:cc5199df4b43 105 connectivityLed = BUILTIN_LED_ON;
MACRUM 45:cc5199df4b43 106 }
MACRUM 45:cc5199df4b43 107
MACRUM 45:cc5199df4b43 108 int main()
MACRUM 45:cc5199df4b43 109 {
MACRUM 45:cc5199df4b43 110 pc.baud(115200);
MACRUM 45:cc5199df4b43 111 lcdTextUpdated(static_cast<string>(lcd_text).c_str());
MACRUM 45:cc5199df4b43 112 acc.enable();
MACRUM 45:cc5199df4b43 113
MACRUM 45:cc5199df4b43 114 // 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/)
MACRUM 45:cc5199df4b43 115 EventQueue* eventQueue = client.eventQueue();
Jan Jongboom 35:4b11d20d60e9 116
MACRUM 45:cc5199df4b43 117 // Handle interrupts on the eventQueue - so we don't do things in interrupt context
MACRUM 45:cc5199df4b43 118 btn.fall(eventQueue->event(&fall));
MACRUM 45:cc5199df4b43 119
MACRUM 45:cc5199df4b43 120 // Toggle the built-in LED every second (until we're connected, see `registered` function)
MACRUM 45:cc5199df4b43 121 connectivityTicker.attach(eventQueue->event(&toggleConnectivityLed), 1.0f);
Jan Jongboom 35:4b11d20d60e9 122
MACRUM 45:cc5199df4b43 123 readAccelTicker.attach(eventQueue->event(&readAccel), 1.0f);
MACRUM 44:5a1db9f98f5f 124
MACRUM 45:cc5199df4b43 125 // Functions can be executed through mbed Device Connector via POST calls (also defer to the event thread, so we never block)
MACRUM 45:cc5199df4b43 126 client.define_function("led/0/play", eventQueue->event(&play));
MACRUM 45:cc5199df4b43 127
MACRUM 45:cc5199df4b43 128 // Connect to the internet (see mbed_app.json for the connectivity method)
Jan Jongboom 35:4b11d20d60e9 129 NetworkInterface* network = easy_connect(true);
Jan Jongboom 35:4b11d20d60e9 130 if (!network) {
Jan Jongboom 35:4b11d20d60e9 131 return 1;
Jan Jongboom 35:4b11d20d60e9 132 }
Jan Jongboom 35:4b11d20d60e9 133
MACRUM 45:cc5199df4b43 134 // Connect to mbed Device Connector
MACRUM 45:cc5199df4b43 135 struct MbedClientOptions opts = client.get_default_options(); // opts contains information like the DeviceType
Jan Jongboom 35:4b11d20d60e9 136 bool setup = client.setup(opts, network);
Jan Jongboom 35:4b11d20d60e9 137 if (!setup) {
MACRUM 45:cc5199df4b43 138 pc.printf("Client setup failed\n");
Jan Jongboom 35:4b11d20d60e9 139 return 1;
Jan Jongboom 35:4b11d20d60e9 140 }
Jan Jongboom 35:4b11d20d60e9 141 client.on_registered(&registered);
Jan Jongboom 35:4b11d20d60e9 142 }