UDP.write() errors

I have a program that needs to output data over ethernet using UDP.write(). The data consists of a variety of variable types, and I am getting a lot of errors about the types. The code block that is giving me trouble is below.

UDP.write("Art-Net");			// packet descriptors
UDP.write(0x00);
UDP.write(0x00, 0x21);			// op-code
UDP.write(IP);				// IP address
UDP.write(0x36, 0x19);			// port
UDP.write(0x00, 0x01);			// firmware version
UDP.write(0x00, 0x00);			// net, subnet, universe
UDP.write(0x03, 0x62);			// OEM code
UDP.write(0x00);			// UBEA version
UDP.write(0xD0);			// status
UDP.write("AV");			// manufacturer code
UDP.write("4-port node      ",	 0x00);	// short name
UDP.write("                                                                ");	// long name
UDP.write("#", 0x00, 0x01, " [" + artPollCount, "]                                                      ");	// node report
UDP.write(0x00, 0x04);			// number of ports
UDP.write(0x80, 0x80, 0x80, 0x80);	// port modes
UDP.write(0x08, 0x08, 0x08, 0x08);	// inputs
UDP.write(0x80, 0x80, 0x80, 0x80);	// outputs
UDP.write(0x00, 0x00, 0x00, 0x00);	// input universes
UDP.write(0x00, 0x01, 0x02, 0x03);	// output universes
UDP.write(0x00, 0x00, 0x00);		// video, macro, remote
UDP.write(0x00, 0x00, 0x00);		// spares
UDP.write(0x00);			// device type
UDP.write(MAC);				// MAC address
UDP.write(0x00, 0x00, 0x00, 0x00, 0x00);// binding
UDP.write(0x08);			// addressing
UDP.write(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);	// filler

The error messages generated by the compiler are below.

[/size]Artnet_to_DMX.cpp: In function ‘void loop()’:
Artnet_to_DMX:72: error: call of overloaded ‘write(int)’ is ambiguous
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:75: error: invalid conversion from ‘int’ to ‘const uint8_t*’
Artnet_to_DMX:75: error: initializing argument 1 of ‘virtual size_t EthernetUDP::write(const uint8_t*, size_t)’
Artnet_to_DMX:78: error: invalid conversion from ‘int’ to ‘const uint8_t*’
Artnet_to_DMX:78: error: initializing argument 1 of ‘virtual size_t EthernetUDP::write(const uint8_t*, size_t)’
Artnet_to_DMX:79: error: call of overloaded ‘write(int)’ is ambiguous
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:82: error: invalid conversion from ‘const char*’ to ‘const uint8_t*’
Artnet_to_DMX:82: error: initializing argument 1 of ‘virtual size_t EthernetUDP::write(const uint8_t*, size_t)’
Artnet_to_DMX:84: error: no matching function for call to ‘EthernetUDP::write(const char [2], int, int, const char*, const char [56])’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:86: error: no matching function for call to ‘EthernetUDP::write(int, int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:87: error: no matching function for call to ‘EthernetUDP::write(int, int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:88: error: no matching function for call to ‘EthernetUDP::write(int, int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:89: error: no matching function for call to ‘EthernetUDP::write(int, int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:90: error: no matching function for call to ‘EthernetUDP::write(int, int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:91: error: no matching function for call to ‘EthernetUDP::write(int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
Artnet_to_DMX:92: error: no matching function for call to ‘EthernetUDP::write(int, int, int)’
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:69: note: candidates are: virtual size_t EthernetUDP::write(uint8_t)
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:71: note: virtual size_t EthernetUDP::write(const uint8_t*, size_t)

It looks like I am trying to pass an incorrect data type to write(), but I don’t know how to fix it. I’m sure it’s fairly simple, but I’m pretty new to c++.

UDP is not like Serial. You cannot treat it like a stream like that.

I'm assuming you use EthernetUdp.h from Arduino 1.0.

First, you have to wrap all your write() calls inside a single beginPacket() and endPacket() pair.

I also don't understand why you put an IP address and port number in the packet. This will make your network protocol not work properly through NAT gateways. The recipient of your datagram will be able to tell where it came from simply by using recvfrom(), so you don't need to include your own address.

Second, you can add data (more than once) in one of two formats:

  • as an unsigned char (uint8_t, or byte)
  • as a buffer (uint8_t *, length)

Sadly, this function requires a uint8_t, rather than the more common void*, so you have to use casting.

To add two bytes:
UDP.write(0x10); UDP.write(0x20);

To add a string:
UDP.write((uint8_t *)"hello, world!", 14);

You may also find strlen() handy to know how long a string is, rather than hard-coding it. Just make sure you know how to decode it on the other side!