This is the coordinator node.

Dependencies:   mbed-rtos mbed Zigbee_communication EthernetInterface WebSocketClient

Committer:
ShaolinPoutine
Date:
Tue Feb 14 20:34:45 2017 +0000
Revision:
6:bd6001a78537
Parent:
4:d9968ed09187
Child:
7:4b85a9051b3a
Added web comm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EmileArseneault 2:51d67a8b2325 1 // Emile Arseneault : arse1503
EmileArseneault 2:51d67a8b2325 2 // Raphael Drouin : dror2204
EmileArseneault 2:51d67a8b2325 3
EmileArseneault 2:51d67a8b2325 4 // Coordinator Node
EmileArseneault 2:51d67a8b2325 5
ShaolinPoutine 0:4cfeffb4d773 6 #include "mbed.h"
ShaolinPoutine 0:4cfeffb4d773 7 #define FRAMEDELIMITER 0x7E
ShaolinPoutine 0:4cfeffb4d773 8 #include "rtos.h"
ShaolinPoutine 4:d9968ed09187 9 #include "xbee.h"
ShaolinPoutine 0:4cfeffb4d773 10
ShaolinPoutine 0:4cfeffb4d773 11 // 4269
ShaolinPoutine 0:4cfeffb4d773 12 Serial pc(USBTX, USBRX);
ShaolinPoutine 0:4cfeffb4d773 13 DigitalOut myled(LED1);
ShaolinPoutine 0:4cfeffb4d773 14
ShaolinPoutine 4:d9968ed09187 15 Mail<char, 250> mymail;
ShaolinPoutine 6:bd6001a78537 16 Mail<char[256], 16> webmail;
ShaolinPoutine 6:bd6001a78537 17 XBee xbee(p8, p13, p14, &mymail, &webmail);
ShaolinPoutine 0:4cfeffb4d773 18
ShaolinPoutine 0:4cfeffb4d773 19 void ReadSerial()
ShaolinPoutine 0:4cfeffb4d773 20 {
ShaolinPoutine 0:4cfeffb4d773 21 // 00 13 A2 00
ShaolinPoutine 0:4cfeffb4d773 22 // 40 3E 09 63
ShaolinPoutine 4:d9968ed09187 23 xbee.SendATCommand('S', 'H');
ShaolinPoutine 4:d9968ed09187 24 xbee.SendATCommand('S', 'L');
ShaolinPoutine 0:4cfeffb4d773 25 }
ShaolinPoutine 0:4cfeffb4d773 26
EmileArseneault 2:51d67a8b2325 27 void InitialisePANID(char PANIDMSB, char PANIDLSB)
EmileArseneault 2:51d67a8b2325 28 {
EmileArseneault 2:51d67a8b2325 29 char PanID[2] = {PANIDMSB, PANIDLSB};
EmileArseneault 2:51d67a8b2325 30
ShaolinPoutine 4:d9968ed09187 31 xbee.SendATCommand('I', 'D', &PanID[0], 2);
ShaolinPoutine 6:bd6001a78537 32 wait(5);
ShaolinPoutine 6:bd6001a78537 33 xbee.SendATCommand('M','Y');
ShaolinPoutine 6:bd6001a78537 34 xbee.SendATCommand('O','P');
EmileArseneault 2:51d67a8b2325 35 }
EmileArseneault 2:51d67a8b2325 36
EmileArseneault 2:51d67a8b2325 37 void BroadcastHelloWorld()
EmileArseneault 2:51d67a8b2325 38 {
EmileArseneault 2:51d67a8b2325 39 char hello[5] = {'H', 'e', 'l', 'l', 'o'};
EmileArseneault 2:51d67a8b2325 40 char world[5] = {'w', 'o', 'r', 'l', 'd'};
EmileArseneault 2:51d67a8b2325 41
EmileArseneault 2:51d67a8b2325 42 while (1)
EmileArseneault 2:51d67a8b2325 43 {
ShaolinPoutine 4:d9968ed09187 44 xbee.ZigBeeTransmit(0xFFFE, 0x000000000000FFFF, &hello[0], 5);
ShaolinPoutine 4:d9968ed09187 45 xbee.ZigBeeTransmit(0xFFFE, 0x000000000000FFFF, &world[0], 5);
EmileArseneault 2:51d67a8b2325 46 wait(2);
EmileArseneault 2:51d67a8b2325 47 }
EmileArseneault 2:51d67a8b2325 48 }
EmileArseneault 2:51d67a8b2325 49
ShaolinPoutine 0:4cfeffb4d773 50 void xbee_reader()
ShaolinPoutine 0:4cfeffb4d773 51 {
ShaolinPoutine 0:4cfeffb4d773 52 while(1)
ShaolinPoutine 0:4cfeffb4d773 53 {
ShaolinPoutine 4:d9968ed09187 54 xbee.InterpretMessage();
ShaolinPoutine 0:4cfeffb4d773 55 wait(0.001);
ShaolinPoutine 0:4cfeffb4d773 56 }
ShaolinPoutine 0:4cfeffb4d773 57 }
ShaolinPoutine 0:4cfeffb4d773 58
ShaolinPoutine 0:4cfeffb4d773 59 void tick()
ShaolinPoutine 0:4cfeffb4d773 60 {
ShaolinPoutine 0:4cfeffb4d773 61 myled = !myled;
ShaolinPoutine 0:4cfeffb4d773 62 }
ShaolinPoutine 0:4cfeffb4d773 63
ShaolinPoutine 4:d9968ed09187 64 void msg_reader()
ShaolinPoutine 4:d9968ed09187 65 {
ShaolinPoutine 4:d9968ed09187 66 while (true) {
ShaolinPoutine 4:d9968ed09187 67 osEvent evt = mymail.get();
ShaolinPoutine 4:d9968ed09187 68 if (evt.status == osEventMail)
ShaolinPoutine 4:d9968ed09187 69 {
ShaolinPoutine 4:d9968ed09187 70 char *c = (char*)evt.value.p;
ShaolinPoutine 4:d9968ed09187 71 pc.putc(*c);
ShaolinPoutine 4:d9968ed09187 72 mymail.free(c);
ShaolinPoutine 4:d9968ed09187 73 }
ShaolinPoutine 4:d9968ed09187 74 }
ShaolinPoutine 4:d9968ed09187 75 }
ShaolinPoutine 4:d9968ed09187 76
ShaolinPoutine 6:bd6001a78537 77 void web()
ShaolinPoutine 6:bd6001a78537 78 {
ShaolinPoutine 6:bd6001a78537 79 while (true) {
ShaolinPoutine 6:bd6001a78537 80 osEvent evt = webmail.get();
ShaolinPoutine 6:bd6001a78537 81 if (evt.status == osEventMail)
ShaolinPoutine 6:bd6001a78537 82 {
ShaolinPoutine 6:bd6001a78537 83 pc.printf("received");
ShaolinPoutine 6:bd6001a78537 84 char *c = (char*)evt.value.p;
ShaolinPoutine 6:bd6001a78537 85 // Send email
ShaolinPoutine 6:bd6001a78537 86 pc.printf(c);
ShaolinPoutine 6:bd6001a78537 87 mymail.free(c);
ShaolinPoutine 6:bd6001a78537 88 }
ShaolinPoutine 6:bd6001a78537 89 }
ShaolinPoutine 6:bd6001a78537 90 }
ShaolinPoutine 6:bd6001a78537 91
ShaolinPoutine 0:4cfeffb4d773 92 int main() {
ShaolinPoutine 4:d9968ed09187 93 pc.printf("\r\nI am coordinator\r\n");
ShaolinPoutine 4:d9968ed09187 94
ShaolinPoutine 0:4cfeffb4d773 95 Ticker ticker;
ShaolinPoutine 0:4cfeffb4d773 96 ticker.attach(&tick, 1);
ShaolinPoutine 4:d9968ed09187 97
ShaolinPoutine 4:d9968ed09187 98 Thread thread(xbee_reader);
ShaolinPoutine 4:d9968ed09187 99 Thread thread_reader(msg_reader);
ShaolinPoutine 6:bd6001a78537 100 Thread thread2(web);
ShaolinPoutine 0:4cfeffb4d773 101
ShaolinPoutine 0:4cfeffb4d773 102 wait(3);
ShaolinPoutine 0:4cfeffb4d773 103 ReadSerial();
EmileArseneault 2:51d67a8b2325 104 InitialisePANID(0x42, 0x69);
ShaolinPoutine 6:bd6001a78537 105
ShaolinPoutine 6:bd6001a78537 106 xbee.appendBuffer("This message contains numbers ");
ShaolinPoutine 6:bd6001a78537 107 xbee.appendBuffer("1");
ShaolinPoutine 6:bd6001a78537 108 xbee.appendBuffer("0");
ShaolinPoutine 6:bd6001a78537 109 xbee.sendBuffer();
ShaolinPoutine 6:bd6001a78537 110
EmileArseneault 2:51d67a8b2325 111 BroadcastHelloWorld();
ShaolinPoutine 0:4cfeffb4d773 112
ShaolinPoutine 0:4cfeffb4d773 113 while(1) {
ShaolinPoutine 0:4cfeffb4d773 114 }
ShaolinPoutine 0:4cfeffb4d773 115 }