авто из германии
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

Tutorial Arduino: control of temperature and light with a buzzer – Controllo della temperature e della luminosità con l’aggiunta di un buzzer

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

For this tutorial consists on controlling the light using a buzzer and controlling the temperature.

Here are the components we used:

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

The web page has one table for the temperature with a text box and the actual temperature and one simple table for the 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.

For the light, depending on the light 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 temperature and light control refresh with buzzer

Same temperature:

arduino temperature and light control refresh with buzzer

Lower temperature:

arduino temperature and light control refresh with buzzer

Higher temperature:

arduino temperature and light control refresh with buzzer

Code:

#include &lt;String.h&gt;<br />
#include &lt;SPI.h&gt;<br />
#include &lt;Ethernet.h&gt;<br />
byte mac[] = {<br />
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address<br />
byte ip[] = {<br />
  192, 168, 1, 25 }; // ip arduino internet in<br />
byte subnet[] = {<br />
  255, 255, 255, 0 }; //subnet mask<br />
EthernetServer server(80); //server port<br />
String readString; //string<br />
String string;<br />
int temp1=0 , temp2=0;<br />
int vrif = 1.1;<br />
int Rele = 0, lastlight=0;<br />
int Ref_light=60;</p>
<p>void setup(){<br />
  Ethernet.begin(mac, ip, subnet);<br />
  analogReference(INTERNAL); //lowers the voltage reference<br />
  pinMode(8,OUTPUT); //cold<br />
  pinMode(9,OUTPUT); // hot<br />
  pinMode(7,OUTPUT); // rele<br />
  pinMode(6,OUTPUT);<br />
  pinMode(5,INPUT);<br />
  pinMode(0,INPUT);</p>
<p>  Serial.begin(9600);<br />
}<br />
void loop()<br />
{<br />
  /*Client*/</p>
<p>  int i=0; //y1 = temperatura e light = light level from user<br />
  long int Rlight=0;<br />
  float TempAverage=0;<br />
  int y1=0, light=0, dif=0; // references posted by the user!<br />
  EthernetClient client = server.available();</p>
<p>  TempAverage=analogRead(5);<br />
  Rlight=analogRead(0);</p>
<p>  TempAverage = (90.0 * vrif * TempAverage) / 1024.0;</p>
<p>  Rlight*=100;<br />
  Rlight/=1024;</p>
<p>  if (client) {<br />
    boolean currentLineIsBlank = true;<br />
    //tone(13,960,1000);<br />
    while (1)<br />
    {<br />
      if (client.available()) {</p>
<p>        char c = client.read();</p>
<p>        readString.concat(c);<br />
        //Serial.println(readString);<br />
        //if HTTP request has ended<br />
        if (c == '\n' &amp;&amp;  currentLineIsBlank)<br />
        {<br />
          Serial.println(readString);<br />
          string = readString;<br />
          if(readString.indexOf(&quot;T=&quot;) &gt; 0)<br />
          {<br />
            for(i=0;string[i];i++)<br />
            {<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)<br />
              {<br />
                switch(string[i+2])<br />
                {<br />
                case 48:<br />
                  y1+= 0;<br />
                  break;<br />
                case 49:<br />
                  y1+=10;<br />
                  break;<br />
                case 50:<br />
                  y1+=20;<br />
                  break;<br />
                case 51:<br />
                  y1+=30;<br />
                  break;<br />
                case 52:<br />
                  y1+=40;<br />
                  break;<br />
                case 53:<br />
                  y1+=50;<br />
                  break;<br />
                case 54:<br />
                  y1+=60;<br />
                  break;<br />
                case 55:<br />
                  y1+=70;<br />
                  break;<br />
                case 56:<br />
                  y1+=80;<br />
                  break;<br />
                case 57:<br />
                  y1+=90;<br />
                  break;<br />
                }<br />
                switch(string[i+3])<br />
                {<br />
                case 48:<br />
                  y1+= 0;<br />
                  break;<br />
                case 49:<br />
                  y1+=1;<br />
                  break;<br />
                case 50:<br />
                  y1+=2;<br />
                  break;<br />
                case 51:<br />
                  y1+=3;<br />
                  break;<br />
                case 52:<br />
                  y1+=4;<br />
                  break;<br />
                case 53:<br />
                  y1+=5;<br />
                  break;<br />
                case 54:<br />
                  y1+=6;<br />
                  break;<br />
                case 55:<br />
                  y1+=7;<br />
                  break;<br />
                case 56:<br />
                  y1+=8;<br />
                  break;<br />
                case 57:<br />
                  y1+=9;<br />
                  break;<br />
                }<br />
              }<br />
            }<br />
          }<br />
          break;<br />
        }<br />
      }<br />
    }</p>
<p>    Serial.println(y1);<br />
    Serial.println(light);</p>
<p>    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      Rele=1;<br />
    }<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      Rele=1;<br />
    }<br />
    if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      Rele=0;<br />
    }<br />
    if(Rele == 1)<br />
    TempAverage-=12;</p>
