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
Diff: source/main.cpp
- Revision:
- 48:9df1210d36d2
- Parent:
- 47:9c2cb9e43446
--- a/source/main.cpp Tue Jan 16 09:47:17 2018 +0000 +++ b/source/main.cpp Sat Feb 03 14:27:44 2018 +0000 @@ -2,12 +2,14 @@ #include "security.h" #include "easy-connect.h" #include "simple-mbed-client.h" -#include "KX022.h" -#include "BM1383GLV.h" +#include "kx123.h" +#include "BM1383AGLV.h" Serial pc(USBTX, USBRX); -KX022 acc(I2C_SDA, I2C_SCL); -BM1383GLV sensor(I2C_SDA, I2C_SCL); +I2C i2c(I2C_SDA, I2C_SCL); +RegisterWriter i2c_rw(i2c); +KX123 acc(i2c_rw, KX123_SECONDARY_SLAVE_ADDRESS); +BM1383AGLV sensor(I2C_SDA, I2C_SCL); SimpleMbedClient client; @@ -29,39 +31,39 @@ // 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); -SimpleResourceInt press = client.define_resource("press/0/p", 0, M2MBase::GET_ALLOWED); -SimpleResourceInt temp = client.define_resource("press/0/t", 0, M2MBase::GET_ALLOWED); +// Define Rohm sensor module resources +//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); +//SimpleResourceInt press = client.define_resource("press/0/p", 0, M2MBase::GET_ALLOWED); +//SimpleResourceInt temp = client.define_resource("press/0/t", 0, M2MBase::GET_ALLOWED); SimpleResourceString accel = client.define_resource("accel/0/xyz", "0,0,0"); SimpleResourceString pressure = client.define_resource("press/0/pt", "0,0"); void readSensor() { char buf[40]; - int x, y, z; + float res[3]; if (connected == false) return; - aX = x = (int)(acc.getAccX() * 10000); - aY = y = (int)(acc.getAccY() * 10000); - aZ = z = (int)(acc.getAccZ() * 10000); - press = (int)(sensor.getPressure() * 100); - temp = (int)(sensor.getTemperature() * 100); + acc.getresults_g(res); + +// aX = (int)(res[0] * 10000); +// aY = (int)(res[1] * 10000); +// aZ = (int)(res[2] * 10000); +// press = (int)(sensor.getPressure() * 100); +// temp = (int)(sensor.getTemperature() * 100); - sprintf(buf, "%f,%f,%f", acc.getAccX(), acc.getAccY(), acc.getAccZ()); + sprintf(buf, "%f,%f,%f", res[0], res[1], res[2]); accel = buf; pc.printf("%s\n", buf); sprintf(buf, "%7.2f,%5.2f", sensor.getPressure(), sensor.getTemperature()); pressure = buf; - pc.printf("%s\n", buf); - + pc.printf("%s\n", buf); } void fall() @@ -112,6 +114,7 @@ int main() { pc.baud(115200); + acc.set_defaults(); // 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(); @@ -122,7 +125,7 @@ // 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); + readSensorTicker.attach(eventQueue->event(&readSensor), 3.0f); // Functions can be executed through mbed Device Connector via POST calls (also defer to the event thread, so we never block) client.define_function("led/0/play", eventQueue->event(&play));