Temp Monitor, send email alerts and set value from web shield

Hi everyone im new with Arduino and programming and i need help with this.
I need to monitor a datacenter Temperature send mails with internal SMTP, set max. temp alert and set min alerts. I need to sent those values from webserver. I´ve found a code and changed it but a I need help with the other part of programming.

Thaks for any help that you can give me.
Here is my code

// ESTE FUNCIONA TODO ULTIMO CODIGO USADO

/* YourDuino Ethernet Temperature/humidity Web Server
 Reads a DHT11 Sensor
 Outputs a web page with Temperature and Humidity
 terry@yourduino.com 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * DHT11 Sensor connected to Pin 2
 
 Based on code by David A. Mellis & Tom Igoe
 
 */
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <Wire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define DHT11PIN 2  // The Temperature/Humidity sensor

// 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(10,0,81,2);
IPAddress gateway(10,0,81, 254);
IPAddress subnet(255, 255, 255, 0);
//IPAddress ip(192,168,1,177);
//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);

dht11 DHT11;  //The Sensor Object
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/ 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}//--(end setup )---


void loop()   /*----( LOOP: RUNS OVER AND OVER AGAIN )----*/
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // 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("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<title>DataCenter NN</title>"); //Cambia nombre de la ventana del navegador
          client.println("<H1>");
          client.print("DATACENTER NN!");   
          client.println("</H1>");
          client.println("
");    

          /*----(Get sensor reading, calculate and print results )-----------------*/

          int chk = DHT11.read(DHT11PIN);

          Serial.print("Read sensor: ");
          switch (chk)
          {
          case 0: 
            Serial.println("OK"); 
            break;
          case -1: 
            Serial.println("Checksum error"); 
            break;
          case -2: 
            Serial.println("Time out error");
            client.println("Time out error"); 
            break;
          default: 
            Serial.println("Unknown error"); 
            break;
          }  

          client.println("<H2>");
          client.print("Temperature (C): ");
          client.println((float)DHT11.temperature-2, 1);  // OFFSET client.println((float)DHT11.temperature-N, 1) donde N es = a dif de temperatura
          client.println(" °");
          client.println("</H2>");
          //client.println("
");
          client.println("<H2>");
          client.print("Humidity (%): ");
          client.println((float)DHT11.humidity, 0);
          client.println(" °");
          client.println("</H2>"); 
          //client.println("
");   
          client.println("<H2>");
          client.print("Dew Point (C): ");
          client.println(dewPoint(DHT11.temperature, DHT11.humidity));
          client.println(" °");
          client.println("</H2>");
          //client.println("
");   
          client.println("<H2>");
          client.print("Dew PointFast (C): ");
          client.println(dewPointFast(DHT11.temperature, DHT11.humidity));
          client.println(" °");
          client.println("</H2>");
          //client.println("
");   

//DEFINE ALARMA DE TEMPERATURA
  int temp = DHT11.temperature-2;       // LEE SENSOR
  if (temp > 21){                       // ALERTA TEMP ALTA
    client.println("<font color=RED>TEMPERATURA ALTA</font>"); // ALERTA TEMP ALTA
    Serial.println("TEMPERATURA ALTA"); // ALERTA TEMP ALTA
  }  
  if (temp < 17){                       // ALERTA TEMP BAJA
    client.println("TEMPERATURA BAJA"); // ALERTA TEMP BAJA
    Serial.println("TEMPERATURA BAJA"); // ALERTA TEMP BAJA
  }
//DEFINE ALARMA DE TEMPERATURA

          /*--------( End Sensor Read )--------------------------------*/
          client.println("</html>");
          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);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
} // END Loop

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
  return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
  return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
  double A0= 373.15/(273.15 + celsius);
  double SUM = -7.90298 * (A0-1);
  SUM += 5.02808 * log10(A0);
  SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  SUM += log10(1013.246);
  double VP = pow(10, SUM-3) * humidity;
  double T = log(VP/0.61078);   // temp var
  return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  double Td = (b * temp) / (a - temp);
  return Td;
}
/* ( THE END ) */

What other part of programming?

Why would you read the temperature only when the client asks for it? You should be reading the temperature on every pass through loop(), or as often as required, so that you have data when the client asks for the temperature.

I think that it is rather silly to tell the client that there was a timeout error, and then tell the client the fictitious temperature.

Is the dew point in the server room REALLY important? Is it likely to EVER get cold enough for dew to form?

Is it really necessary to calculate dew point slowly and quickly?

ieee488:
What other part of programming?

Hi ieee488, first of all thank for the replay. and second sorry for my english.
I need the part where, when i get high or low temperature, its send an email. How it connect to smtp, port, sender and rcpt.
And the part to change high or low temperature alert from webserver.
thaks

PaulS:
Why would you read the temperature only when the client asks for it? You should be reading the temperature on every pass through loop(), or as often as required, so that you have data when the client asks for the temperature.

I think that it is rather silly to tell the client that there was a timeout error, and then tell the client the fictitious temperature.

Is the dew point in the server room REALLY important? Is it likely to EVER get cold enough for dew to form?

Is it really necessary to calculate dew point slowly and quickly?

HI PaulS, as i said to ieee488, thanks for the replay and sorry for my english.
Yes i know that is silly that part, I need to every time, no just when theres a client connect. But as I said at the beginning of my post im not a programmer, i don really know how program to read the sensor every x time.

