tes
Dependencies: ASyncTicker EthernetInterface WebSocketClient mbed-rtos mbed MbedJSONValue xbee_lib
main.cpp@6:c1bd3fadce09, 2014-02-03 (annotated)
- Committer:
- ammanvedi
- Date:
- Mon Feb 03 12:07:53 2014 +0000
- Revision:
- 6:c1bd3fadce09
- Parent:
- 5:80a7d03c94f5
- Child:
- 7:7039646b7083
v1.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ammanvedi | 0:d73af3f5c81d | 1 | #include "mbed.h" |
ammanvedi | 0:d73af3f5c81d | 2 | #include "EthernetInterface.h" |
ammanvedi | 0:d73af3f5c81d | 3 | #include <stdio.h> |
ammanvedi | 0:d73af3f5c81d | 4 | #include "Websocket.h" |
ammanvedi | 1:6c1df6c9be4d | 5 | #include "xbee.h" |
ammanvedi | 1:6c1df6c9be4d | 6 | #include "xbeeFrame.h" |
ammanvedi | 4:84abfe990493 | 7 | #include "MbedJSONValue.h" |
ammanvedi | 3:28cd6502dafd | 8 | #include <ctype.h> |
ammanvedi | 4:84abfe990493 | 9 | #include <string> |
ammanvedi | 5:80a7d03c94f5 | 10 | #include "btNode.h" |
ammanvedi | 6:c1bd3fadce09 | 11 | #include <map> |
ammanvedi | 4:84abfe990493 | 12 | #define PORT 80 |
ammanvedi | 3:28cd6502dafd | 13 | |
ammanvedi | 4:84abfe990493 | 14 | // status leds |
ammanvedi | 4:84abfe990493 | 15 | // led 1 |
ammanvedi | 4:84abfe990493 | 16 | // lit - successful ethernet connection |
ammanvedi | 3:28cd6502dafd | 17 | DigitalOut led_ethernet(LED1); |
ammanvedi | 4:84abfe990493 | 18 | |
ammanvedi | 4:84abfe990493 | 19 | // led 2 |
ammanvedi | 4:84abfe990493 | 20 | // lit - successful socket connection |
ammanvedi | 3:28cd6502dafd | 21 | DigitalOut led_socket(LED2); |
ammanvedi | 3:28cd6502dafd | 22 | |
ammanvedi | 6:c1bd3fadce09 | 23 | map<int, btNode> BTnodes; |
ammanvedi | 6:c1bd3fadce09 | 24 | |
ammanvedi | 4:84abfe990493 | 25 | EthernetInterface ethernet; |
ammanvedi | 4:84abfe990493 | 26 | Websocket ws("ws://192.168.0.4:8080/"); |
ammanvedi | 3:28cd6502dafd | 27 | |
ammanvedi | 5:80a7d03c94f5 | 28 | |
ammanvedi | 5:80a7d03c94f5 | 29 | |
ammanvedi | 4:84abfe990493 | 30 | void pull_updates() |
ammanvedi | 4:84abfe990493 | 31 | { |
ammanvedi | 4:84abfe990493 | 32 | // build json request string |
ammanvedi | 4:84abfe990493 | 33 | MbedJSONValue request; |
ammanvedi | 4:84abfe990493 | 34 | |
ammanvedi | 4:84abfe990493 | 35 | std::string s; |
ammanvedi | 4:84abfe990493 | 36 | |
ammanvedi | 4:84abfe990493 | 37 | // fill the object |
ammanvedi | 4:84abfe990493 | 38 | request["TYPE"] = "UPDATE"; |
ammanvedi | 4:84abfe990493 | 39 | request["ORIGIN"] = "HUB"; |
ammanvedi | 4:84abfe990493 | 40 | request["ID"] = "N/A"; |
ammanvedi | 4:84abfe990493 | 41 | request["DATA"] = "N/A"; |
ammanvedi | 4:84abfe990493 | 42 | request["STATUS"] = "N/A"; |
ammanvedi | 4:84abfe990493 | 43 | |
ammanvedi | 4:84abfe990493 | 44 | // serialize it into a JSON string |
ammanvedi | 4:84abfe990493 | 45 | s = request.serialize(); |
ammanvedi | 4:84abfe990493 | 46 | |
ammanvedi | 4:84abfe990493 | 47 | // printf("json: %s\r\n", s.c_str()); |
ammanvedi | 4:84abfe990493 | 48 | |
ammanvedi | 4:84abfe990493 | 49 | char str[100]; |
ammanvedi | 4:84abfe990493 | 50 | char id[30]; |
ammanvedi | 4:84abfe990493 | 51 | char new_msg[50]; |
ammanvedi | 4:84abfe990493 | 52 | |
ammanvedi | 1:6c1df6c9be4d | 53 | // string with a message |
ammanvedi | 4:84abfe990493 | 54 | // sprintf(str, "PU"); |
ammanvedi | 4:84abfe990493 | 55 | ws.send((char *) s.c_str()); |
ammanvedi | 4:84abfe990493 | 56 | |
ammanvedi | 4:84abfe990493 | 57 | // clear the buffer and wait a sec... |
ammanvedi | 4:84abfe990493 | 58 | memset(str, 0, 100); |
ammanvedi | 4:84abfe990493 | 59 | wait(0.5f); |
ammanvedi | 3:28cd6502dafd | 60 | |
ammanvedi | 4:84abfe990493 | 61 | // websocket server should echo whatever we sent it |
ammanvedi | 4:84abfe990493 | 62 | if (ws.read(str)) |
ammanvedi | 4:84abfe990493 | 63 | { |
ammanvedi | 4:84abfe990493 | 64 | memset(id, 0, 30); |
ammanvedi | 4:84abfe990493 | 65 | memset(new_msg, 0, 50); |
ammanvedi | 4:84abfe990493 | 66 | |
ammanvedi | 4:84abfe990493 | 67 | // response from server is JSON |
ammanvedi | 4:84abfe990493 | 68 | // parse back to a traversable object |
ammanvedi | 4:84abfe990493 | 69 | MbedJSONValue updt_res; |
ammanvedi | 4:84abfe990493 | 70 | |
ammanvedi | 4:84abfe990493 | 71 | parse(updt_res, str); |
ammanvedi | 6:c1bd3fadce09 | 72 | printf("> %s from svr with data %s\n\r", updt_res["TYPE"].get<std::string>().c_str(), updt_res["DATA"].get<std::string>().c_str()); |
ammanvedi | 4:84abfe990493 | 73 | sscanf((char *) updt_res["DATA"].get<std::string>().c_str(), "%s %[^\t]", id, new_msg); |
ammanvedi | 4:84abfe990493 | 74 | |
ammanvedi | 4:84abfe990493 | 75 | // send string to xbee HERE |
ammanvedi | 4:84abfe990493 | 76 | if (strlen(new_msg) == 0) |
ammanvedi | 2:1c8d8d728b93 | 77 | { |
ammanvedi | 6:c1bd3fadce09 | 78 | printf("--> Nothing to update.\n\r\n\r"); |
ammanvedi | 2:1c8d8d728b93 | 79 | return; |
ammanvedi | 6:c1bd3fadce09 | 80 | }else{ |
ammanvedi | 6:c1bd3fadce09 | 81 | printf("--> id : %s string: %s \n\r", id, new_msg); |
ammanvedi | 2:1c8d8d728b93 | 82 | } |
ammanvedi | 5:80a7d03c94f5 | 83 | |
ammanvedi | 5:80a7d03c94f5 | 84 | std::string real_msg(new_msg); |
ammanvedi | 5:80a7d03c94f5 | 85 | |
ammanvedi | 6:c1bd3fadce09 | 86 | btNode bt = BTnodes.find(atoi(id))->second; |
ammanvedi | 5:80a7d03c94f5 | 87 | |
ammanvedi | 6:c1bd3fadce09 | 88 | printf("> Found device with ID : %d\n\r\n\r", bt.getID()); |
ammanvedi | 5:80a7d03c94f5 | 89 | |
ammanvedi | 6:c1bd3fadce09 | 90 | std::string result = bt.SendMessage(real_msg); |
ammanvedi | 6:c1bd3fadce09 | 91 | |
ammanvedi | 6:c1bd3fadce09 | 92 | printf("--> xbee response : %s\n\r\n\r", result.c_str()); |
ammanvedi | 6:c1bd3fadce09 | 93 | |
ammanvedi | 6:c1bd3fadce09 | 94 | } |
ammanvedi | 0:d73af3f5c81d | 95 | } |
ammanvedi | 0:d73af3f5c81d | 96 | |
ammanvedi | 4:84abfe990493 | 97 | int main() |
ammanvedi | 0:d73af3f5c81d | 98 | { |
ammanvedi | 5:80a7d03c94f5 | 99 | |
ammanvedi | 6:c1bd3fadce09 | 100 | btNode b(30); |
ammanvedi | 6:c1bd3fadce09 | 101 | BTnodes.insert(pair<int, btNode>(30,b)); |
ammanvedi | 5:80a7d03c94f5 | 102 | |
ammanvedi | 3:28cd6502dafd | 103 | led_ethernet = 0; |
ammanvedi | 4:84abfe990493 | 104 | led_socket = 0; |
ammanvedi | 0:d73af3f5c81d | 105 | |
ammanvedi | 0:d73af3f5c81d | 106 | ethernet.init(); // connect with DHCP |
ammanvedi | 4:84abfe990493 | 107 | |
ammanvedi | 0:d73af3f5c81d | 108 | int ret_val = ethernet.connect(); |
ammanvedi | 4:84abfe990493 | 109 | |
ammanvedi | 4:84abfe990493 | 110 | if (0 == ret_val) |
ammanvedi | 4:84abfe990493 | 111 | { |
ammanvedi | 3:28cd6502dafd | 112 | printf("\n\rIP Address: %s\n\r", ethernet.getIPAddress()); |
ammanvedi | 4:84abfe990493 | 113 | |
ammanvedi | 3:28cd6502dafd | 114 | led_ethernet = 1; |
ammanvedi | 4:84abfe990493 | 115 | } |
ammanvedi | 4:84abfe990493 | 116 | else |
ammanvedi | 4:84abfe990493 | 117 | { |
ammanvedi | 0:d73af3f5c81d | 118 | error("ethernet failed to connect: %d.\n\r", ret_val); |
ammanvedi | 0:d73af3f5c81d | 119 | } |
ammanvedi | 4:84abfe990493 | 120 | |
ammanvedi | 2:1c8d8d728b93 | 121 | int interval = 5; |
ammanvedi | 4:84abfe990493 | 122 | |
ammanvedi | 4:84abfe990493 | 123 | if (ws.connect()) |
ammanvedi | 3:28cd6502dafd | 124 | { |
ammanvedi | 3:28cd6502dafd | 125 | led_socket = 1; |
ammanvedi | 3:28cd6502dafd | 126 | } |
ammanvedi | 4:84abfe990493 | 127 | |
ammanvedi | 1:6c1df6c9be4d | 128 | Timer timer; |
ammanvedi | 4:84abfe990493 | 129 | |
ammanvedi | 1:6c1df6c9be4d | 130 | timer.start(); |
ammanvedi | 2:1c8d8d728b93 | 131 | |
ammanvedi | 4:84abfe990493 | 132 | // every <interval> seconds call to the server to pull updates |
ammanvedi | 4:84abfe990493 | 133 | while (true) |
ammanvedi | 4:84abfe990493 | 134 | { |
ammanvedi | 4:84abfe990493 | 135 | if (timer.read() >= interval) |
ammanvedi | 4:84abfe990493 | 136 | { |
ammanvedi | 4:84abfe990493 | 137 | // perform checks |
ammanvedi | 1:6c1df6c9be4d | 138 | pull_updates(); |
ammanvedi | 1:6c1df6c9be4d | 139 | timer.reset(); |
ammanvedi | 1:6c1df6c9be4d | 140 | timer.start(); |
ammanvedi | 0:d73af3f5c81d | 141 | } |
ammanvedi | 1:6c1df6c9be4d | 142 | } |
ammanvedi | 6:c1bd3fadce09 | 143 | //ws.close(); |
ammanvedi | 0:d73af3f5c81d | 144 | } |
ammanvedi | 4:84abfe990493 | 145 | |
ammanvedi | 4:84abfe990493 | 146 | |
ammanvedi | 6:c1bd3fadce09 | 147 |