Etiquetas

lunes, 31 de agosto de 2015

Ethernet and LM35 temperature sensor

Ethernet and LM35 temperature sensor


In this exercise we will create a program that measures environmental Arduino using the LM35 temperature sensor. Besides we use our Arduino Uno to create a server on the network and provide the measured value with the sensor. It has also included a warning in case the temperature rises above 24 degrees Celsius.

/*
Autor: Ángel Barquín                         Fecha: 05-02-2015
Programa: ethernet mas temperatura           Versión: 1.0
Dispositivo: ATMEL 328                       Compilador: AVR
Entorno ID: 1.5.5-r2                         Simulador: VSM
Tarjeta de aplicación: Arduino
*/

///////////////////////////////////////////////////////////////////
// The program measures medianto the environmental temperature sensor LM35.
// Here we use our Arduino Uno to create a server on the network,
// And provide the measured value with the sensor. Also included is notice
// If the temperature rises above 24 degrees Celsius.
// Arduino 1 has -Aref = Aref = GND and + 5V.
// So we can not measure negative temperatures.
///////////////////////////////////////////////////////////////////


// Libraries included

#include <SPI.h>
#include <Ethernet.h>
// We define variables temperature
int temp;
int valorLeido;

// Initializes the HTTP server on port 80 and the Mac and manually enter the IP
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(172, 100, 4, 177);
EthernetServer server(80);  //creating an object of class ethernetServer.


void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
  {
     // wait for serial port to connect. Needed for Leonardo only
  }
  Ethernet.begin(mac, ip);//ethernet connection initialization
  server.begin();
  Serial.print("the server's ip: ");
  Serial.println(Ethernet.localIP());
  analogReference(INTERNAL);
}


void loop() 
{
   valorLeido=analogRead(4);//Read the value of the analog pin specified. This means to be assigned
    // Input voltages between 0 and 5 volts in integer values between 0 and 1023
    // 100 microseconds (0.0001 s) it takes to read an analog input,
    // so the maximum reading rate is about 10,000 times per second.
   temp= (valorLeido*1.1*100)/1023;
  // listen until a connected client.
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new customer");
    Serial.println();
    // http request must end with a blank line.
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();//We read the petition.
        Serial.write(c);//the request is shown by the serial.
        

        
        if (c == '\n' && currentLineIsBlank)//we see that what we have received is a request.
        {
          // send a standard http header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  //the connection is closed after completion of the answer
          client.println("Refresh: 1");  // the page is refreshed every 1 second.
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                  
          client.print("<h2>");
          client.print("<h2 align=center>The measured temperature is displayed with an LM35 sensor<h2>");
          client.print("<h2>");
          client.print("<br />");
          client.println("<center>");
          client.print("<h6>");
          client.print("Ambient temperature:  ");
          client.print("<br />");
          client.println(temp);
          
          client.println("degrees Celsius");
          client.print("<h6>");
          client.println("<center>");
          if(temp>26 && temp<28)// if the temperature is between 26 and 28 degrees can see a                                                  //picture we warn
          {
           client.println("<body bgcolor=yellow>");
           client.print("<img src=https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcScZQcjwfFrrVwDI1V0jcRscC7fZTEqXpNYE93FLXutlfKN5CsE>");
          }
          if(temp<=26 && temp>0)
          {
            client.println("<body bgcolor=blue>");
           client.print("<img src=http://aventalearning.com/content168staging/foreignlanguage/spanish1a/images/niceweather.GIF>");
          }
          if(temp>=28)// if the temperature is under 26 degrees 
                              // show one image that reflects cold
          {
            client.println("<body bgcolor=red>");
            client.println("<center>");
            client.print("<br />");
            client.print("<h2>");
            client.print("The temperature is too high");
            client.print("<h2>");
            client.println("<center>");
            client.print("<img src=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7pit7HUeYuIdlJMfHAzODDzivw3wV13jNstiNsFkI3xLVnn9gaURkks655PUrOcVvrwYC0TeuVlfWIcRqVs3ggdKL3MXkPywMJBn8AGVyEa4Ow_nxq36n9bx0obdipKW4yNimY1G7nVE/s1600/hot.gif>");
          }
          
          client.println("<br />");
          client.println("</html>");
          break;//once we sent the data back to check clients making requests
        }
        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);
    
    client.stop();// close connection
    Serial.println("This is the request received");
    Serial.println("------------------------");
    Serial.println("Customer offline");
    Serial.println("-------------------------------");
  }

}

No hay comentarios:

Publicar un comentario