Example node for Yodiwo's Plegma API
Dependencies: EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL
Revision 0:00797f1ebe04, committed 2015-09-01
- Comitter:
- mitsarionas
- Date:
- Tue Sep 01 10:23:13 2015 +0000
- Child:
- 1:c5abc450140c
- Commit message:
- 1st p0st
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/wolfSSL/code/HTTPClient/#ac9de60bbf72
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPD.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/okini3939/code/HTTPD/#d18dff347122
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPServer.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/leihen/code/HTTPServer/#cc3f5c53d0d5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/mqtt/code/MQTT/#e335fcc1a663
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/SDFileSystem/#2286a4e7fa31
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,175 @@ +#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 "HTTPRequestHandler.h" +#include "FsHandler.h" +#include "RpcHandler.h" +#include "pairing_backend.h" + +#define DAT0 PTE3 // MOSI +#define CMD PTE1 // MISO +#define CLK PTE2 // SCLK +#define CD PTE4 // CS + + +EthernetInterface eth; + +DigitalOut led1(LED1); +InterruptIn sw2(SW2); +uint32_t button_pressed; +Thread *thread2; +//HTTPServer server; +HTTPD *httpd; +// Instantiate a local file system handler named 'local' which will be used later to access files on the mbed. +//LocalFileSystem local("local"); + + +SDFileSystem sd(DAT0, CMD, CLK, CD, "sd"); // MOSI, MISO, SCLK, CS +//Serial pc(USBTX, USBRX); // Virtual COM Port + + +pairing_context pairing_state; + +void pairing_done(char *nodeKey, char *secretKey); + +void sw2_press(void) +{ + thread2->signal_set(0x1); +} + +void led_thread(void const *argument) +{ + while (true) { + led1 = !led1; + Thread::wait(1000); + } +} + +void button_thread(void const *argument) +{ + while (true) { + Thread::signal_wait(0x1); + button_pressed++; + } +} + +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(); + +// HTTPFsRequestHandler::mount("/sd/", "/"); +// server.addHandler<HTTPFsRequestHandler>("/"); + +// 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); + +// Thread thread3(httpd_thread); +// eth.disconnect(); +// + + button_pressed = 0; + sw2.fall(&sw2_press); + while (true) { + Thread::wait(5000); +// printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed); + fflush(stdout); +// button_pressed = 0; + } +} + +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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rpc.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rpc/#325e3da833e1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#21b438192b0f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8ed44a420e5c \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pairing_backend.cpp Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,86 @@ +#include "pairing_backend.h" +#include "HTTPClient.h" +#include "mbed.h" +#include "rtos.h" + +HTTPClient http; +char recvBuff[1024*20]; +int __get_tokens(pairing_context *ctx); + +int pairing_context_init_with_defaults(pairing_context *ctx, onPaired_callback callback) +{ + ctx->postUrl = "http://10.30.254.199:3334/pairing/"; + ctx->uuid = "1337CNode"; + ctx->name = "Nodas"; + ctx->onPaired = callback; + return 0; +} + +#define STACK_SIZE 24000 + +void get_tokens_thread(void const *arg); + +int start_pairing(pairing_context *ctx) +{ + printf("getting tokens from server\n"); + Thread t(get_tokens_thread, ctx, osPriorityNormal, STACK_SIZE); + while (t.get_state() != Thread::Inactive) { +// printf("yielding... %d\n", t.get_state()); +// Thread::yield(); +// Thread::wait(1000); + } + printf("thread ended?\n"); + return 0; +} + +char str[512]; +char url[100]; + +void get_tokens_thread(void const *arg) +{ + printf("in thread\n"); + __get_tokens((pairing_context *)arg); +} + +int __get_tokens(pairing_context *ctx) +{ + + strcpy(url, ctx->postUrl); + strcat(url, "/gettokens"); +// http.dumpReqHeader(true); +// http.dumpResHeader(true); + int ret; + + //GET data +// printf("\nTrying to fetch page...\n"); +// ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); +// if (!ret) +// { +// printf("Page fetched successfully - read %d characters\n", strlen(str)); +// printf("Result: %s\n", str); +// } +// else +// { +// printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); +// } + + //POST data + HTTPMap map; + HTTPText inText(str, 512); + map.put("name", ctx->name); + map.put("uuid", ctx->uuid); + printf("\nTrying to post data...\n"); + ret = http.post(url, map, &inText); + if (!ret) + { + printf("Executed POST successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + + } + else + { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } + return ret; + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pairing_backend.h Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,38 @@ + +#ifndef __PAIRING_BACKEND_H__ +#define __PAIRING_BACKEND_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (*onPaired_callback)(char *nodeKey, char *secretKey); + +typedef struct +{ + char *postUrl; + char *uuid; + char *name; + char *token1; + char *token2; + char *nodeKey; + char *secretKey; + onPaired_callback onPaired; +} pairing_context; + +int pairing_context_init_with_defaults(pairing_context *ctx, onPaired_callback callback); + +int start_pairing(pairing_context *ctx); + + + + + + + + +#ifdef __cplusplus +} +#endif + +#endif /* __PAIRING_BACKEND_H__ */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wolfSSL.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/wolfSSL/code/wolfSSL/#28278596c2a2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yodiwo.lib Tue Sep 01 10:23:13 2015 +0000 @@ -0,0 +1,1 @@ +yodiwo#384a04dc912e