luni, 16 februarie 2015

Placa de retea cu inscriptor SD si.. Arduino (partea a III-a: comanda periferice prin retea)

poze pierdute repostate in 06.10.2015

   Acum ceva timp am folosit placa de retea cu cipset W5100 de tip shield (se aseaza direct pe Arduino Uno, fara alte legaturi) pentru a avea informatii in retea despre temperatura si umiditate (vezi articolul Webserver cu informatii despre temperatura si umiditate oferite de senzorul DHT11), apoi am scris date pe un card SD (vezi Memorare date pe cardul micro SD), acum voi aprinde si stinge un LED.
   Fizic, placa de retea "shield" cu W5100, arata asa:
   Pentru experimente, am folosit ca baza articolul Arduino Webserver Control Lights, Relays, Servos, etc... care are are sursa pe blogul http://randomnerdtutorials.com/, mai precis articolul Arduino – Webserver with an Arduino + Ethernet Shield
   La montajul meu am folosit un led multicolor, comandand doar led-ul verde, care e conectat la D9 nefolosit de placa de retea, iar cel rosu si cel albastru sunt conectate pe pini folositi si de placa de retea (D10 si D11):
   Starile sunt:
- led verde stins:
- led verde aprins:
   Pentru comanda am folosit un telefon conectat prin wireless la retea... dar merge foarte bine si de pe calculator, vedeti in filmuletul LED controlat prin retea sau wireless unde este o varianta simplificata de pagina, doar buton de aprindere si buton de stingere LED, cu indicare stare LED si linkuri la pagina autorului si la a mea:

   Poze cu cele 2 stari si pagina nefinisata:
- led verde stins:
- led verde aprins:
   Pentru a stii cum e configurata reteaua, se cauta IP-ul calculatorului fix, cu care se face si incarcarea sketch-ului, gasind ca IP-ul este 192.168.2.17, Subset Mask e 255.255.255.1 si Default gateway este 192.168.2.1.. pentru placa de retea folosita la Arduino o sa aloc 192.168.2.178...
   Sketch-ul folosit de mine este:
/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects
 Modified by Nicu Florica aka niq_ro
 visit http://nicuflorica.blogspot.ro/
 or http://arduinotehniq.blogspot.com
 or http://www.tehnic.go.ro

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
int led = 9;
int pos = 0; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 2, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 2, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;



void setup() {
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           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://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
   
           client.println("<H2>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H1>Arduino with Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");   
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<H2>");
 
 if (readString.indexOf("?button1on") >0){
           client.print("<p>LED is ON</p>");
           }
 if (readString.indexOf("?button1off") >0){
           client.print("<p>LED is OFF</p>");
           }

           client.println("<br />");     
           client.println("<br />"); 
           client.println("<H4>");
           client.println("<p>Created by Rui Santos. Visit <a href=http://randomnerdtutorials.com target=blank>http://randomnerdtutorials.com</a> for more projects!</p>");  
           client.println("<br />"); 
           client.println("<p>Tested by Nicu Florica aka niq_ro. Visit <a href=http://www.tehnic.go.ro target=blank>http://www.tehnic.go.ro</a> for more projects!</p>");  
           client.println("<br />"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           //    client.print("<p>LED is ON</p>");
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           //    client.print("<p>LED is OFF</p>");
           }
              //clearing string for next read
            readString="";  
     
         }
       }
    }
}
}
   

Niciun comentariu:

Trimiteți un comentariu