Tutorial Arduino: How to check and adjust room temperature from browser

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.

Here’s how it looks:

 arduino_air_conditioning1

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

arduino_airconditioning

When the temperature chosen is lower than the actual one:

arduino_airconditioning2

When the temperature chosen is higher than the actual one:

arduino_airconditioning3

The sketch:

arduino_air_conditioning8

The code we used:

#include <String.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address
byte ip[] = { 
  192, 168, 1, 25 }; // ip arduino internet in
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

int outPin = 9; // pin 
String readString; //string
boolean LEDON = false; // flag status 

float vrif = 1.1; // for the temperature

void setup()
{
  Ethernet.begin(mac, ip, subnet);
  pinMode(outPin, OUTPUT);
  analogReference(INTERNAL); //lowers the voltage reference

  Serial.begin(9600);
}

void loop()
{
  // Temperature:
  float x=0, x2= 0;
  float from_sv=0;
  x = analogRead(5);


  x2 += (95.0 * vrif * x) / 1024.0;
  Serial.println(x2);


  /*inizio client*/

  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        readString.concat(c); 
        //if HTTP request has ended
        if (c == '\n' && currentLineIsBlank) {
          Serial.print(readString);

          if(readString.indexOf("L=1") > 0) {
            digitalWrite(outPin, HIGH); 
            LEDON = true;
            Serial.print("ON pin ");
            Serial.println(outPin);
          }
          else
          {
            if(readString.indexOf("L=0") > 0){
              digitalWrite(outPin, LOW);
              LEDON = false;
              Serial.print("OFF pin ");
              Serial.println(outPin);
            }
            if(readString.indexOf("T=") > 0)
            }
            // Web Page html
            client.println("HTTP/1.1 200 OK.....");
          client.println("Content-Type: text/html");
          client.println();


          client.print("<html> <style> table ,th, dt{ border:1px solid black; border-collapse:collapse;}"); // creates the table and costumizes the border for the temperature
          client.println("th, dt {padding:5px;}</style>"); // also border costumization

            client.println("<head><title>ARDUINO Led Control</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ><a href=""https://www.hydrasolutions.it""><img src=""https://2.bp.blogspot.com/-p5i_N2bHBOk/U7b48Dwy_tI/AAAAAAAAABY/IEIbQcWPJRQ/s1600/logotr.png"" alt=""Hydra Icon/></a><br><br></head><body>");

          client.print("<p><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></p>");

          client.println("<table><tr><th>Led status:</th>");
          client.print("<th>");
          if (LEDON) {
            client.println("<span style='color:green; font-weight:bold;'>ON</span></font>");
          }
          else
          {
            client.println("<span style='color:grey; font-weight:bold;'>OFF</span></font>");
          }
          client.print("</th>");
          client.print("<tr><th>Reference Temperature</th>"); 
          client.print("<th> <form action="" method=""get""><input type=""text"" name=""T"" value=""Insert value""><input type=""submit"" value=""check""><?phpif(!isset($_GET['temp']))$var_1 = "";else$var_1 = $_GET['temp'];$_SESSION['data'] = $var_1;?></th>");
          client.println("<th><?PHP echo $_SESSION['data'];?> </th></tr>");
          client.println("<tr><th>Temperature of the room</th>");
          client.print("<th>");     
          client.print(x2);   
          client.println("</th>"); 
          client.println("</body></html>");
          readString="";
          client.stop();

        } 
      }
    }




  } // fine loop
}


23 of July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: How to turn on and off a led from a web page while showing the temperature of the room

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

 

This project consists on turning ON and OFF a led from a web page and get the temperature of the room the circuit is in.

We used :

  • 1 Arduino
  • 1 Breadboard
  • 1 Ethernet shield
  • 1 Lm35
  • 1 Led
  • 1 Resistance
  • 1 Rj45

The first part of this tutorial is equal to our last one. Where you have to build a system to turn on and off a led.

This project is based on that one, but with a new interface and a new feature.

