Example node for Yodiwo's Plegma API
Dependencies: EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL
Diff: main.cpp
- Revision:
- 3:11b767300d32
- Parent:
- 2:b7489c070d1f
- Child:
- 4:cc4c5b6d9730
--- a/main.cpp Fri Sep 04 08:41:34 2015 +0000 +++ b/main.cpp Tue Sep 15 14:43:15 2015 +0000 @@ -1,18 +1,19 @@ #include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" -#include "MQTTEthernet.h" -#include "MQTTClient.h" #include "HTTPD.h" //#include "HTTPServer.h" //#include "LocalFileSystem.h" #include "SDFileSystem.h" // SD File System functions -#include "FsHandler.h" -#include "RpcHandler.h" +//#include "FsHandler.h" +//#include "RpcHandler.h" #include "pairing_backend.h" #include "pairing_http_handler.h" #include "config.h" +#include "yodiwo_functions.h" +#include "nodas.h" +#include "yodiwo_extra.h" #define DAT0 PTE3 // MOSI #define CMD PTE1 // MISO @@ -22,7 +23,7 @@ EthernetInterface eth; -DigitalOut led1(LED1); +DigitalOut blinking(LED4); InterruptIn sw2(SW2); uint32_t button_pressed; Thread *thread2; @@ -35,12 +36,8 @@ SDFileSystem sd(DAT0, CMD, CLK, CD, "sd"); // MOSI, MISO, SCLK, CS //Serial pc(USBTX, USBRX); // Virtual COM Port -//MQTTEthernet ipstack; -MQTTSocket ipstack; -//MQTT::Client<MQTTEthernet, Countdown> *client; - - -config_t configuration; +Yodiwo_Tools_APIGenerator_CNodeYConfig_t configuration; +Yodiwo_Tools_APIGenerator_CNodeConfig_t *activeConfig; extern pairing_context pairing_state; @@ -48,13 +45,20 @@ void sw2_press(void) { +// button_event(1, true); thread2->signal_set(0x1); } +void sw2_release(void) +{ +// button_event(1, false); + thread2->signal_set(0x2); +} + void led_thread(void const *argument) { while (true) { - led1 = !led1; + blinking = blinking; Thread::wait(1000); } } @@ -62,17 +66,27 @@ void button_thread(void const *argument) { while (true) { - Thread::signal_wait(0x1); + osEvent evt = Thread::signal_wait(0x1 | 0x02); +// button_event(1, true); + if (evt.value.signals & 0x01) { + button_event(1, true); + } + if (evt.value.signals & 0x02) { + button_event(1, false); + } + button_pressed++; } } -void callback_pairing (int id); +//void callback_pairing (int id); + +int launch_mqtt(); int main() { Thread thread(led_thread); -// thread2 = new Thread(button_thread); + thread2 = new Thread(button_thread); eth.init(); eth.connect(); @@ -80,45 +94,59 @@ int r = read_config(&configuration, "/sd/config.json"); printf("read config returned: %d\n", r); + activeConfig = &configuration.Configs.elems[configuration.ActiveID]; - printf("p: %p %p\n", configuration.nodeKey, configuration.nodeSecret); - printf("...: %d %d\n", configuration.nodeKey[0], configuration.nodeKey[1]); - if (!configuration.nodeKey) { + printf("p: %p %p\n", activeConfig->NodeKey, activeConfig->NodeSecret); + printf("...: %d %d\n", activeConfig->NodeKey[0], activeConfig->NodeKey[1]); + if (!activeConfig->NodeKey) { printf("not paired, launching HTTP server for pairing\n"); start_pairing_http_server(ð, 80, pairing_done); } else { - - printf("found NodeKey: %s, proceeding to connection\n", configuration.nodeKey); + printf("found NodeKey: %s, proceeding to connection\n", activeConfig->NodeKey); + launch_mqtt(); } - - ipstack = MQTTSocket(); -// client = new MQTT::Client<MQTTEthernet, Countdown>(ipstack); // eth.disconnect(); button_pressed = 0; sw2.fall(&sw2_press); + sw2.rise(&sw2_release); while (true) { Thread::wait(5000); // printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed); fflush(stdout); -// button_pressed = 0; + button_pressed = 0; } } +int launch_mqtt() +{ + initialize_things(activeConfig->NodeKey); + init_yodiwo(activeConfig->Name, &things, publisher); + register_led_handlers(); + return mqtt_init(activeConfig->MqttBrokerHostname, + activeConfig->MqttBrokerPort, + activeConfig->MqttBrokerCertFile, + activeConfig->NodeKey, + activeConfig->NodeSecret + ); +} + void pairing_done(char *nodeKey, char *secretKey) { printf("pairing done!\n"); printf("NokeKey: %s\n", nodeKey); printf("SecretKey: %s\n", secretKey); - configuration.nodeKey = nodeKey; - configuration.nodeSecret = secretKey; + activeConfig->NodeKey = nodeKey; + activeConfig->NodeSecret = secretKey; int r = write_config(&configuration, "/sd/config.json"); if (r < 0) { printf("error writing config to file\n"); } else { printf("successfully written new config to file\n"); + printf("launching mqtt now...\n"); + launch_mqtt(); } }