Udp.write problems with hex /dec / ascii and it not Producing the right result

I've spent over a week trawling the forum and other website looking at Udp / Serial write examples and am really starting to tear my hair out now..

I want to post 3 bytes to a broadcast address, the bradcast bit works but the bytes are all wrong!

void loop()
{
    Udp.beginPacket(UDPServer,(8888));
    Udp.write(0x00);
    Udp.write(0x01);
    Udp.write(0x02);
     Udp.endPacket();
 delay(2000);
}

which should produce a 3 byte word of the above values but instead I get a hex broadcast of:
0xEF 0xBF 0xBD

why?

Try this:

void loop()
{
   byte sendBuffer[3] = {0x00,0x01,0x02};
 
   Udp.beginPacket(UDPServer,(8888));
   Udp.write(sendBuffer,3);
   Udp.endPacket();
   delay(2000);
}

I tried the code but the broadcast response is now blank? I can see the broadcast packet but it is empty?

Full code:

#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
 
// Attempt to automatically configure everything. Set a default MAC
// address to be replaced later, and get the IP address from DHCP
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // Try to replace with DS MAC
//udp port
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
EthernetUDP Udp;
//setup pins for leds and relays and switches
 

// Initialise a DS18B20 temperature sensor
OneWire ds(9); // Set up DS18B20 sensor on pin D9

IPAddress UDPServer(255, 255, 255, 255); // destination device server

/***********************************************************************
******************************* Setup **********************************
***********************************************************************/
void setup()
{
 byte dsAddress[8];
  delay( 500 ); // Give the Ethernet chipset time to fully initialise
 
  ds.reset_search();  // Start the search with the first device
 
    // Offset array to skip DS18B20 family code, and skip mac[0]
    mac[1] = dsAddress[3];
    mac[2] = dsAddress[4];
    mac[3] = dsAddress[5];
    mac[4] = dsAddress[6];
    mac[5] = dsAddress[7];
 
  
  Ethernet.begin( mac ); // mac address is derived fomr the DS18B20 chip address.
  Udp.begin(localPort);


delay(200); // delay to allow the ethernet to setup

    Udp.beginPacket(UDPServer,(8888));
    Udp.print("I am Awake");
    Udp.endPacket();
    
}


/***********************************************************************
******************************* LOOP ***********************************
***********************************************************************/


void loop()
{
   byte sendBuffer[3] = {0x00,0x01,0x02};
 
   Udp.beginPacket(UDPServer,(8888));
   Udp.write(sendBuffer,3);
   Udp.endPacket();
   delay(2000);
}
  Ethernet.begin( mac ); // mac address is derived fomr the DS18B20 chip address.

Why? The MAC address should be that of the Ethernet shield, not related to whatever is connected to the Arduino.

If the address of the DS chip is relevant to whatever you are sending data to, include it in the data you are sending.

    Udp.beginPacket(UDPServer,(8888));

(The) (parentheses) (around) (the) (8888) (are) (for) (?)

I tried the code but the broadcast response is now blank? I can see the broadcast packet but it is empty?

Is the packet empty? Or is the response empty? Details matter.

Thankyou for your reply,

MAC - to derive a different Mac for each device without having to re-code each one i use the 1 wire device to provide a unique address - I then use the second portion of this for the MAC address.

example taken from : Setting an Arduino Ethernet MAC address using a DS18B20 | Freetronics

(parentheses) - the example I used to derive the code used the parentheses and I left them in, no error was returned to my Udp.print statements in testing which worked as expected without error - I have now removed them and started a retest - it made no difference to the outcome.

Udp.print("some stuff") works.
Udp.write(0xFF) dosen't work.

I am using a multicast testing app on android to see the packets on the network - the app shows a packet is received but it has no content (Response was a poor wording - my mistake) I believe this to be an empty packet?

Any pointers greatly appreciated, Stuart.

I am using a multicast testing app on android to see the packets on the network

Does that app expect the packets to contain binary data?

Try making the binary packet contain {0x41, 0x42, 0x43}, instead of {0x00, 0x01, 0x02}.

The app reads Hex or Ascii

When I use 0x41, 0x42, 0x43 I see 0x41, 0x42, 0x43 in the app.. or if i switch to Ascii I see A,B,C as I would expect too..

Any idea why sendBuffer[3] = {0x00, 0x01, 0x02} would not yield the same result?

Any idea why sendBuffer[3] = {0x00, 0x01, 0x02} would not yield the same result?

Before I try to guess, what happens if the array contains 0x02, 0x01, 0x00, instead?

This still displays a blank response.

I expanded on this by trying:

byte sending = 0x00;


void loop()
{
   Udp.beginPacket(UDPServer,8888);
   Udp.write(sending);
   Udp.endPacket();
   delay(1000);
   sending = sending + 1;
}

which produces lots of blank packets until ! $ % & etc.. which are addresses 0x21 onwards..

so maybe there is a problem with the app - multicast tester in that is will not print the HEX addresses for char that can not be displayed?

How are you trying to display them on the receiving end?
Printable and non-printable characters

The App has a setting to 'Receive in Hexadecimal' which then displays the Hex rather than the Ascii, I've asked the developer if the app has a limitation in terms of received addresses.

https://mitchtalmadge.com/project/multicast-tester-android-app/

Can you recommend any software that will display the raw multicast data?

stuart.

Sounds like the receiving app is having the problem. I send and receive non-printable data using UDP without problems.

What operating system are you using?

The app is on android, but I also have a win7 machine for testing if you are able to recommend an app?

the next step is reading the UDP packets on the 2nd arduino....

I don't have an app for android. Why don't you set up the 2nd Arduino as the receiver? I use a RPi as the test device. I have C code for testing UDP on it.

The postman hasn't delivered my W5100 shield yet :~(

Ok.. So,
Having spoken to the developer of the testing app there is a bug that shows the wrong ASCII codes / no codes at all... this has now been fixed and an update released :slight_smile:
and with help from this thread I'm passing the correct bytes into the write statement and all is working as expected

thank you all!