pre commentaire

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Fork of Code_APP3_C by Éric Bisson

Committer:
JoeyDionne
Date:
Tue Feb 14 17:36:39 2017 +0000
Revision:
11:f0ceb634d85c
Parent:
10:565271e4d52a
pre commentaire

Who changed what in which revision?

UserRevisionLine numberNew 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
JoeyDionne 10:565271e4d52a 16 EthernetInterface eth;
JoeyDionne 10:565271e4d52a 17 eth.init(); //Use DHCP
JoeyDionne 10:565271e4d52a 18 //eth.init(mbedIp, mbedMask, mbedGateway); //Use config
ericbisson 9:400cfcf4b06e 19 eth.connect();
ericbisson 9:400cfcf4b06e 20
ericbisson 9:400cfcf4b06e 21 DigitalOut RESET(p8);
ericbisson 9:400cfcf4b06e 22 Serial XBee(p13, p14, 9600);
ericbisson 5:1a60144f7163 23
ericbisson 9:400cfcf4b06e 24 // Selon le lab, reset le Xbee
ericbisson 9:400cfcf4b06e 25 RESET = 0;
ericbisson 9:400cfcf4b06e 26 wait_ms(400);
ericbisson 9:400cfcf4b06e 27 RESET = 1;
ericbisson 9:400cfcf4b06e 28
ericbisson 9:400cfcf4b06e 29 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 30 pc.format(8, SerialBase::None, 1);
ericbisson 9:400cfcf4b06e 31 #endif
ericbisson 9:400cfcf4b06e 32 XBee.format(8, SerialBase::None, 1);
ericbisson 9:400cfcf4b06e 33
ericbisson 9:400cfcf4b06e 34 CArray DATA_TO_READ;
ericbisson 9:400cfcf4b06e 35
ericbisson 9:400cfcf4b06e 36 bool IsInitialized = false;
ericbisson 9:400cfcf4b06e 37 bool bAddressSet = false;
ericbisson 9:400cfcf4b06e 38 char InitBytes = 0;
ericbisson 9:400cfcf4b06e 39
ericbisson 9:400cfcf4b06e 40 Websocket ws(WEBSOCKET_URL);
ericbisson 9:400cfcf4b06e 41 ws.connect();
JoeyDionne 11:f0ceb634d85c 42 wait(1);
ericbisson 5:1a60144f7163 43
LouBe4 3:5dcf9b78f3ad 44 while(1)
ericbisson 9:400cfcf4b06e 45 {
ericbisson 9:400cfcf4b06e 46 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 47 if (pc.readable())
ericbisson 9:400cfcf4b06e 48 {
ericbisson 9:400cfcf4b06e 49 XBee.putc(pc.getc());
ericbisson 9:400cfcf4b06e 50 }
ericbisson 9:400cfcf4b06e 51 #endif
ericbisson 9:400cfcf4b06e 52
ericbisson 9:400cfcf4b06e 53 if (IsInitialized)
ericbisson 9:400cfcf4b06e 54 {
ericbisson 9:400cfcf4b06e 55 DATA_TO_READ._ptr = NULL;
ericbisson 9:400cfcf4b06e 56 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 57 if (XBee.readable())
ericbisson 9:400cfcf4b06e 58 {
ericbisson 9:400cfcf4b06e 59 pc.putc(XBee.getc());
ericbisson 9:400cfcf4b06e 60 }
ericbisson 9:400cfcf4b06e 61 #endif
ericbisson 9:400cfcf4b06e 62 read(&XBee, &DATA_TO_READ);
ericbisson 5:1a60144f7163 63
ericbisson 9:400cfcf4b06e 64 if (DATA_TO_READ._ptr != NULL)
ericbisson 9:400cfcf4b06e 65 {
ericbisson 9:400cfcf4b06e 66 led1 = !led1;
ericbisson 9:400cfcf4b06e 67
ericbisson 9:400cfcf4b06e 68 if (!bAddressSet)
ericbisson 9:400cfcf4b06e 69 {
ericbisson 9:400cfcf4b06e 70 bAddressSet = true;
ericbisson 7:a57409bdf777 71
ericbisson 9:400cfcf4b06e 72 CArray DATA_TO_SEND;
ericbisson 9:400cfcf4b06e 73
ericbisson 9:400cfcf4b06e 74 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 75 DATA_TO_SEND._16bit = DATA_TO_READ._16bit;
ericbisson 9:400cfcf4b06e 76
ericbisson 9:400cfcf4b06e 77 DATA_TO_SEND._FrameType = 0x17; // Remote AT Request
ericbisson 9:400cfcf4b06e 78
ericbisson 9:400cfcf4b06e 79 // options pour l'envoie au coordinateur
ericbisson 9:400cfcf4b06e 80 DATA_TO_SEND.options = new char[1];
ericbisson 9:400cfcf4b06e 81 DATA_TO_SEND.options[0] = 0x02;
ericbisson 9:400cfcf4b06e 82 DATA_TO_SEND.opt_size = 1;
ericbisson 9:400cfcf4b06e 83
ericbisson 9:400cfcf4b06e 84 DATA_TO_SEND._ptr = new char[3];
ericbisson 9:400cfcf4b06e 85 DATA_TO_SEND._ptr[0] = 'L';
ericbisson 9:400cfcf4b06e 86 DATA_TO_SEND._ptr[1] = 'T';
ericbisson 9:400cfcf4b06e 87 DATA_TO_SEND._ptr[2] = 100; // x10ms donc 1000ms donc 1hz
ericbisson 9:400cfcf4b06e 88 DATA_TO_SEND.size = 3;
ericbisson 9:400cfcf4b06e 89
ericbisson 9:400cfcf4b06e 90 send(&XBee, &DATA_TO_SEND);
ericbisson 9:400cfcf4b06e 91 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 92 send(&pc, &DATA_TO_SEND);
ericbisson 9:400cfcf4b06e 93 #endif
ericbisson 9:400cfcf4b06e 94
ericbisson 9:400cfcf4b06e 95 delete DATA_TO_SEND._ptr;
ericbisson 9:400cfcf4b06e 96 delete DATA_TO_SEND.options;
ericbisson 9:400cfcf4b06e 97 }
ericbisson 9:400cfcf4b06e 98
ericbisson 9:400cfcf4b06e 99 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 100 for (int i = 0; i < DATA_TO_READ.size; i++)
ericbisson 9:400cfcf4b06e 101 {
ericbisson 9:400cfcf4b06e 102 pc.putc(DATA_TO_READ._ptr[i]); // debug ; send to pc
ericbisson 9:400cfcf4b06e 103 }
ericbisson 9:400cfcf4b06e 104 #endif
ericbisson 9:400cfcf4b06e 105 ws.send(DATA_TO_READ._ptr);
ericbisson 9:400cfcf4b06e 106
ericbisson 9:400cfcf4b06e 107 delete DATA_TO_READ._ptr;
ericbisson 9:400cfcf4b06e 108 }
ericbisson 9:400cfcf4b06e 109 }
ericbisson 9:400cfcf4b06e 110 else if (XBee.readable())
ericbisson 5:1a60144f7163 111 {
ericbisson 9:400cfcf4b06e 112 #ifdef __DEBUG__
ericbisson 9:400cfcf4b06e 113 pc.putc(XBee.getc());
ericbisson 9:400cfcf4b06e 114 #endif
ericbisson 9:400cfcf4b06e 115 InitBytes++;
ericbisson 9:400cfcf4b06e 116 if (InitBytes == 6)
ericbisson 9:400cfcf4b06e 117 {
ericbisson 9:400cfcf4b06e 118 IsInitialized = true;
ericbisson 9:400cfcf4b06e 119 }
ericbisson 5:1a60144f7163 120 }
ericbisson 5:1a60144f7163 121 }
ericbisson 5:1a60144f7163 122 }