Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

Committer:
chrisisthefish
Date:
Tue Nov 26 06:16:25 2013 +0000
Revision:
7:15cbbbe6105c
Added initial code for Xbee communications.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chrisisthefish 7:15cbbbe6105c 1
chrisisthefish 7:15cbbbe6105c 2 #include "XbeeCommLib.h"
chrisisthefish 7:15cbbbe6105c 3
chrisisthefish 7:15cbbbe6105c 4 float getAnalogVoltage(int analogInputIndex, int totalPacketBytes, int digitalSampleBytes){
chrisisthefish 7:15cbbbe6105c 5 int index = 19 + digitalSampleBytes + analogInputIndex*2;
chrisisthefish 7:15cbbbe6105c 6 if(index > (totalPacketBytes+1)) //+1 because each analog sample is 2 bytes
chrisisthefish 7:15cbbbe6105c 7 return 0;
chrisisthefish 7:15cbbbe6105c 8 else
chrisisthefish 7:15cbbbe6105c 9 return ((data[index] << 8 | data[index + 1]) / 1023.0) * 1.2;
chrisisthefish 7:15cbbbe6105c 10 }
chrisisthefish 7:15cbbbe6105c 11
chrisisthefish 7:15cbbbe6105c 12 void digitalWriteXbee(unsigned int addrHigh, unsigned int addrLow, int outputIndex, bool value){
chrisisthefish 7:15cbbbe6105c 13 unsigned char dataToSend[40];
chrisisthefish 7:15cbbbe6105c 14 int counter = 0;
chrisisthefish 7:15cbbbe6105c 15 int i = 0;
chrisisthefish 7:15cbbbe6105c 16 int addrSum = 0;
chrisisthefish 7:15cbbbe6105c 17 unsigned char data = 0;
chrisisthefish 7:15cbbbe6105c 18
chrisisthefish 7:15cbbbe6105c 19 led3 = 1;
chrisisthefish 7:15cbbbe6105c 20
chrisisthefish 7:15cbbbe6105c 21 printf(" - Sending...");
chrisisthefish 7:15cbbbe6105c 22
chrisisthefish 7:15cbbbe6105c 23 dataToSend[counter++] = 0x7E; // Start of packet
chrisisthefish 7:15cbbbe6105c 24 dataToSend[counter++] = 0x0; // Length MSB (always 0)
chrisisthefish 7:15cbbbe6105c 25 dataToSend[counter++] = 0x10; // Length LSB
chrisisthefish 7:15cbbbe6105c 26 dataToSend[counter++] = 0x17; //0x17 = command request
chrisisthefish 7:15cbbbe6105c 27 dataToSend[counter++] = 0x0; // Frame ID (no reply needed)
chrisisthefish 7:15cbbbe6105c 28
chrisisthefish 7:15cbbbe6105c 29 // Send the 64 bit destination address
chrisisthefish 7:15cbbbe6105c 30 for(i = 3; i > -1; i--){
chrisisthefish 7:15cbbbe6105c 31 dataToSend[counter++] = (addrHigh >> 8*i) & 0xFF;
chrisisthefish 7:15cbbbe6105c 32 addrSum += (addrHigh >> 8*i) & 0xFF;
chrisisthefish 7:15cbbbe6105c 33 }
chrisisthefish 7:15cbbbe6105c 34 for(i = 3; i > -1; i--){
chrisisthefish 7:15cbbbe6105c 35 dataToSend[counter++] = (addrLow >> 8*i) & 0xFF;
chrisisthefish 7:15cbbbe6105c 36 addrSum += (addrLow >> 8*i) & 0xFF;
chrisisthefish 7:15cbbbe6105c 37 }
chrisisthefish 7:15cbbbe6105c 38
chrisisthefish 7:15cbbbe6105c 39 dataToSend[counter++] = 0xFF; // Destination Network
chrisisthefish 7:15cbbbe6105c 40 dataToSend[counter++] = 0xFE; // (Set to 0xFFFE if unknown)
chrisisthefish 7:15cbbbe6105c 41 dataToSend[counter++] = 0x02; // Set to 0x02 to apply these changes
chrisisthefish 7:15cbbbe6105c 42 dataToSend[counter++] = 'D'; // AT Command: D0
chrisisthefish 7:15cbbbe6105c 43 dataToSend[counter++] = '0' + outputIndex;
chrisisthefish 7:15cbbbe6105c 44
chrisisthefish 7:15cbbbe6105c 45 if(value == true)
chrisisthefish 7:15cbbbe6105c 46 data = 5; // Set digital output to be 5 (Digital Out HIGH)
chrisisthefish 7:15cbbbe6105c 47 else
chrisisthefish 7:15cbbbe6105c 48 data = 4; // Set digital output to be 4 (LOW)
chrisisthefish 7:15cbbbe6105c 49 dataToSend[counter++] = data;
chrisisthefish 7:15cbbbe6105c 50
chrisisthefish 7:15cbbbe6105c 51
chrisisthefish 7:15cbbbe6105c 52 //long checksum = 0x17 + addrHigh + addrLow +0xFF + 0xFE + 0x02 + 'D' + '0' + data; //Sum - data = 0x488
chrisisthefish 7:15cbbbe6105c 53 long checksum = 0x17 + addrSum + 0xFF + 0xFE + 0x02 + 'D' + '0' + outputIndex + data; //Sum - data = 0x488
chrisisthefish 7:15cbbbe6105c 54
chrisisthefish 7:15cbbbe6105c 55 dataToSend[counter++] = 0xFF - (checksum & 0xFF);
chrisisthefish 7:15cbbbe6105c 56
chrisisthefish 7:15cbbbe6105c 57 dataToSend[counter] = '\0';
chrisisthefish 7:15cbbbe6105c 58
chrisisthefish 7:15cbbbe6105c 59
chrisisthefish 7:15cbbbe6105c 60 for(int i=0; i < counter; i++){
chrisisthefish 7:15cbbbe6105c 61 xbee1.putc(dataToSend[i]);
chrisisthefish 7:15cbbbe6105c 62 //printf("%x ", dataToSend[i]);
chrisisthefish 7:15cbbbe6105c 63 }
chrisisthefish 7:15cbbbe6105c 64
chrisisthefish 7:15cbbbe6105c 65 printf("Sent\n");
chrisisthefish 7:15cbbbe6105c 66
chrisisthefish 7:15cbbbe6105c 67 led3 = 0;
chrisisthefish 7:15cbbbe6105c 68 }
chrisisthefish 7:15cbbbe6105c 69
chrisisthefish 7:15cbbbe6105c 70
chrisisthefish 7:15cbbbe6105c 71
chrisisthefish 7:15cbbbe6105c 72
chrisisthefish 7:15cbbbe6105c 73 void monitorXbee(){
chrisisthefish 7:15cbbbe6105c 74 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 7:15cbbbe6105c 75 static int digitalInBytes = 0;
chrisisthefish 7:15cbbbe6105c 76 static int analogInBytes = 0;
chrisisthefish 7:15cbbbe6105c 77 static int totalPacketBytes = 0;
chrisisthefish 7:15cbbbe6105c 78 int i = 0;
chrisisthefish 7:15cbbbe6105c 79 static bool lengthCalculated = false;
chrisisthefish 7:15cbbbe6105c 80
chrisisthefish 7:15cbbbe6105c 81 float tempF = 0;
chrisisthefish 7:15cbbbe6105c 82
chrisisthefish 7:15cbbbe6105c 83
chrisisthefish 7:15cbbbe6105c 84 if(dataCounter > 0){ // Do we have any input data at all?
chrisisthefish 7:15cbbbe6105c 85 if(data[0] == 0x7E){ // Valid start byte?
chrisisthefish 7:15cbbbe6105c 86 if(dataCounter > 3){
chrisisthefish 7:15cbbbe6105c 87 if(data[3] != 0x92)
chrisisthefish 7:15cbbbe6105c 88 clear = true;
chrisisthefish 7:15cbbbe6105c 89 else{
chrisisthefish 7:15cbbbe6105c 90 if(dataCounter > 18 && lengthCalculated == false){ // Make sure we have both the digital and analog channel masks.
chrisisthefish 7:15cbbbe6105c 91
chrisisthefish 7:15cbbbe6105c 92 // Check if there are any pins set as digital input
chrisisthefish 7:15cbbbe6105c 93 // The masking on data[16] is b/c we're only using 3 out of the 8 bits
chrisisthefish 7:15cbbbe6105c 94 if((data[16] & 0x1C) | data[17]){
chrisisthefish 7:15cbbbe6105c 95 digitalInBytes = 2;
chrisisthefish 7:15cbbbe6105c 96 }
chrisisthefish 7:15cbbbe6105c 97
chrisisthefish 7:15cbbbe6105c 98 // Check if there are any enabled analog input pins
chrisisthefish 7:15cbbbe6105c 99 for(i = 0; i < 4; i++){
chrisisthefish 7:15cbbbe6105c 100 if( ((data[18] & 0xF) >> i) & 1 ){
chrisisthefish 7:15cbbbe6105c 101 analogInBytes += 2;
chrisisthefish 7:15cbbbe6105c 102 }
chrisisthefish 7:15cbbbe6105c 103 }
chrisisthefish 7:15cbbbe6105c 104 totalPacketBytes = minimumInputBytes + digitalInBytes + analogInBytes;
chrisisthefish 7:15cbbbe6105c 105 //printf("%d digital in bytes\t%d analog in bytes\n", digitalInBytes, analogInBytes);
chrisisthefish 7:15cbbbe6105c 106 //printf("Have %d of %d total bytes\n", dataCounter, totalPacketBytes);
chrisisthefish 7:15cbbbe6105c 107 lengthCalculated = true;
chrisisthefish 7:15cbbbe6105c 108 }
chrisisthefish 7:15cbbbe6105c 109
chrisisthefish 7:15cbbbe6105c 110 if(lengthCalculated && (dataCounter == totalPacketBytes)){
chrisisthefish 7:15cbbbe6105c 111 //Do packet handling here...
chrisisthefish 7:15cbbbe6105c 112
chrisisthefish 7:15cbbbe6105c 113 if(analogInBytes){
chrisisthefish 7:15cbbbe6105c 114 float tmp;
chrisisthefish 7:15cbbbe6105c 115 tmp = green;
chrisisthefish 7:15cbbbe6105c 116 green = blue;
chrisisthefish 7:15cbbbe6105c 117 blue = tmp;
chrisisthefish 7:15cbbbe6105c 118
chrisisthefish 7:15cbbbe6105c 119 tempF = getAnalogVoltage(0, totalPacketBytes, digitalInBytes) * 100;
chrisisthefish 7:15cbbbe6105c 120 printf("Temp %0.2fF\n", tempF);
chrisisthefish 7:15cbbbe6105c 121 lcd.cls();
chrisisthefish 7:15cbbbe6105c 122 lcd.locate(0,5);
chrisisthefish 7:15cbbbe6105c 123 lcd.printf("Remote Temp %0.2fF\n", tempF);
chrisisthefish 7:15cbbbe6105c 124 lcd.locate(0,15);
chrisisthefish 7:15cbbbe6105c 125 tempF = (float)sensor.read() * (9.0/5.0) + 32;
chrisisthefish 7:15cbbbe6105c 126 lcd.printf("Local Temp %.2fF\n", tempF);
chrisisthefish 7:15cbbbe6105c 127 }
chrisisthefish 7:15cbbbe6105c 128
chrisisthefish 7:15cbbbe6105c 129 if(digitalInBytes){
chrisisthefish 7:15cbbbe6105c 130 int digitalMask = (data[16] << 8) | data[17];
chrisisthefish 7:15cbbbe6105c 131 int digitalData = (data[19] << 8) | data[20];
chrisisthefish 7:15cbbbe6105c 132 for(i = 0; i < 10; i++){
chrisisthefish 7:15cbbbe6105c 133 if(digitalMask & 1){
chrisisthefish 7:15cbbbe6105c 134 printf("Digital Input %d is %d\n", i, digitalData & 1);
chrisisthefish 7:15cbbbe6105c 135 }
chrisisthefish 7:15cbbbe6105c 136 digitalMask = digitalMask >> 1;
chrisisthefish 7:15cbbbe6105c 137 digitalData = digitalData >> 1;
chrisisthefish 7:15cbbbe6105c 138 }
chrisisthefish 7:15cbbbe6105c 139 }
chrisisthefish 7:15cbbbe6105c 140
chrisisthefish 7:15cbbbe6105c 141 //printf("*******Temp = %f\n", (data[19] << 8 | data[20])/1023.0 * 120);
chrisisthefish 7:15cbbbe6105c 142
chrisisthefish 7:15cbbbe6105c 143 dataCounter = 0;
chrisisthefish 7:15cbbbe6105c 144 clear = true;
chrisisthefish 7:15cbbbe6105c 145 lengthCalculated = false;
chrisisthefish 7:15cbbbe6105c 146 digitalInBytes = 0;
chrisisthefish 7:15cbbbe6105c 147 analogInBytes = 0;
chrisisthefish 7:15cbbbe6105c 148 totalPacketBytes = 0;
chrisisthefish 7:15cbbbe6105c 149 }
chrisisthefish 7:15cbbbe6105c 150 }
chrisisthefish 7:15cbbbe6105c 151 }
chrisisthefish 7:15cbbbe6105c 152 }
chrisisthefish 7:15cbbbe6105c 153 else{
chrisisthefish 7:15cbbbe6105c 154 // Not a valid start byte so reset
chrisisthefish 7:15cbbbe6105c 155 clear = true;
chrisisthefish 7:15cbbbe6105c 156 lengthCalculated = false;
chrisisthefish 7:15cbbbe6105c 157 }
chrisisthefish 7:15cbbbe6105c 158 }
chrisisthefish 7:15cbbbe6105c 159 }