Test u-blox C027, nodejs, socketio on heroku

Dependencies:   C027 EthernetInterface SocketIO WebSocketClient mbed-rtos mbed

Fork of C027_Socketio_Ethernet by Chau Vo

Committer:
olympux
Date:
Sun Apr 27 13:50:16 2014 +0000
Revision:
6:4af1b4a1b875
Parent:
4:0bdab35cb164
Initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:1c1802ec42a2 1 #include "mbed.h"
olympux 3:259ac92c3ca8 2 #include "C027.h"
samux 1:1c1802ec42a2 3 #include "EthernetInterface.h"
samux 1:1c1802ec42a2 4 #include "Websocket.h"
olympux 4:0bdab35cb164 5 #include "SocketIO.h"
olympux 4:0bdab35cb164 6
olympux 4:0bdab35cb164 7
olympux 4:0bdab35cb164 8 //Debug is disabled by default
olympux 6:4af1b4a1b875 9 #if 0
olympux 4:0bdab35cb164 10 //Enable debug
olympux 4:0bdab35cb164 11 #include <cstdio>
olympux 4:0bdab35cb164 12 #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 13 #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 14 #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 15
olympux 4:0bdab35cb164 16 #else
olympux 4:0bdab35cb164 17 //Disable debug
olympux 4:0bdab35cb164 18 #define DBG(x, ...)
olympux 4:0bdab35cb164 19 #define WARN(x, ...)
olympux 4:0bdab35cb164 20 #define ERR(x, ...)
olympux 4:0bdab35cb164 21
olympux 4:0bdab35cb164 22 #endif
olympux 4:0bdab35cb164 23
olympux 4:0bdab35cb164 24
olympux 4:0bdab35cb164 25 void websocket_test(void const*)
olympux 3:259ac92c3ca8 26 {
olympux 3:259ac92c3ca8 27 char msg[512] = {0};
olympux 4:0bdab35cb164 28
samux 1:1c1802ec42a2 29 EthernetInterface eth;
samux 1:1c1802ec42a2 30 eth.init(); //Use DHCP
samux 1:1c1802ec42a2 31 eth.connect();
olympux 4:0bdab35cb164 32 DBG("IP Address is %s", eth.getIPAddress());
olympux 4:0bdab35cb164 33
olympux 3:259ac92c3ca8 34 Websocket ws("ws://sockets.mbed.org:443/ws/chauvo/wo");
olympux 3:259ac92c3ca8 35 Timer t;
olympux 3:259ac92c3ca8 36 t.start();
olympux 4:0bdab35cb164 37
olympux 3:259ac92c3ca8 38 bool c = ws.connect();
olympux 4:0bdab35cb164 39 DBG("Connect result: %s", c?"OK":"Failed");
olympux 4:0bdab35cb164 40
olympux 4:0bdab35cb164 41 for(int i = 0; i < 10000; i++) {
olympux 3:259ac92c3ca8 42 ws.connect();
olympux 3:259ac92c3ca8 43 // create json string with acc/tmp data
olympux 3:259ac92c3ca8 44 sprintf(msg, "Chau's demo loop: %d", i);
olympux 4:0bdab35cb164 45 ws.send(msg);
olympux 3:259ac92c3ca8 46 wait(0.5f);
olympux 3:259ac92c3ca8 47 memset(msg, 0, 512);
olympux 4:0bdab35cb164 48
olympux 4:0bdab35cb164 49 if (ws.read(msg)) {
olympux 4:0bdab35cb164 50 DBG("rcv: %s\r\n", msg);
olympux 4:0bdab35cb164 51 } else {
olympux 4:0bdab35cb164 52 DBG("Loop %d ws.read() returns 0\n \t %s, line %d @ %6.2f seconds\n", i, __FILE__, __LINE__, t.read());
olympux 3:259ac92c3ca8 53 wait(5.0f);
olympux 3:259ac92c3ca8 54 }
olympux 3:259ac92c3ca8 55 ws.close();
samux 1:1c1802ec42a2 56 }
olympux 4:0bdab35cb164 57
olympux 3:259ac92c3ca8 58 while(1) {
olympux 3:259ac92c3ca8 59 }
olympux 3:259ac92c3ca8 60 }
olympux 3:259ac92c3ca8 61
olympux 4:0bdab35cb164 62 void socketio_test(void const*)
olympux 4:0bdab35cb164 63 {
olympux 4:0bdab35cb164 64 char msg[512] = {0};
olympux 4:0bdab35cb164 65
olympux 4:0bdab35cb164 66 EthernetInterface eth;
olympux 4:0bdab35cb164 67 eth.init(); //Use DHCP
olympux 4:0bdab35cb164 68 eth.connect();
olympux 4:0bdab35cb164 69 DBG("IP Address is %s", eth.getIPAddress());
olympux 4:0bdab35cb164 70
olympux 6:4af1b4a1b875 71 // server1
olympux 6:4af1b4a1b875 72 //SocketIO socketio("websocket-server-c9-chau_vk.c9.io");
olympux 4:0bdab35cb164 73 SocketIO socketio("nne-server1.herokuapp.com");
olympux 6:4af1b4a1b875 74 //SocketIO socketio("nneserver1-tutorial11067089.rhcloud.com"); // is downgraded to xhr-polling
olympux 6:4af1b4a1b875 75
olympux 6:4af1b4a1b875 76 // server2
olympux 6:4af1b4a1b875 77 //SocketIO socketio("realchart-c9-chau_vk.c9.io");
olympux 6:4af1b4a1b875 78
olympux 6:4af1b4a1b875 79
olympux 4:0bdab35cb164 80 Timer t;
olympux 4:0bdab35cb164 81 t.start();
olympux 6:4af1b4a1b875 82
olympux 6:4af1b4a1b875 83 socketio.connect();
olympux 4:0bdab35cb164 84
olympux 4:0bdab35cb164 85 while (1) {
olympux 6:4af1b4a1b875 86 socketio.connect();
olympux 6:4af1b4a1b875 87 bool is_connected = socketio.is_connected();
olympux 6:4af1b4a1b875 88 //DBG("Connect result: %s", is_connected?"OK":"Failed");
olympux 6:4af1b4a1b875 89
olympux 6:4af1b4a1b875 90 if (is_connected && (t.read() > 10)) {
olympux 6:4af1b4a1b875 91 int res = socketio.emit("message","0.5");
olympux 6:4af1b4a1b875 92 t.reset(); // transmit every 10s
olympux 6:4af1b4a1b875 93 }
olympux 4:0bdab35cb164 94 if (socketio.read(msg)) {
olympux 4:0bdab35cb164 95 DBG("rcv: %s", msg);
olympux 4:0bdab35cb164 96 }
olympux 4:0bdab35cb164 97 }
olympux 4:0bdab35cb164 98 }
olympux 4:0bdab35cb164 99
olympux 3:259ac92c3ca8 100 int main()
olympux 3:259ac92c3ca8 101 {
olympux 4:0bdab35cb164 102 //Thread testTask(websocket_test, NULL, osPriorityNormal, 1024 * 4);
olympux 4:0bdab35cb164 103 Thread testTask(socketio_test, NULL, osPriorityNormal, 1024 * 4);
olympux 3:259ac92c3ca8 104 DigitalOut led(LED); // on rev A you should reasign the signal to A0
olympux 3:259ac92c3ca8 105 while(1) {
olympux 3:259ac92c3ca8 106 led=!led;
olympux 3:259ac92c3ca8 107 Thread::wait(1000);
olympux 3:259ac92c3ca8 108 }
olympux 3:259ac92c3ca8 109
olympux 3:259ac92c3ca8 110 return 0;
samux 1:1c1802ec42a2 111 }