Arduino: Accendere e spegnere un led dal browser – Resoconto attività 22 luglio 2014

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

Today we started the day by making the project and its tutorial for the test we were doing, which consisted on controlling a led from the browser (ON/OFF).

Later we focused on the new project, which is based on the last and consists on controlling the led (ON/OFF) with different links. In addiction, it’s possible to check the temperature too.

Finally, we made a video for the current project and its tutorial.

22 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

Arduino: Interagire con PHP – Resoconto attività 21 Luglio 2014

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

Today we spent some time searching for the interaction of the php with arduino to use them for a test which consists on switching a led ON and OFF by pressing the button on the browser.

We’ve been learning php and used a program called xampp to simulate a local server.

We had some problems with functioning of the sketch,  we could not understand  why the serial connection between php and COM3 of arduino was not working properly.

 

 

21 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

Google+