How do I send UDP messages ?

I want to report the status of some binary sensors to a PC over the local LAN with a UDP packet whenever there is a change. My code starts like the example, but always gets “connection failed”. Wireshark, the Ethernet packet display routine shows 1 packet going to and 1 from the PC, but they are type=TCP, not UDP. Is there a way to get the Arduino to send UDP ?

#include <Ethernet.h>

#define lightning 2
#define driveway 3
#define garage 4
#define shop 5

byte status = 0b0;
byte prvstat = 0b0;

byte Ardmac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte Ard_IP = { 192,168,123,73 }; // the Arduino
byte Server = { 192,168,123,70 }; // the PC

Client client(Server, 8888);

void setup()
pinMode(lightning, INPUT); // set pins as input
pinMode(driveway, INPUT);
pinMode(garage, INPUT);
pinMode(shop, INPUT);



if (client.connect())
Serial.println(“connection failed”);

void loop()

I don't think the Arduino ethernet library supports UDP.

I'm pretty sure the underlying hardware does, though, so maybe we'll see that capability in the future.


look at the Arduino Developers mailing list Bjorn Hartmann published a modified version that supports UDP

Ah, yes !

Answer are here

I am also interested in developing something using UDP. I downloaded the UDP library made by Bjorn Hartmann and placed it under C: \arduino-0012\hardware\libraries\UDP folder.

I wrote some test code but now I get a ton of errors like"UdpBytewise.cpp:30:19: error: types.h: No such file or directory" when I compile.

What am I doing wrong? And sorry if this is some basic question but I am just getting started with Arduino.


Luca, were you able to get the examples UDP sketch to compile?

I just downloaded the files and they compile ok under 0012.


Here is an example that works, but the BTEST3.EXE is a HASKELL program I did not write and do not have the source code. It just showed the byte received.

// Basement01
// Code for Arduino Duemilanove - 2009-01-26
// Watch 5v/gnd sensors and send UDP packet to basement PC if there is a status change
// To test - run BTEST3.EXE on a PC on the same LAN

#include <Ethernet.h>
#include <UdpRaw.h>

#define DRIVEWAY_PIN 4
#define SHOP_DOOR_PIN 6

byte my_mac = { 0xD1,0xD2,0xD3,0xD4,0xD5,0xD6 };
byte my_ip = { 192,168,123,13 };
byte server_ip = { 192,168,123,70 };
uint16_t targetPort = 8888;
uint8_t previous_status = 0;
long previous_millis = 0;
long debounce_millis = 30;

void setup()

Ethernet.begin(my_mac, my_ip);

// Serial.begin(9600);
// Serial.println(“Initialization complete”);

void loop()
uint8_t status;
uint16_t retval;

status = read_pins();

// check for status change - debounce sensors
if ((status != previous_status) && (millis() - previous_millis > debounce_millis))
// Serial.print("sending "); // so we know how many and what was sent
// Serial.println(status);

retval = UdpRaw.sendPacket(&status, 1, server_ip, targetPort); // send new status byte
if (retval != 1)
Serial.print("sendPacket returned ");
Serial.println(retval, DEC);
previous_status = status; // save the status for compares
previous_millis = millis();

uint8_t read_pins()
read_pin(LIGHTNING_PIN) |
read_pin(DRIVEWAY_PIN) |
read_pin(GARAGE_DOOR_PIN) |

uint8_t read_pin(int n)
return (digitalRead(n) == HIGH) ? (1 << n) : 0;

Mem, Unfortunately not. I get the same errors when I try to compile the examples as well.

these are some of the errors: UdpBytewise.cpp:30:19: error: types.h: No such file or directory UdpBytewise.cpp:31:19: error: w5100.h: No such file or directory UdpBytewise.cpp:32:20: error: socket.h: No such file or directory UdpBytewise.cpp: In member function 'void UdpBytewiseClass::begin(uint16_t)': UdpBytewise.cpp:46: error: 'Sn_MR_UDP' was not declared in this scope UdpBytewise.cpp:46: error: 'socket' was not declared in this scope

The weirdest thing is that there must be something broken with the developing environment (012) because I get the same messages when I try to compile any old application. I must have done something wrong when I copied the libraries.

I simply downloaded the zip file from Haskel and create a UDP folder under "libraries". Is this the way is supposed to be done?

I guess I don’t understand how the dev. environment works.
Now when I open any old program I get
#include <UdpBytewise.h>
#include <UdpRaw.h>
#include <UdpString.h>
at the top even if those libraries were not included in the original work.
What happens when I select “import library”? I thought it would simply add the #include in the code but evidently is also adding something else in the background.
How do I “un-import” a library and remove it completely so that I can start from a clean environment?


You should have a directory called ethernet in the libraries directory. Ethernet should contain the files distributed with 0012:

Plus .o files for each of the .cpp files

The UDP files should have been added to this same directory, so you should also have:

The readme for the UDP files warns about errors if the UDPStrings files are compiled at the same time so these should be moved to another directory (see the readme file for more info on this)

If your file structure doesn’t look like this then you probably copied the udp files to the wrong place.

Thanks mem it works now. I copied the UDPRaw files under "Ethernet" directory and then deleted the "UDP" directory I created.

Thanks again!

Good to hear you have it going, have fun!

I am finally able to send UDP packets using the Bjorn library :-)

I am using wireshark to see what I am sending out.

How can I send out Hex values using UdpBytewise.print?

I was able to send one character at a time by doing UdpBytewise.print(01, BYTE); but how do I send a full string of HEX characters like 12 a7 00 a1 00 0f ?


Hi Dogsled,

I can't find the file BTEST3.EXE? Where can I get or download?

Thanks in advance

Is anything still happening with this?

I can’t get anywhere with these libraries using IDE V 15 -18, just get a mass of compilation errors :frowning:

@Folderol, the UdpBytewise files (.cpp and .h) need to be in the Ethernet directory, the same directory as the distributed Ethernet library files (not the user libraries directory).

If you still get errors, perhaps you could say what sketch you are compiling and the OS you use and what the errors are.

I had placed the files there. The sketch I wanted to compile was the DHCP server one.

I got a huge long list of errors just starting up the IDE (before loading the sketch) saying there were problems with the library itself - removing the files again restored things to normal.

I'm currently using version 0018 of the IDE on linux-debian (squeeze).

I'll have another try later tonight if I get time.

It may help if you post the first half dozen errors.

Perhaps someone using UdpBytewise with debian can comment.

OK, I just started from scratch again. I can open the IDE without problems (don't know what I did wrong before). However, when I try to compile the DHCP server sketch I get the following (there is a lot more!)

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:20: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:22: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:39:3: error: invalid preprocessing directive #issues

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:48: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:49: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:50: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:51: error: stray '$' in program

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:124:148: error: invalid digit "9" in octal constant

/home/will/LocalProgs/Arduino-0018/libraries/Ethernet/UdpRaw.cpp:132: error: stray '$' in program

Looking at the library file, I'm wondering if it is corrupted. It looks rather strange. Has anyone got a set that the know definitely works?

Have you tried copying the library again? Post the UdpRaw.cpp file you have. Looking at the code (, there does not seem to be anything that is consistent with the error messages you posted.