Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
XbeeCommLib.cpp@9:4b1e3531dd00, 2013-12-04 (annotated)
- Committer:
- chrisisthefish
- Date:
- Wed Dec 04 02:35:47 2013 +0000
- Revision:
- 9:4b1e3531dd00
- Parent:
- 8:e32fcca16102
- Child:
- 10:de0be690b3c0
Changed too much stuff to comment on everything.; Got the xbee structs working; Got the RPC stuff working; Added functions for handling incoming RPC changes; Added timing functions to the motion detectors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chrisisthefish | 8:e32fcca16102 | 1 | #include "XbeeCommLib.h" |
chrisisthefish | 8:e32fcca16102 | 2 | |
chrisisthefish | 8:e32fcca16102 | 3 | |
chrisisthefish | 8:e32fcca16102 | 4 | float getAnalogVoltage(int startingPacket, int analogIndex, int totalPacketBytes){ |
chrisisthefish | 8:e32fcca16102 | 5 | int dataIndex = startingPacket + analogIndex*2; |
chrisisthefish | 8:e32fcca16102 | 6 | if(dataIndex > (totalPacketBytes+1)) //+1 because each analog sample is 2 bytes |
chrisisthefish | 8:e32fcca16102 | 7 | return 0; |
chrisisthefish | 8:e32fcca16102 | 8 | else |
chrisisthefish | 8:e32fcca16102 | 9 | return ((data[dataIndex] << 8 | data[dataIndex + 1]) / 1023.0) * 1.2; |
chrisisthefish | 8:e32fcca16102 | 10 | } |
chrisisthefish | 8:e32fcca16102 | 11 | |
chrisisthefish | 8:e32fcca16102 | 12 | |
chrisisthefish | 8:e32fcca16102 | 13 | void digitalWriteXbee(unsigned int addrHigh, unsigned int addrLow, int outputIndex, bool value){ |
chrisisthefish | 8:e32fcca16102 | 14 | unsigned char dataToSend[40]; |
chrisisthefish | 8:e32fcca16102 | 15 | int counter = 0; |
chrisisthefish | 8:e32fcca16102 | 16 | int i = 0; |
chrisisthefish | 8:e32fcca16102 | 17 | int addrSum = 0; |
chrisisthefish | 8:e32fcca16102 | 18 | unsigned char data = 0; |
chrisisthefish | 8:e32fcca16102 | 19 | |
chrisisthefish | 8:e32fcca16102 | 20 | //led3 = 1; |
chrisisthefish | 8:e32fcca16102 | 21 | |
chrisisthefish | 8:e32fcca16102 | 22 | printf(" - Sending..."); |
chrisisthefish | 8:e32fcca16102 | 23 | |
chrisisthefish | 8:e32fcca16102 | 24 | dataToSend[counter++] = 0x7E; // Start of packet |
chrisisthefish | 8:e32fcca16102 | 25 | dataToSend[counter++] = 0x0; // Length MSB (always 0) |
chrisisthefish | 8:e32fcca16102 | 26 | dataToSend[counter++] = 0x10; // Length LSB |
chrisisthefish | 8:e32fcca16102 | 27 | dataToSend[counter++] = 0x17; //0x17 = command request |
chrisisthefish | 8:e32fcca16102 | 28 | dataToSend[counter++] = 0x0; // Frame ID (no reply needed) |
chrisisthefish | 8:e32fcca16102 | 29 | |
chrisisthefish | 8:e32fcca16102 | 30 | // Send the 64 bit destination address |
chrisisthefish | 8:e32fcca16102 | 31 | for(i = 3; i > -1; i--){ |
chrisisthefish | 8:e32fcca16102 | 32 | dataToSend[counter++] = (addrHigh >> 8*i) & 0xFF; |
chrisisthefish | 8:e32fcca16102 | 33 | addrSum += (addrHigh >> 8*i) & 0xFF; |
chrisisthefish | 8:e32fcca16102 | 34 | } |
chrisisthefish | 8:e32fcca16102 | 35 | for(i = 3; i > -1; i--){ |
chrisisthefish | 8:e32fcca16102 | 36 | dataToSend[counter++] = (addrLow >> 8*i) & 0xFF; |
chrisisthefish | 8:e32fcca16102 | 37 | addrSum += (addrLow >> 8*i) & 0xFF; |
chrisisthefish | 8:e32fcca16102 | 38 | } |
chrisisthefish | 8:e32fcca16102 | 39 | |
chrisisthefish | 8:e32fcca16102 | 40 | dataToSend[counter++] = 0xFF; // Destination Network |
chrisisthefish | 8:e32fcca16102 | 41 | dataToSend[counter++] = 0xFE; // (Set to 0xFFFE if unknown) |
chrisisthefish | 8:e32fcca16102 | 42 | dataToSend[counter++] = 0x02; // Set to 0x02 to apply these changes |
chrisisthefish | 8:e32fcca16102 | 43 | dataToSend[counter++] = 'D'; // AT Command: D0 |
chrisisthefish | 8:e32fcca16102 | 44 | dataToSend[counter++] = '0' + outputIndex; |
chrisisthefish | 8:e32fcca16102 | 45 | |
chrisisthefish | 8:e32fcca16102 | 46 | if(value == true) |
chrisisthefish | 8:e32fcca16102 | 47 | data = 5; // Set digital output to be 5 (Digital Out HIGH) |
chrisisthefish | 8:e32fcca16102 | 48 | else |
chrisisthefish | 8:e32fcca16102 | 49 | data = 4; // Set digital output to be 4 (LOW) |
chrisisthefish | 8:e32fcca16102 | 50 | dataToSend[counter++] = data; |
chrisisthefish | 8:e32fcca16102 | 51 | |
chrisisthefish | 8:e32fcca16102 | 52 | |
chrisisthefish | 8:e32fcca16102 | 53 | //long checksum = 0x17 + addrHigh + addrLow +0xFF + 0xFE + 0x02 + 'D' + '0' + data; //Sum - data = 0x488 |
chrisisthefish | 8:e32fcca16102 | 54 | long checksum = 0x17 + addrSum + 0xFF + 0xFE + 0x02 + 'D' + '0' + outputIndex + data; //Sum - data = 0x488 |
chrisisthefish | 8:e32fcca16102 | 55 | |
chrisisthefish | 8:e32fcca16102 | 56 | dataToSend[counter++] = 0xFF - (checksum & 0xFF); |
chrisisthefish | 8:e32fcca16102 | 57 | |
chrisisthefish | 8:e32fcca16102 | 58 | dataToSend[counter] = '\0'; |
chrisisthefish | 8:e32fcca16102 | 59 | |
chrisisthefish | 8:e32fcca16102 | 60 | |
chrisisthefish | 8:e32fcca16102 | 61 | for(int i=0; i < counter; i++){ |
chrisisthefish | 8:e32fcca16102 | 62 | xbeeSerial.putc(dataToSend[i]); |
chrisisthefish | 8:e32fcca16102 | 63 | //printf("%x ", dataToSend[i]); |
chrisisthefish | 8:e32fcca16102 | 64 | } |
chrisisthefish | 8:e32fcca16102 | 65 | |
chrisisthefish | 8:e32fcca16102 | 66 | printf("Sent\n"); |
chrisisthefish | 8:e32fcca16102 | 67 | |
chrisisthefish | 8:e32fcca16102 | 68 | //led3 = 0; |
chrisisthefish | 8:e32fcca16102 | 69 | } |
chrisisthefish | 8:e32fcca16102 | 70 | |
chrisisthefish | 8:e32fcca16102 | 71 | |
chrisisthefish | 8:e32fcca16102 | 72 | |
chrisisthefish | 8:e32fcca16102 | 73 | |
chrisisthefish | 8:e32fcca16102 | 74 | void monitorXbee(){ |
chrisisthefish | 8:e32fcca16102 | 75 | int minimumInputBytes = 20; //20 is minimum number of bytes. 19 for bytes 0-18 and 1 for the checksum! There will be more if there is digital or analog input data |
chrisisthefish | 8:e32fcca16102 | 76 | static int digitalInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 77 | static int analogInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 78 | static int totalPacketBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 79 | int i = 0; |
chrisisthefish | 8:e32fcca16102 | 80 | static bool lengthCalculated = false; |
chrisisthefish | 8:e32fcca16102 | 81 | |
chrisisthefish | 8:e32fcca16102 | 82 | float analogData = 0; |
chrisisthefish | 8:e32fcca16102 | 83 | int addrHigh = 0; |
chrisisthefish | 8:e32fcca16102 | 84 | int addrLow = 0; |
chrisisthefish | 8:e32fcca16102 | 85 | int startingPacket = 0; |
chrisisthefish | 8:e32fcca16102 | 86 | int analogPortNumbers[4]; |
chrisisthefish | 8:e32fcca16102 | 87 | int analogCount = 0; |
chrisisthefish | 8:e32fcca16102 | 88 | |
chrisisthefish | 8:e32fcca16102 | 89 | |
chrisisthefish | 8:e32fcca16102 | 90 | |
chrisisthefish | 8:e32fcca16102 | 91 | |
chrisisthefish | 8:e32fcca16102 | 92 | if(dataCounter > 0){ // Do we have any input data at all? |
chrisisthefish | 8:e32fcca16102 | 93 | if(data[0] == 0x7E){ // Valid start byte? |
chrisisthefish | 8:e32fcca16102 | 94 | if(dataCounter > 3){ |
chrisisthefish | 8:e32fcca16102 | 95 | if(data[3] != 0x92) |
chrisisthefish | 8:e32fcca16102 | 96 | clear = true; |
chrisisthefish | 8:e32fcca16102 | 97 | else{ |
chrisisthefish | 8:e32fcca16102 | 98 | if(dataCounter > 18 && lengthCalculated == false){ // Make sure we have both the digital and analog channel masks. |
chrisisthefish | 8:e32fcca16102 | 99 | |
chrisisthefish | 8:e32fcca16102 | 100 | // Check if there are any pins set as digital input |
chrisisthefish | 8:e32fcca16102 | 101 | // The masking on data[16] is b/c we're only using 3 out of the 8 bits |
chrisisthefish | 8:e32fcca16102 | 102 | if((data[16] & 0x1C) | data[17]){ |
chrisisthefish | 8:e32fcca16102 | 103 | digitalInBytes = 2; |
chrisisthefish | 8:e32fcca16102 | 104 | } |
chrisisthefish | 8:e32fcca16102 | 105 | |
chrisisthefish | 8:e32fcca16102 | 106 | // Check if there are any enabled analog input pins |
chrisisthefish | 8:e32fcca16102 | 107 | for(i = 0; i < 4; i++){ |
chrisisthefish | 8:e32fcca16102 | 108 | if( ((data[18] & 0xF) >> i) & 1 ){ |
chrisisthefish | 8:e32fcca16102 | 109 | analogInBytes += 2; |
chrisisthefish | 8:e32fcca16102 | 110 | } |
chrisisthefish | 8:e32fcca16102 | 111 | } |
chrisisthefish | 8:e32fcca16102 | 112 | totalPacketBytes = minimumInputBytes + digitalInBytes + analogInBytes; |
chrisisthefish | 8:e32fcca16102 | 113 | //printf("%d digital in bytes\t%d analog in bytes\n", digitalInBytes, analogInBytes); |
chrisisthefish | 8:e32fcca16102 | 114 | //printf("Have %d of %d total bytes\n", dataCounter, totalPacketBytes); |
chrisisthefish | 8:e32fcca16102 | 115 | lengthCalculated = true; |
chrisisthefish | 8:e32fcca16102 | 116 | } |
chrisisthefish | 8:e32fcca16102 | 117 | |
chrisisthefish | 8:e32fcca16102 | 118 | if(lengthCalculated && (dataCounter == totalPacketBytes)){ |
chrisisthefish | 8:e32fcca16102 | 119 | //Do packet handling here... |
chrisisthefish | 8:e32fcca16102 | 120 | |
chrisisthefish | 9:4b1e3531dd00 | 121 | /*printf("Printing packet - %d bytes:\n", dataCounter); |
chrisisthefish | 9:4b1e3531dd00 | 122 | for(i = 0; i < dataCounter; i++){ |
chrisisthefish | 9:4b1e3531dd00 | 123 | printf("%x\n", data[i]); |
chrisisthefish | 9:4b1e3531dd00 | 124 | } |
chrisisthefish | 9:4b1e3531dd00 | 125 | printf("End of packet\n");*/ |
chrisisthefish | 9:4b1e3531dd00 | 126 | |
chrisisthefish | 9:4b1e3531dd00 | 127 | |
chrisisthefish | 8:e32fcca16102 | 128 | //Determine source address |
chrisisthefish | 8:e32fcca16102 | 129 | for(i = 0; i < 4; i++){ |
chrisisthefish | 8:e32fcca16102 | 130 | addrHigh = addrHigh | (data[4 + i] << (3-i)*8); |
chrisisthefish | 8:e32fcca16102 | 131 | addrLow = addrLow | (data[8 + i] << (3-i)*8); |
chrisisthefish | 8:e32fcca16102 | 132 | |
chrisisthefish | 8:e32fcca16102 | 133 | //Hijack this loop to initialize the analogPortNumbers array |
chrisisthefish | 8:e32fcca16102 | 134 | analogPortNumbers[i] = -1; |
chrisisthefish | 9:4b1e3531dd00 | 135 | } |
chrisisthefish | 9:4b1e3531dd00 | 136 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 137 | printf("\n\nReceived packet from %x %x\n", addrHigh, addrLow); |
chrisisthefish | 8:e32fcca16102 | 138 | |
chrisisthefish | 8:e32fcca16102 | 139 | if(analogInBytes){ |
chrisisthefish | 9:4b1e3531dd00 | 140 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 141 | printf("Analog Data Present\n"); |
chrisisthefish | 8:e32fcca16102 | 142 | //Figure out where the analog bytes start |
chrisisthefish | 8:e32fcca16102 | 143 | if(digitalInBytes) |
chrisisthefish | 8:e32fcca16102 | 144 | startingPacket = 21; |
chrisisthefish | 8:e32fcca16102 | 145 | else |
chrisisthefish | 8:e32fcca16102 | 146 | startingPacket = 19; |
chrisisthefish | 8:e32fcca16102 | 147 | |
chrisisthefish | 8:e32fcca16102 | 148 | //Determine how many analog inputs are active, and which pins they use |
chrisisthefish | 9:4b1e3531dd00 | 149 | // printf("Analog inputs "); |
chrisisthefish | 8:e32fcca16102 | 150 | for(i = 0; i < 4; i++){ |
chrisisthefish | 8:e32fcca16102 | 151 | if( ((data[18] & 0xF) >> i) & 1 ){ |
chrisisthefish | 9:4b1e3531dd00 | 152 | // printf("%d ", i); |
chrisisthefish | 8:e32fcca16102 | 153 | analogPortNumbers[analogCount] = i; |
chrisisthefish | 8:e32fcca16102 | 154 | analogCount++; |
chrisisthefish | 8:e32fcca16102 | 155 | } |
chrisisthefish | 8:e32fcca16102 | 156 | } |
chrisisthefish | 9:4b1e3531dd00 | 157 | // printf("active\n"); |
chrisisthefish | 9:4b1e3531dd00 | 158 | |
chrisisthefish | 9:4b1e3531dd00 | 159 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 160 | printf("%d Ananlog inputs total\n", analogCount); |
chrisisthefish | 8:e32fcca16102 | 161 | |
chrisisthefish | 8:e32fcca16102 | 162 | //Decode analog data and push into appropriate struct |
chrisisthefish | 8:e32fcca16102 | 163 | for(i = 0; i < analogCount; i++){ |
chrisisthefish | 8:e32fcca16102 | 164 | analogData = getAnalogVoltage(startingPacket, i, totalPacketBytes); |
chrisisthefish | 9:4b1e3531dd00 | 165 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 166 | printf("Analog Data Input Index %d = %f\n", analogPortNumbers[i], analogData); |
chrisisthefish | 8:e32fcca16102 | 167 | analogInputHandle(root, addrHigh, addrLow, analogPortNumbers[i], analogData); |
chrisisthefish | 8:e32fcca16102 | 168 | } |
chrisisthefish | 8:e32fcca16102 | 169 | } |
chrisisthefish | 8:e32fcca16102 | 170 | |
chrisisthefish | 8:e32fcca16102 | 171 | if(digitalInBytes){ |
chrisisthefish | 9:4b1e3531dd00 | 172 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 173 | printf("Digital Data Present\n"); |
chrisisthefish | 9:4b1e3531dd00 | 174 | // unsigned short digitalMask = (data[16] << 8) | data[17]; |
chrisisthefish | 8:e32fcca16102 | 175 | unsigned short digitalData = (data[19] << 8) | data[20]; |
chrisisthefish | 8:e32fcca16102 | 176 | |
chrisisthefish | 8:e32fcca16102 | 177 | digitalInputHandle(root, addrHigh, addrLow, digitalData); |
chrisisthefish | 9:4b1e3531dd00 | 178 | //printf("Digital Mask = %x\nDigital Data = %x\n", digitalMask, digitalData); |
chrisisthefish | 9:4b1e3531dd00 | 179 | |
chrisisthefish | 8:e32fcca16102 | 180 | } |
chrisisthefish | 8:e32fcca16102 | 181 | |
chrisisthefish | 8:e32fcca16102 | 182 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 183 | clear = true; |
chrisisthefish | 8:e32fcca16102 | 184 | lengthCalculated = false; |
chrisisthefish | 8:e32fcca16102 | 185 | digitalInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 186 | analogInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 187 | totalPacketBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 188 | } |
chrisisthefish | 8:e32fcca16102 | 189 | } |
chrisisthefish | 8:e32fcca16102 | 190 | } |
chrisisthefish | 8:e32fcca16102 | 191 | } |
chrisisthefish | 8:e32fcca16102 | 192 | else{ |
chrisisthefish | 8:e32fcca16102 | 193 | // Not a valid start byte so reset |
chrisisthefish | 8:e32fcca16102 | 194 | clear = true; |
chrisisthefish | 8:e32fcca16102 | 195 | lengthCalculated = false; |
chrisisthefish | 8:e32fcca16102 | 196 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 197 | } |
chrisisthefish | 8:e32fcca16102 | 198 | } |
chrisisthefish | 8:e32fcca16102 | 199 | } |