kEIS
Dependencies: C12832_lcd EthernetInterface_ccx LM75B mbed-rtos mbed
main.cpp@0:410dd8d5bd07, 2013-09-28 (annotated)
- Committer:
- khayakawa
- Date:
- Sat Sep 28 16:37:47 2013 +0000
- Revision:
- 0:410dd8d5bd07
KEIS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
khayakawa | 0:410dd8d5bd07 | 1 | #include "mbed.h" |
khayakawa | 0:410dd8d5bd07 | 2 | #include "EthernetInterface.h" |
khayakawa | 0:410dd8d5bd07 | 3 | #include "C12832_lcd.h" |
khayakawa | 0:410dd8d5bd07 | 4 | #include "LM75B.h" |
khayakawa | 0:410dd8d5bd07 | 5 | |
khayakawa | 0:410dd8d5bd07 | 6 | C12832_LCD lcd; |
khayakawa | 0:410dd8d5bd07 | 7 | |
khayakawa | 0:410dd8d5bd07 | 8 | const char* SERVER_ADDRESS = "192.168.50.121"; |
khayakawa | 0:410dd8d5bd07 | 9 | const int SERVER_PORT = 80; |
khayakawa | 0:410dd8d5bd07 | 10 | |
khayakawa | 0:410dd8d5bd07 | 11 | const char* MY_ADDRESS = "192.168.50.125"; |
khayakawa | 0:410dd8d5bd07 | 12 | const char* MY_MASK = "255.255.255.0"; |
khayakawa | 0:410dd8d5bd07 | 13 | const char* MY_GATEWAY = "192.168.50.254"; |
khayakawa | 0:410dd8d5bd07 | 14 | |
khayakawa | 0:410dd8d5bd07 | 15 | EthernetInterface eth; |
khayakawa | 0:410dd8d5bd07 | 16 | DigitalOut myled1(LED1); |
khayakawa | 0:410dd8d5bd07 | 17 | LM75B tmp(p28,p27); |
khayakawa | 0:410dd8d5bd07 | 18 | Serial pc(USBTX, USBRX); // tx, rx |
khayakawa | 0:410dd8d5bd07 | 19 | Serial dev_xbee(p9, p10); // tx, rx |
khayakawa | 0:410dd8d5bd07 | 20 | |
khayakawa | 0:410dd8d5bd07 | 21 | int main(void) { |
khayakawa | 0:410dd8d5bd07 | 22 | float temp1 ; |
khayakawa | 0:410dd8d5bd07 | 23 | |
khayakawa | 0:410dd8d5bd07 | 24 | int pkt_phase=0; |
khayakawa | 0:410dd8d5bd07 | 25 | int pkt_count=0; |
khayakawa | 0:410dd8d5bd07 | 26 | char* ch[16]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; |
khayakawa | 0:410dd8d5bd07 | 27 | char v_val[2]; |
khayakawa | 0:410dd8d5bd07 | 28 | int over_flag=0; |
khayakawa | 0:410dd8d5bd07 | 29 | TCPSocketConnection socket; |
khayakawa | 0:410dd8d5bd07 | 30 | char val; |
khayakawa | 0:410dd8d5bd07 | 31 | int v_val_int; |
khayakawa | 0:410dd8d5bd07 | 32 | int tmp1,tmp2, i, n; |
khayakawa | 0:410dd8d5bd07 | 33 | int sd_data_on[20]={0x7E,0x00,0x10,0x17,0x01,0x00,0x13,0xA2,0x00,0x40,0x92,0xFF,0x77,0xFF,0xFE,0x02,0x44,0x32,0x05,0x70}; |
khayakawa | 0:410dd8d5bd07 | 34 | int sd_data_off[20]={0x7E,0x00,0x10,0x17,0x01,0x00,0x13,0xA2,0x00,0x40,0x92,0xFF,0x77,0xFF,0xFE,0x02,0x44,0x32,0x04,0x71}; |
khayakawa | 0:410dd8d5bd07 | 35 | lcd.cls(); |
khayakawa | 0:410dd8d5bd07 | 36 | |
khayakawa | 0:410dd8d5bd07 | 37 | eth.init(MY_ADDRESS,MY_MASK,MY_GATEWAY); |
khayakawa | 0:410dd8d5bd07 | 38 | eth.connect(); |
khayakawa | 0:410dd8d5bd07 | 39 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
khayakawa | 0:410dd8d5bd07 | 40 | |
khayakawa | 0:410dd8d5bd07 | 41 | while(1){ |
khayakawa | 0:410dd8d5bd07 | 42 | myled1=1; |
khayakawa | 0:410dd8d5bd07 | 43 | val=dev_xbee.getc(); |
khayakawa | 0:410dd8d5bd07 | 44 | pkt_count++; |
khayakawa | 0:410dd8d5bd07 | 45 | if(pkt_phase==0 && val==0x7E){ |
khayakawa | 0:410dd8d5bd07 | 46 | pkt_phase=1; |
khayakawa | 0:410dd8d5bd07 | 47 | pkt_count=1; |
khayakawa | 0:410dd8d5bd07 | 48 | } |
khayakawa | 0:410dd8d5bd07 | 49 | |
khayakawa | 0:410dd8d5bd07 | 50 | if(pkt_phase==1 && pkt_count==4){ |
khayakawa | 0:410dd8d5bd07 | 51 | if( val==0x92 ){ |
khayakawa | 0:410dd8d5bd07 | 52 | pkt_phase=2; |
khayakawa | 0:410dd8d5bd07 | 53 | }else{ |
khayakawa | 0:410dd8d5bd07 | 54 | pkt_count=0; |
khayakawa | 0:410dd8d5bd07 | 55 | pkt_phase=0; |
khayakawa | 0:410dd8d5bd07 | 56 | } |
khayakawa | 0:410dd8d5bd07 | 57 | } |
khayakawa | 0:410dd8d5bd07 | 58 | |
khayakawa | 0:410dd8d5bd07 | 59 | if(pkt_phase==2 && pkt_count==12){ |
khayakawa | 0:410dd8d5bd07 | 60 | if( val==0x77){ |
khayakawa | 0:410dd8d5bd07 | 61 | pkt_phase=3; |
khayakawa | 0:410dd8d5bd07 | 62 | }else{ |
khayakawa | 0:410dd8d5bd07 | 63 | pkt_count=0; |
khayakawa | 0:410dd8d5bd07 | 64 | pkt_phase=0; |
khayakawa | 0:410dd8d5bd07 | 65 | } |
khayakawa | 0:410dd8d5bd07 | 66 | } |
khayakawa | 0:410dd8d5bd07 | 67 | |
khayakawa | 0:410dd8d5bd07 | 68 | |
khayakawa | 0:410dd8d5bd07 | 69 | if(pkt_phase==3 && pkt_count==22){ |
khayakawa | 0:410dd8d5bd07 | 70 | pkt_phase=4; |
khayakawa | 0:410dd8d5bd07 | 71 | v_val[0]=val; |
khayakawa | 0:410dd8d5bd07 | 72 | } |
khayakawa | 0:410dd8d5bd07 | 73 | |
khayakawa | 0:410dd8d5bd07 | 74 | if(pkt_phase==4 && pkt_count==23){ |
khayakawa | 0:410dd8d5bd07 | 75 | pkt_phase=0; |
khayakawa | 0:410dd8d5bd07 | 76 | pkt_count=0; |
khayakawa | 0:410dd8d5bd07 | 77 | v_val[1]=val; |
khayakawa | 0:410dd8d5bd07 | 78 | |
khayakawa | 0:410dd8d5bd07 | 79 | v_val_int = (int)v_val[0]*256 + (int)v_val[1]; |
khayakawa | 0:410dd8d5bd07 | 80 | if(v_val_int < 1000 ){ |
khayakawa | 0:410dd8d5bd07 | 81 | for(i=0 ; i<20 ; i++){ |
khayakawa | 0:410dd8d5bd07 | 82 | dev_xbee.putc(sd_data_on[i]); |
khayakawa | 0:410dd8d5bd07 | 83 | } |
khayakawa | 0:410dd8d5bd07 | 84 | } |
khayakawa | 0:410dd8d5bd07 | 85 | if(v_val_int >= 1000 ){ |
khayakawa | 0:410dd8d5bd07 | 86 | for(i=0 ; i<20 ; i++){ |
khayakawa | 0:410dd8d5bd07 | 87 | dev_xbee.putc(sd_data_off[i]); |
khayakawa | 0:410dd8d5bd07 | 88 | } |
khayakawa | 0:410dd8d5bd07 | 89 | } |
khayakawa | 0:410dd8d5bd07 | 90 | lcd.cls(); |
khayakawa | 0:410dd8d5bd07 | 91 | lcd.locate(0,3); |
khayakawa | 0:410dd8d5bd07 | 92 | lcd.printf("v_val1:%d ",v_val_int); |
khayakawa | 0:410dd8d5bd07 | 93 | temp1=tmp.read(); |
khayakawa | 0:410dd8d5bd07 | 94 | if (socket.connect(SERVER_ADDRESS, SERVER_PORT) < 0) { |
khayakawa | 0:410dd8d5bd07 | 95 | pc.printf("Unable to connect to (%s) on port (%d)\n", SERVER_ADDRESS, SERVER_PORT); |
khayakawa | 0:410dd8d5bd07 | 96 | }else { |
khayakawa | 0:410dd8d5bd07 | 97 | pc.printf("connection succeded"); |
khayakawa | 0:410dd8d5bd07 | 98 | char buffer[900]; |
khayakawa | 0:410dd8d5bd07 | 99 | sprintf(buffer,"POST /next7/REST/Record/HayaLab03 HTTP/1.1\r\n" |
khayakawa | 0:410dd8d5bd07 | 100 | "Host: 192.168.50.121\r\n" |
khayakawa | 0:410dd8d5bd07 | 101 | "Content-Type: application/xml\r\n" |
khayakawa | 0:410dd8d5bd07 | 102 | "Content-Length: 690\r\n" |
khayakawa | 0:410dd8d5bd07 | 103 | "\r\n" |
khayakawa | 0:410dd8d5bd07 | 104 | "<PostData xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" |
khayakawa | 0:410dd8d5bd07 | 105 | "<PassCode>A123</PassCode>\r\n" |
khayakawa | 0:410dd8d5bd07 | 106 | "<StrDataKey />\r\n" |
khayakawa | 0:410dd8d5bd07 | 107 | "<StrDataValue />\r\n" |
khayakawa | 0:410dd8d5bd07 | 108 | "<NumDataKey>\r\n" |
khayakawa | 0:410dd8d5bd07 | 109 | "<a:string>Temp1</a:string>\r\n" |
khayakawa | 0:410dd8d5bd07 | 110 | "<a:string>lx1</a:string>\r\n" |
khayakawa | 0:410dd8d5bd07 | 111 | "</NumDataKey>\r\n" |
khayakawa | 0:410dd8d5bd07 | 112 | "<NumDataValue>\r\n" |
khayakawa | 0:410dd8d5bd07 | 113 | "<a:decimal>%.2f</a:decimal>\r\n" |
khayakawa | 0:410dd8d5bd07 | 114 | "<a:decimal>%d</a:decimal>\r\n" |
khayakawa | 0:410dd8d5bd07 | 115 | "</NumDataValue>\r\n" |
khayakawa | 0:410dd8d5bd07 | 116 | "<DatetimeDataKey />\r\n" |
khayakawa | 0:410dd8d5bd07 | 117 | "<DatetimeDataValue />\r\n" |
khayakawa | 0:410dd8d5bd07 | 118 | "<ExNumDataKey />\r\n" |
khayakawa | 0:410dd8d5bd07 | 119 | "<ExNumDataValue />\r\n" |
khayakawa | 0:410dd8d5bd07 | 120 | "</PostData>\r\n" |
khayakawa | 0:410dd8d5bd07 | 121 | ,temp1, v_val_int); |
khayakawa | 0:410dd8d5bd07 | 122 | // Send xml to Server |
khayakawa | 0:410dd8d5bd07 | 123 | socket.send_all(buffer,sizeof(buffer) - 1); |
khayakawa | 0:410dd8d5bd07 | 124 | |
khayakawa | 0:410dd8d5bd07 | 125 | // receive reply |
khayakawa | 0:410dd8d5bd07 | 126 | n=0; |
khayakawa | 0:410dd8d5bd07 | 127 | char buf[400]; |
khayakawa | 0:410dd8d5bd07 | 128 | n = socket.receive(buf, 400); |
khayakawa | 0:410dd8d5bd07 | 129 | buf[n] = '\0'; |
khayakawa | 0:410dd8d5bd07 | 130 | // printf("%d %s",n, buf); |
khayakawa | 0:410dd8d5bd07 | 131 | lcd.locate(0,15); |
khayakawa | 0:410dd8d5bd07 | 132 | lcd.printf("temperature=%.2f\n",temp1); |
khayakawa | 0:410dd8d5bd07 | 133 | } |
khayakawa | 0:410dd8d5bd07 | 134 | socket.close(); |
khayakawa | 0:410dd8d5bd07 | 135 | // Thread::wait(60000); |
khayakawa | 0:410dd8d5bd07 | 136 | |
khayakawa | 0:410dd8d5bd07 | 137 | |
khayakawa | 0:410dd8d5bd07 | 138 | } |
khayakawa | 0:410dd8d5bd07 | 139 | |
khayakawa | 0:410dd8d5bd07 | 140 | |
khayakawa | 0:410dd8d5bd07 | 141 | tmp1 = val/16; |
khayakawa | 0:410dd8d5bd07 | 142 | tmp2 = val%16; |
khayakawa | 0:410dd8d5bd07 | 143 | pc.printf("%s%s",ch[tmp1],ch[tmp2]); |
khayakawa | 0:410dd8d5bd07 | 144 | myled1=0; |
khayakawa | 0:410dd8d5bd07 | 145 | |
khayakawa | 0:410dd8d5bd07 | 146 | } |
khayakawa | 0:410dd8d5bd07 | 147 | |
khayakawa | 0:410dd8d5bd07 | 148 | |
khayakawa | 0:410dd8d5bd07 | 149 | } |