# Photon can send but not recieve udp packets

**URL:** <https://community.particle.io/t/photon-can-send-but-not-recieve-udp-packets/51250>\
**Category:** Troubleshooting\
**Tags:** photon\
**Created:** [July 18, 2019, 4:49pm UTC](https://community.particle.io/t/photon-can-send-but-not-recieve-udp-packets/51250 "2019-07-18T16:49:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bean](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bean](https://community.particle.io/u/bean)\
**Post date:** [July 18, 2019, 4:49pm UTC](https://community.particle.io/t/photon-can-send-but-not-recieve-udp-packets/51250/1 "2019-07-18T16:49:59Z")

</div>

I’m trying to use a Node.js server with a photon running a UDP client to send messages back and forth. It doesn’t look like the photon is receiving the messages because it doesn’t respond and it’s supposed to publish something to the particle device console which it doesn’t do either.

Photon code:

```auto
UDP Udp;

IPAddress nodeServer(172,20,10,2);
int port = 8888;

void setup() {
    Udp.begin(port);
    
    while (!WiFi.ready()) {
	    delay(10);
	    Particle.process();
	    if (WiFi.ready()) {
	        break;
	    }
	}
	
	Particle.publish("wifi ready");
}

void loop() {
    if (Udp.parsePacket() > 0) {
        
        Particle.publish("we got a udp");
        
        char data[50];
        int c = 0;
        
        while (Udp.available()) { // read data sent into data array
            data[c] = Udp.read();
            c++;
        }
        
        char cmd[5];

        for (int i = 0; i < strlen(data); i++) {
            cmd[i] = data[i];
        }
        
        if (strcmp(cmd, "ping") == 0) {
            Udp.sendPacket("got ping", 8, nodeServer, port);
        }
    }
}

```

nodejs server to receive data:

```auto
var PORT = 8888;
var HOST = '172.20.10.2'; 

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('listening', function() {
    var address = server.address();
    console.log('UDP server listening on ' + address.address + ':' + address.port);
});

server.on('message', function(message, remote) {
    console.log(remote.address + ':' + remote.port + ' - ' + message);
})

server.bind(PORT, HOST);

```

nodejs to send the initial ping

```auto
var PORT = 8888;
var HOST = '172.20.10.3';

var dgram = require('dgram');
var message = 'ping';

var client = dgram.createSocket('udp4');

client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    client.close();
});

```

I’m not sure if the particle.publish statements aren’t working or the code isn’t running or the particle device console is just broken because I don’t get _any_ output, even the messages that come after a successful flash. I can still ping the photon from the cloud and it says it’s online, and the LED is breathing cyan, but no events.

I can ping the photon by running `ping 172.20.10.3` in my terminal and that works, and I know I can send data off of the photon using UDP because I have a program that does that. I tested it after I started getting this issue and it still works, so I figure it must be sending data to the photon that’s the issue.

I tried changing the port from 33333 to 8888, but neither worked. Any ideas?

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![bean](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bean](https://community.particle.io/u/bean)\
**Post date:** [July 18, 2019, 5:17pm UTC](https://community.particle.io/t/photon-can-send-but-not-recieve-udp-packets/51250/2 "2019-07-18T17:17:07Z")

</div>

update: I installed wireshark and looked at the UDP packets being sent. I can see the packets going from my computer to the photon, and when I run the working code that sends packets from the photon I can see those as well, but the photon never sends anything back.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 18, 2019, 7:34pm UTC](https://community.particle.io/t/photon-can-send-but-not-recieve-udp-packets/51250/3 "2019-07-18T19:34:32Z")

</div>

Although I don't quite see why this question could not have been added to your [other thread](https://community.particle.io/t/photon-doesnt-send-udp-packages-to-nodejs-server/50856), one thing I noticed immediately

You are sending via

```java
client.send(message, 0, message.length, PORT, HOST, function(err, bytes)

```

_(`message.length` of `var message = 'ping'` will be `4`)_

and then copy the incoming packet (which is _only four_ bytes long as the zero-terminator is not sent) like that

```cpp
        for (int i = 0; i < strlen(data); i++) {
            cmd[i] = data[i];
        }

```

So even _if_ `data` was containing the zero-terminator (which it most likely won't due to lacking initialisation) you'd only copy _ **four** _ characters (again without the zero-terminator) and hence

```cpp
        if (strcmp(cmd, "ping") == 0) {

```

will most likely _not_ signal equality.

Some basic debugging steps (like adding `Serial.println(data)` and `Serial.println(cmd)`) should help testing this hypothesis in no time (and wouldn't be too much to ask for _before_ consulting others).

BTW, failing to initialise local variables was also a point in this thread of yours

> [@Photon udp sends "�" characters at the start and end of a file](https://community.particle.io/t/photon-udp-sends-characters-at-the-start-and-end-of-a-file/51214):
>
> I’m trying to write a program to read a file off of a photon board and send it to a node.js server on a computer, which then saves it to a file. Here’s my photon code: #include \<SdFat.h\> #include "SdFat.h" #include \<string.h\> #define SD\_CS\_PIN SS UDP Udp; SdFat SD; SdFile myFile; IPAddress nodeServer(172,20,10,2); int port = 33333; char filename[20] = "sensor0000.TXT"; int counter; char data[100]; void setup() { if (!SD.begin(SD\_CS\_PIN)) { Serial.println("initialization…
