Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
Diff: main.cpp
- Revision:
- 8:e32fcca16102
- Parent:
- 5:ae3cbcf75d78
- Child:
- 9:4b1e3531dd00
diff -r ae3cbcf75d78 -r e32fcca16102 main.cpp --- a/main.cpp Mon Dec 02 19:28:26 2013 +0000 +++ b/main.cpp Mon Dec 02 22:25:29 2013 +0000 @@ -1,10 +1,32 @@ +/* + +Analog Types: +0 = No connection +1 = Analog Devices TMP36 Temperature Sensor + + +Digital Types: +0 = No connection +1 = Light Control (Digital Output) +2 = Motion Sense (Digital Input) + +*/ + + #include "mbed.h" #include "EthernetNetIf.h" #include "HTTPServer.h" #include "SerialRPCInterface.h" +#include "XbeeCommLib.h" DigitalIn pb(p8); +Serial xbeeSerial(p9, p10); +C12832_LCD lcd; + +unsigned char data[500]; +int dataCounter = 0; +bool clear = false; //Create port variables float ReadTemperatureSensor1; @@ -29,6 +51,8 @@ EthernetNetIf eth; HTTPServer svr; + + struct xbee { // radio prototype with addresss, location, pointer to sensor list unsigned int addrHigh; // upper 16 bits address of sensor unsigned int addrLow; // lower address of sensor @@ -40,133 +64,181 @@ struct xbee * next; // pointer to next struct }; -struct xbee* addnode(struct xbee*,unsigned int,unsigned int ); -int getDigitalValue(int , short ); -void digitalInputHandle(struct xbee* ,unsigned int , unsigned int , unsigned short ); -void analogInputHandle(struct xbee* ,unsigned int , unsigned int , int , float ); +struct xbee *root; + + + + +void xbeeSerialCallback() { + if(clear){ + dataCounter = 0; + clear = false; + } + if(dataCounter < 500){ + while(xbeeSerial.readable() == true && dataCounter < 500){ + data[dataCounter] = xbeeSerial.getc(); + dataCounter++; + } + } + else{ + printf("Serial data buffer overflow. Resetting buffer...\n"); + dataCounter = 0; + data[dataCounter] = xbeeSerial.getc(); + } +} + + int main() { - - // Ethernet Setup - EthernetErr ethErr = eth.setup(); - if(ethErr) - { + + xbeeSerial.attach(&xbeeSerialCallback); + + // Ethernet Setup + EthernetErr ethErr = eth.setup(); + if(ethErr) + { printf("Error %d in setup.\n", ethErr); return -1; - } - printf("Setup OK\n"); + } + printf("Setup OK\n"); + + // Add RPCHandler + svr.addHandler<RPCHandler>("/rpc"); + + // Show that the server is ready and listening + svr.bind(80); + printf("Listening...\n"); - // Add RPCHandler - svr.addHandler<RPCHandler>("/rpc"); - - // Show that the server is ready and listening - svr.bind(80); - printf("Listening...\n"); - - /* This won't change, or we would lose the list in memory */ - struct xbee *root; + /* This won't change, or we would lose the list in memory */ + //struct xbee *root; root->next = NULL; /* The node root points to has its next pointer equal to a null pointer - set */ - struct xbee* xbee1; - struct xbee* xbee2; - struct xbee* xbee3; - - xbee1 = addnode(root,0,1); - xbee2 = addnode(root,0,2); - xbee3 = addnode(root,0,3); + set */ + struct xbee* xbee1; + struct xbee* xbee2; + struct xbee* xbee3; + + xbee1 = addnode(root, 0x0013a200, 0x4079d00b); //Router0 + xbee1->analogType[1] = 1; //Analog Devices TMP36 Temp Sensor + xbee1->digitalType[0] = 1; //Light control (output) + xbee1->digitalType[3] = 1; //Motion sensor (input) + + xbee2 = addnode(root, 0x0013a200, 0x4079d023); //Router1 + xbee2->analogType[1] = 1; //Analog Devices TMP36 Temp Sensor + xbee2->digitalType[0] = 1; //Light control (output) + + xbee3 = addnode(root, 0, 3); + xbee3->digitalType[3] = 1; //Motion sensor (input) + - // TODO: Read sensors and set RPC variables to initial values - - //Main program loop - while(true){ - Net::poll(); + // TODO: Read sensors and set RPC variables to initial values - // TODO: Poll sensors and reset RPC variables. - - } + //Main program loop + while(true){ + Net::poll(); + monitorXbee(); + // TODO: Poll sensors and reset RPC variables. + } } -struct xbee* addnode(struct xbee* root,unsigned int addrhigh,unsigned int addrlow){ - - struct xbee* node; - node = root; - +struct xbee* addnode(struct xbee* root, unsigned int addrhigh, unsigned int addrlow){ + struct xbee* node; + node = root; + if ( node != 0 ) { - while ( node->next != 0) - { - node = node->next; + while ( node->next != 0){ + node = node->next; } } -node = (struct xbee *) malloc( sizeof(struct xbee) ); -node->next = NULL; -node->addrHigh =addrhigh; -node->addrLow =addrlow; - return node; -} -int getDigitalValue(int i, short pins){ -return ((pins>>i)&1); + node = (struct xbee *) malloc( sizeof(struct xbee) ); + node->next = NULL; + node->addrHigh = addrhigh; + node->addrLow = addrlow; + return node; } -float analogInputFormat(float data, int type){ - switch (type){ - case 1: - data = data; - break; - case 2: - data = data; - break; - case 3: - data = data; - break; - default: - data = data; - break; - } -return(data); + + +int getDigitalValue(int i, short pins){ + return ((pins>>i)&1); } -void digitalInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, unsigned short data){ -struct xbee* node; -node = root; + +float analogInputFormat(float data, int type){ + // Incoming data is in volts: + // Example: data = 0.7 -> 0.7 Volts + + switch (type){ + case 1: //Analog Devices TMP36 Temp Sensor: 1 deg F per 10mV + return data * 100; //Multiply voltage by 100 to get Temperature + case 2: + return data; + case 3: + return data; + default: + return data; + } +// return(data); +} + + + +void digitalInputHandle(struct xbee* root, unsigned int addrhigh, unsigned int addrlow, unsigned short data){ + struct xbee* node; + node = root; if ( node != 0 ) { - while ( node->addrHigh != addrhigh) - { + while ( node->addrHigh != addrhigh){ node = node->next; } - while(node->addrLow !=addrlow){ - node = node->next; - } + while(node->addrLow !=addrlow){ + node = node->next; + } } else { - printf("There is no node in the list"); + printf("There is no node in the list"); } - if(node !=0){ - node->digitalData = data; + + if(node != 0){ + node->digitalData = data; } else{ - printf("Node is not in the list"); - } + printf("Node is not in the list"); + } + + + /* + Add code here to check for digital input changes. + This is important to be able to detect motion + */ + + } + + void analogInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, int index, float data){ -struct xbee* node; -node = root; - + struct xbee* node; + node = root; + + if(index < 0){ + printf("ERROR: Analog input index is negative\n"); + } + if ( node != 0 ) { - while ( node->addrHigh != addrhigh) - { - node = node->next; - } - while(node->addrLow !=addrlow){ - node = node->next; + while ( node->addrHigh != addrhigh){ + node = node->next; + } + + while(node->addrLow != addrlow){ + node = node->next; } }else { - printf("There is no node in the list"); + printf("There is no node in the list"); } - if(node !=0){ - node->analogData[index] = data; - } - else{ - printf("Node is not in the list"); - } + + if(node != 0 && index >= 0){ + node->analogData[index] = analogInputFormat(data, node->analogType[index]); + } + else{ + printf("Node is not in the list"); + } } \ No newline at end of file