tes
Dependencies: ASyncTicker EthernetInterface WebSocketClient mbed-rtos mbed MbedJSONValue xbee_lib
Diff: main.cpp
- Revision:
- 1:6c1df6c9be4d
- Parent:
- 0:d73af3f5c81d
- Child:
- 2:1c8d8d728b93
diff -r d73af3f5c81d -r 6c1df6c9be4d main.cpp --- a/main.cpp Mon Jan 27 21:50:20 2014 +0000 +++ b/main.cpp Mon Jan 27 22:46:13 2014 +0000 @@ -5,21 +5,91 @@ #include "Websocket.h" #include "Updateable.h" #include "ASyncTicker.h" +#include "xbee.h" +#include "xbeeFrame.h" #define PORT 80 EthernetInterface ethernet; Websocket ws("ws://192.168.0.4:8080/"); -Ticker pull_ticker; +xbeeFrame xbee(p9,p10,p11); +const char dest_address[8] = {0x00, 0x13, 0xA2, 0x00, 0x40, 0x9B, 0x6D, 0xB0}; +char send_data[50] = "xbee string"; void pull_updates(){ - wait(0.1f); - printf("%s", "hello"); + char str[100]; + char id[30]; + char new_msg[50]; + // string with a message + sprintf(str, "PU"); + ws.send(str); + + // clear the buffer and wait a sec... + + + memset(str, 0, 100); + wait(0.5f); + + // websocket server should echo whatever we sent it + if (ws.read(str)) { + memset(id,0,30); + memset(new_msg, 0, 50); + sscanf((char *)str, "%s %s ", id, new_msg); + printf("id : %s string: %s (original: %s) \n\r", id, new_msg, str); + + //send string to xbee HERE + + char to_send[100]; + char* p = to_send; + char* r = send_data; + while(*r) + *p++ = *r++; + *p++ = ' '; + r = new_msg; + while(*r) + *p++ = *r++; + *p = '\0'; + + char data_buf[50]; + xbee.InitFrame(); + xbee.SetDestination((unsigned char *)dest_address); + xbee.SetPayload(to_send); + xbee.AssembleFrame(); + xbee.SendFrame(); + + + for(int i = 0; i<2; i++) + { + xbee.ReceiveFrame(data_buf, 500); + if(xbee.frameReceived) + { + xbee.frameReceived = 0; + if(xbee.GetType() == TX_STATUS) + { + if(xbee.GetStatus() == 0) + printf("Send success!\n\r"); + else + printf("Send failed :(\n\r"); + } + else if (xbee.GetType() == RX_PACKET_64) + printf("Received data: %s\n\r", data_buf); + } + } + + + + + } + + + } + + int main () { @@ -35,24 +105,21 @@ ws.connect(); - char str[100]; - // string with a message - sprintf(str, "PU"); - ws.send(str); - - // clear the buffer and wait a sec... - memset(str, 0, 100); - wait(0.5f); + Timer timer; + timer.start(); - // websocket server should echo whatever we sent it - if (ws.read(str)) { - printf("rcv'd: %s\n\r", str); + while(true){ + if(timer.read() >= 5){ + //perform checks + pull_updates(); + timer.reset(); + timer.start(); } + } + + //pull_updates(); + + ws.close(); - pull_ticker.attach(&pull_updates, 5.0); - while(1){ - - wait(0.1f); - } }