The new interface is made by this code:

 client.println("HTTP/1.1 200 OK.....");
        client.println("Content-Type: text/html");
        client.println();
       client.print("<html> <style> table ,th, dt{ border:1px solid black; border-collapse:collapse;}"); // creates the table and costumizes the border for the temperature
       client.println("th, dt {padding:5px;}</style>"); // also border costumization
       client.print("<p><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></p>");
       client.println("<table><tr><th>Led status:</th>");
       client.print("<th>");
          if (LEDON) {
              client.println("<span style='color:green; font-weight:bold;'>ON</span></font>");
            }
            else
            {
              client.println("<span style='color:grey; font-weight:bold;'>OFF</span></font>");
          }
      client.print("</th>");
      client.print("<tr><th>Reference Temperature</th>"); client.print("<th></th>");
      client.println("<tr><th>Temperature of the room</th>");
      client.print("<th>");     client.print(x2);   client.println("</th>"); 
        client.println("</body></html>");


Here we just built a different way of showing the on off switch and the status of the led and the temperature.

The sketch for this is :

arduino led temperature sketch

The pictures for this are :

When it’s ON:

arduino led temperature on

When it’s OFF:

arduino led temperature off

 

22 July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: How to turn on a Led from a browser – Come accendere un Led da un browser

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

For this tutorial, we’re testing the connection between the arduino and  the internet.

Components used:

  • 1 Arduino
  • 1 Ethernet Shield
  • 1 Led
  • 1 Resistance
  • 1 Cable RJ45

The web page is simple, it has a title and 2 links, one to turn on the led and another to turn it off.

Here is how it looks when the led is on:

arduino led control on

When the led if off:

arduino led control off

The sketch:

arduino led control sketch

 

Here is the code we used:

#include <String.h>
#include <SPI.h>
#include <Ethernet.h>
 
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address
byte ip[] = { 192, 168, 1, 25 }; // ip arduino internet in
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
 
int outPin = 9; // pin 
String readString; //string
boolean LEDON = false; // flag status 
 

void setup(){
Ethernet.begin(mac, ip, subnet);
pinMode(outPin, OUTPUT);

Serial.begin(9600);
}
 
void loop()
{

/*Client*/

EthernetClient client = server.available();
if (client) {
  boolean currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
        readString.concat(c); //storages each character of the packet

      //if HTTP request has ended
      if (c == '\n' && currentLineIsBlank) {
         Serial.print(readString);
         
        if(readString.indexOf("L=1") > 0) {// Reads the information for the led to turn on
          digitalWrite(outPin, HIGH); // turns on the led
          LEDON = true;
          Serial.print("ON pin ");
          Serial.println(outPin);
          }
          else
          {
            if(readString.indexOf("L=0") > 0){
          digitalWrite(outPin, LOW); // turn off the led
          LEDON = false;
          Serial.print("OFF pin ");
          Serial.println(outPin);
        }
          }

        // Web page:
        client.println("HTTP/1.1 200 OK.....");
        client.println("Content-Type: text/html");
        client.println();
        client.print("<html><head><title>ARDUINO Led Control</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>");
        client.println("<h1>Led Control</h1>");
        client.println("<hr />");
        client.print("<h1>PIN control n. ");
        client.print(outPin);
        client.println("</h1>");
        client.println("<br />");
        //Led status
        client.print("<font size='5'>PIN status: ");
          if (LEDON) {
              client.println("<span style='color:green; font-weight:bold;'>ON</span></font>");
            }
            else
            {
              client.println("<span style='color:grey; font-weight:bold;'>OFF</span></font>");
          }
        client.print("<h2><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></h2>");
        client.println("<hr />");
        client.println("<hr />");
        client.println("</div>");
        client.println("</body></html>");
         readString="";
        client.stop();
         
        } 
    }
  }
} 
}


We simply receive the packet through the Ethernet and search for the keyword: L=1 || L=0.
The ‘1’ means ON and ‘0’ OFF.

Then it’s just a matter of controlling the led via arduino and close or maintain the connection with the client.

22 July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: How to check the temperature from a browser – Controllare la temperatura da browser.

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

This project consists on checking the temperature on the browser.

We are going to use arduino plus the shield to create a small web server in order to check the temperature.

We used:

·         1 Arduino uno

·         1 Ethernet shield

·         1 Breadboard

