авто из германии
Gta android скачать

Tutorial Arduino: how to control the temperature and how to verify the light – come controllare la temperatura e controllare il livello di luce

Scritto da Alessandra De Rosa on . Postato in Formazione, Prodotti, Progetto Leonardo da Vinci, Servizi, Tutorials Arduino

In this tutorial, we’re going to explain you how to control the temperature and how to verify the light.

Here are the components we used:

  • 1 Arduino
  • 1 Ethernet Shield
  • 1 Relay
  • 1 Temperature sensor
  • 1 Light sensor
  • 4 Leds
  • 5 Resistors
  • 1 Buzzer

The web page has one table for the temperature with a text box, with the actual temperature and another  table for the light with a text box also, and the actual light.

Depending on the values we put comparing with the temperature, above the table appears text saying when to turn on/off the air conditioner. The same happens for the light but instead of appearing text, the led turns ON when the difference between the reference light and the actual light is greater than 10%.

In addiction, depending on the light the sensor gets, if the light level is 10% higher/lower than the internal reference temperature the buzzer makes sound (in the video it is set to 60%).

How it looks:

arduino

Lower temperature:

arduino

Higher temperature:

arduino

Light OFF because the difference between the light written and the actual one is lower than 10%:

arduino web

Light OFF because the difference between the light written and the actual one is greater than 10%:

arduino web

The code we used:

#include &lt;String.h&gt;&lt;/p&gt;<br />
&lt;p&gt;#include &lt;SPI.h&gt;&lt;/p&gt;<br />
&lt;p&gt;#include &lt;Ethernet.h&gt;&lt;/p&gt;<br />
&lt;p&gt;byte mac[] = {&lt;br /&gt;<br />
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address&lt;/p&gt;<br />
&lt;p&gt;byte ip[] = {&lt;br /&gt;<br />
  192, 168, 1, 25 }; // ip arduino internet in&lt;/p&gt;<br />
&lt;p&gt;byte subnet[] = {&lt;br /&gt;<br />
  255, 255, 255, 0 }; //subnet mask&lt;/p&gt;<br />
&lt;p&gt;EthernetServer server(80); //server port&lt;/p&gt;<br />
&lt;p&gt;String readString; //string&lt;br /&gt;<br />
String string;&lt;br /&gt;<br />
int temp1=0 , temp2=0;&lt;/p&gt;<br />
&lt;p&gt;int vrif = 1.1;&lt;/p&gt;<br />
&lt;p&gt;int Rele = 0, lastlight=0;&lt;/p&gt;<br />
&lt;p&gt;int Ref_light=60;&lt;/p&gt;<br />
&lt;p&gt;void setup(){&lt;/p&gt;<br />
&lt;p&gt;  Ethernet.begin(mac, ip, subnet);&lt;/p&gt;<br />
&lt;p&gt;  analogReference(INTERNAL); //lowers the voltage reference&lt;br /&gt;<br />
  pinMode(8,OUTPUT); //cold&lt;br /&gt;<br />
  pinMode(9,OUTPUT); // hot&lt;br /&gt;<br />
  pinMode(7,OUTPUT); // rele&lt;br /&gt;<br />
  pinMode(6,OUTPUT); // buzzer&lt;br /&gt;<br />
  pinMode(5,INPUT); // analog temp sensor&lt;br /&gt;<br />
  pinMode(0,INPUT); // analog light sensor&lt;br /&gt;<br />
  pinMode(2,OUTPUT); // Led Control&lt;/p&gt;<br />
&lt;p&gt;  Serial.begin(9600);&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt;void loop()&lt;/p&gt;<br />
&lt;p&gt;{&lt;br /&gt;<br />
  /*Client*/&lt;/p&gt;<br />
&lt;p&gt;  int i=0;&lt;br /&gt;<br />
  long int Rlight=0;&lt;br /&gt;<br />
  float TempAverage=0;&lt;br /&gt;<br />
  int y1=0, light=0, dif=0, clight=0;&lt;br /&gt;<br />
  EthernetClient client = server.available();&lt;/p&gt;<br />
&lt;p&gt;  TempAverage=analogRead(5); // get light and temp values&lt;br /&gt;<br />
  Rlight=analogRead(0);&lt;/p&gt;<br />
&lt;p&gt;  TempAverage = (90.0 * vrif * TempAverage) / 1024.0; // calculating the real light and temp values&lt;/p&gt;<br />
&lt;p&gt;  Rlight*=100;&lt;br /&gt;<br />
  Rlight/=1024;&lt;/p&gt;<br />
&lt;p&gt;  if (client) {    // check for client &lt;/p&gt;<br />
&lt;p&gt;    boolean currentLineIsBlank = true;&lt;/p&gt;<br />
&lt;p&gt;    while (1) // read the URL returned by the web page&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      if (client.available()) {&lt;/p&gt;<br />
&lt;p&gt;        char c = client.read();&lt;/p&gt;<br />
&lt;p&gt;        readString.concat(c);  // assembling the URL in a Arduino String&lt;/p&gt;<br />
&lt;p&gt;        if (c == '\n' &amp;&amp;  currentLineIsBlank)   //if HTTP request has ended&lt;br /&gt;<br />
        {&lt;/p&gt;<br />
&lt;p&gt;          Serial.println(readString);&lt;br /&gt;<br />
          string = readString;&lt;/p&gt;<br />
&lt;p&gt;          if(readString.indexOf(&quot;T=&quot;) &gt; 0) //check for Temperature Reference chage&lt;br /&gt;<br />
          {&lt;/p&gt;<br />
&lt;p&gt;            for(i=0;string[i];i++) // getting the value converted from char to int from the URL&lt;br /&gt;<br />
            {&lt;br /&gt;<br />
              if(string[i] == 'T' &amp;&amp; string[i+1] == '=' &amp;&amp; string[i+2] &gt;=48  &amp;&amp; string[i+2] &lt;=57 &amp;&amp; string[i+3] &gt;= 48 &amp;&amp; string[i+3] &lt;= 57)&lt;br /&gt;<br />
              {&lt;/p&gt;<br />
&lt;p&gt;                switch(string[i+2])&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                case 48:&lt;br /&gt;<br />
                  y1+= 0;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 49:&lt;br /&gt;<br />
                  y1+=10;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 50:&lt;br /&gt;<br />
                  y1+=20;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 51:&lt;br /&gt;<br />
                  y1+=30;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 52:&lt;br /&gt;<br />
                  y1+=40;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 53:&lt;br /&gt;<br />
                  y1+=50;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 54:&lt;br /&gt;<br />
                  y1+=60;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 55:&lt;br /&gt;<br />
                  y1+=70;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 56:&lt;br /&gt;<br />
                  y1+=80;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 57:&lt;br /&gt;<br />
                  y1+=90;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                }&lt;br /&gt;<br />
                switch(string[i+3])&lt;br /&gt;<br />
                {&lt;br /&gt;<br />
                case 48:&lt;br /&gt;<br />
                  y1+= 0;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 49:&lt;br /&gt;<br />
                  y1+=1;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 50:&lt;br /&gt;<br />
                  y1+=2;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 51:&lt;br /&gt;<br />
                  y1+=3;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 52:&lt;br /&gt;<br />
                  y1+=4;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 53:&lt;br /&gt;<br />
                  y1+=5;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 54:&lt;br /&gt;<br />
                  y1+=6;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 55:&lt;br /&gt;<br />
                  y1+=7;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 56:&lt;br /&gt;<br />
                  y1+=8;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                case 57:&lt;br /&gt;<br />
                  y1+=9;&lt;br /&gt;<br />
                  break;&lt;br /&gt;<br />
                }&lt;/p&gt;<br />
&lt;p&gt;              }&lt;br /&gt;<br />
            }&lt;/p&gt;<br />
&lt;p&gt;          }&lt;/p&gt;<br />
&lt;p&gt;            if(readString.indexOf(&quot;L=&quot;) &gt; 0) // check for Light Reference change&lt;br /&gt;<br />
           {&lt;/p&gt;<br />
&lt;p&gt;           for(i=0;string[i] != '&#92;&#48;';i++) // getting the value converted from char to int from the URL&lt;br /&gt;<br />
           {&lt;/p&gt;<br />
&lt;p&gt;           if(string[i] == 'L' &amp;&amp;string[i+1] == '=' &amp;&amp; string[i+2] &gt;=48  &amp;&amp; string[i+2] &lt;=57 &amp;&amp; string[i+3] &gt;= 48 &amp;&amp; string[i+3] &lt;= 57)&lt;br /&gt;<br />
           {&lt;/p&gt;<br />
&lt;p&gt;           switch(string[i+2])&lt;br /&gt;<br />
           {&lt;br /&gt;<br />
           case 48:&lt;br /&gt;<br />
           light+= 0;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 49:&lt;br /&gt;<br />
           light+=10;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 50:&lt;br /&gt;<br />
           light+=20;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 51:&lt;br /&gt;<br />
           light+=30;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 52:&lt;br /&gt;<br />
           light+=40;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 53:&lt;br /&gt;<br />
           light+=50;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 54:&lt;br /&gt;<br />
           light+=60;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 55:&lt;br /&gt;<br />
           light+=70;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 56:&lt;br /&gt;<br />
           light+=80;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 57:&lt;br /&gt;<br />
           light+=90;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           }&lt;br /&gt;<br />
           switch(string[i+3])&lt;br /&gt;<br />
           {&lt;br /&gt;<br />
           case 48:&lt;br /&gt;<br />
           light+= 0;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 49:&lt;br /&gt;<br />
           light+=1;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 50:&lt;br /&gt;<br />
           light+=2;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 51:&lt;br /&gt;<br />
           light+=3;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 52:&lt;br /&gt;<br />
           light+=4;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 53:&lt;br /&gt;<br />
           light+=5;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 54:&lt;br /&gt;<br />
           light+=6;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 55:&lt;br /&gt;<br />
           light+=7;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 56:&lt;br /&gt;<br />
           light+=8;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           case 57:&lt;br /&gt;<br />
           light+=9;&lt;br /&gt;<br />
           break;&lt;br /&gt;<br />
           }&lt;/p&gt;<br />
&lt;p&gt;           }&lt;br /&gt;<br />
           }&lt;br /&gt;<br />
           }&lt;/p&gt;<br />
&lt;p&gt;          break;&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;      }&lt;br /&gt;<br />
    }&lt;/p&gt;<br />
&lt;p&gt;    Serial.println(y1);&lt;br /&gt;<br />
    Serial.println(light);&lt;/p&gt;<br />
&lt;p&gt;    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0) // control the Relay (air conditioning) depending on the temperature&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      Rele=1;&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      Rele=1;&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      Rele=0;&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    if(Rele == 1)&lt;br /&gt;<br />
    TempAverage-=12;&lt;/p&gt;<br />
&lt;p&gt;    if(Rlight&gt;light)  //check for light issues&lt;br /&gt;<br />
    clight= Rlight-light;&lt;br /&gt;<br />
    else&lt;br /&gt;<br />
    clight=light-Rlight;&lt;/p&gt;<br />
&lt;p&gt;    if(clight &gt; 10)&lt;br /&gt;<br />
    digitalWrite(2,HIGH);&lt;br /&gt;<br />
    else&lt;br /&gt;<br />
    digitalWrite(2,LOW);&lt;/p&gt;<br />
&lt;p&gt;    // Web page:&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;!DOCTYPE html&gt;&quot;);&lt;br /&gt;<br />
    client.println(&quot;&lt;?php session_start(); ?&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;html&gt; &quot;);   &lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;head&gt;&lt;meta http-equiv=\&quot;refresh\&quot; content=\&quot;10\&quot;&gt;&lt;title&gt;ARDUINO Led Control&lt;/title&gt;&lt;meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' &gt;&lt;a href=&quot;&quot;http://www.hydrasolutions.it&quot;&quot;&gt;&lt;img src=&quot;&quot;http://2.bp.blogspot.com/-p5i_N2bHBOk/U7b48Dwy_tI/AAAAAAAAABY/IEIbQcWPJRQ/s1600/logotr.png&quot;&quot; alt=&quot;&quot;Hydra Icon/&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/head&gt;&lt;body&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;br /&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot; &lt;/th&gt;<br />
&lt;td&gt;<br />
&lt;table border=&quot;&quot;&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;Reference Temperature &lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
    client.println(&quot;<br />
&lt;th&gt;<br />
&lt;form action=&quot;&quot;method=get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;T&quot;&quot; value=&quot;&quot;Insert&quot;&quot;&gt;&lt;input type=&quot;&quot;submit&quot;&quot; value=&quot;&quot;check&quot;&quot;&gt;&lt;?phpif(!isset($_GET['temp']))$var_1 = ;else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;&gt;&lt;/form&gt;<br />
&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
    client.println(&quot;<br />
&lt;th&gt;Reference Light&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
    client.println(&quot;<br />
&lt;th&gt;<br />
&lt;form action=&quot;&quot;method=get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;L&quot;&quot; value=&quot;&quot;Insert&quot;&quot;&gt;&lt;input type=&quot;&quot;submit&quot;&quot; value=&quot;&quot;Try&quot;&quot;&gt;&lt;?phpif(!isset($_GET['temp']))$var_2 = ;else$var_2 = $_GET['temp'];$_SESSION['data'] = $var_2;?&gt;&lt;/form&gt;<br />
&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;Reference temperature set to &lt;/th&gt;<br />
&lt;th&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    if(y1 == 0)&lt;br /&gt;<br />
      client.print(temp1);&lt;br /&gt;<br />
    else&lt;br /&gt;<br />
      client.print(y1);&lt;br /&gt;<br />
   client.println(&quot;&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;   client.println(&quot;<br />
&lt;th&gt;Refence light set to &lt;/th&gt;<br />
&lt;th&gt;&quot;);&lt;br /&gt;<br />
   if(light == 0)&lt;br /&gt;<br />
    client.print(temp2);&lt;br /&gt;<br />
    else&lt;br /&gt;<br />
    client.print(light);&lt;br /&gt;<br />
   client.println(&quot;%&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;   client.println(&quot;&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;Temperature of the room   &lt;/th&gt;<br />
&lt;th&gt;&quot;);&lt;br /&gt;<br />
   if(Rele == 1)&lt;br /&gt;<br />
      TempAverage-=12;&lt;br /&gt;<br />
    else&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    client.println(TempAverage);&lt;br /&gt;<br />
    client.println(&quot;&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;<br />
&lt;th&gt;Light leven of the room    &lt;/th&gt;<br />
&lt;th&gt;&quot;);&lt;br /&gt;<br />
    client.print(Rlight);&lt;br /&gt;<br />
   client.println(&quot;%&lt;/th&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
   client.println(&quot;&lt;/table&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;p&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      client.println(&quot;<br />
&lt;p&gt;Turn heater ON!&lt;/p&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
      digitalWrite(7,HIGH); // RELE&lt;br /&gt;<br />
      digitalWrite(8,LOW); // blue&lt;br /&gt;<br />
      digitalWrite(9,HIGH); // reed&lt;/p&gt;<br />
&lt;p&gt;    }&lt;br /&gt;<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      client.println(&quot;<br />
&lt;p&gt;Turn Air conditioner ON!&lt;/p&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
      digitalWrite(7,HIGH); // RELE&lt;br /&gt;<br />
      digitalWrite(8,HIGH); // blue&lt;br /&gt;<br />
      digitalWrite(9,LOW); // reed&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      client.println(&quot;<br />
&lt;p&gt;Teperature OK!&lt;/p&gt;<br />
&lt;p&gt;&quot;);&lt;br /&gt;<br />
      digitalWrite(7,LOW); // REL&lt;br /&gt;<br />
      digitalWrite(8,LOW); // green&lt;br /&gt;<br />
      digitalWrite(9,LOW); // reed&lt;br /&gt;<br />
    }  &lt;/p&gt;<br />
&lt;p&gt;    client.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);&lt;/p&gt;<br />
&lt;p&gt;    readString=&quot;&quot;;&lt;/p&gt;<br />
&lt;p&gt;    if(y1 != temp1 &amp;&amp; y1 != 0)  //saving the References in a global variable in order for the values not to be losed in the loop process&lt;br /&gt;<br />
      temp1=y1;&lt;/p&gt;<br />
&lt;p&gt;    if(light != temp2 &amp;&amp; light != 0)&lt;br /&gt;<br />
      temp2=light;&lt;/p&gt;<br />
&lt;p&gt;    client.stop();&lt;/p&gt;<br />
&lt;p&gt;   delay(2000);&lt;/p&gt;<br />
&lt;p&gt;    if(light &gt; Ref_light+10) //making the buzzer buzz when the light levels are diferent&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      tone(6,960,1000);&lt;br /&gt;<br />
      delay(500);&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
    if(light &lt; Ref_light-10)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
      tone(6,960,1000);&lt;br /&gt;<br />
      delay(500);&lt;br /&gt;<br />
    }&lt;/p&gt;<br />
&lt;p&gt;  }&lt;/p&gt;<br />
&lt;p&gt;}&lt;br /&gt;<br />

 31 of July 2014

Alexandre Leitao

Gonçalo Neto

Google+