<p>    // Web page:<br />
    client.println(&quot;&lt;!DOCTYPE html&gt;&quot;);<br />
    client.println(&quot;&lt;?php session_start(); ?&gt;&quot;);<br />
    client.println(&quot;&lt;html&gt; &quot;);<br />
    client.println(&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;&quot;);<br />
    client.println(&quot; &lt;tr&gt;&lt;td&gt;&lt;table border=&quot;&quot;&gt;&lt;tr&gt;&lt;th&gt;Reference Temperature &lt;/th&gt;&quot;);<br />
    client.println(&quot;&lt;th&gt;&lt;form action=&quot;&quot; method=&quot;&quot;get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;T&quot;&quot; value=&quot;&quot;Insert value&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 = &quot;&quot;;else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;?&gt;&lt;/th&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;/tr&gt;&lt;th&gt;Reference temperature set to &lt;/th&gt;&lt;th&gt;&quot;);<br />
    if(y1 == 0)<br />
      client.print(temp1);<br />
    else<br />
      client.print(y1);</p>
<p>    client.print(&quot;&lt;/th&gt;&lt;tr&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;th&gt;Temperature of the room   &lt;/th&gt;&lt;th&gt;&quot;);<br />
    if(Rele == 1)<br />
      TempAverage-=12;<br />
    else<br />
    {<br />
    }<br />
    client.println(TempAverage);<br />
    client.print(&quot;&lt;/th&gt;&lt;/tr&gt;&quot;);<br />
    client.println(&quot;&lt;/table&gt;&lt;/td&gt;&quot;);<br />
    client.println(&quot;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;table border=&quot;&quot;&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;th&gt;Light leven of the room    &lt;/th&gt;&lt;th&gt;&quot;);<br />
    client.print(Rlight);<br />
    client.print(&quot;%&lt;/th&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&quot;);<br />
    client.println(&quot;&lt;/table&gt;&quot;);<br />
    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Turn heater ON!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,HIGH); // RELE<br />
      digitalWrite(8,LOW); // blue<br />
      digitalWrite(9,HIGH); // reed<br />
    }<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Turn Air conditioner ON!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,HIGH); // RELE<br />
      digitalWrite(8,HIGH); // blue<br />
      digitalWrite(9,LOW); // reed<br />
    }<br />
    if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Teperature OK!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,LOW); // REL<br />
      digitalWrite(8,LOW); // green<br />
      digitalWrite(9,LOW); // reed<br />
    }<br />
    client.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);<br />
    readString=&quot;&quot;;<br />
    if(y1 != temp1 &amp;&amp; y1 != 0)<br />
      temp1=y1;<br />
    if(light != temp2 &amp;&amp; light != 0)<br />
      temp2=light;<br />
    client.stop();<br />
   delay(2000);<br />
    if(light &gt; Ref_light+10)<br />
    {<br />
      tone(6,960,1000);<br />
      delay(500);<br />
    }<br />
    if(light &lt; Ref_light-10)<br />
    {<br />
      tone(6,960,1000);<br />
      delay(500);<br />
    }<br />
  }<br />
}<br />

30 of July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial arduino: room light and temperature levels check and adjust with autorefresh

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

For this tutorial, we’re going to explain you all about this project, which consists on controlling the temperature and the light from the web page. It’s based on old projects but this one has new features.

Here are the components we used:

  • 1 Arduino
  • 1 Ethernet Shield
  • 1 Relay
  • 1 Temperature sensor
  • 1 Light sensor
  • 3 Leds
  • 3 Resistors

The web page has two different tables, one for the temperature and one for the light. Above the text boxes appears the value we put in the text box, which represents the temperature/light we want it to be. Above that there’s the actual temperature/light in the room where the sensor is located.

Depending on the values we put comparing with the temperature, above the table appears text saying when to turn on/off the air conditioner.

How it looks:

arduino temperature and light control refresh

Same temperature:

arduino temperature and light control refresh

Lower temperature:

arduino temperature and light control refresh

Higher temperature:

arduino temperature and light control refresh

Code:

