Arduino UNO R4 - Ethernet

Ce guide vous montre comment connecter l'Arduino UNO R4 à Internet ou à votre réseau local en utilisant le module Ethernet W5500. Nous aborderons les détails suivants :

Arduino UNO R4 Ethernet

À propos du module Ethernet W5500

Le module Ethernet W5500 offre deux interfaces :

  • Interface Ethernet RJ45 : Connectez-le à un routeur ou à un commutateur à l'aide d'un câble Ethernet.
  • SPI interface: Connectez-le à une carte Arduino UNO R4 en utilisant cette interface. Elle comprend 10 broches :
    • Broche NC : Laissez cette broche sans connexion.
    • Broche INT : Laissez cette broche sans connexion.
    • Broche RST : Il s'agit de la broche de réinitialisation. Connectez-la à la broche EN sur l'Arduino UNO R4.
    • Broche GND : Connectez cette broche à la broche GND sur l'Arduino UNO R4.
    • Broche 5V : Connectez cette broche à la broche 5V sur l'Arduino UNO R4.
    • Broche 3,3 V : Laissez cette broche sans connexion.
    • Broche MISO : Connectez cette broche à la broche SPI MISO sur l'Arduino UNO R4.
    • Broche MOSI : Connectez cette broche à la broche SPI MOSI sur l'Arduino UNO R4.
    • Broche SCS : Connectez cette broche à la broche SPI CS sur l'Arduino UNO R4.
    • Broche SCLK : Connectez cette broche à la broche SPI SCK sur l'Arduino UNO R4.
    Schéma des broches du module Ethernet
    image source: diyables.io

Schéma de câblage entre Arduino UNO R4 et le module Ethernet W5500

Schéma de câblage du module Ethernet Arduino UNO R4

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.

Voir Comment alimenter l'Arduino UNO R4..

Code Arduino UNO R4 pour le module Ethernet - Réalisation d'une requête HTTP via Ethernet

Ce code agit comme un client Web. Il envoie des requêtes HTTP au serveur Web à l'adresse http://example.com/.

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-ethernet-module */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; // or POST char HOST_NAME[] = "example.com"; String PATH_NAME = "/"; void setup() { Serial.begin(9600); delay(1000); Serial.println("Arduino Uno R4 - Ethernet Tutorial"); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true) ; } // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else { // if not connected: Serial.println("connection failed"); } } void loop() { }

Étapes rapides

Suivez ces instructions étape par étape :

  • Si c'est votre première utilisation de l'Arduino Uno R4 WiFi/Minima, reportez-vous au tutoriel sur Arduino UNO R4 - Installation du logiciel..
  • Connectez le module Ethernet à votre Arduino UNO R4 selon le schéma fourni.
  • Connectez le module Ethernet à votre routeur ou à votre switch en utilisant un câble Ethernet.
  • Connectez la carte Arduino Uno R4 à votre ordinateur à l'aide d'un câble USB.
  • Ouvrez l’IDE Arduino sur votre ordinateur.
  • Sélectionnez la carte Arduino Uno R4 appropriée (par exemple Arduino Uno R4 Minima) et le port COM.
  • Cliquez sur l’icône Libraries sur le côté gauche de l’IDE Arduino.
  • Dans la boîte de recherche, tapez « Ethernet » et localisez la bibliothèque Ethernet par Various.
  • Appuyez sur le bouton Install pour ajouter la bibliothèque Ethernet.
Bibliothèque Ethernet Arduino UNO R4
  • Ouvrez le Moniteur série dans l'IDE Arduino.
  • Copiez le code donné et collez-le dans l'IDE Arduino.
  • Cliquez sur le bouton Upload dans l'IDE Arduino pour envoyer le code à l'Arduino Uno R4.
  • Pour afficher la sortie, regardez le Moniteur série où les résultats seront affichés comme indiqué.
COM6
Send
Arduino UNO R4 - Ethernet Tutorial Connected to server HTTP/1.1 200 OK Accept-Ranges: bytes Age: 208425 Cache-Control: max-age=604800 Content-Type: text/html; charset=UTF-8 Date: Fri, 12 Jul 2024 07:08:42 GMT Etag: "3147526947" Expires: Fri, 19 Jul 2024 07:08:42 GMT Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT Server: ECAcc (lac/55B8) Vary: Accept-Encoding X-Cache: HIT Content-Length: 1256 Connection: close <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> disconnected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Note:

Si un autre appareil sur le même réseau partage votre adresse MAC, il pourrait y avoir des problèmes.

Code Arduino UNO R4 pour module Ethernet - Serveur Web

Le code ci-dessous transforme l'Arduino UNO R4 en un serveur Web. Ce serveur envoie une page Web simple aux navigateurs Internet.

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-ethernet-module */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetServer server(80); void setup() { Serial.begin(9600); delay(1000); Serial.println("Arduino Uno R4 - Ethernet Tutorial"); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true) ; } server.begin(); Serial.print("Arduino Uno R4 - Web Server IP Address: "); 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 bool 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"); // the connection will be closed after completion of the response client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<body>"); client.println("<h1>Arduino Uno R4 - Web Server with Ethernet</h1>"); client.println("</body>"); client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }

Étapes rapides

  • Copiez le code ci-dessus et collez-le dans l'IDE Arduino.
  • Cliquez sur le bouton Upload dans l'IDE Arduino pour téléverser le code vers l'Arduino Uno R4.
  • Vérifiez le résultat sur le Moniteur série, il s'affichera comme suit :
COM6
Send
Arduino UNO R4 - Ethernet Tutorial Arduino UNO R4 - Web Server IP Address: 192.168.0.2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Copiez l'adresse IP indiquée ci-dessus et saisissez-la dans la barre d'adresse de votre navigateur Web. Vous verrez une page Web simple affichée par l'Arduino UNO R4.
Arduino UNO R4 Ethernet Serveur Web

Tutoriels connexes

※ NOS MESSAGES

  • N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !