Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

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?

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