tes
Dependencies: ASyncTicker EthernetInterface WebSocketClient mbed-rtos mbed MbedJSONValue xbee_lib
main.cpp@1:6c1df6c9be4d, 2014-01-27 (annotated)
- Committer:
- ammanvedi
- Date:
- Mon Jan 27 22:46:13 2014 +0000
- Revision:
- 1:6c1df6c9be4d
- Parent:
- 0:d73af3f5c81d
- Child:
- 2:1c8d8d728b93
v1
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 <string.h> |
ammanvedi | 0:d73af3f5c81d | 5 | #include "Websocket.h" |
ammanvedi | 0:d73af3f5c81d | 6 | #include "Updateable.h" |
ammanvedi | 0:d73af3f5c81d | 7 | #include "ASyncTicker.h" |
ammanvedi | 1:6c1df6c9be4d | 8 | #include "xbee.h" |
ammanvedi | 1:6c1df6c9be4d | 9 | #include "xbeeFrame.h" |
ammanvedi | 0:d73af3f5c81d | 10 | #define PORT 80 |
ammanvedi | 0:d73af3f5c81d | 11 | |
ammanvedi | 0:d73af3f5c81d | 12 | |
ammanvedi | 0:d73af3f5c81d | 13 | EthernetInterface ethernet; |
ammanvedi | 0:d73af3f5c81d | 14 | Websocket ws("ws://192.168.0.4:8080/"); |
ammanvedi | 1:6c1df6c9be4d | 15 | xbeeFrame xbee(p9,p10,p11); |
ammanvedi | 1:6c1df6c9be4d | 16 | const char dest_address[8] = {0x00, 0x13, 0xA2, 0x00, 0x40, 0x9B, 0x6D, 0xB0}; |
ammanvedi | 1:6c1df6c9be4d | 17 | char send_data[50] = "xbee string"; |
ammanvedi | 0:d73af3f5c81d | 18 | |
ammanvedi | 0:d73af3f5c81d | 19 | |
ammanvedi | 0:d73af3f5c81d | 20 | |
ammanvedi | 0:d73af3f5c81d | 21 | void pull_updates(){ |
ammanvedi | 1:6c1df6c9be4d | 22 | char str[100]; |
ammanvedi | 1:6c1df6c9be4d | 23 | char id[30]; |
ammanvedi | 1:6c1df6c9be4d | 24 | char new_msg[50]; |
ammanvedi | 1:6c1df6c9be4d | 25 | // string with a message |
ammanvedi | 1:6c1df6c9be4d | 26 | sprintf(str, "PU"); |
ammanvedi | 1:6c1df6c9be4d | 27 | ws.send(str); |
ammanvedi | 1:6c1df6c9be4d | 28 | |
ammanvedi | 1:6c1df6c9be4d | 29 | // clear the buffer and wait a sec... |
ammanvedi | 1:6c1df6c9be4d | 30 | |
ammanvedi | 1:6c1df6c9be4d | 31 | |
ammanvedi | 1:6c1df6c9be4d | 32 | memset(str, 0, 100); |
ammanvedi | 1:6c1df6c9be4d | 33 | wait(0.5f); |
ammanvedi | 1:6c1df6c9be4d | 34 | |
ammanvedi | 1:6c1df6c9be4d | 35 | // websocket server should echo whatever we sent it |
ammanvedi | 1:6c1df6c9be4d | 36 | if (ws.read(str)) { |
ammanvedi | 1:6c1df6c9be4d | 37 | memset(id,0,30); |
ammanvedi | 1:6c1df6c9be4d | 38 | memset(new_msg, 0, 50); |
ammanvedi | 1:6c1df6c9be4d | 39 | sscanf((char *)str, "%s %s ", id, new_msg); |
ammanvedi | 1:6c1df6c9be4d | 40 | printf("id : %s string: %s (original: %s) \n\r", id, new_msg, str); |
ammanvedi | 1:6c1df6c9be4d | 41 | |
ammanvedi | 1:6c1df6c9be4d | 42 | //send string to xbee HERE |
ammanvedi | 1:6c1df6c9be4d | 43 | |
ammanvedi | 1:6c1df6c9be4d | 44 | char to_send[100]; |
ammanvedi | 1:6c1df6c9be4d | 45 | char* p = to_send; |
ammanvedi | 1:6c1df6c9be4d | 46 | char* r = send_data; |
ammanvedi | 1:6c1df6c9be4d | 47 | while(*r) |
ammanvedi | 1:6c1df6c9be4d | 48 | *p++ = *r++; |
ammanvedi | 1:6c1df6c9be4d | 49 | *p++ = ' '; |
ammanvedi | 1:6c1df6c9be4d | 50 | r = new_msg; |
ammanvedi | 1:6c1df6c9be4d | 51 | while(*r) |
ammanvedi | 1:6c1df6c9be4d | 52 | *p++ = *r++; |
ammanvedi | 1:6c1df6c9be4d | 53 | *p = '\0'; |
ammanvedi | 1:6c1df6c9be4d | 54 | |
ammanvedi | 1:6c1df6c9be4d | 55 | char data_buf[50]; |
ammanvedi | 1:6c1df6c9be4d | 56 | xbee.InitFrame(); |
ammanvedi | 1:6c1df6c9be4d | 57 | xbee.SetDestination((unsigned char *)dest_address); |
ammanvedi | 1:6c1df6c9be4d | 58 | xbee.SetPayload(to_send); |
ammanvedi | 1:6c1df6c9be4d | 59 | xbee.AssembleFrame(); |
ammanvedi | 1:6c1df6c9be4d | 60 | xbee.SendFrame(); |
ammanvedi | 1:6c1df6c9be4d | 61 | |
ammanvedi | 1:6c1df6c9be4d | 62 | |
ammanvedi | 1:6c1df6c9be4d | 63 | for(int i = 0; i<2; i++) |
ammanvedi | 1:6c1df6c9be4d | 64 | { |
ammanvedi | 1:6c1df6c9be4d | 65 | xbee.ReceiveFrame(data_buf, 500); |
ammanvedi | 1:6c1df6c9be4d | 66 | if(xbee.frameReceived) |
ammanvedi | 1:6c1df6c9be4d | 67 | { |
ammanvedi | 1:6c1df6c9be4d | 68 | xbee.frameReceived = 0; |
ammanvedi | 1:6c1df6c9be4d | 69 | if(xbee.GetType() == TX_STATUS) |
ammanvedi | 1:6c1df6c9be4d | 70 | { |
ammanvedi | 1:6c1df6c9be4d | 71 | if(xbee.GetStatus() == 0) |
ammanvedi | 1:6c1df6c9be4d | 72 | printf("Send success!\n\r"); |
ammanvedi | 1:6c1df6c9be4d | 73 | else |
ammanvedi | 1:6c1df6c9be4d | 74 | printf("Send failed :(\n\r"); |
ammanvedi | 1:6c1df6c9be4d | 75 | } |
ammanvedi | 1:6c1df6c9be4d | 76 | else if (xbee.GetType() == RX_PACKET_64) |
ammanvedi | 1:6c1df6c9be4d | 77 | printf("Received data: %s\n\r", data_buf); |
ammanvedi | 1:6c1df6c9be4d | 78 | } |
ammanvedi | 1:6c1df6c9be4d | 79 | } |
ammanvedi | 1:6c1df6c9be4d | 80 | |
ammanvedi | 1:6c1df6c9be4d | 81 | |
ammanvedi | 1:6c1df6c9be4d | 82 | |
ammanvedi | 1:6c1df6c9be4d | 83 | |
ammanvedi | 1:6c1df6c9be4d | 84 | } |
ammanvedi | 1:6c1df6c9be4d | 85 | |
ammanvedi | 1:6c1df6c9be4d | 86 | |
ammanvedi | 1:6c1df6c9be4d | 87 | |
ammanvedi | 0:d73af3f5c81d | 88 | |
ammanvedi | 0:d73af3f5c81d | 89 | } |
ammanvedi | 0:d73af3f5c81d | 90 | |
ammanvedi | 1:6c1df6c9be4d | 91 | |
ammanvedi | 1:6c1df6c9be4d | 92 | |
ammanvedi | 0:d73af3f5c81d | 93 | int main () |
ammanvedi | 0:d73af3f5c81d | 94 | { |
ammanvedi | 0:d73af3f5c81d | 95 | |
ammanvedi | 0:d73af3f5c81d | 96 | ethernet.init(); // connect with DHCP |
ammanvedi | 0:d73af3f5c81d | 97 | int ret_val = ethernet.connect(); |
ammanvedi | 0:d73af3f5c81d | 98 | |
ammanvedi | 0:d73af3f5c81d | 99 | if (0 == ret_val) { |
ammanvedi | 0:d73af3f5c81d | 100 | printf("IP Address: %s\n\r", ethernet.getIPAddress()); |
ammanvedi | 0:d73af3f5c81d | 101 | } else { |
ammanvedi | 0:d73af3f5c81d | 102 | error("ethernet failed to connect: %d.\n\r", ret_val); |
ammanvedi | 0:d73af3f5c81d | 103 | } |
ammanvedi | 0:d73af3f5c81d | 104 | |
ammanvedi | 0:d73af3f5c81d | 105 | |
ammanvedi | 0:d73af3f5c81d | 106 | ws.connect(); |
ammanvedi | 0:d73af3f5c81d | 107 | |
ammanvedi | 1:6c1df6c9be4d | 108 | Timer timer; |
ammanvedi | 1:6c1df6c9be4d | 109 | timer.start(); |
ammanvedi | 0:d73af3f5c81d | 110 | |
ammanvedi | 1:6c1df6c9be4d | 111 | while(true){ |
ammanvedi | 1:6c1df6c9be4d | 112 | if(timer.read() >= 5){ |
ammanvedi | 1:6c1df6c9be4d | 113 | //perform checks |
ammanvedi | 1:6c1df6c9be4d | 114 | pull_updates(); |
ammanvedi | 1:6c1df6c9be4d | 115 | timer.reset(); |
ammanvedi | 1:6c1df6c9be4d | 116 | timer.start(); |
ammanvedi | 0:d73af3f5c81d | 117 | } |
ammanvedi | 1:6c1df6c9be4d | 118 | } |
ammanvedi | 1:6c1df6c9be4d | 119 | |
ammanvedi | 1:6c1df6c9be4d | 120 | //pull_updates(); |
ammanvedi | 1:6c1df6c9be4d | 121 | |
ammanvedi | 1:6c1df6c9be4d | 122 | |
ammanvedi | 0:d73af3f5c81d | 123 | ws.close(); |
ammanvedi | 0:d73af3f5c81d | 124 | |
ammanvedi | 0:d73af3f5c81d | 125 | } |