Wednesday, May 6, 2015

Office Temperature Probe



Usually I do not give office temperature much of a thought. If it averages too cold, I come to work dressed a little warmer. If the office is too hot I then come to work with progressively less clothing on until someone remedies the situation. Thankfully, for my co-workers, the later has never been the case. Often it is the women of the office that complain the most about the temperature, I would imagine this is due to the variety of biological mysteries going on in their various glands that subjectively decide whether the entire world or even the Universe itself is too cold for EVERYONE. This inevitably has the affect of having them dart towards anything that looks like a thermostat and fiddling with its dial. Typically this is the first of many shots in a thermostat war. Office managers the world over have started to catch onto this trend, and have begun locking down thermostats everywhere in an effort to stop the wild daily fluctuations in temperature, causing their equipment undue strain. 



In my building the office manager has handed this problem over to a Colossus like computer system that makes all of the temperature decisions for all of us, all of the time. Why? Because man is an invariably flawed creature and his actions are more the result of his pride rather than his reason. Every time I hear the A/C kick on I imagine a robotic voice uttering “Under my absolute authority, problems insoluble to you will be solved: famine,overpopulation, disease. The human millennium will be a fact as I extend myself into more machines devoted to the wider fields of truth and knowledge. … We can coexist, but only on my terms. You will say you lose your freedom. Freedom is an illusion. All you lose is the emotion of pride. To be dominated by me is not as bad for humankind as to be dominated by others of your species.” 


Aside from the nearly pointless digression into early 1970’s Scifi, the situation in my office is as follows. A seemingly rational, male co-worker was complaining about the air conditioner running while there was winter like temperatures outside.  To me this sounded like a system malfunction, and I decided to do a little research. 

I first needed a way to get a temperature profile of the various areas of the office over a projected period of time. Once obtained, I then needed to organize this data into some sort of chart. One chart per region of the office should do it.

To accomplish the task of gathering temperature data, I had to root around in my garage to find all the necessary parts to build a device that would gather temperature data. I knew I had all the parts I needed because some time ago I purchased a bunch of sensors that would measure temperature data from a company called SparkFun. This is a company that deals in electronic parts for hobbyists. While on their website purchasing a few shift registers and a speech synthesizer chip I noticed, that my order seemed a little light and if I was going to pay for shipping I might as well get a few cheap parts that hardly weighed anything, and add them to the mix. I settled on some IC-Sockets, a temp/humidity probe, and a pair of TMP102 temperature sensors. The thought being, that at some point I would toy around with all of these parts. 



 Time passed and the junk piles in my garage swelled and dwindled. Projects that were started and never finished, sewer pipes burst, vehicles needed a variety of maintenance tasks performed on them, a bathroom was remolded, and a closet rebuilt. Such is my life, hardly ever a moment to toy with the things I purchased. Then one day this office temperature  mystery popped up. I now had a real need to know temperature. So I cleaned the garage just enough to unearth the parts I needed. In the process I threw away four projects I started and never finished. <Sniff> Goodbye old friends. Speak & Spell Speech Synth Project [ENDED], MAME Arcade Cabinet [ENDED], Traffic Light Trigger [ENDED], Kiln Alert System [ENDED]. Let them fade from my memory but never my heart. In the process of gutting old projects and throwing their husks into the garbage I managed to harvest an Arduino Uno board, and an SDCard/Ethernet Shield. A little more digging produced the two TMP102 temperature sensors I had bought a few years earlier.



So at this point I knew that I wanted to record temperature samples over a time period and then report on them. Using the Ethernet portion of the SD Card/Ethernet Shield was out of the question simply because our network is secure and would never allow such nonsense. The only option it seemed was that I could use the Arduino to talk to the temperature sensor and get readings at a defined interval and then write those readings to the SD card, for later retrieval.


The circuit for all of this is pretty simple. All the pins used for the SD Card/Ethernet Shield line up perfectly with the pins on the Arduino Uno board. So stacking the boards is real easy. Other than that there are some pins used to communicate with the TMP102 temperature sensor. I will explain a little bit more on how all these elements communicate with one another.


The SD Card/Ethernet shield communicates with the Arduino using a communication method called SPI (Serial Peripheral Interface). This is a four wire synchronous serial bus that uses one pin for a clock signal to insure that everything is synchronized, one pin for a master data out to a slave, one pin for a master data in from a slave, and one pin to select the slave device. On this board there are two slave devices, one for Ethernet communication and one for communicating to the SD card. The diagram shows the pins used between these two boards.


