Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

Committer:
mmujica6
Date:
Mon Dec 02 19:28:26 2013 +0000
Revision:
5:ae3cbcf75d78
Parent:
4:6091cb494e73
Child:
8:e32fcca16102
Added RPC Variables, main loop, and Ethernet initialization. ; ; I also imported 3 libraries necessary for RPC server to run and switched the mbed official library to Mihail Stoyanov's version, which has some additions necessary for the RPC library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chrisisthefish 0:c498b8bcfc46 1 #include "mbed.h"
mmujica6 5:ae3cbcf75d78 2 #include "EthernetNetIf.h"
mmujica6 5:ae3cbcf75d78 3 #include "HTTPServer.h"
mmujica6 5:ae3cbcf75d78 4 #include "SerialRPCInterface.h"
chrisisthefish 0:c498b8bcfc46 5
hpham33 2:9503a713b648 6 DigitalIn pb(p8);
hpham33 2:9503a713b648 7
hpham33 2:9503a713b648 8
mmujica6 5:ae3cbcf75d78 9 //Create port variables
mmujica6 5:ae3cbcf75d78 10 float ReadTemperatureSensor1;
mmujica6 5:ae3cbcf75d78 11 float ReadTemperatureSensor2;
mmujica6 5:ae3cbcf75d78 12 int ReadMotionDetector1;
mmujica6 5:ae3cbcf75d78 13 int ReadMotionDetector2;
mmujica6 5:ae3cbcf75d78 14 int ReadLightSwitch1;
mmujica6 5:ae3cbcf75d78 15 int ReadLightSwitch2;
mmujica6 5:ae3cbcf75d78 16 int WriteLightSwitch1;
mmujica6 5:ae3cbcf75d78 17 int WriteLightSwitch2;
mmujica6 5:ae3cbcf75d78 18
mmujica6 5:ae3cbcf75d78 19 //Make these variables accessible over RPC by attaching them to an RPCVariable
mmujica6 5:ae3cbcf75d78 20 RPCVariable<float> RPCTemperatureSensor1(&ReadTemperatureSensor1, "TemperatureSensor1");
mmujica6 5:ae3cbcf75d78 21 RPCVariable<float> RPCTemperatureSensor2(&ReadTemperatureSensor2, "TemperatureSensor2");
mmujica6 5:ae3cbcf75d78 22 RPCVariable<int> RPCMotionDetector1(&ReadMotionDetector1, "MotionDetector1");
mmujica6 5:ae3cbcf75d78 23 RPCVariable<int> RPCMotionDetector2(&ReadMotionDetector2, "MotionDetector2");
mmujica6 5:ae3cbcf75d78 24 RPCVariable<int> RPCReadLightSwitch1(&ReadLightSwitch1, "ReadLightSwitch1");
mmujica6 5:ae3cbcf75d78 25 RPCVariable<int> RPCReadLightSwitch2(&ReadLightSwitch2, "ReadLightSwitch2");
mmujica6 5:ae3cbcf75d78 26 RPCVariable<int> RPCWriteLightSwitch1(&WriteLightSwitch1, "WriteLightSwitch1");
mmujica6 5:ae3cbcf75d78 27 RPCVariable<int> RPCWriteLightSwitch2(&WriteLightSwitch2, "WriteLightSwitch2");
mmujica6 5:ae3cbcf75d78 28
mmujica6 5:ae3cbcf75d78 29 EthernetNetIf eth;
mmujica6 5:ae3cbcf75d78 30 HTTPServer svr;
hpham33 2:9503a713b648 31
hpham33 2:9503a713b648 32 struct xbee { // radio prototype with addresss, location, pointer to sensor list
hpham33 2:9503a713b648 33 unsigned int addrHigh; // upper 16 bits address of sensor
hpham33 2:9503a713b648 34 unsigned int addrLow; // lower address of sensor
hpham33 2:9503a713b648 35 unsigned short digitalData;
hpham33 2:9503a713b648 36 unsigned short digitalDataOutput;
hpham33 2:9503a713b648 37 int digitalType[10];
hpham33 2:9503a713b648 38 float analogData[4];
hpham33 2:9503a713b648 39 int analogType[4];
hpham33 2:9503a713b648 40 struct xbee * next; // pointer to next struct
hpham33 2:9503a713b648 41 };
hpham33 2:9503a713b648 42
hpham33 2:9503a713b648 43 struct xbee* addnode(struct xbee*,unsigned int,unsigned int );
hpham33 2:9503a713b648 44 int getDigitalValue(int , short );
hpham33 2:9503a713b648 45 void digitalInputHandle(struct xbee* ,unsigned int , unsigned int , unsigned short );
hpham33 2:9503a713b648 46 void analogInputHandle(struct xbee* ,unsigned int , unsigned int , int , float );
chrisisthefish 0:c498b8bcfc46 47
chrisisthefish 0:c498b8bcfc46 48 int main() {
mmujica6 5:ae3cbcf75d78 49
mmujica6 5:ae3cbcf75d78 50 // Ethernet Setup
mmujica6 5:ae3cbcf75d78 51 EthernetErr ethErr = eth.setup();
mmujica6 5:ae3cbcf75d78 52 if(ethErr)
mmujica6 5:ae3cbcf75d78 53 {
mmujica6 5:ae3cbcf75d78 54 printf("Error %d in setup.\n", ethErr);
mmujica6 5:ae3cbcf75d78 55 return -1;
mmujica6 5:ae3cbcf75d78 56 }
mmujica6 5:ae3cbcf75d78 57 printf("Setup OK\n");
mmujica6 5:ae3cbcf75d78 58
mmujica6 5:ae3cbcf75d78 59 // Add RPCHandler
mmujica6 5:ae3cbcf75d78 60 svr.addHandler<RPCHandler>("/rpc");
mmujica6 5:ae3cbcf75d78 61
mmujica6 5:ae3cbcf75d78 62 // Show that the server is ready and listening
mmujica6 5:ae3cbcf75d78 63 svr.bind(80);
mmujica6 5:ae3cbcf75d78 64 printf("Listening...\n");
mmujica6 5:ae3cbcf75d78 65
hpham33 2:9503a713b648 66 /* This won't change, or we would lose the list in memory */
hpham33 2:9503a713b648 67 struct xbee *root;
hpham33 2:9503a713b648 68 root->next = NULL;
hpham33 2:9503a713b648 69 /* The node root points to has its next pointer equal to a null pointer
hpham33 2:9503a713b648 70 set */
hpham33 2:9503a713b648 71 struct xbee* xbee1;
hpham33 2:9503a713b648 72 struct xbee* xbee2;
hpham33 2:9503a713b648 73 struct xbee* xbee3;
hpham33 2:9503a713b648 74
hpham33 2:9503a713b648 75 xbee1 = addnode(root,0,1);
hpham33 2:9503a713b648 76 xbee2 = addnode(root,0,2);
hpham33 2:9503a713b648 77 xbee3 = addnode(root,0,3);
mmujica6 5:ae3cbcf75d78 78
mmujica6 5:ae3cbcf75d78 79 // TODO: Read sensors and set RPC variables to initial values
mmujica6 5:ae3cbcf75d78 80
mmujica6 5:ae3cbcf75d78 81 //Main program loop
mmujica6 5:ae3cbcf75d78 82 while(true){
mmujica6 5:ae3cbcf75d78 83 Net::poll();
mmujica6 5:ae3cbcf75d78 84
mmujica6 5:ae3cbcf75d78 85 // TODO: Poll sensors and reset RPC variables.
mmujica6 5:ae3cbcf75d78 86
mmujica6 5:ae3cbcf75d78 87 }
hpham33 2:9503a713b648 88 }
hpham33 2:9503a713b648 89
hpham33 2:9503a713b648 90 struct xbee* addnode(struct xbee* root,unsigned int addrhigh,unsigned int addrlow){
hpham33 2:9503a713b648 91
hpham33 2:9503a713b648 92 struct xbee* node;
hpham33 2:9503a713b648 93 node = root;
hpham33 2:9503a713b648 94
hpham33 2:9503a713b648 95 if ( node != 0 ) {
hpham33 2:9503a713b648 96 while ( node->next != 0)
hpham33 2:9503a713b648 97 {
hpham33 2:9503a713b648 98 node = node->next;
hpham33 2:9503a713b648 99 }
chrisisthefish 0:c498b8bcfc46 100 }
hpham33 2:9503a713b648 101 node = (struct xbee *) malloc( sizeof(struct xbee) );
hpham33 2:9503a713b648 102 node->next = NULL;
hpham33 2:9503a713b648 103 node->addrHigh =addrhigh;
hpham33 2:9503a713b648 104 node->addrLow =addrlow;
hpham33 2:9503a713b648 105 return node;
chrisisthefish 0:c498b8bcfc46 106 }
hpham33 2:9503a713b648 107 int getDigitalValue(int i, short pins){
hpham33 2:9503a713b648 108 return ((pins>>i)&1);
hpham33 2:9503a713b648 109 }
hpham33 2:9503a713b648 110 float analogInputFormat(float data, int type){
hpham33 2:9503a713b648 111 switch (type){
hpham33 2:9503a713b648 112 case 1:
hpham33 2:9503a713b648 113 data = data;
hpham33 2:9503a713b648 114 break;
hpham33 2:9503a713b648 115 case 2:
hpham33 2:9503a713b648 116 data = data;
hpham33 2:9503a713b648 117 break;
hpham33 2:9503a713b648 118 case 3:
hpham33 2:9503a713b648 119 data = data;
hpham33 2:9503a713b648 120 break;
hpham33 2:9503a713b648 121 default:
hpham33 2:9503a713b648 122 data = data;
hpham33 2:9503a713b648 123 break;
hpham33 2:9503a713b648 124 }
hpham33 2:9503a713b648 125 return(data);
hpham33 2:9503a713b648 126 }
hpham33 2:9503a713b648 127
hpham33 2:9503a713b648 128 void digitalInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, unsigned short data){
hpham33 2:9503a713b648 129 struct xbee* node;
hpham33 2:9503a713b648 130 node = root;
hpham33 2:9503a713b648 131
hpham33 2:9503a713b648 132 if ( node != 0 ) {
hpham33 2:9503a713b648 133 while ( node->addrHigh != addrhigh)
hpham33 2:9503a713b648 134 {
hpham33 2:9503a713b648 135 node = node->next;
hpham33 3:c4bec8e7cc07 136 }
hpham33 3:c4bec8e7cc07 137 while(node->addrLow !=addrlow){
hpham33 2:9503a713b648 138 node = node->next;
hpham33 2:9503a713b648 139 }
hpham33 2:9503a713b648 140 }
hpham33 4:6091cb494e73 141 else {
hpham33 4:6091cb494e73 142 printf("There is no node in the list");
hpham33 4:6091cb494e73 143 }
hpham33 4:6091cb494e73 144 if(node !=0){
hpham33 2:9503a713b648 145 node->digitalData = data;
hpham33 4:6091cb494e73 146 }
hpham33 4:6091cb494e73 147 else{
hpham33 4:6091cb494e73 148 printf("Node is not in the list");
hpham33 4:6091cb494e73 149 }
hpham33 2:9503a713b648 150 }
hpham33 2:9503a713b648 151 void analogInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, int index, float data){
hpham33 2:9503a713b648 152 struct xbee* node;
hpham33 2:9503a713b648 153 node = root;
hpham33 2:9503a713b648 154
hpham33 2:9503a713b648 155 if ( node != 0 ) {
hpham33 2:9503a713b648 156 while ( node->addrHigh != addrhigh)
hpham33 2:9503a713b648 157 {
hpham33 2:9503a713b648 158 node = node->next;
hpham33 3:c4bec8e7cc07 159 }
hpham33 2:9503a713b648 160 while(node->addrLow !=addrlow){
hpham33 2:9503a713b648 161 node = node->next;
hpham33 2:9503a713b648 162 }
hpham33 4:6091cb494e73 163 }else {
hpham33 4:6091cb494e73 164 printf("There is no node in the list");
hpham33 2:9503a713b648 165 }
hpham33 4:6091cb494e73 166 if(node !=0){
hpham33 2:9503a713b648 167 node->analogData[index] = data;
hpham33 4:6091cb494e73 168 }
hpham33 4:6091cb494e73 169 else{
hpham33 4:6091cb494e73 170 printf("Node is not in the list");
hpham33 4:6091cb494e73 171 }
hpham33 2:9503a713b648 172 }