Arduino > Ethernet
Contents (hide)
La logithèque Ethernet est distribuée avec Arduino : documentation de la logithèque Ethernet
Certaines versions d'Arduino ont un bogue lors de l'inclusion de la logithèque. Assurez vous que la logithèque est inclue ainsi au début de votre code :
#include <SPI.h> // needed for Arduino versions later than 0018 #include <Ethernet.h>
Chaque Arduino Ethernet et ordinateur doit avoir une adresse mac et ip différente. L'adresse mac d'un ordinateur est fixe. L'adresse mac d'un Arduino Ethernet peut être modifiée lors de l'appel à Ethernet.begin().
Le routeur doit assigner une adresse fixe au mac de l'ordinateur et au mac du Ethernet Shield.
Ici, on voit que l'Arduino Ethernet (WIZnetEFFEED) n'est pas correctement configuré sur le routeur :
On doit lui assigner une adresse fixe selon son mac. On doit aussi assigner une adresse fixe à son ordinateur :
Ici, on voit que l'Arduino Ethernet et l'ordinateur sont correctement configurés sur le routeur :
Extrait de code :
/* mac: the MAC (Media access control) address for the device (array of 6 bytes). this is the Ethernet hardware address of your shield. Newer Arduino Ethernet Shields include a sticker with the device's MAC address. For older shields, choose your own. */ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); }
Code Arduino pour tester le DHCP :
/* DHCP connection test. Please note that the connection takes a few seconds to establish. By : Thomas O Fredericks tof @ t-o-f.info */ #include <SPI.h> // needed for Arduino versions later than 0018 #include <Ethernet.h> #include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008 /* mac: the MAC (Media access control) address for the device (array of 6 bytes). this is the Ethernet hardware address of your shield. Newer Arduino Ethernet Shields include a sticker with the device's MAC address. For older shields, choose your own. */ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; int DHCPSuccess; void setup() { Serial.begin(57600); DHCPSuccess = Ethernet.begin(mac); } void loop() { Serial.print("Connection :"); if ( DHCPSuccess ) { Serial.println(" DHCP active"); Serial.print("IP :"); Serial.println(Ethernet.localIP()); } else { Serial.println(" Could not find DHCP server"); Serial.println("Trying again..."); DHCPSuccess = Ethernet.begin(mac); } delay(1000); }
Configurer le port Ethernet de votre ordinateur avec une adresse fixe :
Extrait de code :
/* mac: the MAC (Media access control) address for the device (array of 6 bytes). this is the Ethernet hardware address of your shield. Newer Arduino Ethernet Shields include a sticker with the device's MAC address. For older shields, choose your own. */ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; /* ip: the IP address of the device (array of 4 bytes) */ byte ip[] = { 192, 168, 6, 66}; void setup() { Ethernet.begin(mac, ip); }
Envoyer la lecture des entrées analogiques 0 à 5 par UDP à l'ordinateur. Récupérer les données dans Max.
// Include Ethernet Library : http://arduino.cc/en/Reference/Ethernet #include <SPI.h> // needed for Arduino versions later than 0018 #include <Ethernet.h> /* UDP send example. Please note that the Ethernet connection takes a few seconds to establish. This example reads all analog pins and sends them out in a message as : "analogs 1023 1023 944 864 775 715" For best results: Set a fixed address for the Ethernet Shield's MAC on your router and use a DHCP. By : Thomas O Fredericks tof @ t-o-f.info 2014-12-02 */ // Comment the following line to disable DHCP #define USE_DHCP byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // the MAC (Media access control) address for the device (array of 6 bytes) byte ip[] = { 192, 168, 6, 66}; // If no DHCP : the IP address of this device (array of 4 bytes) // UDP local port int udpLocalPort = 8888; // UDP remote port int udpRemotePort = 9999; byte udpRemoteIp[] = { 192, 168, 6, 6 }; // the IP address of the target device (array of 4 bytes) EthernetUDP udp; void setup() { #ifdef USE_DHCP Ethernet.begin(mac); #else Ethernet.begin(mac, ip); #endif udp.begin(udpLocalPort); } void loop() { udp.beginPacket( udpRemoteIp , udpRemotePort ); udp.print("analogs"); for ( int i = 0 ; i < 6; i++ ) { udp.write(32); udp.print(analogRead(i)); } udp.endPacket(); // Pause the loop. delay(1000); }
<pre><code> ----------begin_max5_patcher---------- 361.3ocmRFsaCBBEF9ZaReGHbsy.Tq0tq1dNVVVn5YsznfAvN2Z169PPccsc KK0Kfb9+OGNG9viymEg2n5.CFcO5ITTzQmRjWqWIZTHBWy6Jp3Feh3ZvX3aA b7foE5rdCtjWo1ZPTBaQXYcJCkmQPqxxQqHroRDk9BTa1eW5j3qJo0H9.5sn rDxndC2VrSH29hFJrgQcQpyFsZY+JaoeilmPPOOVirsVHq.qehYm1AIu12A7 iZAuZp4pVqKc66MPnCX7ONrfs+znd0OmOqe2sE+uAmDdyciuja0c6QRvlzV1 j3tiGPOznzVzZ220HF6VHFMc.VdVkEBH+Jxn2Dxh+CrwNCag7vUB4E++4G+d iyfoQ0pKFa1.IPmbCJAiUH4VgRdRRtevPzuSZmnrD79S3pTX3ap.OgIWeL4M MG.sY3jCSj6wcuR2GmEGhExPbfdXMbPLVRdPhqcOLV2qRqNfzt7Lryw2P2xW JlGyfC -----------end_max5_patcher----------- </code></pre>
Contrôler une DEL branchée à la broche numérique 6 de l'Arduino.
// Include Ethernet Library : http://arduino.cc/en/Reference/Ethernet #include <SPI.h> // needed for Arduino versions later than 0018 #include <Ethernet.h> /* UDP receive example. Please note that the Ethernet connection takes a few seconds to establish. This example toggles a LED connected to Arduino's digital pin 6 when it receives a "d6 0" or "d6 1" message. For best results: Set a fixed address for the Ethernet Shield's MAC on your router and use DHCP. By : Thomas O Fredericks tof @ t-o-f.info 2014-12-02 */ // Comment the following line to disable DHCP #define USE_DHCP #define LED_PIN 6 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // the MAC (Media access control) address for the device (array of 6 bytes) byte ip[] = { 192, 168, 6, 66 }; // If no DHCP : the IP address of this device (array of 4 bytes) byte udpRemoteIp[] = { 192, 168, 6, 6 }; // the IP address of the target device (array of 4 bytes) int udpRemotePort = 9999; // UDP remote port int udpLocalPort = 8888; // UDP local port char receivedBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, EthernetUDP udp; void setup() { #ifdef USE_DHCP Ethernet.begin(mac); #else Ethernet.begin(mac, ip); #endif udp.begin(udpLocalPort); pinMode(LED_PIN,OUTPUT); } void loop() { // if there's data available, read a packet int packetSize = udp.parsePacket(); if ( packetSize > 0 ) { udp.read( receivedBuffer , UDP_TX_PACKET_MAX_SIZE ); // Check if message starts with d6 if ( messageStartsWith(receivedBuffer,"d6") ) { int data = nextWordAsInt(); digitalWrite(LED_PIN,data); } } } int messageStartsWith( char * message, char * wordSearched ) { // Tokenize message char * messageWord = strtok (message," "); if ( messageWord != NULL ) return (strcmp(wordSearched,messageWord) == 0); return 0; } int nextWordAsInt() { char * messageWord = strtok (NULL," "); if ( messageWord != NULL ) return atoi( messageWord ); else return 0; }
<pre><code> ----------begin_max5_patcher---------- 409.3ocqSF0aBCBDG+4ZheGHj8XWSop0t8j6ywhwfxsJlVnAnttY128QA6rc tYLy0G5Et6f6+8iiCiGEfWKa.MF8H5YTPvAqm.muVOAcNBvkzlMETsKQrQlm W.3viwpnlMa4h7UJXiweRSlLKJNDQl6LIwc+QKOsIEsDLfZEHnqsmlcewcAE 0kbQAXbkizyqr1blaNyIJ45c2O+KM4Sz7VE3EDlKrRaoK5GiG0ZslvqtkKAs llepmMPiqUwrTzcjKhhDOCl5LSINtj0GEC51jNuuHEFgEQtp7jhSKvWOHl86 f.epvskPye2EgjDE+moi.d0V0ygSYyNj.LQ0rpHMHXnETFSYQIh7PRDIMKJM JMEsnRpLnL62k.YRlifShi6YFNT8yyM2DII+GjzmHtfKN6olqQaC7M9pk0pM cU53UJpWqx.sgKnFtTzKIxvj1xYLPL3oEiqaet4ZvKbge0xZ90HqY2jrNROZ U0dPoOdxdEYGC2IUsqSC8q4B+Z+cIVA64caIy6hprSVF6XUsxORzjkhsQbEz 96S.4ymH3A -----------end_max5_patcher----------- </code></pre>