r3ikeo7
Published © CC BY-SA

Ethernet shield+servomotore+led

l'uso dell'ethernet shield con un servomotore e dei led

IntermediateFull instructions provided693
Ethernet shield+servomotore+led

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Arduino Ethernet Shield 2
Arduino Ethernet Shield 2
×1
5 mm LED: Red
5 mm LED: Red
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

SCHEMA

Code

CODICE

Arduino
/*
 Created by RICCARDO POGLIACOMI
 

 Arduino Uno with Ethernet Shield Webserver
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
Servo myservo;
int led1 = 2;          //relay1
int led2 = 13;          //relay2
int led3 = 4;          //relay3
int led4 = 5;          //relay4
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //indirisso fisico
byte ip[] = {192, 168, 1, 177}; //indirizzo ip locale
byte subnet[] = {255, 255, 255, 0}; //indirizzo subnet
byte gateway[] = {192, 168, 1, 1}; //indirizzo gateway                 //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {
 // Apro la comunicazione seriale:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //Imposto i pin come OUTPUT
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(5, OUTPUT); //pin selected to control
  myservo.attach(7);  //the pin for the servo control

  // Inizio la comunicazione Ethernet con il server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Creo una connessione al client
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //Leggo i caratteri da HTTP
        if (readString.length() < 100) {
          //Inserisco i caratteri nella stringa 
          readString += c;
          //Serial.print(c);
         }

         //Se la richiesta HTTP è andata a buon fine
         if (c == '\n') {          
           Serial.println(readString); //scrivi sul monitor seriale per debugging
     
           client.println("HTTP/1.1 200 OK"); //Invio nuova pagina
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://postazionistreamer.altervista.org/ARDUINO.css' />");
           client.println("<TITLE>CONTRLLO DISPOSITIVI DA REMOTO</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>CONTRLLO DISPOSITIVI DA REMOTO</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>ARDUINO UNO CON ETHERNET SHIELD</H2>");
           client.println("<br />");  
           client.println("<a href=\"/?on\"\">ACCENDI SERVOMOTORE</a>");          //Modifica a tuo piacimento:"Accendi LED 1"
           client.println("<a href=\"/?off\"\">SPEGNI SERVO MOTORE</a><br />");    //Modifica a tuo piacimento:"Spegni LED 1" 
           client.println("<br />");  
           client.println("<br />");
           client.println("<a href=\"/?button2on\"\">ACCENDI LED 2</a>");          //Modifica a tuo piacimento:"Accendi LED 2"
           client.println("<a href=\"/?button2off\"\">SPEGNI LED 2</a><br />");    //Modifica a tuo piacimento:"Spegni LED 2"
           client.println("<br />");   
           client.println("<br />");
           client.println("<a href=\"/?button3on\"\">ACCENDI LED 3</a>");          //Modifica a tuo piacimento:"Accendi LED 3"
           client.println("<a href=\"/?button3off\"\">SPEGNI LED 3</a><br />");    //Modifica a tuo piacimento:"Spegni LED 3"
           client.println("<br />");   
           client.println("<br />");
           client.println("<a href=\"/?button4on\"\">ACCENDI LED 4</a>");          //Modifica a tuo piacimento:"Accendi LED 4"
           client.println("<a href=\"/?button4off\"\">SPEGNI LED 4</a><br />");    //Modifica a tuo piacimento:"Spegni LED 4"
           client.println("<br />");   
         
                                            
           client.println("<br />"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           client.stop();
           //Controlli su Arduino: Se è stato premuto il pulsante sul webserver
           
           if (readString.indexOf("?button2on") >0){
               digitalWrite(led2, HIGH);  
           }
           if (readString.indexOf("?button2off") >0){
               digitalWrite(led2, LOW);
           }
           if (readString.indexOf("?button3on") >0){
               digitalWrite(led3, HIGH);  
           }
           if (readString.indexOf("?button3off") >0){
               digitalWrite(led3, LOW);
           }
           if (readString.indexOf("?button4on") >0){
               digitalWrite(led4, HIGH);  
           }
           if (readString.indexOf("?button4off") >0){
               digitalWrite(led4, LOW);
           }
           
           
           if(readString.indexOf("?on") >0)//checks for on
          {
            myservo.write(10);
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("?off") >0)//checks for off
          {
            myservo.write(300);
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }

            //Cancella la stringa una volta letta
            readString="";  
           
         }
       }
    }
}
}

Credits

r3ikeo7

r3ikeo7

1 project • 0 followers

Comments