Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

main.cpp

Committer:
mmujica6
Date:
2013-12-02
Revision:
5:ae3cbcf75d78
Parent:
4:6091cb494e73
Child:
8:e32fcca16102

File content as of revision 5:ae3cbcf75d78:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPServer.h"
#include "SerialRPCInterface.h"

DigitalIn pb(p8);


//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* 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 );

int main() {

  // 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,0,1);
     xbee2 = addnode(root,0,2);
     xbee3 = addnode(root,0,3);
     
     // TODO: Read sensors and set RPC variables to initial values
     
  //Main program loop
  while(true){
     Net::poll();
    
     // 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){
    switch (type){
    case 1:
    data = data;
    break;
    case 2:
    data = data;
    break;
    case 3:
    data = data;
    break;
    default:
    data = data;
    break;
    }
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");
 }
}
void analogInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, int index, float 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->analogData[index] = data;
 }
 else{
 printf("Node is not in the list");
 }
}