====== Ethernet ======
Avec Pure Data, Arduino, Raspberry Pi, Teensy, ...\\
Bizarrement, certains //switch// ne permettent pas à certaines cartes Ethernet de communiquer.
===== Shield Arduino =====
* http://arduino.cc/en/Reference/Ethernet
{{http://arduino.cc/en/uploads/Reference/arduino_uno_ethernet_pins.png?300}}
{{http://arduino.cc/en/uploads/Reference/arduino_mega_ethernet_pins.png?450}}
===== UDP =====
Communication Arduino / Pure Data
* **Branchement : ** 1 ordinateur et 1 carte Arduino + Ethernet Shield, branchés en câble Ethernet RJ45 à un routeur.
{{:logiciels:ethernet:arduino-ethernet.jpg?300|}}
++++ Code Arduino |
// TEST RECEPTION/ ENVOI UDP
#include
#include
#include
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 102);
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
EthernetUDP Udp;
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.beginPacket(Udp.remoteIP(), 5000);
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
++++
==== Pure Data ====
* {{:logiciels:ethernet:test-udp.pd| télécharger}}
{{:logiciels:ethernet:test-udp.png?300|}}
===== OSC =====
Quelques exemples :
* http://cnmat.berkeley.edu/oscuino
* http://liamtmlacey.blogspot.fr/2011/03/arduino-to-maxmsp-via-osc-guide-and.html
{{ vimeo>86386671?medium }}
==== Arduino : ArdOSC ====
Attention, télécharger la bibliothèque [[https://github.com/recotana/ArdOSC|ArdOSC]] modifiée (juste un problème de chemins) : {{:logiciels:ethernet:ardosc.zip|}}
++++ Code Arduino |
// TEST RECEPTION/ENVOIE OSC
// from Examples > ArdOSC > OSCArguments
#include
#include
#include
// ------------ CONFIG --------------- //
byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
byte myIp[] = { 192, 168, 1, 102 }; // IP
int serverPort = 8888; // Receive port
int destPort = 9000; // Send port
OSCServer server; // to receive messages
OSCClient client; // to send messages
char oscmsg1[]="/pwm";
char oscmsg2[]="/test";
// ------------ SETUP ------------ //
void setup(){
Serial.begin(19200); // for debug
Ethernet.begin(myMac ,myIp);
server.begin(serverPort);
server.addCallback(oscmsg1,&func1); // callback function (receive)
}
// ------------ LOOP ------------ //
void loop(){
if(server.aviableCheck()>0){
//Serial.println("alive! ");
}
}
// --------- FUNCTIONS ------------ //
void func1(OSCMessage *_mes){
logIp(_mes); // debug, print IP
logOscAddress(_mes); // debug, print Address
// Get content message
byte *sourceIp = _mes->getIpAddress(); //get source ip address
int tmpI=_mes->getArgInt32(0); //get 1st argument(int32)
float tmpF=_mes->getArgFloat(1); //get 2nd argument(float)
int strSize=_mes->getArgStringSize(2); //get 3rd argument(string)
char tmpStr[strSize]; //string memory allocation
_mes->getArgString(2,tmpStr);
// Send OSC message from received and parsed message
OSCMessage newMes; //create new osc message
newMes.setAddress(sourceIp,destPort); //set destination ip address & port no
newMes.beginMessage(oscmsg2); //set arguments
newMes.addArgInt32(tmpI+10);
newMes.addArgFloat(tmpF+0.01);
newMes.addArgString(tmpStr);
client.send(&newMes); //send osc message
}
// -----TOOLS FUNCTIONS --------- //
void logIp(OSCMessage *_mes){
byte *ip = _mes->getIpAddress();
Serial.print("IP:");
Serial.print(ip[0],DEC);
Serial.print(".");
Serial.print(ip[1],DEC);
Serial.print(".");
Serial.print(ip[2],DEC);
Serial.print(".");
Serial.print(ip[3],DEC);
Serial.print(" ");
}
void logOscAddress(OSCMessage *_mes){
Serial.println(_mes->getOSCAddress());
}
++++
==== Pure Data ====
* {{:logiciels:ethernet:test-osc.pd|Télécharger le patch}}
{{:logiciels:ethernet:test-osc.png|}}