Example node for Yodiwo's Plegma API
Dependencies: EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL
Revision 7:11ff316c37ba, committed 2015-09-28
- Comitter:
- mitsarionas
- Date:
- Mon Sep 28 09:11:03 2015 +0000
- Parent:
- 6:94a7e44a0679
- Child:
- 8:66d34592c1ad
- Commit message:
- final renames etc.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.json Mon Sep 28 09:11:03 2015 +0000 @@ -0,0 +1,18 @@ +{ + "ActiveID": 0, + "Configs": + [ + { + "MqttBrokerCertFile": "", + "MqttBrokerHostname": "cyan.yodiwo.com", + "MqttBrokerPort": 1883, + "Name": "YoPlegma", + "PairingServerUrl": "http://cyan.yodiwo.com/pairing/", + "Uuid": "1337YoPlegma", + "WebPort": 80, + "YpchannelServer": "cyan.yodiwo.com", + "YpchannelServerPort": 5555 + } + ] +} +
--- a/main.cpp Mon Sep 28 09:03:01 2015 +0000 +++ b/main.cpp Mon Sep 28 09:11:03 2015 +0000 @@ -1,3 +1,11 @@ +/* + * YoPlegma (codename Nodas) + * Yodiwo Plegma API example node + * + * NOTES: + * a configuration file, named config.json (sample exists in this project) needs to be placed in the root of the SD card + */ + #include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" @@ -9,7 +17,7 @@ #include "pairing_http_handler.h" #include "config.h" #include "yodiwo_functions.h" -#include "nodas_things.h" +#include "yoplegma_things.h" #include "mqtt_helpers.h" #include "yodiwo_helpers.h"
--- a/nodas_things.cpp Mon Sep 28 09:03:01 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,262 +0,0 @@ -#include "mbed.h" -#include "rtos.h" - -#include <stdint.h> - -#include "nodas_things.h" -#include "yodiwo_functions.h" -#include "jsmn.h" -#include "yodiwo_api.h" -#include "yodiwo_helpers.h" - -///////////////////////////////// LEDS -DigitalOut redled(LED1); -DigitalOut greenled(LED2); -DigitalOut blueled(LED3); - -///////////////////////////////// THINGS -Yodiwo_Plegma_Port_t button_port; -Yodiwo_Plegma_Port_t axel_port; -Yodiwo_Plegma_Port_t _led_ports[3]; - -Yodiwo_Plegma_Thing_t *button; -Yodiwo_Plegma_Thing_t *axel; -Yodiwo_Plegma_Thing_t *led; - -Yodiwo_Plegma_Thing_t _things[3]; -Array_Yodiwo_Plegma_Thing_t things; - - -int shaken_event() -{ - Yodiwo_Plegma_PortEvent_t event; - Array_Yodiwo_Plegma_PortEvent_t array_events; - - event.PortKey = axel->Ports.elems[0].PortKey; - event.State = "True"; - event.RevNum = 1; - - array_events.num = 1; - array_events.elems = &event; - - return portevents(&array_events); -} - -int button_event(int buttonId, bool pressed) -{ - printf("button %d: %s\n", buttonId, (pressed) ? "pressed" : "released"); - - Yodiwo_Plegma_PortEvent_t event; - Array_Yodiwo_Plegma_PortEvent_t array_events; - - event.PortKey = button->Ports.elems[0].PortKey; - event.State = (char *)((pressed) ? "True" : "False"); - event.RevNum = 1; - - array_events.num = 1; - array_events.elems = &event; - - return portevents(&array_events); -} - -void led_event(DigitalOut &led, Yodiwo_Plegma_PortEvent_t *event) -{ - if (!strcmp(event->State, "on") || !strcmp(event->State, "1") || !strcmp(event->State, "true") - || !strcmp(event->State, "True") || !strcmp(event->State, "On")) { - printf("ON\n"); - led = 0; - } else { - printf("OFF\n"); - led = 1; - } -} - -int handle_red_led(Yodiwo_Plegma_PortEvent_t *event) -{ - printf("event for red led\n"); - led_event(redled, event); - return 0; -} - -int handle_green_led(Yodiwo_Plegma_PortEvent_t *event) -{ - printf("event for green led\n"); - led_event(greenled, event); - return 0; -} - -int handle_blue_led(Yodiwo_Plegma_PortEvent_t *event) -{ - printf("event for blue led\n"); - led_event(blueled, event); - return 0; -} - -void initialize_things(char *nodeKey) -{ - // init thing pointers - button = &_things[0]; - axel = &_things[1]; - led = &_things[2]; - - //BUTTON - button_port.ConfFlags = Yodiwo_ePortConf_None; //why not - button_port.Description = "the state of the freedom button"; - button_port.Name = "BTN State"; - button_port.Type = Yodiwo_ePortType_Boolean; - button_port.State = "False"; - button_port.RevNum = 1; - button_port.ioDirection = Yodiwo_ioPortDirection_Output; - button_port.PortKey = ""; - - button->ThingKey = ""; - button->Name = "FRDM Button"; - button->Config.num = 0; - button->Config.elems = NULL; - button->Ports.num = 1; - button->Ports.elems = &button_port; - button->Type = ""; - button->BlockType = ""; - button->UIHints.Description = ""; - button->UIHints.IconURI = "/Content/VirtualGateway/img/icon-thing-genericbutton.png"; - - //AXEL - axel_port.ConfFlags = Yodiwo_ePortConf_IsTrigger; //why not - axel_port.Description = "triggered when the shaker is shaked"; - axel_port.Name = "SHAKER SHAKED"; - axel_port.Type = Yodiwo_ePortType_Boolean; - axel_port.State = ""; - axel_port.RevNum = 1; - axel_port.ioDirection = Yodiwo_ioPortDirection_Output; - axel_port.PortKey = ""; - - axel->ThingKey = ""; - axel->Name = "FRDM SHAKER"; - axel->Config.num = 0; - axel->Config.elems = NULL; - axel->Ports.num = 1; - axel->Ports.elems = &axel_port; - axel->Type = ""; - axel->BlockType = ""; - axel->UIHints.Description = ""; - axel->UIHints.IconURI = "/Content/VirtualGateway/img/accelerometer.jpg"; - - //LED - _led_ports[0].ConfFlags = Yodiwo_ePortConf_None; //why not - _led_ports[0].Description = "RED LED"; - _led_ports[0].Name = "RED LED"; - _led_ports[0].Type = Yodiwo_ePortType_Boolean; - _led_ports[0].State = "False"; - _led_ports[0].RevNum = 1; - _led_ports[0].ioDirection = Yodiwo_ioPortDirection_Input; - _led_ports[0].PortKey = ""; - - _led_ports[1].ConfFlags = Yodiwo_ePortConf_None; //why not - _led_ports[1].Description = "GREEN LED"; - _led_ports[1].Name = "GREEN LED"; - _led_ports[0].Type = Yodiwo_ePortType_Boolean; - _led_ports[1].State = "off"; - _led_ports[1].RevNum = 1; - _led_ports[1].ioDirection = Yodiwo_ioPortDirection_Input; - _led_ports[1].PortKey = ""; - - _led_ports[2].ConfFlags = Yodiwo_ePortConf_None; //why not - _led_ports[2].Description = "BLUE LED"; - _led_ports[2].Name = "BLUE LED"; - _led_ports[0].Type = Yodiwo_ePortType_Boolean; - _led_ports[2].State = "off"; - _led_ports[2].RevNum = 1; - _led_ports[2].ioDirection = Yodiwo_ioPortDirection_Input; - _led_ports[2].PortKey = ""; - - led->ThingKey = ""; - led->Name = "FRDM LED"; - led->Config.num = 0; - led->Config.elems = NULL; - led->Ports.num = 3; - led->Ports.elems = _led_ports; - led->Type = ""; - led->BlockType = ""; - led->UIHints.Description = ""; - led->UIHints.IconURI = "/Content/VirtualGateway/img/icon-thing-genericlight.png"; - - things.num = 3; - things.elems = _things; - - if (nodeKey != NULL) { - int r; - r = fill_Thing_Keys(&things.elems[0], nodeKey, 1); - r = fill_Thing_Keys(&things.elems[1], nodeKey, 2); - r = fill_Thing_Keys(&things.elems[2], nodeKey, 3); - - printf("fill keys returned: %d\n", r); - } -} - -void register_led_handlers() -{ - redled = 1; - greenled = 1; - blueled = 1; - register_portevent_handler(things.elems[2].Ports.elems[0].PortKey, handle_red_led); - register_portevent_handler(things.elems[2].Ports.elems[1].PortKey, handle_green_led); - register_portevent_handler(things.elems[2].Ports.elems[2].PortKey, handle_blue_led); -} - - -#include "FXOS8700Q.h" -//FXOS8700Q acc( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield -//FXOS8700Q mag( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield -FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board -FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board - -//MotionSensorDataUnits mag_data; -MotionSensorDataUnits acc_data; -// -//MotionSensorDataCounts mag_raw; -//MotionSensorDataCounts acc_raw; - -#define DOWNSAMPLING 50 - -void axel_thread(const void *args) -{ -// float faX, faY, faZ; -// float fmX, fmY, fmZ; -// int16_t raX, raY, raZ; -// int16_t rmX, rmY, rmZ; - acc.enable(); - printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); - int count = 0; - int shaken_cd = 0; - while (true) { -// if (client->isConnected()) - { - acc.getAxis(acc_data); - ++count %= DOWNSAMPLING; - if (shaken_cd) shaken_cd--; - if ((acc_data.x > 1.5f || acc_data.y > 1.5f || acc_data.z > 1.5f) && !shaken_cd) { - printf("shaken, not stirred\n"); - shaken_cd = DOWNSAMPLING; - shaken_event(); - } - if (!count) { - // mag.getAxis(mag_data); - - -// printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f \n", acc_data.x, acc_data.y, acc_data.z); - - - // printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag_data.x, mag_data.y, mag_data.z); - // acc.getX(&faX); - // acc.getY(&faY); - // acc.getZ(&faZ); - // mag.getX(&fmX); - // mag.getY(&fmY); - // mag.getZ(&fmZ); - // printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f ", faX, faY, faZ); - // printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", fmX, fmY, fmZ); - } - } - Thread::wait(20); - } -} \ No newline at end of file
--- a/nodas_things.h Mon Sep 28 09:03:01 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -#ifndef __NODAS_THINGS_H__ -#define __NODAS_THINGS_H__ - -#include "MQTTClient.h" -#include "MQTTSocket.h" - -#include "yodiwo_api.h" -#include <stdlib.h> - -#define MAX_MSG_LEN 2500 -#define MAX_TOPIC_LEN 150 - - -typedef int (func_ToJson)(char *, size_t, void *); -typedef Yodiwo_Plegma_Json_e (func_FromJson)(char *, size_t, void *); - -int mqtt_init(char *hostname, int port, char *certfile, char *nodeKey, char *nodeSecret); -void on_mqtt_message(MQTT::MessageData &msg); - -int button_event(int buttonId, bool pressed); - -typedef int (*portevent_handler_func)(Yodiwo_Plegma_PortEvent_t *event); - -void initialize_things(char *nodeKey); -int publisher(char *msg, int msg_len, char *msg_type); - - -int handle_red_led(Yodiwo_Plegma_PortEvent_t *event); -int handle_green_led(Yodiwo_Plegma_PortEvent_t *event); -int handle_blue_led(Yodiwo_Plegma_PortEvent_t *event); - -extern Array_Yodiwo_Plegma_Thing_t things; - -void register_led_handlers(); - -void axel_thread(const void *args); - -#endif /* __NODAS_THINGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yoplegma_things.cpp Mon Sep 28 09:11:03 2015 +0000 @@ -0,0 +1,262 @@ +#include "mbed.h" +#include "rtos.h" + +#include <stdint.h> + +#include "yoplegma_things.h" +#include "yodiwo_functions.h" +#include "jsmn.h" +#include "yodiwo_api.h" +#include "yodiwo_helpers.h" + +///////////////////////////////// LEDS +DigitalOut redled(LED1); +DigitalOut greenled(LED2); +DigitalOut blueled(LED3); + +///////////////////////////////// THINGS +Yodiwo_Plegma_Port_t button_port; +Yodiwo_Plegma_Port_t axel_port; +Yodiwo_Plegma_Port_t _led_ports[3]; + +Yodiwo_Plegma_Thing_t *button; +Yodiwo_Plegma_Thing_t *axel; +Yodiwo_Plegma_Thing_t *led; + +Yodiwo_Plegma_Thing_t _things[3]; +Array_Yodiwo_Plegma_Thing_t things; + + +int shaken_event() +{ + Yodiwo_Plegma_PortEvent_t event; + Array_Yodiwo_Plegma_PortEvent_t array_events; + + event.PortKey = axel->Ports.elems[0].PortKey; + event.State = "True"; + event.RevNum = 1; + + array_events.num = 1; + array_events.elems = &event; + + return portevents(&array_events); +} + +int button_event(int buttonId, bool pressed) +{ + printf("button %d: %s\n", buttonId, (pressed) ? "pressed" : "released"); + + Yodiwo_Plegma_PortEvent_t event; + Array_Yodiwo_Plegma_PortEvent_t array_events; + + event.PortKey = button->Ports.elems[0].PortKey; + event.State = (char *)((pressed) ? "True" : "False"); + event.RevNum = 1; + + array_events.num = 1; + array_events.elems = &event; + + return portevents(&array_events); +} + +void led_event(DigitalOut &led, Yodiwo_Plegma_PortEvent_t *event) +{ + if (!strcmp(event->State, "on") || !strcmp(event->State, "1") || !strcmp(event->State, "true") + || !strcmp(event->State, "True") || !strcmp(event->State, "On")) { + printf("ON\n"); + led = 0; + } else { + printf("OFF\n"); + led = 1; + } +} + +int handle_red_led(Yodiwo_Plegma_PortEvent_t *event) +{ + printf("event for red led\n"); + led_event(redled, event); + return 0; +} + +int handle_green_led(Yodiwo_Plegma_PortEvent_t *event) +{ + printf("event for green led\n"); + led_event(greenled, event); + return 0; +} + +int handle_blue_led(Yodiwo_Plegma_PortEvent_t *event) +{ + printf("event for blue led\n"); + led_event(blueled, event); + return 0; +} + +void initialize_things(char *nodeKey) +{ + // init thing pointers + button = &_things[0]; + axel = &_things[1]; + led = &_things[2]; + + //BUTTON + button_port.ConfFlags = Yodiwo_ePortConf_None; //why not + button_port.Description = "the state of the freedom button"; + button_port.Name = "BTN State"; + button_port.Type = Yodiwo_ePortType_Boolean; + button_port.State = "False"; + button_port.RevNum = 1; + button_port.ioDirection = Yodiwo_ioPortDirection_Output; + button_port.PortKey = ""; + + button->ThingKey = ""; + button->Name = "FRDM Button"; + button->Config.num = 0; + button->Config.elems = NULL; + button->Ports.num = 1; + button->Ports.elems = &button_port; + button->Type = ""; + button->BlockType = ""; + button->UIHints.Description = ""; + button->UIHints.IconURI = "/Content/VirtualGateway/img/icon-thing-genericbutton.png"; + + //AXEL + axel_port.ConfFlags = Yodiwo_ePortConf_IsTrigger; //why not + axel_port.Description = "triggered when the shaker is shaked"; + axel_port.Name = "SHAKER SHAKED"; + axel_port.Type = Yodiwo_ePortType_Boolean; + axel_port.State = ""; + axel_port.RevNum = 1; + axel_port.ioDirection = Yodiwo_ioPortDirection_Output; + axel_port.PortKey = ""; + + axel->ThingKey = ""; + axel->Name = "FRDM SHAKER"; + axel->Config.num = 0; + axel->Config.elems = NULL; + axel->Ports.num = 1; + axel->Ports.elems = &axel_port; + axel->Type = ""; + axel->BlockType = ""; + axel->UIHints.Description = ""; + axel->UIHints.IconURI = "/Content/VirtualGateway/img/accelerometer.jpg"; + + //LED + _led_ports[0].ConfFlags = Yodiwo_ePortConf_None; //why not + _led_ports[0].Description = "RED LED"; + _led_ports[0].Name = "RED LED"; + _led_ports[0].Type = Yodiwo_ePortType_Boolean; + _led_ports[0].State = "False"; + _led_ports[0].RevNum = 1; + _led_ports[0].ioDirection = Yodiwo_ioPortDirection_Input; + _led_ports[0].PortKey = ""; + + _led_ports[1].ConfFlags = Yodiwo_ePortConf_None; //why not + _led_ports[1].Description = "GREEN LED"; + _led_ports[1].Name = "GREEN LED"; + _led_ports[0].Type = Yodiwo_ePortType_Boolean; + _led_ports[1].State = "off"; + _led_ports[1].RevNum = 1; + _led_ports[1].ioDirection = Yodiwo_ioPortDirection_Input; + _led_ports[1].PortKey = ""; + + _led_ports[2].ConfFlags = Yodiwo_ePortConf_None; //why not + _led_ports[2].Description = "BLUE LED"; + _led_ports[2].Name = "BLUE LED"; + _led_ports[0].Type = Yodiwo_ePortType_Boolean; + _led_ports[2].State = "off"; + _led_ports[2].RevNum = 1; + _led_ports[2].ioDirection = Yodiwo_ioPortDirection_Input; + _led_ports[2].PortKey = ""; + + led->ThingKey = ""; + led->Name = "FRDM LED"; + led->Config.num = 0; + led->Config.elems = NULL; + led->Ports.num = 3; + led->Ports.elems = _led_ports; + led->Type = ""; + led->BlockType = ""; + led->UIHints.Description = ""; + led->UIHints.IconURI = "/Content/VirtualGateway/img/icon-thing-genericlight.png"; + + things.num = 3; + things.elems = _things; + + if (nodeKey != NULL) { + int r; + r = fill_Thing_Keys(&things.elems[0], nodeKey, 1); + r = fill_Thing_Keys(&things.elems[1], nodeKey, 2); + r = fill_Thing_Keys(&things.elems[2], nodeKey, 3); + + printf("fill keys returned: %d\n", r); + } +} + +void register_led_handlers() +{ + redled = 1; + greenled = 1; + blueled = 1; + register_portevent_handler(things.elems[2].Ports.elems[0].PortKey, handle_red_led); + register_portevent_handler(things.elems[2].Ports.elems[1].PortKey, handle_green_led); + register_portevent_handler(things.elems[2].Ports.elems[2].PortKey, handle_blue_led); +} + + +#include "FXOS8700Q.h" +//FXOS8700Q acc( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield +//FXOS8700Q mag( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield +FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board +FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board + +//MotionSensorDataUnits mag_data; +MotionSensorDataUnits acc_data; +// +//MotionSensorDataCounts mag_raw; +//MotionSensorDataCounts acc_raw; + +#define DOWNSAMPLING 50 + +void axel_thread(const void *args) +{ +// float faX, faY, faZ; +// float fmX, fmY, fmZ; +// int16_t raX, raY, raZ; +// int16_t rmX, rmY, rmZ; + acc.enable(); + printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); + int count = 0; + int shaken_cd = 0; + while (true) { +// if (client->isConnected()) + { + acc.getAxis(acc_data); + ++count %= DOWNSAMPLING; + if (shaken_cd) shaken_cd--; + if ((acc_data.x > 1.5f || acc_data.y > 1.5f || acc_data.z > 1.5f) && !shaken_cd) { + printf("shaken, not stirred\n"); + shaken_cd = DOWNSAMPLING; + shaken_event(); + } + if (!count) { + // mag.getAxis(mag_data); + + +// printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f \n", acc_data.x, acc_data.y, acc_data.z); + + + // printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag_data.x, mag_data.y, mag_data.z); + // acc.getX(&faX); + // acc.getY(&faY); + // acc.getZ(&faZ); + // mag.getX(&fmX); + // mag.getY(&fmY); + // mag.getZ(&fmZ); + // printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f ", faX, faY, faZ); + // printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", fmX, fmY, fmZ); + } + } + Thread::wait(20); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yoplegma_things.h Mon Sep 28 09:11:03 2015 +0000 @@ -0,0 +1,38 @@ +#ifndef __YOPLEGMA_THINGS_H__ +#define __YOPLEGMA_THINGS_H__ + +#include "MQTTClient.h" +#include "MQTTSocket.h" + +#include "yodiwo_api.h" +#include <stdlib.h> + +#define MAX_MSG_LEN 2500 +#define MAX_TOPIC_LEN 150 + + +typedef int (func_ToJson)(char *, size_t, void *); +typedef Yodiwo_Plegma_Json_e (func_FromJson)(char *, size_t, void *); + +int mqtt_init(char *hostname, int port, char *certfile, char *nodeKey, char *nodeSecret); +void on_mqtt_message(MQTT::MessageData &msg); + +int button_event(int buttonId, bool pressed); + +typedef int (*portevent_handler_func)(Yodiwo_Plegma_PortEvent_t *event); + +void initialize_things(char *nodeKey); +int publisher(char *msg, int msg_len, char *msg_type); + + +int handle_red_led(Yodiwo_Plegma_PortEvent_t *event); +int handle_green_led(Yodiwo_Plegma_PortEvent_t *event); +int handle_blue_led(Yodiwo_Plegma_PortEvent_t *event); + +extern Array_Yodiwo_Plegma_Thing_t things; + +void register_led_handlers(); + +void axel_thread(const void *args); + +#endif /* __YOPLEGMA_THINGS_H__ */