EtherTen + DHT22 + DS1307

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

EtherTen + DHT22 + DS1307

Post by dudleydo » Wed Nov 21, 2012 6:54 am

Some code I cobbled together to check out a few bits to see if they would fit in the space available.

One thing I did notice with this board compared to the USBDroid and Eleven, is that after a new upload, I had to pull the power (well USB connector) for the new program to work.

Aside from that, all good :)
#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
  int h = dht.readHumidity();
  int t = dht.readTemperature();

  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

  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 {

  Serial.print("Temperature : ");
  Serial.print(t);
  Serial.print("C ");
  Serial.print("Humidity : ");
  Serial.print(h);
  Serial.println("%");
  outstring1 = "Temp : ";
  outstring2 = outstring1 + t;
  outstring3 = outstring2 + "C ";
  outstring4 = "Hum : ";
  outstring5 = outstring4 + h;
  outstring6 = outstring5 + "%";
  outstring7 = outstring3 + outstring6;

  Serial.print(outstring7);

  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: 19,494 bytes (of a 32,256 byte maximum)

Post Reply