Diferencia entre revisiones de «UDPSendReceiveString»

De ArduWiki
Saltar a: navegación, buscar
(EJemplo)
(Ejemplo)
 
Línea 3: Línea 3:
 
== Ejemplo ==
 
== Ejemplo ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 +
#include <Ethernet.h>
 +
#include <EthernetUdp.h>
 +
 +
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };        //Asignar MAc al modulo
 +
IPAddress ip(192, 168, 1, 177);                              //Asignar IP
 +
unsigned int localPort = 8888;                              //Asignar puerto a escuchar
 +
 +
// buffers for receiving and sending data
 +
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
 +
char ReplyBuffer[] = "acknowledged";        // a string to send back
 +
 +
EthernetUDP Udp;            //Instanciar UDP
 +
byte CS = 10;              //Confugura CS para UNO
 +
//byte CS = 15;              //Confugura CS para ESP8266
 +
//byte CS = 33;              //Confugura CS para ESP32
 +
//byte CS = 5;              //Confugura CS para MKR
 +
 +
void setup() {
 +
  Serial.begin(9600);
 +
  Ethernet.init(CS);
 +
  Ethernet.begin(mac, ip);        //Iniciar Ethernet
 +
 +
  // Check for Ethernet hardware present
 +
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
 +
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
 +
      while (true) {
 +
        delay(1);    //do nothing, no point running without Ethernet hardware
 +
      }
 +
  }
 +
  if (Ethernet.linkStatus() == LinkOFF) {
 +
      Serial.println("Ethernet cable is not connected.");
 +
  }
 +
  Udp.begin(localPort);  //Inicializa UDP
 +
}
 +
 +
void loop() {
 +
  // if there's data available, read a packet
 +
  int packetSize = Udp.parsePacket();
 +
  if (packetSize) {
 +
      Serial.print("Received packet of size ");
 +
      Serial.println(packetSize);
 +
      Serial.print("From ");
 +
      IPAddress remote = Udp.remoteIP();
 +
      for (byte 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.write(ReplyBuffer);
 +
      Udp.endPacket();
 +
  }
 +
  delay(10);
 +
}
 +
</syntaxhighlight>
 +
 +
Para ejecutar con este ejemplo
 +
<syntaxhighlight lang="c++">
 +
 +
// Processing UDP example to send and receive string data from Arduino
 +
// press any key to send the "Hello Arduino" message
 +
 +
 +
import hypermedia.net.*;
 +
UDP udp;  // define the UDP object
 +
 +
void setup() {
 +
  udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
 +
  //udp.log(true); // <-- printout the connection activity
 +
  udp.listen(true);            // and wait for incoming message
 +
}
 +
 +
void draw() {
 +
  //Nada
 +
}
 +
 +
void keyPressed() {
 +
  String ip = "192.168.1.177"; // the remote IP address
 +
  int port = 8888;         // the destination port
 +
  udp.send("Hello World", ip, port );  // the message to send
 +
}
 +
 +
void receive( byte[] data ) { // <-- default handler
 +
  //void receive( byte[] data, String ip, int port ) { // <-- extended handler
 +
  for (byte i=0; i<data.length; i++)
 +
      print(char(data[i]));
 +
      println();
 +
  }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revisión actual del 00:09 31 may 2019

En este ejemplo, con Ethernet Shield y su Arduino podrá enviar y recibir cadenas de texto a través del protocolo UDP (Universal Datagram Packet). Necesitará otro dispositivo para enviar y recibir mensajes.

Ejemplo

#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };         //Asignar MAc al modulo
IPAddress ip(192, 168, 1, 177);                              //Asignar IP
unsigned int localPort = 8888;                               //Asignar puerto a escuchar

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

EthernetUDP Udp;             //Instanciar UDP
byte CS = 10;               //Confugura CS para UNO
//byte CS = 15;               //Confugura CS para ESP8266
//byte CS = 33;               //Confugura CS para ESP32
//byte CS = 5;               //Confugura CS para MKR

void setup() {
   Serial.begin(9600);
   Ethernet.init(CS);
   Ethernet.begin(mac, ip);        //Iniciar Ethernet

   // Check for Ethernet hardware present
   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
         delay(1);     //do nothing, no point running without Ethernet hardware
      }
   }
   if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
   }
   Udp.begin(localPort);   //Inicializa UDP
}

void loop() {
   // if there's data available, read a packet
   int packetSize = Udp.parsePacket();
   if (packetSize) {
      Serial.print("Received packet of size ");
      Serial.println(packetSize);
      Serial.print("From ");
      IPAddress remote = Udp.remoteIP();
      for (byte 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.write(ReplyBuffer);
      Udp.endPacket();
   }
   delay(10);
}

Para ejecutar con este ejemplo

 // Processing UDP example to send and receive string data from Arduino
 // press any key to send the "Hello Arduino" message


import hypermedia.net.*;
UDP udp;  // define the UDP object

void setup() {
   udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
   //udp.log(true); 		 // <-- printout the connection activity
   udp.listen(true);             // and wait for incoming message
}

void draw() {
   //Nada
}

void keyPressed() {
   String ip = "192.168.1.177"; 	 // the remote IP address
   int port = 8888;		         // the destination port
   udp.send("Hello World", ip, port );   // the message to send
}

void receive( byte[] data ) { 			// <-- default handler
   //void receive( byte[] data, String ip, int port ) {	// <-- extended handler
   for (byte i=0; i<data.length; i++)
      print(char(data[i]));
      println();
   }
}

Vea también


Refrencias externas