#include &lt;String.h&gt;</p>
<p>#include &lt;SPI.h&gt;</p>
<p>#include &lt;Ethernet.h&gt;</p>
<p>byte mac[] = {<br />
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address</p>
<p>byte ip[] = {<br />
  192, 168, 1, 25 }; // ip arduino internet in</p>
<p>byte subnet[] = {<br />
  255, 255, 255, 0 }; //subnet mask</p>
<p>EthernetServer server(80); //server port</p>
<p>int outPin = 9; // pin</p>
<p>String readString; //string</p>
<p>int temp1=0 , temp2=0;</p>
<p>int vrif = 1.1;</p>
<p>int Rele = 0;</p>
<p>void setup(){</p>
<p>  Ethernet.begin(mac, ip, subnet);<br />
  pinMode(outPin, OUTPUT);<br />
  analogReference(INTERNAL); //lowers the voltage reference<br />
  pinMode(8,OUTPUT); //cold<br />
  pinMode(9,OUTPUT); // hot<br />
  pinMode(7,OUTPUT); // rele</p>
<p>  pinMode(5,INPUT);<br />
  pinMode(0,INPUT);</p>
<p>  Serial.begin(9600);<br />
}</p>
<p>void loop()</p>
<p>{<br />
  /*Client*/<br />
  String string;<br />
  int i=0; //y1 = temperatura e light = light level from user<br />
  long int Rlight=0;<br />
  float TempAverage=0;<br />
  int y1=0, light=0; // references posted by the user!<br />
  EthernetClient client = server.available();</p>
<p>  TempAverage=analogRead(5);<br />
  Rlight=analogRead(0);</p>
<p>  TempAverage = (95.0 * vrif * TempAverage) / 1024.0;</p>
<p>  Rlight*=100;<br />
  Rlight/=1024;</p>
<p>  if (client) {</p>
<p>    boolean currentLineIsBlank = true;</p>
<p>    while (1)<br />
    {<br />
      if (client.available()) {</p>
<p>        char c = client.read();</p>
<p>        readString.concat(c); </p>
<p>        //Serial.println(readString);</p>
<p>        //if HTTP request has ended</p>
<p>        if (c == '\n' &amp;&amp;  currentLineIsBlank)<br />
        {</p>
<p>          Serial.println(readString);<br />
          string = readString;<br />
          if(readString.indexOf(&quot;T=&quot;) &gt; 0)<br />
          {<br />
            for(i=0;string[i];i++)<br />
            {<br />
              if(string[i] == 'T' &amp;&amp; string[i+1] == '=' &amp;&amp; string[i+2] &gt;=48 &amp;&amp; string[i+3] &lt;= 57)<br />
              {</p>
<p>                switch(string[i+2])<br />
                {<br />
                case 48:<br />
                  y1+= 0;<br />
                  break;<br />
                case 49:<br />
                  y1+=10;<br />
                  break;<br />
                case 50:<br />
                  y1+=20;<br />
                  break;<br />
                case 51:<br />
                  y1+=30;<br />
                  break;<br />
                case 52:<br />
                  y1+=40;<br />
                  break;<br />
                case 53:<br />
                  y1+=50;<br />
                  break;<br />
                case 54:<br />
                  y1+=60;<br />
                  break;<br />
                case 55:<br />
                  y1+=70;<br />
                  break;<br />
                case 56:<br />
                  y1+=80;<br />
                  break;<br />
                case 57:<br />
                  y1+=90;<br />
                  break;<br />
                }<br />
                switch(string[i+3])<br />
                {<br />
                case 48:<br />
                  y1+= 0;<br />
                  break;<br />
                case 49:<br />
                  y1+=1;<br />
                  break;<br />
                case 50:<br />
                  y1+=2;<br />
                  break;<br />
                case 51:<br />
                  y1+=3;<br />
                  break;<br />
                case 52:<br />
                  y1+=4;<br />
                  break;<br />
                case 53:<br />
                  y1+=5;<br />
                  break;<br />
                case 54:<br />
                  y1+=6;<br />
                  break;<br />
                case 55:<br />
                  y1+=7;<br />
                  break;<br />
                case 56:<br />
                  y1+=8;<br />
                  break;<br />
                case 57:<br />
                  y1+=9;<br />
                  break;<br />
                }</p>
<p>              }<br />
            }</p>
<p>          }</p>
<p>          if(readString.indexOf(&quot;L=&quot;) &gt; 0)<br />
          {<br />
            for(i=0;string[i];i++)<br />
            {<br />
              if(string[i] == 'L' &amp;&amp;string[i+1] == '=' &amp;&amp; string[i+2] &gt;=48 &amp;&amp; string[i+3] &lt;= 57)<br />
              {</p>
<p>                switch(string[i+2])<br />
                {<br />
                case 48:<br />
                  light+= 0;<br />
                  break;<br />
                case 49:<br />
                  light+=10;<br />
                  break;<br />
                case 50:<br />
                  light+=20;<br />
                  break;<br />
                case 51:<br />
                  light+=30;<br />
                  break;<br />
                case 52:<br />
                  light+=40;<br />
                  break;<br />
                case 53:<br />
                  light+=50;<br />
                  break;<br />
                case 54:<br />
                  light+=60;<br />
                  break;<br />
                case 55:<br />
                  light+=70;<br />
                  break;<br />
                case 56:<br />
                  light+=80;<br />
                  break;<br />
                case 57:<br />
                  light+=90;<br />
                  break;<br />
                }<br />
                switch(string[i+3])<br />
                {<br />
                case 48:<br />
                  light+= 0;<br />
                  break;<br />
                case 49:<br />
                  light+=1;<br />
                  break;<br />
                case 50:<br />
                  light+=2;<br />
                  break;<br />
                case 51:<br />
                  light+=3;<br />
                  break;<br />
                case 52:<br />
                  light+=4;<br />
                  break;<br />
                case 53:<br />
                  light+=5;<br />
                  break;<br />
                case 54:<br />
                  light+=6;<br />
                  break;<br />
                case 55:<br />
                  light+=7;<br />
                  break;<br />
                case 56:<br />
                  light+=8;<br />
                  break;<br />
                case 57:<br />
                  light+=9;<br />
                  break;<br />
                }</p>
<p>              }<br />
            }<br />
          }</p>
<p>          break;<br />
        }</p>
<p>      }<br />
    }</p>
<p>    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      Rele=1;<br />
    }<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
     Rele=1;<br />
    }<br />
    if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      Rele=0;<br />
    }</p>
