Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
main.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 | /* |
chrisisthefish | 8:e32fcca16102 | 2 | |
chrisisthefish | 8:e32fcca16102 | 3 | Analog Types: |
chrisisthefish | 8:e32fcca16102 | 4 | 0 = No connection |
chrisisthefish | 8:e32fcca16102 | 5 | 1 = Analog Devices TMP36 Temperature Sensor |
chrisisthefish | 8:e32fcca16102 | 6 | |
chrisisthefish | 8:e32fcca16102 | 7 | |
chrisisthefish | 8:e32fcca16102 | 8 | Digital Types: |
chrisisthefish | 8:e32fcca16102 | 9 | 0 = No connection |
chrisisthefish | 8:e32fcca16102 | 10 | 1 = Light Control (Digital Output) |
chrisisthefish | 8:e32fcca16102 | 11 | 2 = Motion Sense (Digital Input) |
chrisisthefish | 8:e32fcca16102 | 12 | |
chrisisthefish | 8:e32fcca16102 | 13 | */ |
chrisisthefish | 8:e32fcca16102 | 14 | |
chrisisthefish | 8:e32fcca16102 | 15 | |
chrisisthefish | 0:c498b8bcfc46 | 16 | #include "mbed.h" |
mmujica6 | 5:ae3cbcf75d78 | 17 | #include "EthernetNetIf.h" |
mmujica6 | 5:ae3cbcf75d78 | 18 | #include "HTTPServer.h" |
mmujica6 | 5:ae3cbcf75d78 | 19 | #include "SerialRPCInterface.h" |
chrisisthefish | 8:e32fcca16102 | 20 | #include "XbeeCommLib.h" |
chrisisthefish | 0:c498b8bcfc46 | 21 | |
hpham33 | 2:9503a713b648 | 22 | DigitalIn pb(p8); |
chrisisthefish | 9:4b1e3531dd00 | 23 | DigitalOut led1(LED1); |
chrisisthefish | 9:4b1e3531dd00 | 24 | DigitalOut led4(LED4); |
hpham33 | 2:9503a713b648 | 25 | |
chrisisthefish | 8:e32fcca16102 | 26 | Serial xbeeSerial(p9, p10); |
chrisisthefish | 8:e32fcca16102 | 27 | |
chrisisthefish | 8:e32fcca16102 | 28 | unsigned char data[500]; |
chrisisthefish | 8:e32fcca16102 | 29 | int dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 30 | bool clear = false; |
hpham33 | 2:9503a713b648 | 31 | |
mmujica6 | 5:ae3cbcf75d78 | 32 | //Create port variables |
chrisisthefish | 9:4b1e3531dd00 | 33 | float ReadTemperatureSensor0 = 0.0; |
chrisisthefish | 9:4b1e3531dd00 | 34 | float ReadTemperatureSensor1 = 0.0; |
chrisisthefish | 9:4b1e3531dd00 | 35 | int ReadMotionDetector0 = 1; //Motion detector is low-active |
chrisisthefish | 9:4b1e3531dd00 | 36 | int ReadMotionDetector1 = 1; //Motion detector is low-active |
chrisisthefish | 9:4b1e3531dd00 | 37 | int ReadLightSwitch0 = 0; |
chrisisthefish | 9:4b1e3531dd00 | 38 | int ReadLightSwitch1 = 0; |
chrisisthefish | 9:4b1e3531dd00 | 39 | int WriteLightSwitch0 = 0; |
chrisisthefish | 9:4b1e3531dd00 | 40 | int WriteLightSwitch1 = 0; |
mmujica6 | 5:ae3cbcf75d78 | 41 | |
mmujica6 | 5:ae3cbcf75d78 | 42 | //Make these variables accessible over RPC by attaching them to an RPCVariable |
chrisisthefish | 9:4b1e3531dd00 | 43 | RPCVariable<float> RPCTemperatureSensor0(&ReadTemperatureSensor0, "TemperatureSensor0"); |
mmujica6 | 5:ae3cbcf75d78 | 44 | RPCVariable<float> RPCTemperatureSensor1(&ReadTemperatureSensor1, "TemperatureSensor1"); |
chrisisthefish | 9:4b1e3531dd00 | 45 | RPCVariable<int> RPCMotionDetector0(&ReadMotionDetector0, "MotionDetector0"); |
mmujica6 | 5:ae3cbcf75d78 | 46 | RPCVariable<int> RPCMotionDetector1(&ReadMotionDetector1, "MotionDetector1"); |
chrisisthefish | 9:4b1e3531dd00 | 47 | RPCVariable<int> RPCReadLightSwitch0(&ReadLightSwitch0, "ReadLightSwitch0"); |
mmujica6 | 5:ae3cbcf75d78 | 48 | RPCVariable<int> RPCReadLightSwitch1(&ReadLightSwitch1, "ReadLightSwitch1"); |
chrisisthefish | 9:4b1e3531dd00 | 49 | RPCVariable<int> RPCWriteLightSwitch0(&WriteLightSwitch0, "WriteLightSwitch0"); |
mmujica6 | 5:ae3cbcf75d78 | 50 | RPCVariable<int> RPCWriteLightSwitch1(&WriteLightSwitch1, "WriteLightSwitch1"); |
mmujica6 | 5:ae3cbcf75d78 | 51 | |
mmujica6 | 5:ae3cbcf75d78 | 52 | EthernetNetIf eth; |
mmujica6 | 5:ae3cbcf75d78 | 53 | HTTPServer svr; |
hpham33 | 2:9503a713b648 | 54 | |
chrisisthefish | 8:e32fcca16102 | 55 | |
chrisisthefish | 8:e32fcca16102 | 56 | |
hpham33 | 2:9503a713b648 | 57 | struct xbee { // radio prototype with addresss, location, pointer to sensor list |
hpham33 | 2:9503a713b648 | 58 | unsigned int addrHigh; // upper 16 bits address of sensor |
hpham33 | 2:9503a713b648 | 59 | unsigned int addrLow; // lower address of sensor |
hpham33 | 2:9503a713b648 | 60 | unsigned short digitalData; |
hpham33 | 2:9503a713b648 | 61 | unsigned short digitalDataOutput; |
hpham33 | 2:9503a713b648 | 62 | int digitalType[10]; |
hpham33 | 2:9503a713b648 | 63 | float analogData[4]; |
hpham33 | 2:9503a713b648 | 64 | int analogType[4]; |
chrisisthefish | 9:4b1e3531dd00 | 65 | float* analogRpcDataPointer[4]; |
chrisisthefish | 9:4b1e3531dd00 | 66 | int* digitalOutRpcDataPointer[10]; |
chrisisthefish | 9:4b1e3531dd00 | 67 | int* digitalInRpcDataPointer[10]; |
chrisisthefish | 9:4b1e3531dd00 | 68 | Timer* timerList[10]; |
hpham33 | 2:9503a713b648 | 69 | struct xbee * next; // pointer to next struct |
hpham33 | 2:9503a713b648 | 70 | }; |
hpham33 | 2:9503a713b648 | 71 | |
chrisisthefish | 8:e32fcca16102 | 72 | struct xbee *root; |
chrisisthefish | 8:e32fcca16102 | 73 | |
chrisisthefish | 9:4b1e3531dd00 | 74 | struct xbee xbeeList[3]; |
chrisisthefish | 9:4b1e3531dd00 | 75 | int xbeeCount = 3; |
chrisisthefish | 9:4b1e3531dd00 | 76 | |
chrisisthefish | 8:e32fcca16102 | 77 | |
chrisisthefish | 8:e32fcca16102 | 78 | |
chrisisthefish | 8:e32fcca16102 | 79 | |
chrisisthefish | 8:e32fcca16102 | 80 | void xbeeSerialCallback() { |
chrisisthefish | 9:4b1e3531dd00 | 81 | //printf("Xbee\n"); |
chrisisthefish | 8:e32fcca16102 | 82 | if(clear){ |
chrisisthefish | 8:e32fcca16102 | 83 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 84 | clear = false; |
chrisisthefish | 8:e32fcca16102 | 85 | } |
chrisisthefish | 8:e32fcca16102 | 86 | if(dataCounter < 500){ |
chrisisthefish | 8:e32fcca16102 | 87 | while(xbeeSerial.readable() == true && dataCounter < 500){ |
chrisisthefish | 9:4b1e3531dd00 | 88 | led4 = 1; |
chrisisthefish | 8:e32fcca16102 | 89 | data[dataCounter] = xbeeSerial.getc(); |
chrisisthefish | 8:e32fcca16102 | 90 | dataCounter++; |
chrisisthefish | 9:4b1e3531dd00 | 91 | led4 = 0; |
chrisisthefish | 8:e32fcca16102 | 92 | } |
chrisisthefish | 8:e32fcca16102 | 93 | } |
chrisisthefish | 8:e32fcca16102 | 94 | else{ |
chrisisthefish | 8:e32fcca16102 | 95 | printf("Serial data buffer overflow. Resetting buffer...\n"); |
chrisisthefish | 8:e32fcca16102 | 96 | dataCounter = 0; |
chrisisthefish | 8:e32fcca16102 | 97 | data[dataCounter] = xbeeSerial.getc(); |
chrisisthefish | 8:e32fcca16102 | 98 | } |
chrisisthefish | 8:e32fcca16102 | 99 | } |
chrisisthefish | 8:e32fcca16102 | 100 | |
chrisisthefish | 8:e32fcca16102 | 101 | |
chrisisthefish | 0:c498b8bcfc46 | 102 | |
chrisisthefish | 0:c498b8bcfc46 | 103 | int main() { |
chrisisthefish | 8:e32fcca16102 | 104 | |
chrisisthefish | 9:4b1e3531dd00 | 105 | //xbeeSerial.attach(&xbeeSerialCallback); |
chrisisthefish | 8:e32fcca16102 | 106 | |
chrisisthefish | 9:4b1e3531dd00 | 107 | printf("\n\nBeginning Setup\n"); |
chrisisthefish | 8:e32fcca16102 | 108 | // Ethernet Setup |
chrisisthefish | 8:e32fcca16102 | 109 | EthernetErr ethErr = eth.setup(); |
chrisisthefish | 9:4b1e3531dd00 | 110 | if(ethErr){ |
chrisisthefish | 9:4b1e3531dd00 | 111 | printf("Error %d in setup.\n", ethErr); |
chrisisthefish | 9:4b1e3531dd00 | 112 | return -1; |
chrisisthefish | 8:e32fcca16102 | 113 | } |
chrisisthefish | 8:e32fcca16102 | 114 | printf("Setup OK\n"); |
chrisisthefish | 8:e32fcca16102 | 115 | |
chrisisthefish | 8:e32fcca16102 | 116 | // Add RPCHandler |
chrisisthefish | 8:e32fcca16102 | 117 | svr.addHandler<RPCHandler>("/rpc"); |
chrisisthefish | 8:e32fcca16102 | 118 | |
chrisisthefish | 8:e32fcca16102 | 119 | // Show that the server is ready and listening |
chrisisthefish | 8:e32fcca16102 | 120 | svr.bind(80); |
chrisisthefish | 9:4b1e3531dd00 | 121 | printf("RPC Server Ready\n"); |
mmujica6 | 5:ae3cbcf75d78 | 122 | |
chrisisthefish | 8:e32fcca16102 | 123 | /* This won't change, or we would lose the list in memory */ |
chrisisthefish | 8:e32fcca16102 | 124 | //struct xbee *root; |
chrisisthefish | 9:4b1e3531dd00 | 125 | root = (struct xbee *) malloc( sizeof(struct xbee) ); |
chrisisthefish | 9:4b1e3531dd00 | 126 | root->next = NULL; |
hpham33 | 2:9503a713b648 | 127 | /* The node root points to has its next pointer equal to a null pointer |
chrisisthefish | 8:e32fcca16102 | 128 | set */ |
chrisisthefish | 9:4b1e3531dd00 | 129 | //struct xbee* xbee1; |
chrisisthefish | 9:4b1e3531dd00 | 130 | // struct xbee* xbee2; |
chrisisthefish | 9:4b1e3531dd00 | 131 | // struct xbee* xbee3; |
chrisisthefish | 9:4b1e3531dd00 | 132 | |
chrisisthefish | 9:4b1e3531dd00 | 133 | // printf("Beginning Xbee Radio Struct Setup\n"); |
chrisisthefish | 9:4b1e3531dd00 | 134 | // xbee1 = addnode(root, 0x0013a200, 0x4079d00b); //Router0 |
chrisisthefish | 9:4b1e3531dd00 | 135 | // xbee1->analogType[1] = 1; //Analog Devices TMP36 Temp Sensor |
chrisisthefish | 9:4b1e3531dd00 | 136 | // xbee1->digitalType[0] = 1; //Light control (output) |
chrisisthefish | 9:4b1e3531dd00 | 137 | // xbee1->digitalType[3] = 1; //Motion sensor (input) |
chrisisthefish | 9:4b1e3531dd00 | 138 | // xbee1->analogRpcDataPointer[1] = &ReadTemperatureSensor1; |
chrisisthefish | 9:4b1e3531dd00 | 139 | // printf("xbee1 setup\n"); |
chrisisthefish | 9:4b1e3531dd00 | 140 | xbeeList[0].addrHigh = 0x0013a200; |
chrisisthefish | 9:4b1e3531dd00 | 141 | xbeeList[0].addrLow = 0x4079d00b; //Router0 |
chrisisthefish | 9:4b1e3531dd00 | 142 | xbeeList[0].analogType[1] = 1; //Analog Devices TMP36 Temp Sensor |
chrisisthefish | 9:4b1e3531dd00 | 143 | xbeeList[0].digitalType[0] = 1; //Light control (output) |
chrisisthefish | 9:4b1e3531dd00 | 144 | xbeeList[0].digitalType[2] = 2; //Motion sensor (input) |
chrisisthefish | 9:4b1e3531dd00 | 145 | xbeeList[0].analogRpcDataPointer[1] = &ReadTemperatureSensor0; |
chrisisthefish | 9:4b1e3531dd00 | 146 | // printf("Xbee0: Storing RPC variable address %d\nStored address is %d\n", &ReadTemperatureSensor1, xbeeList[0].analogRpcDataPointer[1]); |
chrisisthefish | 9:4b1e3531dd00 | 147 | xbeeList[0].digitalOutRpcDataPointer[0] = &ReadLightSwitch0; |
chrisisthefish | 9:4b1e3531dd00 | 148 | xbeeList[0].digitalInRpcDataPointer[2] = &ReadMotionDetector0; |
chrisisthefish | 9:4b1e3531dd00 | 149 | Timer motionTimer0; |
chrisisthefish | 9:4b1e3531dd00 | 150 | xbeeList[0].timerList[2] = &motionTimer0; |
chrisisthefish | 8:e32fcca16102 | 151 | |
chrisisthefish | 8:e32fcca16102 | 152 | |
chrisisthefish | 9:4b1e3531dd00 | 153 | // xbee2 = addnode(root, 0x0013a200, 0x4079d023); //Router1 |
chrisisthefish | 9:4b1e3531dd00 | 154 | // xbee2->analogType[1] = 1; //Analog Devices TMP36 Temp Sensor |
chrisisthefish | 9:4b1e3531dd00 | 155 | // xbee2->digitalType[0] = 1; //Light control (output) |
chrisisthefish | 9:4b1e3531dd00 | 156 | // printf("xbee2 setup\n"); |
chrisisthefish | 9:4b1e3531dd00 | 157 | xbeeList[1].addrHigh = 0x0013a200; |
chrisisthefish | 9:4b1e3531dd00 | 158 | xbeeList[1].addrLow = 0x4079d023; //Router1 |
chrisisthefish | 9:4b1e3531dd00 | 159 | xbeeList[1].analogType[1] = 1; //Analog Devices TMP36 Temp Sensor |
chrisisthefish | 9:4b1e3531dd00 | 160 | xbeeList[1].digitalType[0] = 1; //Light control (output) |
chrisisthefish | 9:4b1e3531dd00 | 161 | xbeeList[1].digitalType[2] = 2; //Motion sensor (input) |
chrisisthefish | 9:4b1e3531dd00 | 162 | xbeeList[1].analogRpcDataPointer[1] = &ReadTemperatureSensor1; |
chrisisthefish | 9:4b1e3531dd00 | 163 | // printf("Xbee1: Storing RPC variable address %d\nStored address is %d\n", &ReadTemperatureSensor2, xbeeList[1].analogRpcDataPointer[1]); |
chrisisthefish | 9:4b1e3531dd00 | 164 | xbeeList[1].digitalOutRpcDataPointer[0] = &ReadLightSwitch1; |
chrisisthefish | 9:4b1e3531dd00 | 165 | xbeeList[1].digitalInRpcDataPointer[2] = &ReadMotionDetector1; |
chrisisthefish | 9:4b1e3531dd00 | 166 | Timer motionTimer1; |
chrisisthefish | 9:4b1e3531dd00 | 167 | xbeeList[1].timerList[2] = &motionTimer1; |
chrisisthefish | 8:e32fcca16102 | 168 | |
chrisisthefish | 8:e32fcca16102 | 169 | |
chrisisthefish | 9:4b1e3531dd00 | 170 | //xbee3 = addnode(root, 0, 3); |
chrisisthefish | 9:4b1e3531dd00 | 171 | // xbee3->digitalType[3] = 1; //Motion sensor (input) |
chrisisthefish | 9:4b1e3531dd00 | 172 | |
chrisisthefish | 9:4b1e3531dd00 | 173 | printf("Initialization finished\n\n"); |
mmujica6 | 5:ae3cbcf75d78 | 174 | |
chrisisthefish | 8:e32fcca16102 | 175 | // TODO: Read sensors and set RPC variables to initial values |
mmujica6 | 5:ae3cbcf75d78 | 176 | |
chrisisthefish | 9:4b1e3531dd00 | 177 | Timer tm; |
chrisisthefish | 9:4b1e3531dd00 | 178 | tm.start(); |
chrisisthefish | 8:e32fcca16102 | 179 | //Main program loop |
chrisisthefish | 8:e32fcca16102 | 180 | while(true){ |
chrisisthefish | 8:e32fcca16102 | 181 | Net::poll(); |
chrisisthefish | 9:4b1e3531dd00 | 182 | xbeeSerialCallback(); |
chrisisthefish | 8:e32fcca16102 | 183 | monitorXbee(); |
chrisisthefish | 9:4b1e3531dd00 | 184 | compareDigitalReadWrite(); |
chrisisthefish | 9:4b1e3531dd00 | 185 | |
chrisisthefish | 9:4b1e3531dd00 | 186 | |
chrisisthefish | 8:e32fcca16102 | 187 | // TODO: Poll sensors and reset RPC variables. |
chrisisthefish | 9:4b1e3531dd00 | 188 | |
chrisisthefish | 9:4b1e3531dd00 | 189 | if(tm.read() > 1){ |
chrisisthefish | 9:4b1e3531dd00 | 190 | led1 = !led1; |
chrisisthefish | 9:4b1e3531dd00 | 191 | tm.start(); |
chrisisthefish | 9:4b1e3531dd00 | 192 | |
chrisisthefish | 9:4b1e3531dd00 | 193 | monitorTimers(); |
chrisisthefish | 9:4b1e3531dd00 | 194 | } |
chrisisthefish | 8:e32fcca16102 | 195 | } |
hpham33 | 2:9503a713b648 | 196 | } |
hpham33 | 2:9503a713b648 | 197 | |
chrisisthefish | 9:4b1e3531dd00 | 198 | struct xbee* addnode(struct xbee* root, unsigned int addrHigh, unsigned int addrLow){ |
chrisisthefish | 8:e32fcca16102 | 199 | struct xbee* node; |
chrisisthefish | 8:e32fcca16102 | 200 | node = root; |
chrisisthefish | 8:e32fcca16102 | 201 | |
hpham33 | 2:9503a713b648 | 202 | if ( node != 0 ) { |
chrisisthefish | 8:e32fcca16102 | 203 | while ( node->next != 0){ |
chrisisthefish | 8:e32fcca16102 | 204 | node = node->next; |
hpham33 | 2:9503a713b648 | 205 | } |
chrisisthefish | 0:c498b8bcfc46 | 206 | } |
chrisisthefish | 8:e32fcca16102 | 207 | node = (struct xbee *) malloc( sizeof(struct xbee) ); |
chrisisthefish | 8:e32fcca16102 | 208 | node->next = NULL; |
chrisisthefish | 9:4b1e3531dd00 | 209 | node->addrHigh = addrHigh; |
chrisisthefish | 9:4b1e3531dd00 | 210 | node->addrLow = addrLow; |
chrisisthefish | 8:e32fcca16102 | 211 | return node; |
hpham33 | 2:9503a713b648 | 212 | } |
chrisisthefish | 8:e32fcca16102 | 213 | |
chrisisthefish | 8:e32fcca16102 | 214 | |
chrisisthefish | 8:e32fcca16102 | 215 | int getDigitalValue(int i, short pins){ |
chrisisthefish | 8:e32fcca16102 | 216 | return ((pins>>i)&1); |
hpham33 | 2:9503a713b648 | 217 | } |
hpham33 | 2:9503a713b648 | 218 | |
chrisisthefish | 8:e32fcca16102 | 219 | |
chrisisthefish | 8:e32fcca16102 | 220 | float analogInputFormat(float data, int type){ |
chrisisthefish | 8:e32fcca16102 | 221 | // Incoming data is in volts: |
chrisisthefish | 8:e32fcca16102 | 222 | // Example: data = 0.7 -> 0.7 Volts |
chrisisthefish | 8:e32fcca16102 | 223 | |
chrisisthefish | 8:e32fcca16102 | 224 | switch (type){ |
chrisisthefish | 8:e32fcca16102 | 225 | case 1: //Analog Devices TMP36 Temp Sensor: 1 deg F per 10mV |
chrisisthefish | 8:e32fcca16102 | 226 | return data * 100; //Multiply voltage by 100 to get Temperature |
chrisisthefish | 8:e32fcca16102 | 227 | case 2: |
chrisisthefish | 8:e32fcca16102 | 228 | return data; |
chrisisthefish | 8:e32fcca16102 | 229 | case 3: |
chrisisthefish | 8:e32fcca16102 | 230 | return data; |
chrisisthefish | 8:e32fcca16102 | 231 | default: |
chrisisthefish | 8:e32fcca16102 | 232 | return data; |
chrisisthefish | 8:e32fcca16102 | 233 | } |
chrisisthefish | 8:e32fcca16102 | 234 | // return(data); |
chrisisthefish | 8:e32fcca16102 | 235 | } |
chrisisthefish | 8:e32fcca16102 | 236 | |
chrisisthefish | 8:e32fcca16102 | 237 | |
chrisisthefish | 8:e32fcca16102 | 238 | |
chrisisthefish | 9:4b1e3531dd00 | 239 | void digitalInputHandle(struct xbee* root, unsigned int addrHigh, unsigned int addrLow, unsigned short data){ |
chrisisthefish | 9:4b1e3531dd00 | 240 | //struct xbee* node; |
chrisisthefish | 9:4b1e3531dd00 | 241 | // node = root; |
chrisisthefish | 9:4b1e3531dd00 | 242 | |
chrisisthefish | 9:4b1e3531dd00 | 243 | //printf("Digital input handler: Address: %x %x\nDigital Data = %d\n", addrHigh, addrLow, data); |
chrisisthefish | 9:4b1e3531dd00 | 244 | |
chrisisthefish | 9:4b1e3531dd00 | 245 | for(int i = 0; i < xbeeCount; i++){ |
chrisisthefish | 9:4b1e3531dd00 | 246 | if(xbeeList[i].addrHigh == addrHigh && xbeeList[i].addrLow == addrLow){ |
chrisisthefish | 9:4b1e3531dd00 | 247 | //Addresses match match |
chrisisthefish | 9:4b1e3531dd00 | 248 | |
chrisisthefish | 9:4b1e3531dd00 | 249 | //Place the digital data in the appropriate position in the struct |
chrisisthefish | 9:4b1e3531dd00 | 250 | xbeeList[i].digitalData = data; |
chrisisthefish | 9:4b1e3531dd00 | 251 | |
chrisisthefish | 9:4b1e3531dd00 | 252 | //Need to update appropriate RPC variables |
chrisisthefish | 9:4b1e3531dd00 | 253 | break; |
chrisisthefish | 9:4b1e3531dd00 | 254 | } |
chrisisthefish | 9:4b1e3531dd00 | 255 | if(i == (xbeeCount - 1)){ |
chrisisthefish | 9:4b1e3531dd00 | 256 | printf("No matching addresses found\n"); |
chrisisthefish | 9:4b1e3531dd00 | 257 | } |
chrisisthefish | 9:4b1e3531dd00 | 258 | } |
chrisisthefish | 9:4b1e3531dd00 | 259 | |
chrisisthefish | 9:4b1e3531dd00 | 260 | |
chrisisthefish | 9:4b1e3531dd00 | 261 | /* |
chrisisthefish | 9:4b1e3531dd00 | 262 | Add code here to check for digital input changes. |
chrisisthefish | 9:4b1e3531dd00 | 263 | This is important to be able to detect motion |
chrisisthefish | 9:4b1e3531dd00 | 264 | */ |
chrisisthefish | 9:4b1e3531dd00 | 265 | |
hpham33 | 2:9503a713b648 | 266 | |
chrisisthefish | 9:4b1e3531dd00 | 267 | /*if ( node != 0 ) { |
chrisisthefish | 9:4b1e3531dd00 | 268 | while ( node->addrHigh != addrHigh){ |
hpham33 | 2:9503a713b648 | 269 | node = node->next; |
hpham33 | 3:c4bec8e7cc07 | 270 | } |
chrisisthefish | 9:4b1e3531dd00 | 271 | while(node->addrLow !=addrLow){ |
chrisisthefish | 8:e32fcca16102 | 272 | node = node->next; |
chrisisthefish | 8:e32fcca16102 | 273 | } |
hpham33 | 2:9503a713b648 | 274 | } |
hpham33 | 4:6091cb494e73 | 275 | else { |
chrisisthefish | 8:e32fcca16102 | 276 | printf("There is no node in the list"); |
hpham33 | 4:6091cb494e73 | 277 | } |
chrisisthefish | 8:e32fcca16102 | 278 | |
chrisisthefish | 8:e32fcca16102 | 279 | if(node != 0){ |
chrisisthefish | 8:e32fcca16102 | 280 | node->digitalData = data; |
hpham33 | 4:6091cb494e73 | 281 | } |
hpham33 | 4:6091cb494e73 | 282 | else{ |
chrisisthefish | 8:e32fcca16102 | 283 | printf("Node is not in the list"); |
chrisisthefish | 9:4b1e3531dd00 | 284 | }*/ |
chrisisthefish | 8:e32fcca16102 | 285 | |
hpham33 | 2:9503a713b648 | 286 | } |
chrisisthefish | 8:e32fcca16102 | 287 | |
chrisisthefish | 8:e32fcca16102 | 288 | |
chrisisthefish | 9:4b1e3531dd00 | 289 | void analogInputHandle(struct xbee* root,unsigned int addrHigh, unsigned int addrLow, int index, float data){ |
chrisisthefish | 9:4b1e3531dd00 | 290 | //struct xbee* node; |
chrisisthefish | 9:4b1e3531dd00 | 291 | // node = root; |
chrisisthefish | 9:4b1e3531dd00 | 292 | float analogData; |
chrisisthefish | 9:4b1e3531dd00 | 293 | |
chrisisthefish | 9:4b1e3531dd00 | 294 | //printf("Analog input handler: Address: %x %x\nAnalog Index = %d Analog Value = %f\n", addrHigh, addrLow, index, data); |
chrisisthefish | 8:e32fcca16102 | 295 | |
chrisisthefish | 8:e32fcca16102 | 296 | if(index < 0){ |
chrisisthefish | 8:e32fcca16102 | 297 | printf("ERROR: Analog input index is negative\n"); |
chrisisthefish | 9:4b1e3531dd00 | 298 | return; |
chrisisthefish | 9:4b1e3531dd00 | 299 | } |
chrisisthefish | 9:4b1e3531dd00 | 300 | |
chrisisthefish | 9:4b1e3531dd00 | 301 | |
chrisisthefish | 9:4b1e3531dd00 | 302 | for(int i = 0; i < xbeeCount; i++){ |
chrisisthefish | 9:4b1e3531dd00 | 303 | if(xbeeList[i].addrHigh == addrHigh && xbeeList[i].addrLow == addrLow){ |
chrisisthefish | 9:4b1e3531dd00 | 304 | //Addresses match match |
chrisisthefish | 9:4b1e3531dd00 | 305 | if(DEBUG) |
chrisisthefish | 9:4b1e3531dd00 | 306 | printf("Xbee %d radio address match\n", i); |
chrisisthefish | 9:4b1e3531dd00 | 307 | |
chrisisthefish | 9:4b1e3531dd00 | 308 | //Place the analog data in the appropriate position in the struct |
chrisisthefish | 9:4b1e3531dd00 | 309 | analogData = analogInputFormat(data, xbeeList[i].analogType[index]); |
chrisisthefish | 9:4b1e3531dd00 | 310 | xbeeList[i].analogData[index] = analogData; |
chrisisthefish | 9:4b1e3531dd00 | 311 | |
chrisisthefish | 9:4b1e3531dd00 | 312 | if(xbeeList[i].analogRpcDataPointer[index] != NULL){ |
chrisisthefish | 9:4b1e3531dd00 | 313 | //Also push the analog data to the RPC variable |
chrisisthefish | 9:4b1e3531dd00 | 314 | *xbeeList[i].analogRpcDataPointer[index] = analogData; |
chrisisthefish | 9:4b1e3531dd00 | 315 | //printf("Storing value %f into RPC pointer. Value stored is %f\n", analogData, *xbeeList[i].analogRpcDataPointer[index]); |
chrisisthefish | 9:4b1e3531dd00 | 316 | }else |
chrisisthefish | 9:4b1e3531dd00 | 317 | printf("No valid RPC variable found\n"); |
chrisisthefish | 9:4b1e3531dd00 | 318 | break; |
chrisisthefish | 9:4b1e3531dd00 | 319 | } |
chrisisthefish | 9:4b1e3531dd00 | 320 | if(i == (xbeeCount - 1)){ |
chrisisthefish | 9:4b1e3531dd00 | 321 | printf("No matching addresses found\n"); |
chrisisthefish | 9:4b1e3531dd00 | 322 | } |
chrisisthefish | 8:e32fcca16102 | 323 | } |
chrisisthefish | 8:e32fcca16102 | 324 | |
chrisisthefish | 9:4b1e3531dd00 | 325 | |
chrisisthefish | 9:4b1e3531dd00 | 326 | |
chrisisthefish | 9:4b1e3531dd00 | 327 | |
chrisisthefish | 9:4b1e3531dd00 | 328 | |
chrisisthefish | 9:4b1e3531dd00 | 329 | |
chrisisthefish | 9:4b1e3531dd00 | 330 | |
chrisisthefish | 9:4b1e3531dd00 | 331 | /*if ( node != 0 ) { |
chrisisthefish | 9:4b1e3531dd00 | 332 | printf("Comparing addrHigh %x to %x\n", node->addrHigh, addrHigh); |
chrisisthefish | 9:4b1e3531dd00 | 333 | while ( node->addrHigh != addrHigh){ |
chrisisthefish | 9:4b1e3531dd00 | 334 | if(node->next != NULL){ |
chrisisthefish | 9:4b1e3531dd00 | 335 | node = node->next; |
chrisisthefish | 9:4b1e3531dd00 | 336 | }else{ |
chrisisthefish | 9:4b1e3531dd00 | 337 | printf("Reached end of search with no addrHigh match\n"); |
chrisisthefish | 9:4b1e3531dd00 | 338 | break; |
chrisisthefish | 9:4b1e3531dd00 | 339 | } |
chrisisthefish | 9:4b1e3531dd00 | 340 | printf("Comparing addrHigh %x to %x\n", node->addrHigh, addrHigh); |
chrisisthefish | 8:e32fcca16102 | 341 | } |
chrisisthefish | 8:e32fcca16102 | 342 | |
chrisisthefish | 9:4b1e3531dd00 | 343 | printf("Comparing addrLow %x to %x\n", node->addrLow, addrLow); |
chrisisthefish | 9:4b1e3531dd00 | 344 | while(node->addrLow != addrLow){ |
chrisisthefish | 9:4b1e3531dd00 | 345 | if(node->next != NULL){ |
chrisisthefish | 9:4b1e3531dd00 | 346 | node = node->next; |
chrisisthefish | 9:4b1e3531dd00 | 347 | }else{ |
chrisisthefish | 9:4b1e3531dd00 | 348 | printf("Reached end of search with no addrLow match\n"); |
chrisisthefish | 9:4b1e3531dd00 | 349 | break; |
chrisisthefish | 9:4b1e3531dd00 | 350 | } |
chrisisthefish | 9:4b1e3531dd00 | 351 | printf("Comparing addrLow %x to %x\n", node->addrLow, addrLow); |
hpham33 | 2:9503a713b648 | 352 | } |
hpham33 | 4:6091cb494e73 | 353 | }else { |
chrisisthefish | 8:e32fcca16102 | 354 | printf("There is no node in the list"); |
hpham33 | 2:9503a713b648 | 355 | } |
chrisisthefish | 8:e32fcca16102 | 356 | |
chrisisthefish | 8:e32fcca16102 | 357 | if(node != 0 && index >= 0){ |
chrisisthefish | 9:4b1e3531dd00 | 358 | //Place the analog data in the appropriate position in the struct |
chrisisthefish | 9:4b1e3531dd00 | 359 | analogData = analogInputFormat(data, node->analogType[index]); |
chrisisthefish | 9:4b1e3531dd00 | 360 | node->analogData[index] = analogData; |
chrisisthefish | 9:4b1e3531dd00 | 361 | |
chrisisthefish | 9:4b1e3531dd00 | 362 | if(node->analogRpcDataPointer[index] != NULL){ |
chrisisthefish | 9:4b1e3531dd00 | 363 | //Also push the analog data to the RPC variable |
chrisisthefish | 9:4b1e3531dd00 | 364 | *node->analogRpcDataPointer[index] = analogData; |
chrisisthefish | 9:4b1e3531dd00 | 365 | printf("Storing value %f into RPC pointer. Value stored is %f\n", analogData, *node->analogRpcDataPointer[index]); |
chrisisthefish | 9:4b1e3531dd00 | 366 | } |
chrisisthefish | 8:e32fcca16102 | 367 | } |
chrisisthefish | 8:e32fcca16102 | 368 | else{ |
chrisisthefish | 8:e32fcca16102 | 369 | printf("Node is not in the list"); |
chrisisthefish | 9:4b1e3531dd00 | 370 | }*/ |
chrisisthefish | 9:4b1e3531dd00 | 371 | } |
chrisisthefish | 9:4b1e3531dd00 | 372 | |
chrisisthefish | 9:4b1e3531dd00 | 373 | |
chrisisthefish | 9:4b1e3531dd00 | 374 | |
chrisisthefish | 9:4b1e3531dd00 | 375 | |
chrisisthefish | 9:4b1e3531dd00 | 376 | void compareDigitalReadWrite(){ |
chrisisthefish | 9:4b1e3531dd00 | 377 | int mask = 1; |
chrisisthefish | 9:4b1e3531dd00 | 378 | int i, digiIndex; |
chrisisthefish | 9:4b1e3531dd00 | 379 | int rpcValue = 0; |
chrisisthefish | 9:4b1e3531dd00 | 380 | int digiValue = 0; |
chrisisthefish | 9:4b1e3531dd00 | 381 | |
chrisisthefish | 9:4b1e3531dd00 | 382 | for(i = 0; i < xbeeCount; i++){ //Loop through all xbees |
chrisisthefish | 9:4b1e3531dd00 | 383 | mask = 1; |
chrisisthefish | 9:4b1e3531dd00 | 384 | |
chrisisthefish | 9:4b1e3531dd00 | 385 | for(digiIndex = 0; digiIndex < 10; digiIndex++){ //Loop through all digital inputs to see if they do not match the corresponding RPC variable |
chrisisthefish | 9:4b1e3531dd00 | 386 | |
chrisisthefish | 9:4b1e3531dd00 | 387 | if(xbeeList[i].digitalType[digiIndex] == 1){ //Is this digital I/O a light control (digital output)? |
chrisisthefish | 9:4b1e3531dd00 | 388 | |
chrisisthefish | 9:4b1e3531dd00 | 389 | if(*xbeeList[i].digitalOutRpcDataPointer[digiIndex]) |
chrisisthefish | 9:4b1e3531dd00 | 390 | rpcValue = 1; |
chrisisthefish | 9:4b1e3531dd00 | 391 | else |
chrisisthefish | 9:4b1e3531dd00 | 392 | rpcValue = 0; |
chrisisthefish | 9:4b1e3531dd00 | 393 | |
chrisisthefish | 9:4b1e3531dd00 | 394 | if((xbeeList[i].digitalData & mask) != rpcValue){ |
chrisisthefish | 9:4b1e3531dd00 | 395 | printf("Xbee%d: Digital output %d = %d doesn't match RPC variable = %d Updating Xbee...\n", i, digiIndex, xbeeList[i].digitalData & mask, rpcValue); |
chrisisthefish | 9:4b1e3531dd00 | 396 | //This means that the digital output is in the incorrect state |
chrisisthefish | 9:4b1e3531dd00 | 397 | //Therefore write out the RPC state to the radio |
chrisisthefish | 9:4b1e3531dd00 | 398 | digitalWriteXbee(xbeeList[i].addrHigh, xbeeList[i].addrLow, digiIndex, rpcValue); |
chrisisthefish | 9:4b1e3531dd00 | 399 | //printf("Old digitalData = %d\t", xbeeList[i].digitalData); |
chrisisthefish | 9:4b1e3531dd00 | 400 | xbeeList[i].digitalData = (xbeeList[i].digitalData & ~mask) | (rpcValue << digiIndex); //Update the digitalData value with the new output value |
chrisisthefish | 9:4b1e3531dd00 | 401 | //printf("Updated digitalData = %d, mask = %d, rpcValue = %d, digiIndex = %d\n", xbeeList[i].digitalData, mask, rpcValue, digiIndex); |
chrisisthefish | 9:4b1e3531dd00 | 402 | } |
chrisisthefish | 9:4b1e3531dd00 | 403 | //Otherwise, it matches |
chrisisthefish | 9:4b1e3531dd00 | 404 | } |
chrisisthefish | 9:4b1e3531dd00 | 405 | |
chrisisthefish | 9:4b1e3531dd00 | 406 | if(xbeeList[i].digitalType[digiIndex] == 2){ //Is this digital I/O a motion detector (digital input)? |
chrisisthefish | 9:4b1e3531dd00 | 407 | digiValue = (xbeeList[i].digitalData & mask); |
chrisisthefish | 9:4b1e3531dd00 | 408 | |
chrisisthefish | 9:4b1e3531dd00 | 409 | //Get the RPC value |
chrisisthefish | 9:4b1e3531dd00 | 410 | if(*xbeeList[i].digitalInRpcDataPointer[digiIndex]) |
chrisisthefish | 9:4b1e3531dd00 | 411 | rpcValue = 1; |
chrisisthefish | 9:4b1e3531dd00 | 412 | else |
chrisisthefish | 9:4b1e3531dd00 | 413 | rpcValue = 0; |
chrisisthefish | 9:4b1e3531dd00 | 414 | |
chrisisthefish | 9:4b1e3531dd00 | 415 | if(digiValue != rpcValue){ //This means there has been a state change, either motion detected, or sensor returning to normal |
chrisisthefish | 9:4b1e3531dd00 | 416 | |
chrisisthefish | 9:4b1e3531dd00 | 417 | if(digiValue == 0 || rpcValue == 0){ //Motion detected |
chrisisthefish | 9:4b1e3531dd00 | 418 | |
chrisisthefish | 9:4b1e3531dd00 | 419 | // printf("Motion detected on Xbee%d\n", i); |
chrisisthefish | 9:4b1e3531dd00 | 420 | //if(digiValue) |
chrisisthefish | 9:4b1e3531dd00 | 421 | // rpcValue = 1; |
chrisisthefish | 9:4b1e3531dd00 | 422 | // else |
chrisisthefish | 9:4b1e3531dd00 | 423 | // rpcValue = 0; |
chrisisthefish | 9:4b1e3531dd00 | 424 | |
chrisisthefish | 9:4b1e3531dd00 | 425 | rpcValue = 1; |
chrisisthefish | 9:4b1e3531dd00 | 426 | *xbeeList[i].digitalInRpcDataPointer[digiIndex] = rpcValue; |
chrisisthefish | 9:4b1e3531dd00 | 427 | |
chrisisthefish | 9:4b1e3531dd00 | 428 | *xbeeList[i].digitalOutRpcDataPointer[0] = 1; //Turn on the light |
chrisisthefish | 9:4b1e3531dd00 | 429 | xbeeList[i].timerList[digiIndex]->start(); //Restart timer |
chrisisthefish | 9:4b1e3531dd00 | 430 | } |
chrisisthefish | 9:4b1e3531dd00 | 431 | } |
chrisisthefish | 9:4b1e3531dd00 | 432 | |
chrisisthefish | 9:4b1e3531dd00 | 433 | } |
chrisisthefish | 9:4b1e3531dd00 | 434 | mask = mask << 1; |
chrisisthefish | 9:4b1e3531dd00 | 435 | } |
chrisisthefish | 8:e32fcca16102 | 436 | } |
chrisisthefish | 9:4b1e3531dd00 | 437 | } |
chrisisthefish | 9:4b1e3531dd00 | 438 | |
chrisisthefish | 9:4b1e3531dd00 | 439 | |
chrisisthefish | 9:4b1e3531dd00 | 440 | |
chrisisthefish | 9:4b1e3531dd00 | 441 | void monitorTimers(){ |
chrisisthefish | 9:4b1e3531dd00 | 442 | |
chrisisthefish | 9:4b1e3531dd00 | 443 | for(int i = 0; i < xbeeCount; i++){ //Loop through all xbees |
chrisisthefish | 9:4b1e3531dd00 | 444 | for(int digiIndex = 0; digiIndex < 10; digiIndex++){ //Loop through all digital I/O |
chrisisthefish | 9:4b1e3531dd00 | 445 | if(xbeeList[i].digitalType[digiIndex] == 2){ //See if I/O is a motion detector input |
chrisisthefish | 9:4b1e3531dd00 | 446 | if(xbeeList[i].timerList[digiIndex]->read() > 15){ //Check if timer has expired |
chrisisthefish | 9:4b1e3531dd00 | 447 | *xbeeList[i].digitalOutRpcDataPointer[0] = 0; //If timer has expired, turn light off |
chrisisthefish | 9:4b1e3531dd00 | 448 | } |
chrisisthefish | 9:4b1e3531dd00 | 449 | } |
chrisisthefish | 9:4b1e3531dd00 | 450 | } |
chrisisthefish | 9:4b1e3531dd00 | 451 | } |
chrisisthefish | 9:4b1e3531dd00 | 452 | } |