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

Arduino: Usare l’Ethernet shield e il browser – Resoconto attività 18 luglio 2014

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

Today, we focused on the code for the project we started yesterday, which consists of getting the temperature on the browser using the Ethernet Shield.

What we did on the browser was pretty much making the temperature of the room the sensor is located appear and right next to it there’s the system date and time.

The code itself uses html and php, we’ve been very busy making it and it has been challenging, however we managed to get it done.

 

18 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

Arduino: Introduzione all’Ethernet Shield – Resoconto attività 17 luglio 2014

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

Today we managed to get the full code for the project electrical consumption measurement finished.

Later, we started to make an introduction to the Ethernet Shield by making a tutorial for how to turn ON the led by checking a checkbox on the browser, with images and every explanation.

After we finish it, we started to make a similar project, which will be used to make a remote check of the temperature inside the apartment.

The first try we just sent the value and it worked just fine, but now we’re trying to create a better interface for the users, which will be finished tomorrow.

 

17 July 2014

Alexandre Leitao

Gonçalo Neto

Tutorial Arduino: How to measure an apartment electrical consumption – Misurare il consumo energetico di un appartamento.

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

The apartment for testing is represented here:

arduino appartamento progetto

The components we will be using:

  • 1 arduino
  • 6 xbees
  • 6 breadboards
  • 11 relays
  • 6 ac current sensors
  • 1 display

The main sketch for this is :

arduino components project 1

arduino components 2

The project has 2 separated parts, the first is the main one, which has the arduino and the xbee to receive the data from all the other sensors in the apartment, the second is used in every division.

The first one is represented here :
 arduino_components_stanza2

This one is where we have the sensor measuring the energy usage at all the apartment, and the display.

This circuit controls all the apartment energy consumed.

The Second one is represented here :

 arduino_components_stanza3

In this one we have and xbee sending data and controlling the relays.

The relay control on the divisions is made by the arduino, he is the one who checks if everything is ok and if it needs to turn OFF anything or not.

The receiving and sending of the data is made in turns, arduino will receive the data from one room, analyze it , store it and then send the option (ON or OFF) to the correspondent xbee.

All these steps are used for every single xbee in the apartment.
After receiving and treating the data arduino will show for 25 seconds, the data for every room in the display and the generic one too.

The logic used consists of:

1- Receiving and processing the  data from:

                – Kitchen

                – living room

                – Bedroom

                – WC

                – Garage

2- If one of the divisions is not communicating -> breaks and tries again.

3- While( count != 25000 ) // Display divisions-> 25000 loops = 25 seconds

{

. Displays the data

}

The code we wrote:

<LiquidCrystal.h> // library for the LCD

LiquidCrystal lcd(7,6,5,4,3,2); // pins for the LCD


int an0= 0; // A0- Garage                 5 (house division number)
int an1= 1; // A1- Kitchen                1
int an2= 2; // A2- Living room         2
int an3= 3; // A3- Room                    3
int an4= 4; // A4- Toilet                    4
int an5= 5; // A5- sensor
int button= 0;
int received;

void setup()
 {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(9, INPUT); // Button
  pinMode(13,OUTPUT); // Relay
}
void loop() 
{
int storage [5]; // saves each house division's information
int cnt=0; // makes the storage get the next division

for(cnt=0; cnt<5 ;cnt++)
{
received= Analog.read(cnt); // information about a house division
  if (received == 0)
    break;
storage[cnt]= received; // saves that information
}

// LCD:
lcd.setCursor(0,0);
lcd.print("Apartment energy");
lcd.setCursor(0,1);
lcd.print(analogRead(5));



if(digitalRead(9)==HIGH) // if the button gets clicked
button++;


if(button==1) // Kitchen
{
lcd.setCursor(0,0);
lcd.print("Room 1 is using");
lcd.setCursor(0,1);
lcd.print(storage[0]);
}

if(button==2) // Living room
{
lcd.setCursor(0,0);
lcd.print("Room 2 is using");
lcd.setCursor(0,1);
lcd.print(storage[1]);
}
if(button==3) // Room
{
lcd.setCursor(0,0);
lcd.print("Room 3 is using");
lcd.setCursor(0,1);
lcd.print(storage[2]);
}
if(button==4) // toilet
{
lcd.setCursor(0,0);
lcd.print("Room 4 is using");
lcd.setCursor(0,1);
lcd.print(storage[3]);
}
if(button==5) // Garage
{
lcd.setCursor(0,0);
lcd.print("Room 5 is using");
lcd.setCursor(0,1);
lcd.print(storage[4]);
}
}


17 July 2014

Alexandre Leitao

Gonçalo Neto

Google+