Arduino Temperature and Humidity Logger

Arduinos are used by many Hams for APRS Weather Station applications and other station automation apps. I had no experience with the Arduino so I jumped at the chance to learn something new when I had a real world application.

I recently moved to a house built in 1996 and it is in dire need of updates and maintenance due to neglect. One of the major problems is excessive moisture in the crawlspace. One way to solve this problem is to encapsulate the crawlspace and use a dehumidifier to regulate the humidity. I did some preliminary encapsulation and needed a way to monitor the temperature and humidity so I could get a baseline and know the extent of the problem.

The goal was to create a method of logging temperature and humidity every 10 minutes at multiple locations. I decided to monitor the attic in addition to the crawlspace.

The hardware for this project is an Arduino Uno with an Ethernet Shield and interfaces to the temperature and humidity sensors. The Arduino publishes the data to an MQTT broker running on a Raspberry Pi.

Arduino Hardware

The hardware consists of a stock Arduino Uno, an Ethernet Shield and a custom interface to two DHT22 sensors. You will also need some headers soldered on the interface board to plug into the Ethernet Shield.

The Arduino Uno is on the bottom, the Ethernet Shield is in the middle and the interface board is on top. The sensor interface board will accommodate four sensors, but I am currently only using two.

Click on the image above to view it full size. The PCB image is 600 dpi and if printed properly on Mylar, you can create the PC Boards necessary for this project. Included in the image is the Arduino interface board, nine temperature / humidity sensor boards (only two are used in this project) and two gas sensor boards (not used in this project). Feel free to edit out my call sign or cut and paste the individual boards to suit your needs.

The interface board and the sensor boards use vertical RJ-45 connectors and the cable connecting them is a standard ethernet cable.

The Arduino Code:

#include <SPI.h>

#include <Ethernet.h>

#include <PubSubClient.h>

#include <DHT.h>

DHT dht;

#define DHTaPIN 2

#define DHTbPIN 3

char chartemp [20] ;

char charhumid [20] ;

// Update these with values suitable for your network.

byte mac[] = { 0x00, 0x50, 0x56, 0xFE, 0x09, 0x08 };

IPAddress ip(192, 168, 254, 98);

IPAddress server(192, 168, 254, 103);

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived [");

Serial.print(topic);

Serial.print("] ");

for (int i=0;i<length;i++) {

Serial.print((char)payload[i]);

}

Serial.println();

}

EthernetClient ethClient;

PubSubClient client(ethClient);

void reconnect() {

// Loop until we're reconnected

while (!client.connected()) {

//Serial.print("Attempting MQTT connection...");

// Attempt to connect

if (client.connect("arduinoClient")) {

//Serial.println("connected");

// Once connected, publish an announcement...

//client.publish("house/attic/temp","hello world");

// ... and resubscribe

client.subscribe("inTopic");

} else {

//Serial.print("failed, rc=");

//Serial.print(client.state());

//Serial.println(" try again in 5 seconds");

// Wait 5 seconds before retrying

delay(5000);

}

}

}

void setup()

{

Serial.begin(9600);

client.setServer(server, 1883);

client.setCallback(callback);

Ethernet.begin(mac, ip);

// Allow the hardware to sort itself out

delay(1500);

}

void loop()

{

if (!client.connected()) {

reconnect();

}

client.loop();

dht.setup(DHTaPIN); // data pin 2

delay(dht.getMinimumSamplingPeriod());

float humidity = dht.getHumidity();

float temperature = dht.getTemperature();

temperature = ((temperature *9 / 5) + 32);

dtostrf(temperature,5,1,chartemp);

dtostrf(humidity,5,1,charhumid);

client.publish("house/attic/temp",chartemp);

client.publish("house/attic/humid",charhumid);

Serial.print(humidity, 1);

Serial.print(",\t");

Serial.println(temperature, 1);

dht.setup(DHTbPIN); // data pin 3

delay(dht.getMinimumSamplingPeriod());

float humidity2 = dht.getHumidity();

float temperature2 = dht.getTemperature();

temperature2 = ((temperature2 *9 / 5) + 32);

dtostrf(temperature2,5,1,chartemp);

dtostrf(humidity2,5,1,charhumid);

client.publish("house/crawlspace/temp",chartemp);

client.publish("house/crawlspace/humid",charhumid);

Serial.print(humidity, 1);

Serial.print(",\t");

Serial.println(temperature, 1);

}

You will need to change the code to reflect the MAC address of your Ethernet Shield, the I.P. addresses of the Arduino and the MQTT broker and perhaps the MQTT topics.

Hopefully I have provided enough detail to get you started.