·         1 RJ45 cable

The code needs two  libraries, Ethernet.h and SPI.h, those will allow you to use the functions for your shield.

The web page was a table with 3 columns, in the first is the room where the temperature is being measured , the second has the temperature itself and the third has the date and time.

Picture of the project:

 arduino browser

The sketch:

 arduino browser ethernet shield sketch

The code we used is:

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


// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 25); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default
float vrif = 1.1;

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(5, INPUT);       // Sensor pin A5
    analogReference(INTERNAL);
}  

void loop()
{
  float x=0, x2= 0;
  float from_sv=0;
  x = analogRead(5);
  
  x2 += (95.0 * vrif * x) / 1024.0;
  
  Serial.println(x2);
  delay(5000);
  EthernetClient client = server.available();  // try to get client
    
    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                     
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<?php session_start(); ?>");
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<style>table,th,td{border:1px solid black;}</style>");
                   
                    client.println("<head>");
                    client.println("<a href=""https://www.hydrasolutions.it""><img src=""https://2.bp.blogspot.com/-p5i_N2bHBOk/U7b48Dwy_tI/AAAAAAAAABY/IEIbQcWPJRQ/s1600/logotr.png"" alt=""Hydra Icon/></a>"); client.print("<br><br>");
                    client.println("<h1><font size=""5"">Home Automation project!</font></h1>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<table>");
                    client.println("<tr>");
                    client.println("<th>Temperature on room 1 : </th>");client.println("<th>");client.println(x2);client.println(" celcius");client.println("</th>"); client.println("<th> <iframe src=""https://free.timeanddate.com/clock/i48tyunu/n215/tt0/tw1/tm1/td1/th1/ts1"" frameborder=""0"" width=""181"" height=""18""></iframe></th>");
                    
                    client.println("</tr>");
                    client.println("</table>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}


To start, we need to store the arduino’s IP and MAC address and the port you will be using for the server, just like in the last project.

Then, in the setup you start the Ethernet shield and the serial connections.

In the loop you see if you have a client to connected ON and then you process it sending him the web page using the command:

client.println(" ");


As you know web pages are written in HTML and you need to use for each line of HTML a line of client.println();

The temperature is measured everytime the loop function 10 in 10 sec.

It is displays on the page as soon as the request arrives.

17 July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: How to create a small web server in order to remote control a led.

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

This project is a small introduction to the ethernet shield mechanics.

We are going to use arduino plus the shield to create a small web server in order to change the state of a Led, ON or OFF.

We used:

  • 1 arduino uno
  • 1 ethernet shield
  • 1 breadboard
  • 1 led
  • 1 resistance
  • 1 RJ45 cable

The first step is assembling your arduino with the shield.

Here is an example:

arduino ethernet shield

Now its just a matter of connecting the Led to the shield, after this mounted, you can start writing code.

The code needs two new libraries, ethernet.h and SPI.h, those will allow you to use the functions for your shield.

 The sketch:

 arduino linked to ethernet shield

1-      When The led is ON:

arduino led on

2-      When the led if OFF:

 arduino led off

The code we used is:

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 25); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(2, OUTPUT);       // LED on pin 2
}  

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino LED Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>LED</h1>");
                    client.println("<p>Click to switch LED on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox(client);
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
    if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }
    
    if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=""checkbox"" name=""LED2"" value=""2"" onclick=2""submit();"" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=""checkbox"" name=""LED2"" value=""2""  onclick=""submit();"">LED2");
    }
}


So the first things to notice are the variables, that need to store the arduino’s IP and MAC address, and the port you will be using for the server.

After that, the code is pretty self-explanatory, in the setup you start the Ethernet shield and the serial connections.

In the loop you see if you have a client to connect on and then you process it sending him the web page using the command:

client.println("<line>");


As you know web pages are written in HTML and you need to use for each line of HTML a line of client.println();

We have a function in the middle of the HTML seeing if the checkbox that we have in our page is clicked or not.

If it is clicked we light up the Led, otherwise nothing happens.

It is a pretty simple project, but it’s a start towards bigger projects using Ethernet.

17 July 2014

Alexandre Leitao

Gonçalo Neto

Google+