<p>    // Web page:</p>
<p>    client.println(&quot;&lt;!DOCTYPE html&gt;&quot;);<br />
    client.println(&quot;&lt;?php session_start(); ?&gt;&quot;);</p>
<p>    client.println(&quot;&lt;html&gt; &quot;); </p>
<p>    client.println(&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;&quot;);<br />
   /* client.println(&quot;&lt;table border=&quot;&quot;&gt;&lt;tr&gt;&lt;th&gt;System Status&lt;/th&gt;&lt;th&gt; &quot;);<br />
    if(Rele == 1)<br />
    {<br />
      client.println(&quot;ON&quot;);<br />
      client.println(&quot;&lt;/th&gt;&lt;/table&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;&quot;);<br />
    }<br />
    else<br />
    {<br />
     client.println(&quot;OFF&quot;);<br />
     client.println(&quot;&lt;/th&gt;&lt;/table&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;&quot;);<br />
    }<br />
    */<br />
    client.println(&quot; &lt;tr&gt;&lt;td&gt;&lt;table border=&quot;&quot;&gt;&lt;tr&gt;&lt;th&gt;Reference Temperature &lt;/th&gt;&quot;);<br />
    client.println(&quot;&lt;th&gt;&lt;form action=&quot;&quot; method=&quot;&quot;get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;T&quot;&quot; value=&quot;&quot;Insert value&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 = &quot;&quot;;else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;?&gt;&lt;/th&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;/tr&gt;&lt;th&gt;Reference temperature set to &lt;/th&gt;&lt;th&gt;&quot;);</p>
<p>    if(y1 == 0)<br />
    client.print(temp1);<br />
    else<br />
    client.print(y1);</p>
<p>    client.print(&quot;&lt;/th&gt;&lt;tr&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;th&gt;Temperature of the room   &lt;/th&gt;&lt;th&gt;&quot;);<br />
    if(Rele == 1)<br />
    TempAverage-=12;<br />
    else<br />
    {<br />
    }<br />
    client.println(TempAverage);<br />
    client.print(&quot;&lt;/th&gt;&lt;/tr&gt;&quot;);</p>
<p>    client.println(&quot;&lt;/table&gt;&lt;/td&gt;&quot;);</p>
<p>    client.println(&quot;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;table border=&quot;&quot;&gt;&lt;tr&gt;&lt;th&gt;Reference Light&lt;/th&gt;&lt;th&gt;&quot;);<br />
    client.println(&quot;&lt;form action=&quot;&quot; method=&quot;&quot;get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;L&quot;&quot; value=&quot;&quot;Insert percentage&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 = &quot;&quot;;else$var_2 = $_GET['temp'];$_SESSION['data'] = $var_2;?&gt;&lt;/th&gt;&lt;/tr&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;/tr&gt;&lt;th&gt;Refence light set to &lt;/th&gt;&lt;th&gt;&quot;);</p>
<p>    if(light == 0)<br />
    client.print(temp2);<br />
    else<br />
    client.print(light);</p>
<p>    client.print(&quot;%&lt;/th&gt;&quot;);<br />
    client.println(&quot;&lt;tr&gt;&lt;th&gt;Light leven of the room    &lt;/th&gt;&lt;th&gt;&quot;);<br />
    client.print(Rlight);<br />
    client.print(&quot;%&lt;/th&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&quot;);</p>
<p>    client.println(&quot;&lt;/table&gt;&quot;);</p>
<p>    if(temp1 &lt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Turn heater ON!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,HIGH); // RELE<br />
      digitalWrite(8,LOW); // blue<br />
      digitalWrite(9,HIGH); // reed</p>
