Arduino: Basic Network Temp and Humidity monitor

by on Aug.23, 2012, under Microcontrollers, Networking

I don’t know why I can’t flip this image. It’s cursed.

The (albeit crooked) image above is a basic environmental monitor I built for the server rack that I keep my house’s servers in. This project features network connectivity via an Arduino Ethernet shield, an HTF3223 humidity sensor, a TMP36 temperature sensor and a Sparkfun serial LCD for a decent monitoring station that is self-reliant.  Read more for build details and the code to get it all working.

Foreword:

I built this project to monitor the server rack temperature but being that it is in another room (and I’m a lazy bastard), I decided to work with the Ethernet shield to make it network accessible.  The cool thing about the Arduino Ethernet Shield is that as of Arduino 1.0, the Ethernet Shield now supports DHCP, making projects like this a breeze as far as getting on the network.  The added benefit of using an Ethernet Shield was that instead of just looking at it at a glance, I could now set up a script on one of the servers to “poll” the IP address (displayed conveniently on the LCD) and download the current temperature and humidity to a file for later processing.  This project allowed me to check the temperature and humidity from anywhere with Internet access using any web browser I wanted (of course with proper port-forwarding rules in place).

Granted, in the grand scheme of Arduino projects, this one is not very difficult nor is it very complex however this leaves a lot of room for improvement.  For instance, I could have a fan turn on if the temperature gets abnormally high and turn off again when the temperature dropped down to an acceptable level.  The humidity sensor was a last minute add, primarily because I was curious and had an extra sensor lying about.

Requirements:

For this project, you will need the following:

Hardware:

My project is wired as follows:

  1. Serial LCD – Red lead to +5V, Black lead to GND, Yellow lead to Digital Pin 1(RX).
  2. TMP36  - Held with the flat part towards you – Left lead to +5V, Center lead to Analog Pin A0, Right lead to GND.
  3. HF3223 – Held with the white sensor facing you and connector pointing to ground. – Pin 1 unconnected, Pin 2 to GND, Pin 3 to Digital Pin 7, Pin 4 to +5V.

Software:


/*
This project requires the following items:
Arduino Ethernet Shield
TMP36 sensor - Sense attached to Analog 0
When held like a "D", top pin is GND, middle pin is Sense, Bottom pin is +5V

SparkFun Serial LCD display attached to GND, +5V and TX (pin 2)
Humirel HF3223 4 pin Humidity sensor
When held with the sensor facing you and the connector block on the right:
Pin 1 (topmost) = +5V
Pin 2 to pin D7
Pin 3 Vss
Pin 4 (float/NC)
---------------------
When running, the LCD will show the temperature in Fahrenheit and the Ethershield's IP
Browsing to that IP will give a simple "The tempearture is XX degrees F"
Code from this sample was used from the Temperature sketch part of the Arduino
Experimentation Kit Example Code CIRC-10 "Temperature" and from the Barometric
Pressure Server under Samples->Ethernet->"BarometricPressureWebServer" example.

Code for the Humirel sensor came from:

http://g7nbp.blogspot.com/2011/09/arduino-htf3223-humidity-sensor.html

*
#include <SPI.h>
#include <Ethernet.h>

const int temperaturePin = 0; //the analog pin the TMP36′s Vout (sense) pin is connected to
const int hpin = 7; //HTF323 sense pin

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; // Ethernet shield MAC address
EthernetClient client;
EthernetServer server(80);
void setup()
{
delay(500);
pinMode(hpin, INPUT);
Serial.begin(9600); //Start Serial (needed for LCD)
Serial.write(0xFE); //ATTN LCD
Serial.write(0x01); //Clear LCD
Serial.write(0x7C); //ATTN
Serial.write(157); //Full Display Brightness
Serial.write(0xFE); //Attn
Serial.write(128); //Mv cursor to line 1, col 1 on LCD
delay(1000); // Do not f* with this line. It ensures that the LCD is ready to receive data.
Serial.print("Please Wait..."); // Wait message for DHCP request
if (Ethernet.begin(mac) == 0) {
Serial.write(0xFE); // Attn
Serial.write(128); // Mv to line 1, col 1
Serial.print("DHCP Address Failed - STOP"); // Print message and halt Arduino. We need an IP.
for(;;)
; //Endless loop.
}
server.begin(); // Start Web server.
Serial.write(0xFE); //Attn
Serial.write(0x01); //Clear Screen

}
void loop() // run over and over again
{
//Get the voltage reading from the temperature sensor
float temperature = getVoltage(temperaturePin);

//Convert it from 10 mv per degree wit 500 mV offset
//to degrees Fahrenheit(((volatge – 500mV) times 100) times 9/5 + 32 degrees)
temperature = ((temperature - .5) * 100) * 9/5 + 32 ;

//Get raw sensor frequency
long sensorValue = getFrequency(hpin); // get the raw sensor frequency
long humidity = convertToRH(sensorValue); // convert it to RH%

// Do some LCD stuff then print out our formatted lines.
Serial.write(0xFE); // LCDAttention
Serial.write(128); // Mv to Ln 1, col 1
Serial.print("H:");
Serial.print(humidity, DEC);
Serial.print("% T:");
Serial.print(temperature); //print the result to LCD and computer (if attached)
Serial.print("F");
Serial.write(0xFE); //Attention
Serial.write(192); //Mv to Ln 2 col 1
//This routine prints the IP address to the display.
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
// Monitors the Ethernet connection for a client request
listenForEthernetClients(temperature, humidity);
delay(1000); //waiting a second
}
/*
* getVoltage() – returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
void listenForEthernetClients(int temp, int hum) {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// print the current readings, in HTML format:
client.print("Temperature: ");
client.print(temp);
client.print(" degrees F");
client.print("<br>Humidity: ");
client.print(hum);
client.print(" percent");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop();
}
}

long getFrequency(int pin){
#define SAMPLES 4096
long freq = 0;
for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
return freq / SAMPLES;
}
long convertToRH(long sensorValue){
 long rh = (9740-sensorValue)/18;
 return rh;
}

Hooking it all up:

Once you flash your Arduino, you should be good to go.  Connect the Ethernet Shield to your network, then attach the USB cable from a nearby computer. It does not matter if it has the FTDI driver in it or not, we just need the USB port for its power. After a moment, you should see the Humidity (H:) and the Temperature (T:) on the LCD with the IP address of the device on the second line.

Browsing to the IP address should give you a simple page that shows Temperature and Humidity.

Troubleshooting:

If for some reason, the LCD stops updating and you get a connection time out trying to browse to the IP, take a look and make sure that Pin 7 is connected to the HF3223 In my testing, when I moved it around and the lead became disconnected, the Arduino would appear to freeze. Reconnecting it restored connectivity.

If you get the message “DHCP Address Failed – STOP”, then check your Ethernet shield and make sure that you have link. Also make sure that your router is handing out IP addresses.

If you want the temperature in degrees Celsius, remove the “* 9/5 + 32” from this line above in code:  temperature = ((temperature – .5) * 100) * 9/5 + 32 ;

 

Well, that’s a quick one that might be interesting for you. Happy Temperature Monitoring!

 

FIRESTORM_v1

 

:, , , , ,

2 Comments for this entry

  • Dom_TC

    I discovered your site a few months ago when browsing hacking sites and have been greatly enjoying reading through your old articles.

    I was wondering what servers you actually have in your home?
    I personally have a NAS and local dev server (both custom built)

    Also, in your very first article (“What’s on your workbench?”). You mentioned that you would post a tutorial on making an AT bench power supply. Will you ever do this, or has it been done and I just missed it.

    Thanks

  • firestorm_v1

    Hello Dom_TC:

    Thanks for the feedback! I run several servers at my house. My pride and joy right now is an older HP DL380 G5 server that is my Virtualization server running ESXi. I have a couple of additional machines that serve other purposes, namely the router running pfSense and a NAS device.

    You caught me on the ATX power supply mod. I was going to write up a howto for it, but Sparkfun beat me to the punch and designed this really nifty kit for it. Unfortunately there’s not much hacking involved as it’s pretty much a breakout board for any common ATX power supply (bonus is you can use a micro ATX PSU to save some space). You can buy this kit here: https://www.sparkfun.com/products/9774 If you have a MicroCenter near you, I highly recommend picking up the kit, the box can be repurposed as a holder for the board without much fuss. This board has pretty much replaced my existing power supply (and a storm surge didn’t help that fact) so I use it pretty much exclusively with a mini ITX power supply like one of these: http://www.newegg.com/Product/Product.aspx?Item=N82E16817148044.

    Thanks for stopping by and checking out the site!

    FIRESTORM_v1

Leave a Reply