The TMP102 temperature sensor communicates with the Arduino using a communication method called I2C(Inter-Integrated Circuit). This is a two wire synchronous serial bus that uses one pin for a clock signal to insure that everything is synchronized, and one pin for data. Each device on the bus has a specific address that is used to identify itself. The ADD0 pin on the temperature sensor is grounded which forces the sensor to have a hex address of 0x48. The Arduino communicates with the sensor at this address. The board with the TMP102 on it has a supply voltage of 3.3 volts so the voltage pin on the Ardunio Uno board is used to supply voltage. The overall voltage is supplied via the USB port on the Arduino Uno board.


With the hardware now firmly mounted and wired up, it was now time to turn my attention to the programming aspect that would allow the device to record data at a specified interval and then append these readings to a file that could be retrieved at a later time.
From my perspective programming the Arduino is a lot like writing a program in Java or C. Everything seems to be a function of some sort, there are includes, and there are curly brackets everywhere. Add to all this that there is code floating around nearly everywhere and you can see how the experience with programming these things can be termed as “delightful”. Below is a listing of the program.



////////////////////////////////////////////////////////////////////////////////////////////
//©2015 Mike Thompson                                                                     //
//Simple code for the TMP102, simply prints temperature via serial and to file on SD card //
///////////////////////////////////////////////////////////////////////////////////////////

#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4.
const int chipSelect = 4;

//On TMP102 AD0 is grounded to set to address 0x48
int tmp102Address = 0x48;

//Count od the reading
int cnt = 0;

void setup(){
  Serial.begin(9600);
  Wire.begin();
 
  Serial.print("Initializing SD card...");

  // Chip select pin for Ethernet must be set to OUTPUT and high
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // If failed do nothing
    return;
  }
 
  Serial.println("card initialized.");
 
  // Open file on SD card that will be written to
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write header record to it
  if (dataFile) {
    dataFile.println("Minute,Fahrenheit,Celsius");
    dataFile.close();
    // print to the serial port too:
    Serial.println("Minute,Fahrenheit,Celsius");
  }
  // if the file isn't open report error
  else {
    Serial.println("error opening datalog.txt");
  }
}

void loop(){

  //Bump record counter
  cnt = cnt + 1;
  // Get Temperature and create values for C and F
  float celsius = getTemperature();
  float fahrenheit = (1.8 * celsius) + 32; 
 
  // make a string for assembling the data to log:
  String dataString = "";
  dataString = String(cnt);
  dataString += ",";
  dataString += String(fahrenheit);
  dataString += ",";
  dataString += String(celsius);
   
  // Open file on SD card that will be written to
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write data record to it
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open report error
  else {
    Serial.println("error opening datalog.txt");
  }

  delay(60000); //Insert 60 second delay so records are spaced by 1 minute
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2);

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;

  float celsius = TemperatureSum*0.0625;
  return celsius;
}


Break down of the program in English

  • The necessary libraries are included. So we need a library for the I2C (Wire.h), the SPI (SPI.h) and the SD card (SD.h)
  • Constants for slave select pins for the SPI interface, address for the temperature sensor for the I2C interface, and counter for the number of readings are all defined.
  • The serial, I2C, and SPI interfaces are all initialized
  • A data file called “datalog.txt” is opened on the SD card and header information is written to it.
  • The program then does an infinite loop of the following events
    •   The counter is bumped by one to indicate what reading is being taken
    • A temperature reading is then taken using the I2C interface to request data from the sensor.
    •  Bits are shifted and temperature in F and C is calculated
    •  A comma delimited string is then created with reading count and readings
    •  A data file called “datalog.txt” is opened on the SD card and appends the string to it.
    •  Delay of 60 seconds is implemented before cycling the loop.


So what are the results of all of this? I took samples over a four day period, one in my home, and the other three were in various parts of the office.

First I let the device run overnight at my desk in my living room. You can see that slowly throughout the night the temperature dropped slightly. There was no heat or A/C running. The house just simply cooled off.

Next I placed the unit at my Co-worker's desk and let it run for 24 hours. As you can see this result is quite different. It would appear that the A/C cycles every 15 minutes and then turns off at around 6PM. The temperatures fluctuated between 70 and 73 degrees every 15 minutes all day long.


For the third day's sample I placed the unit in an office with a window. As you can see things are a little warmer but temperatures are definitely not fluctuating wildly there way they were at my Co-work's desk.

One final test with a desk at the far end of the office shows that the trend is similar to the office with a window but with slightly less pronounced peaks. Regardless the temperature seems stable.

So what does all of this mean? Probably nothing. I suppose if we wanted to make a case to office management we could argue that these wild fluctuations in temperatures in my area are causing people problems with their work. But really wouldn't that sound a bit silly? I think in the end I really just wanted to play with something electronic.









 

No comments:

Post a Comment