<p>    }<br />
    if(temp1 &gt; (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Turn Air conditioner ON!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,HIGH); // RELE<br />
      digitalWrite(8,HIGH); // blue<br />
      digitalWrite(9,LOW); // reed<br />
    }<br />
     if(temp1 == (int)TempAverage &amp;&amp; temp1 != 0)<br />
    {<br />
      client.println(&quot;&lt;p&gt;Teperature OK!&lt;/p&gt;&quot;);<br />
      digitalWrite(7,LOW); // REL<br />
      digitalWrite(8,LOW); // green<br />
      digitalWrite(9,LOW); // reed<br />
    } </p>
<p>    client.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);</p>
<p>    readString=&quot;&quot;;</p>
<p>    if(y1 != temp1 &amp;&amp; y1 != 0)<br />
   temp1=y1;</p>
<p>  if(light != temp2 &amp;&amp; light != 0)<br />
   temp2=light;</p>
<p>    client.stop();</p>
<p>  }<br />
}<br />

   29 of July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: Temperature control with Auto-refresh

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

This tutorial will explain how this project works and its code. It is based on the last project but this time we’ll having more control of the leds, temperature and relay.

Components:

  • 1 Arduino
  • 1 Ethernet Shield
  • 1 BreadBoard
  • 3 Leds
  • 3 Resistances
  • 1 Temperature sensor

In this project, we’re able to check the temperature in the room that the temperature sensor is located and write the temperature we want it to be, depending on the temperature we want and the real one, there’s a text saying if it is equal, too low or too high.

We simulate the Air conditioning with leds that turn on.

This sketch automatically refreshes when the light values change.

Here’s how it looks:

 Arduino auto refresh web page

When the temperature chosen is the same as the actual one:

arduino_refresh_2

When the temperature chosen is lower than the actual one:

arduino_refresh_3

When the temperature chosen is higher than the actual one:

arduino_refresh_4

 

The sketch:

arduino_refresh_sketch

The code we used:

#include &lt;String.h&gt;<br />
#include &lt;SPI.h&gt;<br />
#include &lt;Ethernet.h&gt;</p>
<p>byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address<br />
byte ip[] = { 192, 168, 1, 25 }; // ip arduino internet in<br />
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask<br />
EthernetServer server(80); //server port</p>
<p>int outPin = 9; // pin<br />
int y1=0; // reference temperature<br />
String readString; //string<br />
boolean LEDON = false; // flag status</p>
<p>float vrif = 1.1; // for the temperature</p>
<p>void setup()<br />
{<br />
Ethernet.begin(mac, ip, subnet);<br />
pinMode(outPin, OUTPUT);<br />
analogReference(INTERNAL); //lowers the voltage reference<br />
pinMode(8,OUTPUT); //cold<br />
pinMode(10,OUTPUT); // hot<br />
pinMode(7,OUTPUT); // rele<br />
pinMode(0,INPUT);<br />
Serial.begin(9600);<br />
}</p>
<p>void loop()<br />
{</p>
<p>  char string[100];<br />
  int y=0;<br />
  long int light=0;<br />
  for(y=0;y&lt;=100;y++) // put the string with 0<br />
  {<br />
    string[y]=0;<br />
  }<br />
  // Temperature:<br />
  float x=0, x2= 0;<br />
  float from_sv=0;<br />
  x = analogRead(5); // temperature<br />
  light= analogRead(0); // light</p>
<p>  // TEMPERATURE<br />
  x2 += (95.0 * vrif * x) / 1024.0;<br />
  //LIGHT<br />
  light= light * 100 ;<br />
  light= light / 1023;</p>
<p>EthernetClient client = server.available();<br />
if (client) {<br />
  boolean currentLineIsBlank = true;<br />
  while (client.connected()) {<br />
    if (client.available()) {<br />
      char c = client.read();<br />
        readString.concat(c);<br />
      //if HTTP request has ended<br />
      if (c == '\n' &amp;&amp; currentLineIsBlank) {<br />
        String string =  String(readString);<br />
        Serial.print(readString);<br />
        if(readString.indexOf(&quot;L=1&quot;) &gt; 0) {// (L=1 or L=0 will be in the string)<br />
          digitalWrite(outPin, HIGH);<br />
          LEDON = true;<br />
          Serial.print(&quot;ON pin &quot;);<br />
          Serial.println(outPin);<br />
          }<br />
          else<br />
          {<br />
            if(readString.indexOf(&quot;L=0&quot;) &gt; 0){ // &quot;&lt;0&quot; means &quot;L=0&quot; is there in the string</p>
<p>          digitalWrite(outPin, LOW);<br />
          LEDON = false;<br />
          Serial.print(&quot;OFF pin &quot;);<br />
          Serial.println(outPin);<br />
        }<br />
        if(readString.indexOf(&quot;T=&quot;) &gt; 0) // &quot;T&quot; is defined in the html part<br />
        {<br />
          y1=0; // y1 is 0 again<br />
          for(int i=0;string[i]!= '&#92;&#48;';i++)<br />
          {<br />
            if(string[i]== 'T' &amp;&amp; string[i+1]== '=')<br />
            {<br />
              Serial.println(string[i+2]); //prints what's next to the &quot;=&quot;<br />
              switch(string[i+2])<br />
              {<br />
                case 48: y1+= 0; break; // y1 represents the value returned by the site<br />
                case 49: y1+=10; break;<br />
                case 50: y1+=20; break;<br />
                case 51: y1+=30; break;<br />
                case 52: y1+=40; break;<br />
                case 53: y1+=50; break;<br />
                case 54: y1+=60; break;<br />
                case 55: y1+=70; break;<br />
                case 56: y1+=80; break;<br />
                case 57: y1+=90; break;<br />
              }<br />
              Serial.println(y1);<br />
              switch(string[i+3])<br />
              {<br />
                case 48: y1+= 0; break;<br />
                case 49: y1+=1; break;<br />
                case 50: y1+=2; break;<br />
                case 51: y1+=3; break;<br />
                case 52: y1+=4; break;<br />
                case 53: y1+=5; break;<br />
                case 54: y1+=6; break;<br />
                case 55: y1+=7; break;<br />
                case 56: y1+=8; break;<br />
                case 57: y1+=9; break;<br />
              }</p>
<p>             Serial.println(y1); // y1 converts, I mean, we get the input like 22 but arduino reads it as another number from ASCII, tyhis way we have to convert to a normal number again(ex: input: 22 -&gt; represents 55 -&gt; converts to 22 again and prints to the web page)<br />
            }<br />
          }</p>
<p>        }<br />
          }</p>
<p>        // WEB PAGE<br />
        client.println(&quot;HTTP/1.1 200 OK.....&quot;);<br />
        client.println(&quot;Content-Type: text/html&quot;);<br />
        client.println();</p>
<p>       client.print(&quot;&lt;html&gt; &lt;style&gt; table ,th, dt{ border:1px solid black; border-collapse:collapse;}&quot;); // creates the table and costumizes the border for the temperature<br />
       client.println(&quot;th, dt {padding:5px;}&lt;/style&gt;&quot;); // also border costumization<br />
       client.println(&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Led status:&lt;/th&gt;&quot;);<br />
       client.print(&quot;&lt;th&gt;&quot;);<br />
          if (LEDON) {<br />
              client.println(&quot;&lt;span style='color:green; font-weight:bold;'&gt;ON&lt;/span&gt;&lt;/font&gt;&quot;);<br />
            }<br />
            else<br />
            {<br />
              client.println(&quot;&lt;span style='color:grey; font-weight:bold;'&gt;OFF&lt;/span&gt;&lt;/font&gt;&quot;);<br />
          }<br />
      client.print(&quot;&lt;/th&gt;&quot;);<br />
      if(y1!=0)<br />
      {<br />
        client.println(&quot; &lt;th rowspan=&quot;&quot;4&quot;&quot;&gt;&quot;);<br />
      if((int)x2 == y1)<br />
      {<br />
        client.println(&quot;Temperature OK!!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,LOW);<br />
        digitalWrite(8,LOW);<br />
        digitalWrite(10,LOW);<br />
        LEDON=false;<br />
      }<br />
      if(x2&gt;y1)<br />
      {<br />
        client.println(&quot;Turn Heater ON!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,HIGH);<br />
        digitalWrite(10,HIGH);<br />
        digitalWrite(8,LOW);<br />
        LEDON=true;<br />
      }<br />
      if(x2&lt;y1)<br />
      {<br />
        client.println(&quot;Turn Air condicioning ON!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,HIGH);<br />
        digitalWrite(8,HIGH);<br />
        digitalWrite(10,LOW);<br />
        LEDON=true;<br />
      }</p>
<p>      }</p>
<p>      client.print(&quot;&lt;tr&gt;&lt;th&gt;Reference Temperature wanted &lt;/th&gt;&quot;); client.print(&quot;&lt;th&gt; &lt;form action=&quot;&quot; method=&quot;&quot;get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;T&quot;&quot; value=&quot;&quot;Insert value&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 = &quot;&quot;;else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;?&gt;&lt;/th&gt;&quot;);<br />
      client.print(&quot;&lt;tr&gt;&lt;th&gt;Reference Temperature is set to&lt;/th&gt;&lt;th&gt;&quot;); client.print(y1); client.print(&quot;&lt;/th&gt;&lt;tr&gt;&quot;);</p>
<p>      client.println(&quot;&lt;?PHP echo $_SESSION['data'];?&gt;&quot;);<br />
      client.println(&quot;&lt;tr&gt;&lt;th&gt;Temperature of the room&lt;/th&gt;&quot;);<br />
      client.print(&quot;&lt;th&gt;&quot;);     client.print(x2);   client.println(&quot;&lt;/th&gt;&quot;);<br />
       client.print(&quot;&lt;tr&gt;&lt;th&gt;Light level&lt;/th&gt;&lt;th&gt;&quot;); client.print(light); client.print(&quot;% &lt;/th&gt;&lt;tr&gt;&lt;/table&gt;&quot;);<br />
        client.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);</p>
<p>        readString=&quot;&quot;;</p>
<p>        client.stop();</p>
<p>        }<br />
    }<br />
  }<br />
 }<br />
 }<br />

 23 of July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: how to control temperature, light and system from browser.

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