The timeout error is an example to send an alert if there is a problem with the sensor. Other purpose of this mini project is to monitor enviroment in a really far far shelter.
We already have monitoring system like this: http://www.geistglobal.com/productsmonitorclimate-monitors/watchdog-15 but here in Argentina i cant find it and there is a difference on the price, 300usd againt 47usd.

The dewpoint, in some case is necesary.

thanks

i don really know how program to read the sensor every x time.

Look at the blink without delay example. Instead of toggling the state of a pin, you will be reading the value from a pin.

The timeout error is an example to send an alert if there is a problem with the sensor.

If you send that message, though, you do not want to send a fictitious temperature value.

The dewpoint, in some case is necesary.

Maybe. But, not two different values, computed two different ways.

I need to sent those values from webserver.

Web servers typically only send data or act upon received data when requested by a client. That being said, sounds like you want to run both server and client code on the arduino, which is possible. Is that what you want? Client sends email alerts, and server receives thermostat/alarm setpoint change request from another client?

PaulS:
Look at the blink without delay example. Instead of toggling the state of a pin, you will be reading the value from a pin.
If you send that message, though, you do not want to send a fictitious temperature value.
Maybe. But, not two different values, computed two different ways.

PaulS:
Look at the blink without delay example. Instead of toggling the state of a pin, you will be reading the value from a pin.
If you send that message, though, you do not want to send a fictitious temperature value.
Maybe. But, not two different values, computed two different ways.

Thanks again Paul.
You are right, i dont need to send two diffrent values and if there is an error is not necessary to send a fictitius temp.

About the delay example, i dont know to write it, other thing that i need y to optimize the code,
Please feel free to change it if you want.

thanks again!

zoomkat:
Web servers typically only send data or act upon received data when requested by a client. That being said, sounds like you want to run both server and client code on the arduino, which is possible. Is that what you want? Client sends email alerts, and server receives thermostat/alarm setpoint change request from another client?

Thanks zoomkat for teake time to replay.
I didnt know thats webserver only send data. i need to be able to change MAX/MIN temperature from anywhere using a webpage. And send email alert if temperature get any MAX/MIN values.

Thanks

Combined client/server test code.

//zoomkat 01-05-16, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send a g to test client GET and
//see what the arduino client/server receives
//web page buttons make pins high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "checkip.dyndns.com"; // (DNS) dyndns web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address

String readString; //used by server to capture GET request 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control

  //pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println(F("server/client 1.0 test 9/02/14")); // keep track of what is loaded
  Serial.println(F("Send a g in serial monitor to test client")); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) 
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call client sendGET function
    }
  }  

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.print(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println();  
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();

            client.println(F("<HTML>"));
            client.println(F("<HEAD>"));
            client.println(F("<TITLE>Arduino GET test page</TITLE>"));
            client.println(F("</HEAD>"));
            client.println(F("<BODY>"));

            client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));

            // DIY buttons
            client.println(F("Pin5"));
            client.println(F("<a href=/?on2 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off3 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin6"));
            client.println(F("<a href=/?on4 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off5 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin7"));
            client.println(F("<a href=/?on6 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off7 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin8"));
            client.println(F("<a href=/?on8 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off9 target=inlineframe>OFF</a>

")); 

            client.println(F("Pins"));
            client.println(F("&nbsp;<a href=/?off2468 target=inlineframe>ALL ON</a>")); 
            client.println(F("&nbsp;<a href=/?off3579 target=inlineframe>ALL OFF</a>")); 

            client.println(F("<IFRAME name=inlineframe style='display:none'>"));          
            client.println(F("</IFRAME>"));

            client.println(F("</BODY>"));
            client.println(F("</HTML>"));
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println(F("Led 5 On"));
            Serial.println();
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println(F("Led 5 Off"));
            Serial.println();
          }
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println(F("Led 6 On"));
            Serial.println();
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println(F("Led 6 Off"));
            Serial.println();
          }
          if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println(F("Led 7 On"));
            Serial.println();
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println(F("Led 7 Off"));
            Serial.println();
          }     
          if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println(F("Led 8 On"));
            Serial.println();
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println(F("Led 8 Off"));
            Serial.println();
          }         

          //clearing string for next read
          readString="";

        }
      }
    }
  }
} 

//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
  if (client.connect(serverName, 80)) {
    Serial.println(F("connected"));
    client.println("GET / HTTP/1.1");
    client.println("Host: checkip.dyndns.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println(F("connection failed"));
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println(F("client disconnected."));
  Serial.println(F("Data from server captured in readString:"));
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();
  Serial.println(F("End of readString"));
  Serial.println(F("=================="));
  Serial.println();
  readString=""; //clear readString variable

}

zoomkat:
Combined client/server test code.

Thanks Zoomkat it works.

ANy help? what about send mail from smtp when i get an alarm?

//DEFINE ALARMA DE TEMPERATURA
  int temp = DHT11.temperature-2;       // LEE SENSOR
  if (temp > 24){                       // ALERTA TEMP ALTA
    client.println("<font color=RED>TEMPERATURA ALTA</font>"); // ALERTA TEMP ALTA
    Serial.println("TEMPERATURA ALTA"); // ALERTA TEMP ALTA
  }  
  if (temp < 17){                       // ALERTA TEMP BAJA
    client.println("TEMPERATURA BAJA"); // ALERTA TEMP BAJA
    Serial.println("TEMPERATURA BAJA"); // ALERTA TEMP BAJA
  }
//DEFINE ALARMA DE TEMPERATURA

ANy help? what about send mail from smtp when i get an alarm?

There have been numerous post of email code in the past, so you might try the forum search in the upper right of this page.