Let's get started by installing the proper firmware on our three XBee modules. We want to setup the XBee firmware the same way as in the XBee Basics Lesson 4 video except this time setup two routers instead of one. Be sure to use the same PAN ID for each. Once you have the proper firmware on the XBees let's put the hardware together as shown in the below figure.
Hardware setup for capturing temperature data from multiple XBees |
The two routers each with a temperature sensor |
XBee coordinator, temperature sensor, and Arduino Uno |
The two main differences between this setup and the setup we saw in XBee Basics Lesson 4 is we have two routers sending the controller temperature data and we also have a temperature sensor tied to the controller. That means in the Arduino code we now need to read the address from the received frame of data from each router to determine which router sent it. We also need to read the temperature from sensor 3 and display it to the user. Below is the code for the Arduino with comments to explain what is happening in each line of code. When you go through the code you will want to have the the format or layout of an XBee API RX data frame on hand so you can understand what is happening in the code when it is handling incoming data from the routers. You can get this information from the XBee manual or from the XBee S2 Quick Reference Guide that was in lesson 3 and 4 of the XBee Basics video series. You can get the reference guide from the tunnelsup blog, if you do use it I encourage you to donate $1 to blog. Click here to go access the reference guide.
/*This program was written for the Arduino Uno. The Uno has an XBee Series 2 RF Module connected to it as a coordinator. The Uno uses the XBee coordinator to communicate with two router or end point XBees with temperature sensors. This program recieves the temperature readings from the two endpoint XBees and writes the data to the serial monitor */
/*Each Xbee has a unque 64 bit address. The first 32 bits are common to all XBee. The following four ints (each int holds an address byte) hold the unique 32 bits of the second half of the XBee address*/
int addr1;
int addr2;
int addr3;
int addr4;
int sen3Counter = 0; //This counter variable is used print sensor 3 every 5 seconds
void setup() {
Serial.begin(9600); //start the serial communication
}
void loop() {
if (Serial.available() >= 21) { // Wait for coordinator to recieve full XBee frame
if (Serial.read() == 0x7E) { // Look for 7E because it is the start byte
for (int i = 1; i<19; i++) { // Skip through the frame to get to the unique 32 bit address
//get each byte of the XBee address
if(i == 8) { addr1 = Serial.read(); }
else if (i==9) { addr2 = Serial.read(); }
else if (i==10) { addr3 = Serial.read(); }
else if (i==11) { addr4 = Serial.read(); }
else { byte discardByte = Serial.read(); } //else throwout byte we don't need it
}
int analogMSB = Serial.read(); // Read the first analog byte data
int analogLSB = Serial.read(); // Read the second byte
float volt = calculateXBeeVolt(analogMSB, analogLSB);//Convert analog values to voltage values
Serial.println(indentifySensor(addr1,addr2,addr3,addr4)); //get identity of XBee and print it
Serial.print("Temperature in F: ");
Serial.println(calculateTempF(volt)); //calculate temperature value from voltage value
}
}
delay(10); //delay to allow operations to complete
//This if else statement is used to print sensor 3 value every 5 second to match the XBee routers
//It uses the delay() function above to calculate 5 seconds
if (sen3Counter < 500) { sen3Counter++; }
else {
Serial.println("Temperature from sensor 3:");//This is sensor 3
Serial.print("Temperature in F: ");
//the following line calculates voltage, then temperature, and then prints temp to serial monitor
//the following line calculates voltage, then temperature, and then prints temp to serial monitor
Serial.println(calculateTempF(calculateArduinoVolt(analogRead(A0))));
sen3Counter = 0; //reset counter back to zero to start another 5 seconds
}
}
//Function takes in the XBee address and returns the identity of the Xbee that sent the temperature data
String indentifySensor(int a1, int a2, int a3, int a4) {
int rout1[] = {64, 176, 163, 166}; //Arrays are the 32 bit address of the two XBees routers
int rout2[] = {64, 177, 63, 221};
if(a1==rout1[0] && a2==rout1[1] && a3==rout1[2] && a4==rout1[3]) { //Check if Sensor 1
return "Temperature from sensor 1:"; } //temp data is from XBee one
else if(a1==rout2[0] && a2==rout2[1] && a3==rout2[2] && a4==rout2[3]) {//Check if Sensor 2
return "Temperature from sensor 2:"; } //temp data is from XBee two
else { return "I don't know this sensor"; } //Data is from an unknown XBee
}
//this function calculates temp in F from temp sensor
float calculateTempF(float v1) {
float temp = 0;
//calculate temp in C, .75 volts is 25 C. 10mV per degree
if (v1 < .75) { temp = 25 - ((.75-v1)/.01); } //if below 25 C
else if (v1 == .75) {temp = 25; }
else { temp = 25 + ((v1 -.75)/.01); } //if above 25
//convert to F
temp =((temp*9)/5) + 32;
return temp;
}
//This function takes an XBee analog pin reading and converts it to a voltage value
float calculateXBeeVolt(int analogMSB, int analogLSB) {
int analogReading = analogLSB + (analogMSB * 256); //Turn the two bytes into an integer value
float volt = ((float)analogReading / 1023)*1.23; //Convert the analog value to a voltage value
return volt;
}
//This function takes an Arduino analog pin reading and converts it to a voltage value
float calculateArduinoVolt(int val) {
float volt = (float)val * (5.0 / 1023.0); //convert ADC value to voltage
return volt;
}
The next step is to take the code and upload it to the Arduino. Do not forget to disconnect the wire connecting the Arduino digital pin 0 to the XBee coordinator when you upload the code to the Arduino or else you will get an error. Once the code it uploaded, reconnect the wire to digital pin 0 and open the serial monitor. If you have everything setup correctly your serial monitor should look something like the one in the below figure.
Serial monitor displaying data from each temperature sensor |
Well that is it for part 2. Normally I would share the list of parts you need for the next part of the project, but in part 3 and part 4 we will look at multiple options for powering the sensors in the project so read it first and then decide what works best for powering your temperature sensor network. If you have any questions on part 2 use the comment area below or feel free to email your question to me at forcetronics@gmail.com. Stay tuned for part 3!
Your blog provides informative information to make a wireless temperature sensor network. Nowadays we use the number of smart devices which has a built-in wireless temperature sensor. Thank you for sharing this information.
ReplyDelete