![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
zzz
Dependencies: C12832_lcd ConfigFile LM75B WiflyInterface2 mbed-rtos mbed
Fork of Xbee_Hello_World_B by
main.cpp@1:15d917484a8d, 2016-09-16 (annotated)
- Committer:
- kingkingyyk
- Date:
- Fri Sep 16 07:34:22 2016 +0000
- Revision:
- 1:15d917484a8d
- Parent:
- 0:122702c9168a
- Child:
- 2:ae1a5862504b
Working AT mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tristanjph | 0:122702c9168a | 1 | #include "mbed.h" |
tristanjph | 0:122702c9168a | 2 | #include "xbee.h" |
kingkingyyk | 1:15d917484a8d | 3 | #include "C12832_lcd.h" |
kingkingyyk | 1:15d917484a8d | 4 | #include <string> |
kingkingyyk | 1:15d917484a8d | 5 | #include "EthernetInterface.h" |
tristanjph | 0:122702c9168a | 6 | |
kingkingyyk | 1:15d917484a8d | 7 | xbee xbee1(p9,p10,p30); //Initalise xbee_lib |
kingkingyyk | 1:15d917484a8d | 8 | C12832_LCD lcd; |
kingkingyyk | 1:15d917484a8d | 9 | DigitalOut LanLED(LED1); |
kingkingyyk | 1:15d917484a8d | 10 | DigitalOut XbeeLED(LED2); |
kingkingyyk | 1:15d917484a8d | 11 | DigitalOut LanDataLED(LED3); |
kingkingyyk | 1:15d917484a8d | 12 | EthernetInterface eth; |
kingkingyyk | 1:15d917484a8d | 13 | |
kingkingyyk | 1:15d917484a8d | 14 | bool setupEthernet() { |
kingkingyyk | 1:15d917484a8d | 15 | lcd.cls(); |
kingkingyyk | 1:15d917484a8d | 16 | lcd.locate(0,0); |
kingkingyyk | 1:15d917484a8d | 17 | lcd.printf("Start Ethernet Setup\n"); |
kingkingyyk | 1:15d917484a8d | 18 | |
kingkingyyk | 1:15d917484a8d | 19 | eth.init("172.16.0.2","255.255.0.0","172.16.0.1"); |
kingkingyyk | 1:15d917484a8d | 20 | if(eth.connect()!=0) { |
kingkingyyk | 1:15d917484a8d | 21 | lcd.printf("Not connected!\n"); |
kingkingyyk | 1:15d917484a8d | 22 | return false; |
kingkingyyk | 1:15d917484a8d | 23 | } else { |
kingkingyyk | 1:15d917484a8d | 24 | lcd.printf("IP Address : %s\n", eth.getIPAddress()); |
kingkingyyk | 1:15d917484a8d | 25 | return true; |
kingkingyyk | 1:15d917484a8d | 26 | } |
kingkingyyk | 1:15d917484a8d | 27 | } |
kingkingyyk | 1:15d917484a8d | 28 | |
kingkingyyk | 1:15d917484a8d | 29 | void setupXbee() { |
kingkingyyk | 1:15d917484a8d | 30 | lcd.cls(); |
kingkingyyk | 1:15d917484a8d | 31 | lcd.locate(0,0); |
kingkingyyk | 1:15d917484a8d | 32 | lcd.printf("Start Xbee Setup\n"); |
kingkingyyk | 1:15d917484a8d | 33 | xbee1.Reset(); |
kingkingyyk | 1:15d917484a8d | 34 | xbee1.ConfigMode(); |
kingkingyyk | 1:15d917484a8d | 35 | xbee1.SetPanId(123); |
kingkingyyk | 1:15d917484a8d | 36 | int mac [8]; |
kingkingyyk | 1:15d917484a8d | 37 | xbee1.GetSerial(mac); |
kingkingyyk | 1:15d917484a8d | 38 | xbee1.WriteSettings(); |
kingkingyyk | 1:15d917484a8d | 39 | xbee1.ExitConfigMode(); |
kingkingyyk | 1:15d917484a8d | 40 | lcd.printf("%d %d %d %d %d %d %d %d\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],mac[6],mac[7]); |
kingkingyyk | 1:15d917484a8d | 41 | lcd.printf("Done Setup!\n"); |
kingkingyyk | 1:15d917484a8d | 42 | } |
tristanjph | 0:122702c9168a | 43 | |
kingkingyyk | 1:15d917484a8d | 44 | int main() { |
kingkingyyk | 1:15d917484a8d | 45 | bool flag=true; |
kingkingyyk | 1:15d917484a8d | 46 | flag=setupEthernet(); |
kingkingyyk | 1:15d917484a8d | 47 | LanLED=flag; |
kingkingyyk | 1:15d917484a8d | 48 | wait(0.5); |
kingkingyyk | 1:15d917484a8d | 49 | setupXbee(); |
tristanjph | 0:122702c9168a | 50 | |
kingkingyyk | 1:15d917484a8d | 51 | if (flag) { |
kingkingyyk | 1:15d917484a8d | 52 | UDPSocket socket; |
kingkingyyk | 1:15d917484a8d | 53 | socket.init(); |
kingkingyyk | 1:15d917484a8d | 54 | Endpoint server; |
kingkingyyk | 1:15d917484a8d | 55 | server.set_address("172.16.0.1",40000); |
kingkingyyk | 1:15d917484a8d | 56 | |
kingkingyyk | 1:15d917484a8d | 57 | int count=0; |
kingkingyyk | 1:15d917484a8d | 58 | char read_data[202]; //Xbee buffer size is 202 bytes |
kingkingyyk | 1:15d917484a8d | 59 | |
kingkingyyk | 1:15d917484a8d | 60 | while(1) { |
kingkingyyk | 1:15d917484a8d | 61 | char temp[202]; |
kingkingyyk | 1:15d917484a8d | 62 | XbeeLED=1; |
kingkingyyk | 1:15d917484a8d | 63 | xbee1.RecieveData(temp,10); //Read data from the XBee |
kingkingyyk | 1:15d917484a8d | 64 | XbeeLED=0; |
kingkingyyk | 1:15d917484a8d | 65 | for (int i=0;i<10 && temp[i]!='\0';i++) { |
kingkingyyk | 1:15d917484a8d | 66 | if (temp[i]=='|') { |
kingkingyyk | 1:15d917484a8d | 67 | count=0; |
kingkingyyk | 1:15d917484a8d | 68 | } else if (temp[i]=='&') { |
kingkingyyk | 1:15d917484a8d | 69 | string real_data=""; |
kingkingyyk | 1:15d917484a8d | 70 | for (int i2=0;i2<count;i2++) { |
kingkingyyk | 1:15d917484a8d | 71 | real_data+=read_data[i2]; |
kingkingyyk | 1:15d917484a8d | 72 | } |
kingkingyyk | 1:15d917484a8d | 73 | lcd.cls(); |
kingkingyyk | 1:15d917484a8d | 74 | lcd.locate(0,3); |
kingkingyyk | 1:15d917484a8d | 75 | lcd.printf("%s",real_data); |
kingkingyyk | 1:15d917484a8d | 76 | LanDataLED=1; |
kingkingyyk | 1:15d917484a8d | 77 | socket.sendTo(server,read_data,real_data.length()); |
kingkingyyk | 1:15d917484a8d | 78 | LanDataLED=0; |
kingkingyyk | 1:15d917484a8d | 79 | count=0; |
kingkingyyk | 1:15d917484a8d | 80 | for (int i2=0;i2<202;i2++) { |
kingkingyyk | 1:15d917484a8d | 81 | read_data[i2]='\0'; |
kingkingyyk | 1:15d917484a8d | 82 | } |
kingkingyyk | 1:15d917484a8d | 83 | } else { |
kingkingyyk | 1:15d917484a8d | 84 | read_data[count++]=temp[i]; |
kingkingyyk | 1:15d917484a8d | 85 | } |
kingkingyyk | 1:15d917484a8d | 86 | } |
kingkingyyk | 1:15d917484a8d | 87 | } |
tristanjph | 0:122702c9168a | 88 | } |
tristanjph | 0:122702c9168a | 89 | } |