Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

main.cpp

Committer:
chrisisthefish
Date:
2013-12-02
Revision:
8:e32fcca16102
Parent:
5:ae3cbcf75d78
Child:
9:4b1e3531dd00

File content as of revision 8:e32fcca16102:

/*

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;
float ReadTemperatureSensor2;
int ReadMotionDetector1;
int ReadMotionDetector2;
int ReadLightSwitch1;
int ReadLightSwitch2;
int WriteLightSwitch1;
int WriteLightSwitch2;

//Make these variables accessible over RPC by attaching them to an RPCVariable
RPCVariable<float> RPCTemperatureSensor1(&ReadTemperatureSensor1, "TemperatureSensor1");
RPCVariable<float> RPCTemperatureSensor2(&ReadTemperatureSensor2, "TemperatureSensor2");
RPCVariable<int> RPCMotionDetector1(&ReadMotionDetector1, "MotionDetector1");
RPCVariable<int> RPCMotionDetector2(&ReadMotionDetector2, "MotionDetector2");
RPCVariable<int> RPCReadLightSwitch1(&ReadLightSwitch1, "ReadLightSwitch1");
RPCVariable<int> RPCReadLightSwitch2(&ReadLightSwitch2, "ReadLightSwitch2");
RPCVariable<int> RPCWriteLightSwitch1(&WriteLightSwitch1, "WriteLightSwitch1");
RPCVariable<int> RPCWriteLightSwitch2(&WriteLightSwitch2, "WriteLightSwitch2");

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
    unsigned short digitalData;
    unsigned short digitalDataOutput;
    int digitalType[10];
    float analogData[4];
    int analogType[4];
    struct xbee * next;    // pointer to next struct
};

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() {
    
    xbeeSerial.attach(&xbeeSerialCallback);
    
    // Ethernet Setup
    EthernetErr ethErr = eth.setup();
    if(ethErr)
    {
    printf("Error %d in setup.\n", ethErr);
    return -1;
    }
    printf("Setup OK\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;
    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, 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();
        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;
    
    if ( node != 0 ) {
        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);
}


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){          
           node = node->next;
        }
        while(node->addrLow !=addrlow){
            node = node->next;
        }
    }
    else {
        printf("There is no node in the list");
    }
    
    if(node != 0){
        node->digitalData = data;
    }
    else{
        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;
    
    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;
        }
    }else {
        printf("There is no node in the list");
    }
    
    if(node != 0 && index >= 0){
        node->analogData[index] = analogInputFormat(data, node->analogType[index]);
    }
    else{
        printf("Node is not in the list");
    }
}