![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app3
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
Fork of Code_APP1 by
main.cpp@9:400cfcf4b06e, 2017-02-14 (annotated)
- Committer:
- ericbisson
- Date:
- Tue Feb 14 04:08:58 2017 +0000
- Revision:
- 9:400cfcf4b06e
- Parent:
- 8:6f2b7f9b0d0d
app3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericbisson | 9:400cfcf4b06e | 1 | #include "xbee.h" |
ericbisson | 9:400cfcf4b06e | 2 | #include "config.h" |
ericbisson | 9:400cfcf4b06e | 3 | #include "EthernetInterface.h" |
ericbisson | 9:400cfcf4b06e | 4 | #include "Websocket.h" |
LouBe4 | 1:3430643e8ed4 | 5 | |
ericbisson | 9:400cfcf4b06e | 6 | //#define __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 7 | //#ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 8 | Serial pc(USBTX, USBRX, 9600); |
ericbisson | 9:400cfcf4b06e | 9 | //#endif |
ericbisson | 9:400cfcf4b06e | 10 | PwmOut led1(LED1); |
ericbisson | 9:400cfcf4b06e | 11 | PwmOut led4(LED4); |
ericbisson | 8:6f2b7f9b0d0d | 12 | |
LouBe4 | 0:52fba6f1554b | 13 | int main() { |
ericbisson | 6:2ec2a74c807f | 14 | |
ericbisson | 9:400cfcf4b06e | 15 | // Setup du port ethernet |
ericbisson | 9:400cfcf4b06e | 16 | EthernetInterface eth; |
ericbisson | 9:400cfcf4b06e | 17 | eth.init(mbedIp, mbedMask, mbedGateway); |
ericbisson | 9:400cfcf4b06e | 18 | eth.connect(); |
ericbisson | 9:400cfcf4b06e | 19 | |
ericbisson | 9:400cfcf4b06e | 20 | DigitalOut RESET(p8); |
ericbisson | 9:400cfcf4b06e | 21 | Serial XBee(p13, p14, 9600); |
ericbisson | 5:1a60144f7163 | 22 | |
ericbisson | 9:400cfcf4b06e | 23 | // Selon le lab, reset le Xbee |
ericbisson | 9:400cfcf4b06e | 24 | RESET = 0; |
ericbisson | 9:400cfcf4b06e | 25 | wait_ms(400); |
ericbisson | 9:400cfcf4b06e | 26 | RESET = 1; |
ericbisson | 9:400cfcf4b06e | 27 | |
ericbisson | 9:400cfcf4b06e | 28 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 29 | pc.format(8, SerialBase::None, 1); |
ericbisson | 9:400cfcf4b06e | 30 | #endif |
ericbisson | 9:400cfcf4b06e | 31 | XBee.format(8, SerialBase::None, 1); |
ericbisson | 9:400cfcf4b06e | 32 | |
ericbisson | 9:400cfcf4b06e | 33 | CArray DATA_TO_READ; |
ericbisson | 9:400cfcf4b06e | 34 | |
ericbisson | 9:400cfcf4b06e | 35 | bool IsInitialized = false; |
ericbisson | 9:400cfcf4b06e | 36 | bool bAddressSet = false; |
ericbisson | 9:400cfcf4b06e | 37 | char InitBytes = 0; |
ericbisson | 9:400cfcf4b06e | 38 | |
ericbisson | 9:400cfcf4b06e | 39 | Websocket ws(WEBSOCKET_URL); |
ericbisson | 9:400cfcf4b06e | 40 | ws.connect(); |
ericbisson | 9:400cfcf4b06e | 41 | wait(1); |
ericbisson | 5:1a60144f7163 | 42 | |
LouBe4 | 3:5dcf9b78f3ad | 43 | while(1) |
ericbisson | 9:400cfcf4b06e | 44 | { |
ericbisson | 9:400cfcf4b06e | 45 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 46 | if (pc.readable()) |
ericbisson | 9:400cfcf4b06e | 47 | { |
ericbisson | 9:400cfcf4b06e | 48 | XBee.putc(pc.getc()); |
ericbisson | 9:400cfcf4b06e | 49 | } |
ericbisson | 9:400cfcf4b06e | 50 | #endif |
ericbisson | 9:400cfcf4b06e | 51 | |
ericbisson | 9:400cfcf4b06e | 52 | if (IsInitialized) |
ericbisson | 9:400cfcf4b06e | 53 | { |
ericbisson | 9:400cfcf4b06e | 54 | DATA_TO_READ._ptr = NULL; |
ericbisson | 9:400cfcf4b06e | 55 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 56 | if (XBee.readable()) |
ericbisson | 9:400cfcf4b06e | 57 | { |
ericbisson | 9:400cfcf4b06e | 58 | pc.putc(XBee.getc()); |
ericbisson | 9:400cfcf4b06e | 59 | } |
ericbisson | 9:400cfcf4b06e | 60 | #endif |
ericbisson | 9:400cfcf4b06e | 61 | read(&XBee, &DATA_TO_READ); |
ericbisson | 5:1a60144f7163 | 62 | |
ericbisson | 9:400cfcf4b06e | 63 | if (DATA_TO_READ._ptr != NULL) |
ericbisson | 9:400cfcf4b06e | 64 | { |
ericbisson | 9:400cfcf4b06e | 65 | led1 = !led1; |
ericbisson | 9:400cfcf4b06e | 66 | |
ericbisson | 9:400cfcf4b06e | 67 | if (!bAddressSet) |
ericbisson | 9:400cfcf4b06e | 68 | { |
ericbisson | 9:400cfcf4b06e | 69 | bAddressSet = true; |
ericbisson | 7:a57409bdf777 | 70 | |
ericbisson | 9:400cfcf4b06e | 71 | CArray DATA_TO_SEND; |
ericbisson | 9:400cfcf4b06e | 72 | |
ericbisson | 9:400cfcf4b06e | 73 | DATA_TO_SEND._64bit = DATA_TO_READ._64bit; // <-- Je récupère l'addresse au premier paquet. Inutile de faire plus pour l'app ... |
ericbisson | 9:400cfcf4b06e | 74 | DATA_TO_SEND._16bit = DATA_TO_READ._16bit; |
ericbisson | 9:400cfcf4b06e | 75 | |
ericbisson | 9:400cfcf4b06e | 76 | DATA_TO_SEND._FrameType = 0x17; // Remote AT Request |
ericbisson | 9:400cfcf4b06e | 77 | |
ericbisson | 9:400cfcf4b06e | 78 | // options pour l'envoie au coordinateur |
ericbisson | 9:400cfcf4b06e | 79 | DATA_TO_SEND.options = new char[1]; |
ericbisson | 9:400cfcf4b06e | 80 | DATA_TO_SEND.options[0] = 0x02; |
ericbisson | 9:400cfcf4b06e | 81 | DATA_TO_SEND.opt_size = 1; |
ericbisson | 9:400cfcf4b06e | 82 | |
ericbisson | 9:400cfcf4b06e | 83 | DATA_TO_SEND._ptr = new char[3]; |
ericbisson | 9:400cfcf4b06e | 84 | DATA_TO_SEND._ptr[0] = 'L'; |
ericbisson | 9:400cfcf4b06e | 85 | DATA_TO_SEND._ptr[1] = 'T'; |
ericbisson | 9:400cfcf4b06e | 86 | DATA_TO_SEND._ptr[2] = 100; // x10ms donc 1000ms donc 1hz |
ericbisson | 9:400cfcf4b06e | 87 | DATA_TO_SEND.size = 3; |
ericbisson | 9:400cfcf4b06e | 88 | |
ericbisson | 9:400cfcf4b06e | 89 | send(&XBee, &DATA_TO_SEND); |
ericbisson | 9:400cfcf4b06e | 90 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 91 | send(&pc, &DATA_TO_SEND); |
ericbisson | 9:400cfcf4b06e | 92 | #endif |
ericbisson | 9:400cfcf4b06e | 93 | |
ericbisson | 9:400cfcf4b06e | 94 | delete DATA_TO_SEND._ptr; |
ericbisson | 9:400cfcf4b06e | 95 | delete DATA_TO_SEND.options; |
ericbisson | 9:400cfcf4b06e | 96 | } |
ericbisson | 9:400cfcf4b06e | 97 | |
ericbisson | 9:400cfcf4b06e | 98 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 99 | for (int i = 0; i < DATA_TO_READ.size; i++) |
ericbisson | 9:400cfcf4b06e | 100 | { |
ericbisson | 9:400cfcf4b06e | 101 | pc.putc(DATA_TO_READ._ptr[i]); // debug ; send to pc |
ericbisson | 9:400cfcf4b06e | 102 | } |
ericbisson | 9:400cfcf4b06e | 103 | #endif |
ericbisson | 9:400cfcf4b06e | 104 | ws.send(DATA_TO_READ._ptr); |
ericbisson | 9:400cfcf4b06e | 105 | wait_ms(5); |
ericbisson | 9:400cfcf4b06e | 106 | char* result; |
ericbisson | 9:400cfcf4b06e | 107 | ws.read(result); |
ericbisson | 9:400cfcf4b06e | 108 | |
ericbisson | 9:400cfcf4b06e | 109 | pc.printf(result); |
ericbisson | 9:400cfcf4b06e | 110 | |
ericbisson | 9:400cfcf4b06e | 111 | delete DATA_TO_READ._ptr; |
ericbisson | 9:400cfcf4b06e | 112 | } |
ericbisson | 9:400cfcf4b06e | 113 | } |
ericbisson | 9:400cfcf4b06e | 114 | else if (XBee.readable()) |
ericbisson | 5:1a60144f7163 | 115 | { |
ericbisson | 9:400cfcf4b06e | 116 | #ifdef __DEBUG__ |
ericbisson | 9:400cfcf4b06e | 117 | pc.putc(XBee.getc()); |
ericbisson | 9:400cfcf4b06e | 118 | #endif |
ericbisson | 9:400cfcf4b06e | 119 | InitBytes++; |
ericbisson | 9:400cfcf4b06e | 120 | if (InitBytes == 6) |
ericbisson | 9:400cfcf4b06e | 121 | { |
ericbisson | 9:400cfcf4b06e | 122 | IsInitialized = true; |
ericbisson | 9:400cfcf4b06e | 123 | } |
ericbisson | 5:1a60144f7163 | 124 | } |
ericbisson | 5:1a60144f7163 | 125 | } |
ericbisson | 5:1a60144f7163 | 126 | } |