Simple DHT & RTC

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]
Post Reply
dudleydo
Posts: 13
Joined: Fri Dec 30, 2011 4:57 am

Simple DHT & RTC

Post by dudleydo » Wed Nov 21, 2012 12:28 pm

DHT22 and EtherTen + RTC example, with floating point :)
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <RealTimeClockDS1307.h>
#include "DHT.h"

#define DHTPIN A2    // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22 (AM2302)

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(249);
DHT dht(DHTPIN, DHTTYPE);
String outstring0, outstring1, outstring2, outstring3, outstring4, outstring5, outstring6, outstring7;

int LEDpin = 6;
String readString = String(30);
String state = String(3);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  dht.begin();

  //Sets the LEDpin as an output
  pinMode(LEDpin,OUTPUT);
  
  digitalWrite(LEDpin,LOW);
  state = "OFF";
}

void loop()
{
  // 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 (readString.length() < 30) {
          readString.concat(c);
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          int LED = readString.indexOf("LED=");

          if (readString.substring(LED,LED+5) == "LED=T") {
            digitalWrite(LEDpin,HIGH);
            state = "ON";
          }
          else if (readString.substring(LED,LED+5) == "LED=F") {
            digitalWrite(LEDpin,LOW);
            state = "OFF";
          } 
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
  RTC.readClock();
int hr = RTC.getHours();
int mi = RTC.getMinutes();
int se = RTC.getSeconds();
int date = RTC.getDate();
int month = RTC.getMonth();
int year = RTC.getYear();

client.print(date);
client.print("/");
client.print(month);
client.print("/");
client.print(year);
client.print(" ");
client.print(hr);
client.print(":");
client.print(mi);
client.print(":");
client.print(se);
client.print("<br><br>");

//dht readings
  outstring1 = "Temp : ";
  float t = dht.readTemperature();
  char tbuf[6];
  dtostrf(t,5,2,tbuf);
  outstring2 = outstring1 + tbuf; 
  outstring3 = outstring2 + "C ";
  outstring4 = "Hum. : ";
  float h = dht.readHumidity();
  char hbuf[6];
  dtostrf(h,5,2,hbuf);
  outstring5 = outstring4 + hbuf;
  outstring6 = outstring5 + "%";
  outstring7 = outstring3 + outstring6;
char buf[46];
outstring7.toCharArray(buf, 46);

  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  outstring0 = "Failed to Read from DHT";
    Serial.println(outstring0);
    client.println(outstring0);
  
else {


  client.println(outstring7);
  client.print("<br><br>");
  client.println();
    
  Serial.println(outstring7.length());

  }

          client.print("LED is ");
          client.print(state);
          client.print("<br><br>");
          
          if (state == "ON") {
            client.println("<a href=\"./?LED=F\">Turn Off<a>");
          }
          else {
            client.println("<a href=\"./?LED=T\">Turn On<a>");
          }
          
          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;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    readString = "";
    // close the connection:
    client.stop();
  }
}
Binary sketch size: 20,670 bytes (of a 32,256 byte maximum)

supercell Kid
Posts: 5
Joined: Thu Nov 22, 2012 5:19 am

Re: Simple DHT & RTC

Post by supercell Kid » Thu Nov 22, 2012 5:48 am

cool I'd like to try this, but how to I go about connecting the freetronics DHT22 & RTC when the freetronics LCD is taking all the pins, not using them, but yeah, taking them. Also Can I just add this code to SD Logging Code?

Post Reply