Read climate sensor data via HTTP

Preface

Server rooms must be monitored for correct air-conditioning. The monitoring has so far been carried out by a climate sensor which wasconnected to a Linux server via RS232. In the meantime, servers have hardly any such interfaces, and in today's virtualization, many potential hardware interfaces are gone.

Now, with the Internet of things, sensors and measuring devices are directly connected to the net. So, what is more, than to bring aclimate sensor into the network. The base for this is an Arduino with POE-enabled Ethernet interface.

The Arduino Ethernet is a board based on the Atmel ATmega328. Instead of a USB interface a Wiznet Ethernet connection is available. With an add-on module the board can be powered over Ethernet cabling (PoE).

Electronics

The electronics is set up within a few minutes. The sensor needs an additional 10k pullup (an incorrect value is showed in the picture)on the data line. Incorrect power polarity destroys the sensor (I tested it for you).

The sensor should positioned a bit off the Arduino board. The PoE hardware produces considerable heat and would therefore influence themeasured value.

Pictures

cabling.png housing.png mit-arduino-klimadaten-ueber-web-abrufen.jpg sam_0420-custom.jpg sam_0426-custom.jpg

Software

/*
 Temp and Humidity Monitoring with Arduino PoE and DHT22
 created 31 July 2013
 by Me

#include <avr/wdt.h>
#include <Ethernet.h>
#include <SPI.h>
#include „DHT.h“

#define DHTPIN 2
#define DHTTYPE DHT22   // DHT 22  (AM2302)

// assign a MAC address for the ethernet controller.
// fill in your address here:
// Sensor A
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xB9, 0x23};
IPAddress ip(192,168,1,11);

// Sensor B
//byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xB9, 0x02};
//IPAddress ip(192,168,1,10);

IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
DHT dht(DHTPIN, DHTTYPE, 3);

float dht_h;
float dht_t;
unsigned long ul_PreviousMillis = 0UL;
unsigned long ul_Interval = 5000UL;
int ledPin = 9;
String HTTP_req;          // stores the HTTP request

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println(„** Initializing…“);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  wdt_enable(WDTO_8S);
}

void loop() {
  // check for a reading no more than once a second.
  unsigned long ul_CurrentMillis = millis();
  if( ul_CurrentMillis – ul_PreviousMillis > ul_Interval) {
    wdt_reset();
    ul_PreviousMillis = ul_CurrentMillis;
    dht_h = dht.readHumidity();
    dht_t = dht.readTemperature();

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(dht_t) || isnan(dht_h)) {
        Serial.println(„Failed to read from DHT“);
    } else {
        Serial.print(„H: „);
        Serial.print(dht_h);
        Serial.print(“ %\t“);
        Serial.print(„T: „);
        Serial.print(dht_t);
        Serial.println(“ *C“);
    }
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  // listen for incoming Ethernet connections:
  listenForEthernetClients();
}

void listenForEthernetClients() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println(„Request: „);
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        HTTP_req += c;
        Serial.print(c);
      }
      if (HTTP_req.indexOf(„\r\n\r\n“)> –1){
        Serial.println(„[EOF]“);
        if (HTTP_req.indexOf(„sensor=humidity“)> –1){
          client.println(dht_h);
          Serial.println(„Is Hum“);
        }
        else if (HTTP_req.indexOf(„sensor=temp“)> –1){
          client.println(dht_t);
          Serial.println(„Is Temp“);
        }
        else {
          client.println(„unk“);
          Serial.println(„Is unknown“);
        }
        HTTP_req = „“;
        // give the web browser time to receive the data
        delay(20);
        // close the connection:
        client.stop();
      }
    }
  }
}