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

Google+