For this new tutorial, we’re going to explain how to control the temperature, light and the system (ON/OFF).

Components:

  • 1 Arduino
  • 1 Ethernet Shield
  • 1 BreadBoard
  • 3 Leds
  • 3 Resistances
  • 1 Temperature sensor
  • 1 Relay

 

 

In this project, we can check the temperature in the room that the sensor is located and write the temperature we want it to be, as in the last project. In addiction, we’re able to check the light the sensor can get. Depending on the temperature we want and the real one, there’s a text saying if it is equal, too low or too high. The text about the light is als pretty simple.

We simulate the Air conditioner with leds that turn on.

How it looks:

arduino air conditioner led

When the temperature chosen is the same as the actual one:

arduino air conditioner same temperature

When the temperature chosen is lower than the actual one:

arduino air conditioner lower temperature

When the temperature chosen is higher than the actual one:

arduino air conditioner higher temperature

The sketch:

arduino air conditioner temperature light

The code we used:

#include &lt;String.h&gt;<br />
#include &lt;SPI.h&gt;<br />
#include &lt;Ethernet.h&gt;</p>
<p>byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address<br />
byte ip[] = { 192, 168, 1, 25 }; // ip arduino internet in<br />
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask<br />
EthernetServer server(80); //server port</p>
<p>int outPin = 9; // pin<br />
int y1=0; // reference temperature<br />
String readString; //string<br />
boolean LEDON = false; // flag status </p>
<p>float vrif = 1.1; // for the temperature</p>
<p>void setup()<br />
{<br />
Ethernet.begin(mac, ip, subnet);<br />
pinMode(outPin, OUTPUT);<br />
analogReference(INTERNAL); //lowers the voltage reference<br />
pinMode(8,OUTPUT); //cold<br />
pinMode(10,OUTPUT); // hot<br />
pinMode(7,OUTPUT); // rele<br />
pinMode(0,INPUT);<br />
Serial.begin(9600);<br />
}</p>
<p>void loop()<br />
{</p>
<p>  char string[100];<br />
  int y=0;<br />
  long int light=0;<br />
  for(y=0;y&lt;=100;y++) // put the string with 0<br />
  {<br />
    string[y]=0;<br />
  }<br />
  // Temperature:<br />
  float x=0, x2= 0;<br />
  float from_sv=0;<br />
  x = analogRead(5); // temperature<br />
  light= analogRead(0); // light</p>
<p>  // TEMPERATURE<br />
  x2 += (95.0 * vrif * x) / 1024.0;<br />
  //LIGHT<br />
  light= light * 100 ;<br />
  light= light / 1023;</p>
<p>/*inizio client*/</p>
<p>EthernetClient client = server.available();<br />
if (client) {<br />
  boolean currentLineIsBlank = true;<br />
  while (client.connected()) {<br />
    if (client.available()) {<br />
      char c = client.read();<br />
        readString.concat(c); //storages character<br />
      //if HTTP request has ended<br />
      if (c == '\n' &amp;&amp; currentLineIsBlank) {<br />
        String string =  String(readString);<br />
        Serial.print(readString);<br />
        if(readString.indexOf(&quot;L=1&quot;) &gt; 0) {// (L=1 or L=0 will be in the string)</p>
<p>          digitalWrite(outPin, HIGH);<br />
          LEDON = true;<br />
          Serial.print(&quot;ON pin &quot;);<br />
          Serial.println(outPin);<br />
          }<br />
          else<br />
          {<br />
            if(readString.indexOf(&quot;L=0&quot;) &gt; 0){ // &quot;&lt;0&quot; means &quot;L=0&quot; is there in the string<br />
          digitalWrite(outPin, LOW);<br />
          LEDON = false;<br />
          Serial.print(&quot;OFF pin &quot;);<br />
          Serial.println(outPin);<br />
        }<br />
        if(readString.indexOf(&quot;T=&quot;) &gt; 0) // &quot;T&quot; is defined in the html part<br />
        {<br />
          y1=0; // y1 is 0 again<br />
          for(int i=0;string[i]!= '&#92;&#48;';i++)<br />
          {<br />
            if(string[i]== 'T' &amp;&amp; string[i+1]== '=')<br />
            {<br />
              Serial.println(string[i+2]); //prints what's next to the &quot;=&quot;<br />
              switch(string[i+2])<br />
              {<br />
                case 48: y1+= 0; break; // y1 represents the value returned by the web page<br />
                case 49: y1+=10; break;<br />
                case 50: y1+=20; break;<br />
                case 51: y1+=30; break;<br />
                case 52: y1+=40; break;<br />
                case 53: y1+=50; break;<br />
                case 54: y1+=60; break;<br />
                case 55: y1+=70; break;<br />
                case 56: y1+=80; break;<br />
                case 57: y1+=90; break;<br />
              }<br />
              Serial.println(y1);<br />
              switch(string[i+3])<br />
              {<br />
                case 48: y1+= 0; break;<br />
                case 49: y1+=1; break;<br />
                case 50: y1+=2; break;<br />
                case 51: y1+=3; break;<br />
                case 52: y1+=4; break;<br />
                case 53: y1+=5; break;<br />
                case 54: y1+=6; break;<br />
                case 55: y1+=7; break;<br />
                case 56: y1+=8; break;<br />
                case 57: y1+=9; break;<br />
              }</p>
<p>             Serial.println(y1);<br />
            }<br />
          }<br />
        }<br />
          }</p>
<p>        // WEB PAGE<br />
        client.println(&quot;HTTP/1.1 200 OK.....&quot;);<br />
        client.println(&quot;Content-Type: text/html&quot;);<br />
        client.println();</p>
<p>       client.print(&quot;&lt;html&gt; &lt;style&gt; table ,th, dt{ border:1px solid black; border-collapse:collapse;}&quot;); // creates the table and costumizes the border for the temperature<br />
       client.println(&quot;th, dt {padding:5px;}&lt;/style&gt;&quot;); // also border costumization</p>
<p>       client.println(&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Led status:&lt;/th&gt;&quot;);<br />
       client.print(&quot;&lt;th&gt;&quot;);<br />
          if (LEDON) {<br />
              client.println(&quot;&lt;span style='color:green; font-weight:bold;'&gt;ON&lt;/span&gt;&lt;/font&gt;&quot;);<br />
            }<br />
            else<br />
            {<br />
              client.println(&quot;&lt;span style='color:grey; font-weight:bold;'&gt;OFF&lt;/span&gt;&lt;/font&gt;&quot;);<br />
          }<br />
      client.print(&quot;&lt;/th&gt;&quot;);<br />
      if(y1!=0)<br />
      {<br />
        client.println(&quot; &lt;th rowspan=&quot;&quot;4&quot;&quot;&gt;&quot;);<br />
      if((int)x2 == y1)<br />
      {<br />
        client.println(&quot;Temperature OK!!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,LOW);<br />
        digitalWrite(8,LOW);<br />
        digitalWrite(10,LOW);<br />
        LEDON=false;<br />
      }<br />
      if(x2&gt;y1)<br />
      {<br />
        client.println(&quot;Turn Heater ON!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,HIGH);<br />
        digitalWrite(10,HIGH);<br />
        digitalWrite(8,LOW);<br />
        LEDON=true;<br />
      }<br />
      if(x2&lt;y1)<br />
      {<br />
        client.println(&quot;Turn Air condicioning ON!!&lt;/th&gt;&quot;);<br />
        digitalWrite(7,HIGH);<br />
        digitalWrite(8,HIGH);<br />
        digitalWrite(10,LOW);<br />
        LEDON=true;<br />
      }</p>
<p>      }</p>
<p>      client.print(&quot;&lt;tr&gt;&lt;th&gt;Reference Temperature wanted &lt;/th&gt;&quot;); client.print(&quot;&lt;th&gt; &lt;form action=&quot;&quot; method=&quot;&quot;get&quot;&quot;&gt;&lt;input type=&quot;&quot;text&quot;&quot; name=&quot;&quot;T&quot;&quot; value=&quot;&quot;Insert value&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 = &quot;&quot;;else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;?&gt;&lt;/th&gt;&quot;);<br />
      client.print(&quot;&lt;tr&gt;&lt;th&gt;Reference Temperature is set to&lt;/th&gt;&lt;th&gt;&quot;); client.print(y1); client.print(&quot;&lt;/th&gt;&lt;tr&gt;&quot;);</p>
<p>      client.println(&quot;&lt;?PHP echo $_SESSION['data'];?&gt;&quot;);<br />
      client.println(&quot;&lt;tr&gt;&lt;th&gt;Temperature of the room&lt;/th&gt;&quot;);<br />
      client.print(&quot;&lt;th&gt;&quot;);     client.print(x2);   client.println(&quot;&lt;/th&gt;&quot;);<br />
       client.print(&quot;&lt;tr&gt;&lt;th&gt;Light level&lt;/th&gt;&lt;th&gt;&quot;); client.print(light); client.print(&quot;% &lt;/th&gt;&lt;tr&gt;&lt;/table&gt;&quot;);<br />
        client.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);</p>
<p>        readString=&quot;&quot;;</p>
<p>        client.stop();</p>
<p>        }<br />
    }<br />
  }</p>
<p>} // fine loop<br />
}<br />

24 of July 2014

Alexandre Leitao

Gonçalo Neto

Google+