send UDP message fron Arduino to PC or other Arduino

I try to send a UDP message from my Arduino UNO to my PC but my sketch don’t work and i don’t understand why…

I have UDP TEST TOOL software on windows and i receive udp message from my phone and i can send udp message to my Arduino perfectly but i can’t send UDP message from Arduino to PC
(this is a test, my idea is to send message from an Arduino to an other Arduino) specifying Arduino and Pc are connected to the same router

This is my sketch…

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

byte mac = { 0xD5, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip = { 192, 168, 1, 177 };

unsigned int localPort = 7777;

byte remote_ip = { 192, 168, 1, 100 };

unsigned int remote_port = 12345;

EthernetUDP Udp;

void setup()
{

Ethernet.begin(mac,ip);

Udp.begin(localPort);

}

void loop()
{

Udp.beginPacket(remote_ip,remote_port);
Udp.write(“HELLO”);
Udp.endPacket();

}

Thanks

When you specify your Arduino's IP address (192.168.1.177) the Gateway (default router) is assumed to be 192.168.1.1. Is that the address of your router? If not you will have to also specify the DNS Server and Gateway addresses in Ethernet.begin().

well if his arduino and pc are on the same subnet you dont go trough your gateway.
And for a direct sending based on a remote ip adres, you wont require DNS
DNS is only used to convert readable names to ip numbers, but if you allready have the remote ip adres of your pc, you dont need it.

Not sure how you got the code, was it sample, in that case your ip adres might be wrong not for your network.

if his pc is windows just type ipconfig /all to see the ip info
to keep it simple if you see something like 192.168.13.56 then only change the last number and use that in your arduino
ofcourse for the talk back change the remote ip to send to to the number you got from ipconfig /all

also you might try basic network trouble shooting like ping check cables etc halfduplex / full duplex / check speed of your network too 100mbit / 10Gbit / etc..

btw some modern routers create ip6 networks (they dont work with 4 numbers for an ip address), be sure to get a ip4 address too.
if both arduino and pc are on wireless more might be required to comunicate (SID / security handshakes wireless login, etc etc)

i try this sketch… now receive some data (example Aæ?%Õ¢ô ) but is not what i want… i want to receive P6H… I think the problem is to set corectly the CHAR variable

this part…char packBuff[UDP_TX_PACKET_MAX_SIZE]; …and this part… Udp.write(packBuff[0] = ‘P’ && packBuff[1]==‘6’ && packBuff[2]==‘H’);

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

byte mac = { 0xD5, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168,1, 177 };

unsigned int localPort = 8080;

byte remote_ip = { 192, 168,1, 100 };

unsigned int remote_port = 6882;

char packBuff[UDP_TX_PACKET_MAX_SIZE];

EthernetUDP Udp;

void setup() {

Ethernet.begin(mac,ip);

Udp.begin(localPort);

}

void loop() {

Udp.beginPacket(remote_ip,remote_port);

Udp.write(packBuff[0] = ‘P’ && packBuff[1]==‘6’ && packBuff[2]==‘H’);

Udp.endPacket();

delay (1000);

}

Have you tried something like this?

void loop() { 
  packBuff[0] = 'P';
  packBuff[1] = '6';
  packBuff[2] = 'H';
  packBuff[3] = 0;

  Udp.beginPacket(remote_ip,remote_port);
  Udp.write(packBuff);
  Udp.endPacket();
  
  delay (1000);
}

don’t work :~

i try to send UDP message from Iphone App Ardumote to PC and work perfectly as you can see from the image attached…display data ASCII P7H and Received HEX Data Log {192.168.1.102} 50 37 48

50 37 48 is P7H…is true?

i would like to see the Ardumote code but I do not know how to do it

I normally ask this first when UDP doesn’t work. Does your shield have a microSD card slot? Do you have a microSD card in the slot?

for debugging what goes over your network use a real network sniffer, like etherreal or netmon (both free).
its the the preferred way, so you can see what realy is beeing send from the arduino and to compare it to other sending devices
because you would see their datagrams over the wire / or wifi..

then you will see where your wrong data comes from and can figure out how to put the right data there.

Now work perfectly…this is the sketch thank you all :slight_smile:

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

byte mac = { 0x98, 0xA3, 0xDB, 0x0D, 0x66, 0x7C };
byte ip = { 192, 168,1, 177 };

unsigned int localPort = 7777;

byte remote_ip = { 192, 168,1, 100 };

unsigned int remote_port = 7777;

char DataOut = “P6H”;

EthernetUDP Udp;

void setup() {

Ethernet.begin(mac,ip);

Udp.begin(localPort);

}

void loop() {

Udp.beginPacket(remote_ip,remote_port);
Udp.write(DataOut);
Udp.endPacket();
delay(2000);
}