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