ch8_mbed_websocket_client to sockets.mbed.org
Dependencies: EthernetInterface HTTPD WebSocketClient mbed-rpc mbed-rtos-for-old mbed
main.cpp@0:a24e2b2f8c8f, 2015-02-04 (annotated)
- Committer:
- mbedschool
- Date:
- Wed Feb 04 03:42:54 2015 +0000
- Revision:
- 0:a24e2b2f8c8f
ch8_mbed)websocket_client_to_sockets_mbed_org
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedschool | 0:a24e2b2f8c8f | 1 | #include "mbed.h" |
mbedschool | 0:a24e2b2f8c8f | 2 | #include "EthernetInterface.h" |
mbedschool | 0:a24e2b2f8c8f | 3 | #include "HTTPD.h" |
mbedschool | 0:a24e2b2f8c8f | 4 | #include "Websocket.h" |
mbedschool | 0:a24e2b2f8c8f | 5 | |
mbedschool | 0:a24e2b2f8c8f | 6 | |
mbedschool | 0:a24e2b2f8c8f | 7 | |
mbedschool | 0:a24e2b2f8c8f | 8 | EthernetInterface *eth; |
mbedschool | 0:a24e2b2f8c8f | 9 | HTTPD *httpd; |
mbedschool | 0:a24e2b2f8c8f | 10 | |
mbedschool | 0:a24e2b2f8c8f | 11 | |
mbedschool | 0:a24e2b2f8c8f | 12 | Serial pc(USBTX, USBRX); |
mbedschool | 0:a24e2b2f8c8f | 13 | InterruptIn probe(p8); |
mbedschool | 0:a24e2b2f8c8f | 14 | LocalFileSystem local("local"); |
mbedschool | 0:a24e2b2f8c8f | 15 | DigitalOut led1(LED1), led2(LED2),led3(LED3); |
mbedschool | 0:a24e2b2f8c8f | 16 | Timer timer; |
mbedschool | 0:a24e2b2f8c8f | 17 | Ticker ticker; |
mbedschool | 0:a24e2b2f8c8f | 18 | |
mbedschool | 0:a24e2b2f8c8f | 19 | uint32_t low_time = 0; |
mbedschool | 0:a24e2b2f8c8f | 20 | //uint32_t low_time = 0; |
mbedschool | 0:a24e2b2f8c8f | 21 | |
mbedschool | 0:a24e2b2f8c8f | 22 | void down(); |
mbedschool | 0:a24e2b2f8c8f | 23 | void up(); |
mbedschool | 0:a24e2b2f8c8f | 24 | |
mbedschool | 0:a24e2b2f8c8f | 25 | void tick() |
mbedschool | 0:a24e2b2f8c8f | 26 | { |
mbedschool | 0:a24e2b2f8c8f | 27 | pc.printf("%d us, %f %%\r\n", low_time, low_time / (30.0 * 1000000)); |
mbedschool | 0:a24e2b2f8c8f | 28 | low_time = 0; |
mbedschool | 0:a24e2b2f8c8f | 29 | |
mbedschool | 0:a24e2b2f8c8f | 30 | } |
mbedschool | 0:a24e2b2f8c8f | 31 | |
mbedschool | 0:a24e2b2f8c8f | 32 | void down() |
mbedschool | 0:a24e2b2f8c8f | 33 | { |
mbedschool | 0:a24e2b2f8c8f | 34 | probe.rise(&up); |
mbedschool | 0:a24e2b2f8c8f | 35 | led1 = 0; |
mbedschool | 0:a24e2b2f8c8f | 36 | timer.start(); |
mbedschool | 0:a24e2b2f8c8f | 37 | } |
mbedschool | 0:a24e2b2f8c8f | 38 | |
mbedschool | 0:a24e2b2f8c8f | 39 | void up() |
mbedschool | 0:a24e2b2f8c8f | 40 | { |
mbedschool | 0:a24e2b2f8c8f | 41 | led1 = 1; |
mbedschool | 0:a24e2b2f8c8f | 42 | timer.stop(); |
mbedschool | 0:a24e2b2f8c8f | 43 | probe.fall(&down); |
mbedschool | 0:a24e2b2f8c8f | 44 | |
mbedschool | 0:a24e2b2f8c8f | 45 | low_time += timer.read_us(); |
mbedschool | 0:a24e2b2f8c8f | 46 | timer.reset(); |
mbedschool | 0:a24e2b2f8c8f | 47 | } |
mbedschool | 0:a24e2b2f8c8f | 48 | |
mbedschool | 0:a24e2b2f8c8f | 49 | void callback_api(int id) { |
mbedschool | 0:a24e2b2f8c8f | 50 | int i, n; |
mbedschool | 0:a24e2b2f8c8f | 51 | char low_time_buf[256]; |
mbedschool | 0:a24e2b2f8c8f | 52 | char buf[256]; |
mbedschool | 0:a24e2b2f8c8f | 53 | |
mbedschool | 0:a24e2b2f8c8f | 54 | sprintf( low_time_buf, "%f", low_time / (30.0 * 1000000)); |
mbedschool | 0:a24e2b2f8c8f | 55 | strcpy(buf, "{"); |
mbedschool | 0:a24e2b2f8c8f | 56 | strcat(buf, "success: true ,"); |
mbedschool | 0:a24e2b2f8c8f | 57 | strcat(buf, "data: {"); |
mbedschool | 0:a24e2b2f8c8f | 58 | strcat(buf, "lowpulseoccupancytime: "); |
mbedschool | 0:a24e2b2f8c8f | 59 | strcat(buf, low_time_buf); |
mbedschool | 0:a24e2b2f8c8f | 60 | strcat(buf, "}"); |
mbedschool | 0:a24e2b2f8c8f | 61 | strcat(buf, "}"); |
mbedschool | 0:a24e2b2f8c8f | 62 | |
mbedschool | 0:a24e2b2f8c8f | 63 | n = strlen(buf); |
mbedschool | 0:a24e2b2f8c8f | 64 | i = httpd->receive(id, &buf[n], sizeof(buf) - n); |
mbedschool | 0:a24e2b2f8c8f | 65 | if (i < 0) return; |
mbedschool | 0:a24e2b2f8c8f | 66 | i += n; |
mbedschool | 0:a24e2b2f8c8f | 67 | buf[i] = 0; |
mbedschool | 0:a24e2b2f8c8f | 68 | printf("API %d %s\r\n", id, buf); |
mbedschool | 0:a24e2b2f8c8f | 69 | httpd->send(id, buf, i, "Content-Type: text/plain\r\n"); |
mbedschool | 0:a24e2b2f8c8f | 70 | } |
mbedschool | 0:a24e2b2f8c8f | 71 | |
mbedschool | 0:a24e2b2f8c8f | 72 | int main() { |
mbedschool | 0:a24e2b2f8c8f | 73 | pc.baud(115200); |
mbedschool | 0:a24e2b2f8c8f | 74 | pc.printf("Dust Sensor test\r\n"); |
mbedschool | 0:a24e2b2f8c8f | 75 | probe.fall(&down); |
mbedschool | 0:a24e2b2f8c8f | 76 | ticker.attach(tick, 30); |
mbedschool | 0:a24e2b2f8c8f | 77 | |
mbedschool | 0:a24e2b2f8c8f | 78 | printf("HTTP Server...\r\n"); |
mbedschool | 0:a24e2b2f8c8f | 79 | |
mbedschool | 0:a24e2b2f8c8f | 80 | eth = new EthernetInterface; |
mbedschool | 0:a24e2b2f8c8f | 81 | eth->init("192.168.21.23", "255.255.255.0", "192.168.21.2" ); |
mbedschool | 0:a24e2b2f8c8f | 82 | // eth->init(); |
mbedschool | 0:a24e2b2f8c8f | 83 | |
mbedschool | 0:a24e2b2f8c8f | 84 | if (eth->connect()) return -1; |
mbedschool | 0:a24e2b2f8c8f | 85 | led3 = 1; |
mbedschool | 0:a24e2b2f8c8f | 86 | |
mbedschool | 0:a24e2b2f8c8f | 87 | |
mbedschool | 0:a24e2b2f8c8f | 88 | printf("Connected to websocket server. \r\n"); |
mbedschool | 0:a24e2b2f8c8f | 89 | |
mbedschool | 0:a24e2b2f8c8f | 90 | |
mbedschool | 0:a24e2b2f8c8f | 91 | printf("IP Address is %s\r\n", eth->getIPAddress()); |
mbedschool | 0:a24e2b2f8c8f | 92 | httpd = new HTTPD; |
mbedschool | 0:a24e2b2f8c8f | 93 | httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api); |
mbedschool | 0:a24e2b2f8c8f | 94 | httpd->attach("/", "/local/"); |
mbedschool | 0:a24e2b2f8c8f | 95 | httpd->start(80); |
mbedschool | 0:a24e2b2f8c8f | 96 | printf("httpd ready\r\n"); |
mbedschool | 0:a24e2b2f8c8f | 97 | led2 = 1; |
mbedschool | 0:a24e2b2f8c8f | 98 | |
mbedschool | 0:a24e2b2f8c8f | 99 | Websocket ws("ws://sockets.mbed.org/ws/mbedschool/viewer"); |
mbedschool | 0:a24e2b2f8c8f | 100 | // Websocket ws("ws://192.168.21.123:8888"); |
mbedschool | 0:a24e2b2f8c8f | 101 | |
mbedschool | 0:a24e2b2f8c8f | 102 | while (!ws.connect()); |
mbedschool | 0:a24e2b2f8c8f | 103 | // ws.send("echo-protocol"); |
mbedschool | 0:a24e2b2f8c8f | 104 | while (1) { |
mbedschool | 0:a24e2b2f8c8f | 105 | |
mbedschool | 0:a24e2b2f8c8f | 106 | char low_time_buf[256]; |
mbedschool | 0:a24e2b2f8c8f | 107 | char buf[256]; |
mbedschool | 0:a24e2b2f8c8f | 108 | sprintf( low_time_buf, "%f", low_time / (30.0 * 1000000)); |
mbedschool | 0:a24e2b2f8c8f | 109 | |
mbedschool | 0:a24e2b2f8c8f | 110 | strcpy(buf, "{"); |
mbedschool | 0:a24e2b2f8c8f | 111 | strcat(buf, "\"lowpulseoccupancytime\":"); |
mbedschool | 0:a24e2b2f8c8f | 112 | strcat(buf, low_time_buf); |
mbedschool | 0:a24e2b2f8c8f | 113 | strcat(buf, "}"); |
mbedschool | 0:a24e2b2f8c8f | 114 | ws.send(buf); |
mbedschool | 0:a24e2b2f8c8f | 115 | |
mbedschool | 0:a24e2b2f8c8f | 116 | |
mbedschool | 0:a24e2b2f8c8f | 117 | wait(1.0); |
mbedschool | 0:a24e2b2f8c8f | 118 | } |
mbedschool | 0:a24e2b2f8c8f | 119 | |
mbedschool | 0:a24e2b2f8c8f | 120 | } |