Skip to content

Instantly share code, notes, and snippets.

@niektemme
Created July 31, 2015 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niektemme/93c1aca6db3c7202212a to your computer and use it in GitHub Desktop.
Save niektemme/93c1aca6db3c7202212a to your computer and use it in GitHub Desktop.
Smart Thermostat Arduino - part XBee receive
...
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
...
//variables for receiving xbee
int mesAvailable = 0;
char chour[3];
char cminute[3];
char cboiler[2];
char crtemp[5];
char cvchecksum[5];
int vhour = 0;
int vminute =0;
int vminuteold =0;
int rvboiler = 0;
int rvtemp = 0;
int rvchecksum=0;
//timer to check if raspberry pi is still sending messages to arduino
int remotemode = 0;
int remotemodeold = 0;
unsigned int rcycle = 3000;
unsigned int maxrcycle = 2000;
...
void loop() {
...
//reading xbee message
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
//convert xbee message to seperate integer values <hour><minute><boiler state><act temperature><checksum>
chour[0] = rx.getData()[0]; //hour
chour[1] = rx.getData()[1];
chour[2] = 0; //0 is used to end array
vhour = atoi(&chour[0]); //only first position of array is needed to confert string array to int
cminute[0] = rx.getData()[2]; //minute
cminute[1] = rx.getData()[3];
cminute[2] = 0; //0 is used to end array
vminute = atoi(&cminute[0]); //only first position of array is needed to confert string array to int
cboiler[0] = rx.getData()[4]; //boiler state 1 or 0
cboiler[1] = 0; //0 is used to end array
rvboiler = atoi(&cboiler[0]); //only first position of array is needed to confert string array to int
crtemp[0] = rx.getData()[5]; //actual temperature
crtemp[1] = rx.getData()[6];
crtemp[2] = rx.getData()[7];
crtemp[3] = rx.getData()[8];
crtemp[4] = 0; //0 is used to end array
rvtemp = atoi(&crtemp[0]); //only first position of array is needed to confert string array to int
cvchecksum[0] = rx.getData()[9]; //checksum
cvchecksum[1] = rx.getData()[10];
cvchecksum[2] = rx.getData()[11];
cvchecksum[3] = rx.getData()[12];
cvchecksum[4] = 0; //0 is used to end array
rvchecksum = atoi(&cvchecksum[0]); //only first position of array is needed to confert string array to int
mesAvailable = 1;
}
}
//check if correct message is avaliable
if (mesAvailable == 1 && (vhour+vminute+rvboiler+rvtemp) == rvchecksum ){
rcycle = 0;
mesAvailable = 0;
} else {
rcycle = rcycle+1;
mesAvailable = 0;
}
//when larger than 3000 reset tot 3000 to prevent int overflow
if (rcycle > 3000) {
rcycle = 3000;
}
//check if thermostat should be in local or remote mode
if (rcycle < maxrcycle) {
remotemode = 1;
} else {
remotemode = 0;
}
if (remotemode == 1) { //if remote mode is true
avgTemp = rvtemp; //set main actual temperature to the actual temperature send by the raspberry pi
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment