Advertisement
AntonioVillanueva

Arduino Nano web server with w5500

Mar 1st, 2022
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. /*
  2.  https://docs.arduino.cc/tutorials/ethernet-shield-rev2/WebServer
  3.   Web Server
  4. ARDUINO NANO
  5. A5 SCL Yellow
  6. A4 SDA Green
  7. D10 CS SPI
  8. D11 MOSI SPI
  9. D12 MISO SPI
  10. D13 SCK SPI
  11.  
  12. ARCELI W5500 SPI
  13. LAN Ethernet Module réseau TCP IP STM32 Interface 3.3V 5V
  14. https://www.amazon.fr/gp/product/B09DYFLMJG/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
  15. GND                           GND
  16. GND                           VCC --> +3V3 arduino nano
  17. MOSI --> D11 arduino nano     VCC --> +3V3 arduino nano
  18. SCK -->  D13 arduino nano     NC
  19. CS  -->  D10 arduino nano     RST
  20. INT                           MISO --> D12 arduino nano
  21.  */
  22.  
  23. #include <SPI.h>
  24. #include <Ethernet.h>
  25.  
  26. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE}; //MAC Address
  27.  
  28. IPAddress ip(192, 168, 6,69);//IP address
  29.  
  30. EthernetServer server(80);//PORT 80 http
  31.  
  32. // Initialize the Ethernet server library
  33.  
  34.  
  35. void setup() {
  36.   Ethernet.init(10);  // CS D10(Arduino Nano) -->CS W5500
  37.  
  38.   Serial.begin(9600); //Serial Debug
  39.   while (!Serial) { } //Wait USB Serial ttyUSB
  40.   Serial.println("Ethernet WebServer");
  41.  
  42.   Ethernet.begin(mac, ip); //Start Server web
  43.  
  44.   // Check for Ethernet hardware present
  45.   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  46.     Serial.println("Ethernet ARCELI W5500  not found.");
  47.     while (true) {
  48.       delay(1); // Bucle No hardware Error
  49.     }
  50.   }else { Serial.println("Ethernet Hardware W5500 OK ");};
  51.  
  52.   if (Ethernet.linkStatus() == LinkOFF) {
  53.     Serial.println("Ethernet cable is not connected.");
  54.   }
  55.  
  56.   // start the server
  57.   server.begin();
  58.   Serial.print("server IP ");
  59.   Serial.println(Ethernet.localIP());
  60. }
  61.  
  62.  
  63. void loop() {
  64.   // listen for incoming clients
  65.   EthernetClient client = server.available();
  66.  
  67.   if (client) {
  68.     Serial.println("new client");
  69.     // an http request ends with a blank line
  70.     boolean currentLineIsBlank = true;
  71.     while (client.connected()) {
  72.       if (client.available()) {
  73.         char c = client.read();
  74.         Serial.write(c);
  75.         // if you've gotten to the end of the line (received a newline
  76.         // character) and the line is blank, the http request has ended,
  77.         // so you can send a reply
  78.        
  79.         if (c == '\n' && currentLineIsBlank) {
  80.           // send a standard http response header
  81.          
  82.           client.println("HTTP/1.1 200 OK");
  83.           client.println("Content-Type: text/html");
  84.           client.println("Connection: close");  // the connection will be closed after completion of the response
  85.           client.println("Refresh: 5");  // refresh the page  every 5 sec
  86.           client.println();
  87.           client.println("<!DOCTYPE HTML>");
  88.           client.println("<html>");
  89.          client.print("ICARO SERVER on ARDUINO NANO WITH w5500 ");
  90.          client.println("<br />");
  91.          client.println("</html>");
  92.          break;
  93.          
  94.         }
  95.  
  96.         if (c == '\n') {
  97.           // you're starting a new line
  98.           currentLineIsBlank = true;
  99.         } else if (c != '\r') {
  100.           // you've gotten a character on the current line
  101.           currentLineIsBlank = false;
  102.         }
  103.       }
  104.     }
  105.     // give the web browser time to receive the data
  106.     delay(1);
  107.     // close the connection:
  108.     client.stop();
  109.     Serial.println("client disconnected");
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement