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("-------------------------------");
  }

}

SPI

Serial Peripheral Interface



The example I show you then use the communication protocol Serial Peripheral Interface (SPI) using the Proteus program for controlling a digital potentiometer






We will choose the microcontroller of Atmel ATMEGA 328P





In this picture we see how we connect the potentiometers











/*

  Digital Pot Control
  
  This example controls an Analog Devices AD5206 digital potentiometer.
  The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
  A - connect this to voltage A - connect this to the stress
  W - this is the pot's wiper, which changes when you set it     W - it is wiper of potentiometer, which changes when configure

 The AD5206 is SPI-compatible,and to command it, you send two bytes, 
 one with the channel number (0 - 5) and one with the resistance value for the
 channel (0 - 255).  

The AD5206 is compatible with SPI, and to command them, you send two bytes,
   one channel number (0 - 5) and one with the resistance value for the
   channel (0-255).

 The circuit:
  * All A pins  of AD5206 connected to +5V      * Todos Unos pasadores de AD5206 conectados a + 5V
  * All B pins of AD5206 connected to ground     * Todos los pines B de AD5206 conectados a tierra
  * An LED and a 220-ohm resisor in series connected from each W pin to ground    * Un LED y un resisor 220 ohmios en serie conectado desde cada pasador W a tierra
  * CS - to digital pin 10  (SS pin)    * CS - al pin digital 10 (pin SS)
  * SDI - to digital pin 11 (MOSI pin)  * SDI - al pin digital 11 (pin MOSI)
  * CLK - to digital pin 13 (SCK pin)   * CLK - al pin digital 13 (pin SCK)
  
  // The Bus SPI (Serial Peripheral Interface) is a communications standard, mainly used for
  transferring information between integrated circuits in electronic equipment. The bus serial peripheral interface
  or SPI bus is a standard for controlling almost any digital electronic device that accepts a serial bit stream
  regulated by a clock (synchronous communication).

It includes a clock line, incoming data, outgoing data and chip select pin, which connects or disconnects the operation
the device with which you want to communicate. Thus, this standard allows multiplexing clock lines.


 created 20 Febrero 2015 
 by Ángel



*/


// include the SPI library:    // SPI library include:
#include <SPI.h>


// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

// const is used to declare a constant field or constant local.
// The constant fields and local variables are not and can not be changed.
// Constants can be numbers, Boolean values, strings or a null reference.
// int variable is an integer data type


// (void setup) initializes and sets the initial values

void setup() {
  // set the slaveSelectPin as an output:                           // Set slaveSelectPin as an outlet:
  pinMode (slaveSelectPin, OUTPUT);    // Set the pin as an output ss
  // initialize SPI:                                                // Initialize SPI:
  SPI.begin();    // Initializes the SPI protocol
}

// (void loop)It is doing loops consecutively

void loop() {
  // go through the six channels of the digital pot:      // Go through the six channels of digital potentiometer:
  // (int channel) declares the variable
  // = initializes
  // ++  increase from 0 to 6
 // Initialization happens first and exactly once. Each time through the loop, the condition is tested;
   // if true, the increase and the block of statements is executed, then the condition is tested again.
   // When the condition becomes false, the loop exits.
  
  for (int channel = 0; channel < 6; channel++) { 
    // change the resistance on this channel from min to max:        // Change the channel resistance from minimum to maximum:
     // for = is used to repeat a block of statements enclosed
    
    // increases the level of 0-255
    for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, level);
     // Write a HIGH or LOW value to a digital pin (potentiometer)
       // If configured as an output pinMode (), the voltage is set to the appropriate value:
      //5V para HIGH, 0V (tierra) para LOW.
      delay(10);
      // Pauses the program for the amount of time (in milliseconds) specified as a parameter.
    }
    // wait a second at the top:       // Wait a second at the top:
    delay(100);
    // change the resistance on this channel from max to min:      //  Change the channel resistance in the MIN MAX:        
    for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, 255 - level);
      // increases the value down
      delay(10);
    }
  }

}

void digitalPotWrite(int address, int value) {
  // take the SS pin low to select the chip:     // Take the low value of the SS pin to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  //  send in the address and value via SPI:       // Send the address and SPI value through:
  SPI.transfer(address);
  SPI.transfer(value);
  // take the SS pin high to de-select the chip:    // Take the high value of the SS pin to deselect the chip:
  digitalWrite(slaveSelectPin,HIGH); 
}

In these images we see through voltmeters as it changes the resistance value in each of the 6 channels of the potentiometer as time passes