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:
- 2:b7489c070d1f
- Parent:
- 0:00797f1ebe04
- Child:
- 3:11b767300d32
--- a/main.cpp Tue Sep 01 12:01:50 2015 +0000 +++ b/main.cpp Fri Sep 04 08:41:34 2015 +0000 @@ -7,10 +7,12 @@ //#include "HTTPServer.h" //#include "LocalFileSystem.h" #include "SDFileSystem.h" // SD File System functions -//#include "HTTPRequestHandler.h" + #include "FsHandler.h" #include "RpcHandler.h" #include "pairing_backend.h" +#include "pairing_http_handler.h" +#include "config.h" #define DAT0 PTE3 // MOSI #define CMD PTE1 // MISO @@ -25,7 +27,7 @@ uint32_t button_pressed; Thread *thread2; //HTTPServer server; -HTTPD *httpd; +extern HTTPD *httpd; // Instantiate a local file system handler named 'local' which will be used later to access files on the mbed. //LocalFileSystem local("local"); @@ -33,8 +35,14 @@ 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; -pairing_context pairing_state; + +config_t configuration; + +extern pairing_context pairing_state; void pairing_done(char *nodeKey, char *secretKey); @@ -59,79 +67,35 @@ } } -void httpd_thread(void const *arg) -{ -// HTTPD *server; -// server = HTTPD::getInstance(); -// -// server->start(8080); -} - void callback_pairing (int id); int main() { Thread thread(led_thread); // thread2 = new Thread(button_thread); - -// FILE *File = fopen("/sd/sdfile.txt", "w"); // open file -// if(File == NULL) { // check if present -// printf("No SD Card or bad format\n"); // print message -// } else { // otherwise -// printf("Ready to write\n"); // message preparing to write -// } -// fprintf(File, "FRDM-K64F"); // write data -// fclose(File); // close file on SD - eth.init(); - eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); -// MQTTEthernet ipstack = MQTTEthernet(); -// MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); -// - -// TCPSocketConnection sock; -// sock.connect("mbed.org", 80); -// -// char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; -// sock.send_all(http_cmd, sizeof(http_cmd)-1); -// -// char buffer[300]; -// int ret; -// while (true) { -// ret = sock.receive(buffer, sizeof(buffer)-1); -// if (ret <= 0) -// break; -// buffer[ret] = '\0'; -// printf("Received %d chars from server:\n%s\n", ret, buffer); -// } -// -// sock.close(); + int r = read_config(&configuration, "/sd/config.json"); + printf("read config returned: %d\n", r); -// HTTPFsRequestHandler::mount("/sd/", "/"); -// server.addHandler<HTTPFsRequestHandler>("/"); + printf("p: %p %p\n", configuration.nodeKey, configuration.nodeSecret); + printf("...: %d %d\n", configuration.nodeKey[0], configuration.nodeKey[1]); + if (!configuration.nodeKey) { + printf("not paired, launching HTTP server for pairing\n"); + start_pairing_http_server(ð, 80, pairing_done); + } else { -// HTTPFsRequestHandler::mount("/sd/", "/"); -// server.addHandler<HTTPRpcRequestHandler>("/"); -// -// if (!server.start(80, ð)) { -// error("Server not starting !"); -// exit(0); -// } -// - httpd = new HTTPD; -// httpd->attach("/cgi-bin/", &callback_cgi); -// httpd->attach("/ws/", &callback_ws); - httpd->attach("/pairing_functions/", &callback_pairing); - httpd->attach("/", "/sd/www/"); - httpd->start(80); + printf("found NodeKey: %s, proceeding to connection\n", configuration.nodeKey); + } + + ipstack = MQTTSocket(); +// client = new MQTT::Client<MQTTEthernet, Countdown>(ipstack); -// Thread thread3(httpd_thread); // eth.disconnect(); -// + button_pressed = 0; sw2.fall(&sw2_press); @@ -143,33 +107,18 @@ } } -void callback_pairing (int id) { - int i, n; - char buf[256]; - - if (!strcmp(httpd->getFilename(id), "start_pairing")) { - printf("starting pairing...\n"); - pairing_context_init_with_defaults(&pairing_state, pairing_done); - start_pairing(&pairing_state); - } - strcpy(buf, httpd->getFilename(id)); - strcat(buf, "\r\n"); - strcat(buf, httpd->getQueryString(id)); - strcat(buf, "\r\n"); - n = strlen(buf); - - i = httpd->receive(id, &buf[n], sizeof(buf) - n); - if (i < 0) return; - i += n; - buf[i] = 0; - - printf("CGI %d %s\r\n", id, buf); - httpd->send(id, buf, i, "Content-Type: text/plain\r\n"); -} 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; + 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"); + } }