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