Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
XbeeCommLib.cpp@8:e32fcca16102, 2013-12-02 (annotated)
- Committer:
- chrisisthefish
- Date:
- Mon Dec 02 22:25:29 2013 +0000
- Revision:
- 8:e32fcca16102
- Child:
- 9:4b1e3531dd00
Re-added Chris' Xbee communication code.; ; Integrated Hai's digital and analog handling code with the Xbee packet decoder code so that we handle incoming packets correctly.
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 | 8:e32fcca16102 | 121 | //Determine source address |
chrisisthefish | 8:e32fcca16102 | 122 | for(i = 0; i < 4; i++){ |
chrisisthefish | 8:e32fcca16102 | 123 | addrHigh = addrHigh | (data[4 + i] << (3-i)*8); |
chrisisthefish | 8:e32fcca16102 | 124 | addrLow = addrLow | (data[8 + i] << (3-i)*8); |
chrisisthefish | 8:e32fcca16102 | 125 | |
chrisisthefish | 8:e32fcca16102 | 126 | //Hijack this loop to initialize the analogPortNumbers array |
chrisisthefish | 8:e32fcca16102 | 127 | analogPortNumbers[i] = -1; |
chrisisthefish | 8:e32fcca16102 | 128 | } |
chrisisthefish | 8:e32fcca16102 | 129 | |
chrisisthefish | 8:e32fcca16102 | 130 | if(analogInBytes){ |
chrisisthefish | 8:e32fcca16102 | 131 | //Figure out where the analog bytes start |
chrisisthefish | 8:e32fcca16102 | 132 | if(digitalInBytes) |
chrisisthefish | 8:e32fcca16102 | 133 | startingPacket = 21; |
chrisisthefish | 8:e32fcca16102 | 134 | else |
chrisisthefish | 8:e32fcca16102 | 135 | startingPacket = 19; |
chrisisthefish | 8:e32fcca16102 | 136 | |
chrisisthefish | 8:e32fcca16102 | 137 | //Determine how many analog inputs are active, and which pins they use |
chrisisthefish | 8:e32fcca16102 | 138 | for(i = 0; i < 4; i++){ |
chrisisthefish | 8:e32fcca16102 | 139 | if( ((data[18] & 0xF) >> i) & 1 ){ |
chrisisthefish | 8:e32fcca16102 | 140 | analogPortNumbers[analogCount] = i; |
chrisisthefish | 8:e32fcca16102 | 141 | analogCount++; |
chrisisthefish | 8:e32fcca16102 | 142 | } |
chrisisthefish | 8:e32fcca16102 | 143 | } |
chrisisthefish | 8:e32fcca16102 | 144 | |
chrisisthefish | 8:e32fcca16102 | 145 | //Decode analog data and push into appropriate struct |
chrisisthefish | 8:e32fcca16102 | 146 | for(i = 0; i < analogCount; i++){ |
chrisisthefish | 8:e32fcca16102 | 147 | analogData = getAnalogVoltage(startingPacket, i, totalPacketBytes); |
chrisisthefish | 8:e32fcca16102 | 148 | analogInputHandle(root, addrHigh, addrLow, analogPortNumbers[i], analogData); |
chrisisthefish | 8:e32fcca16102 | 149 | } |
chrisisthefish | 8:e32fcca16102 | 150 | } |
chrisisthefish | 8:e32fcca16102 | 151 | |
chrisisthefish | 8:e32fcca16102 | 152 | if(digitalInBytes){ |
chrisisthefish | 8:e32fcca16102 | 153 | //unsigned short digitalMask = (data[16] << 8) | data[17]; |
chrisisthefish | 8:e32fcca16102 | 154 | unsigned short digitalData = (data[19] << 8) | data[20]; |
chrisisthefish | 8:e32fcca16102 | 155 | |
chrisisthefish | 8:e32fcca16102 | 156 | digitalInputHandle(root, addrHigh, addrLow, digitalData); |
chrisisthefish | 8:e32fcca16102 | 157 | } |
chrisisthefish | 8:e32fcca16102 | 158 | |
chrisisthefish | 8:e32fcca16102 | 159 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 160 | clear = true; |
chrisisthefish | 8:e32fcca16102 | 161 | lengthCalculated = false; |
chrisisthefish | 8:e32fcca16102 | 162 | digitalInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 163 | analogInBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 164 | totalPacketBytes = 0; |
chrisisthefish | 8:e32fcca16102 | 165 | } |
chrisisthefish | 8:e32fcca16102 | 166 | } |
chrisisthefish | 8:e32fcca16102 | 167 | } |
chrisisthefish | 8:e32fcca16102 | 168 | } |
chrisisthefish | 8:e32fcca16102 | 169 | else{ |
chrisisthefish | 8:e32fcca16102 | 170 | // Not a valid start byte so reset |
chrisisthefish | 8:e32fcca16102 | 171 | clear = true; |
chrisisthefish | 8:e32fcca16102 | 172 | lengthCalculated = false; |
chrisisthefish | 8:e32fcca16102 | 173 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 174 | } |
chrisisthefish | 8:e32fcca16102 | 175 | } |
chrisisthefish | 8:e32fcca16102 | 176 | } |