Ethernet UDP communication - sending issue

Hello all!
I got an Arduino Uno and I put an ethernet shield on top of it. I have a device which has many sensors inside and its protocol is set up such that when this string: A5 FA 01 01 00 A1 is sent to port 2021 of this device, it will give me a lot of sensor data. Something like this:

I also have figured out the mac and IP addresses of the Arduino and my device so that bit works.
I'm having trouble modifying the UDPSendReceiveString code that Arduino provides for UDP communication such that it'd work for my project. I basically want to modify this code so that my Arduino sends this string "A5 FA 01 01 00 A1" and then receives the data, sends it to a minimal browsing interface like Adafruit has instructed here, and ALSO stores it to the microSD card inserted. The main part I'm struggling with is sending the packet and storing to SD card. I've already formatted and tested the SD card an the browsing interface based on the Adafruit tutorial I mentioned above.

Any tips for me to integrate all the things I mentioned and make it work?
I'd really appreciate any help :slight_smile:

sending your request would be something like

const byte readSensorsRequest[]  = {0xA5, 0xFA, 0x01, 0x01, 0x00, 0xA1};

Udp.beginPacket(udpIP, udpPort); // udpIP, udpPort to be defined for your environment 
Udp.write(readSensorsRequest, sizeof readSensorsRequest);
Udp.endPacket();

the example you linked to shows how to get the answer in a buffer

then it's just a matter of dumping that buffer to your SD file

If the ip address of the device I want to send to is 192.168.1.211 and the port is 2021, how do I write that?
I tried:


  const byte packet[]  = {0xA5, 0xFA, 0x01, 0x01, 0x00, 0xA1};

  String ip       = "192.168.1.211"; 
  int port        = 2021;

  udp.beginPacket( ip, port);
  udp.send (packet, ip, port );   
  udp.endPacket();

but it doesn't compile.

And then I tried what you wrote with Udp.remoteIP() and Udp.remotePort() and it does compile but then I dont know what IP address it's sending to. Is it going to look for a dynamic IP? Because if I write the code below, then I haven't specified the device's IP anywhere on the code.

const byte packet[]  = {0xA5, 0xFA, 0x01, 0x01, 0x00, 0xA1};
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 
Udp.write(packet, sizeof packet);
Udp.endPacket();

This is what I got so far:

/*
 * 
 * MODIFIED VERSION OF UDPSendReceiveString from https://create.arduino.cc/example/library/ethernet_2_0_0/ethernet_2_0_0%5Cexamples%5CUDPSendReceiveString/UDPSendReceiveString/preview
 *
 * For this project, this code is to communicate between an arduino (with ethernet shield attached) and a "device" that has many sensors inside and a communication protocol
 * when the string "A5 FA 01 01 00 A1" is sent to device's port 2021, it will give the required data.
 * This sending & receiving action between the arduino and the device is to be repeated every 10 seconds as it will be integrated or called upon by another "Master Code" that includes other commands.
 * 
 */

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


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x88, 0xED }; //MAC address of Arduino as set by manufacturer (does not conflict with another device's MAC on my network)
IPAddress ip(192, 168, 1, 201); //IP address of arduino on my network



unsigned int localPort = 8888;      // local port to listen on

// 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 -NOT NECESSARY B/C THE DEVICE IS NOT AN ARDUINO OR SIMILAR

// An EthernetUDP instance to let us send and receive packets over UDP
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);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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.");
  }

  // start UDP
  Udp.begin(localPort);
}

void loop() {

//SENDING
const byte packet[]  = {0xA5, 0xFA, 0x01, 0x01, 0x00, 0xA1};
//the STATUS PACKET REQUEST 

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 
    
Udp.write(packet, sizeof packet);
//OR -> Udp.send (packet, ip, port);
//Udp.beginPacket(udpIP, udpPort); // udpIP, udpPort to be defined for your environment 

Udp.endPacket();



//RECEIVING (this part makes sense!)
  // 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 (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 <<<<<<<<<<<<<<< NOT NECESSARY
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
*/

  }
  delay(10);
}


/*
  Processing sketch to run with this example <<<<<<<<<<<<<<<<< where should I post this?
 =====================================================

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


 import hypermedia.net.*; <<<<<<<<<<<<<<<< DOESN'T EXIST ANYMORE? Bruh :(

 UDP udp;  // define the UDP object <<<<<<<<<<<<<<<<<<<< ALREADY HAVE ABOVE AS "EthernetUDP = Udp"

 void setup() { <<<<<<<<<<<<<<<< should I put this in the setup() above?
 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() //<<<<<<<<<<<<<<<<<<< wut?
 {
 }

 void keyPressed() {
 String ip       = "192.168.1.150";	// the remote IP address <<<<<<<<<<<<<<<<<<< MUST BE CHANGED TO THE SYSTEM IP THAT IS BEING TESTED
 int port        = 2021;		// the destination port <<<<<<<<<<<<<<<<<<<<<<< Wayne said it must be 2021!

  
 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(int i=0; i < data.length; i++)
 print(char(data[i]));
 println();
 }
 */