WebServer

Bu örnekte, basit bir Web sunucusu oluşturmak için Ethernet Kalkanınızı ve Arduino veya Genuino kartınızı kullanacaksınız. HTTP kütüphanesini kullanarak, cihazınız Ethernet kalkanınızla bir HTTP isteğine cevap verebilir. Bir tarayıcı açıp Ethernet kalkanınızın IP ekranında gittikten sonra, Arduino'nuz bir tarayıcının altı analog pimin tüm giriş değerlerini göstermesi için yeterli HTML ile yanıtlanmıştır.

Gerekli Donanım

devre

Ethernet kalkanı, bir WIZnet Ethernet denetleyicisini SPI veri yolu Üzerinden Arduino Veya Genuino kartlarına bağlamanızı Sağlar. Ethernet denetleyici yongasına SPI bağlantısı için yonga seçimi olarak ICSP başlık pimlerini ve pin 10'u seçin. SD Kart bulunur. Dijital pim 4, SD karttaki ikincil seçim pimini kontrol etmek için kullanılır.

Kalkan bir Ethernet bağlantısıuyla bir ağa bağlanmalıdır. Programdaki ağ üzerinden ağınıza uygun olacak şekilde çalışır.

Fritzing ekranında görüntülemek görüntü . Daha fazla devre örneği için Fritzing proje sayfasına bakın

Ekranında görüntüde, Arduino veya Genuino kartı Ethernet kalkanının altına istiflenecektir.

Şematik

Uyarı

Bu örnek bir SD kart gerektirmez. 4 SD'nin SS (aktif düşük) olarak kullanılabilir ve kullanılmadığı varsayılan olarak INPUT olarak yapılandırılmış olduğu için. İki olası çözüm:

  • SD kartı gerektirir;
  • kurulumda bu kod satırlarını ekle ()

pinMode ( 4 , ÇIKIŞ ) ;
digitalWrite ( 4 , YÜKSEK ) ;
[Kodu Al]

Kod


 /* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); 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 } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply 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(); client.println(" "); client.println("  /* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); 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 } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply 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(); client.println(" "); client.println("  /* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); 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 } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply 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(); client.println(" "); client.println("  /* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); 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 } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply 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(); client.println(" "); client.println("  ");
          // bir meta yenileme etiketi ekleyin, böylece tarayıcı her 5 saniyede bir tekrar çeker:
          client.println ("  ");
          // her analog giriş piminin değerini çıktılayın
          for (int analogChannel = 0; analogChannel <6; analogChannel ++) {
            int sensorReading = analogRead (analog Kanal);
            client.print ("analog giriş");
            client.print (analogChannel);
            client.print ("eşittir");
            client.print (sensorReading);
            client.println (" 
"); } client.println (" "); break; } if (c == '\ n') { // yeni bir satıra başlıyorsunuz currentLineIsBlank = true; } else if (c! = '\ r') { // mevcut satırda bir karakter kazandınız currentLineIsBlank = yanlış; } } } // web tarayıcısına verileri alması için zaman tanıyın gecikmesi (1); // bağlantıyı kapat: ) (Client.stop; Serial.println ("istemci bağlantısı kesildi"); } }

See Also