Go Down

Topic: Need guidance on how to read/write Ethernet packets on Arduino Yun (Read 4297 times) previous topic - next topic

BrainTrance

I read the documentation of Arduino Yun and how it works but still I haven't managed to read/write UDP packets through Ethernet, pass them to AVR and print/manipulate them.
I got confused and frustrated so I need to start over with a fresh mind and some guidance.

I set up a netcat listener on the same network with my Arduino Yun (for now is connected via WiFi).
I tested the communication between them (I connected to my Yun through SSH and connected to the netcat listener) and it works  fine.

What I need to do is write a sketch which automatically connects to the netcat socket and reads/writes the Ethernet packets.


I use netcat because I read that it has less overhead, if there is a better way, I would like to know.

This is my code:

Code: [Select]
#include <Process.h>

Process p;

void setup() {
  Bridge.begin();
  SerialUSB.begin(9600);

  while (!SerialUSB);

 
  p.begin("nc");
  p.addParameter("192.168.1.15 9911");
  p.runAsynchronously();
}

void loop() {
    while (p.available() > 0) {
    char c = p.read();
    SerialUSB.print(c);

  }
 
    SerialUSB.flush();
}



Executing through SSH the command "nc 192.168.1.15 9911" works fine, but it doesn't work on the above code.

Thanks.

Juraj


BrainTrance

why not use BridgeUdp?
I searched for it but I found very little examples of implementations. Actually just one. Have you ever used it?

Juraj

I searched for it but I found very little examples of implementations. Actually just one. Have you ever used it?
try the example from Ethernet library. use BridgeUdp instead of EthernetUdp. (both are implementation of interace UDP)

BrainTrance

Is that what you're talking about? If yes, it expects an arduino ethernet shield, if it doesn't detect one, it stops the program.

Code: [Select]
#include <Ethernet.h>
#include <BridgeUdp.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 1, 15);

unsigned int localPort = 9911;


char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer[] = "acknowledged";

EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // start the Ethernet
  Ethernet.begin(mac, ip);

  Serial.begin(9600);
  while (!Serial) {}

  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);
}

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());

   
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

   
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

Juraj

Bridge is another implementation of Arduino networking. so remove Ethernet.h and Ethernet initialization. and replace EthernetUdp with BridgeUdp

Go Up