# Can you increase the size of a UDP packet?

**URL:** <https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267>\
**Category:** Firmware\
**Created:** [May 21, 2020, 7:43am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267 "2020-05-21T07:43:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 7:43am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/1 "2020-05-21T07:43:49Z")

</div>

Using a photon/argon…

On the photon, it looks like I am limited to a 512 byte UDP packet. Is this a UDP limitation? Or can I increase the size of this packet?

I am using udp.write(String);

If this requires using udp.write(const uint8\_t buffer, int size) I could use some help translating a String to the uint8\_t array (I can’t seem to figure that out). But I am still not sure that would work to get \>512 bytes.

Thanks!

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 10:16am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/3 "2020-05-21T10:16:24Z")

</div>

My mistake… the extra bytes appear to be header/metadata I was seeing with Wireshark.

So it looks like my data payload is limited to 512, and I tested that with:

`for (int i = 0; i<1024;i++){ udp.write("X");}`

---

<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:** [May 21, 2020, 10:26am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/4 "2020-05-21T10:26:47Z")

</div>

> [@jed](#):
>
> I could use some help translating a String to the uint8\_t array (I can’t seem to figure that out).

This might help  
[https://docs.particle.io/reference/device-os/firmware/photon/#tochararray-](https://docs.particle.io/reference/device-os/firmware/photon/#tochararray-)

* * *

_(Update: The following section missed the point that `upd.write()` calls need to be enclosed with `udp.beginPacket()` and `udp.endPacket()` to actually send the data - hence I've hidden it)_

> **hidden suggestions**
>
> But you don't even need to \*copy\* the string, you can just \*take\* the string as is and use this ```cpp const uint8_t* buf = (const uint8_t*)yourString.c_str(); int len = yourString.length(); udp.write(buf; len % 512); // first packet shorter to have the rest exactly 512 for (int i = len % 512; i \< yourString.length(); i += 512) { udp.write(&buf[i], 512); ``` \*(this is not tested, so you may need some type casts here or there ;-) )\*
> 
> Or you use the return value of `udp.write()` like this
> 
> ```cpp
> const uint8_t* buf = (const uint8_t*)yourString.c_str();
> int sent = 0;
> while(sent < strlen((const char*)buf)) 
> sent += udp.write(&buf[sent], strlen((const char*)&buf[sent])); // try sending rest of string and accumulate the number of actually sent bytes
> 
> ```
> 
> and many more possible approaches 😉

But be aware UDP does not guarantee that all packets are actually delivered.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 10:41am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/5 "2020-05-21T10:41:39Z")

</div>

Thank you once again. But why would that work, when I tried calling udp.write() in a loop 1024 times? I also tried to split my string up into smaller portions and write multiple times, in writes \< 512.

Oh… is this because the udp buffer is filling up, and I am trying to write to it too quickly?

And yes, I am aware that not all packets will arrive. The idea here is to have a remote sensor broadcasting its live data to the subnet. That part is working fine.

But what I thought would be a good idea would be to have the sensor buffer a good amount of data, and send that in the same packet as the latest data (say, 1024 samples). I am sending this out as formatted JSON, so the packet would be a couple kilobytes.

---

<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:** [May 21, 2020, 10:45am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/6 "2020-05-21T10:45:17Z")

</div>

> [@jed](#):
>
> I am sending this out as formatted JSON, so the packet would be a couple kilobytes.

When using UDP only in your self controlled environment I'd suggest you stick with a binary format rather than inflating the data with not extra value.  
Keeps things shorter and also faster.

> [@jed](#):
>
> But why would that work, when I tried calling udp.write() in a loop 1024 times? I also tried to split my string up into smaller portions and write multiple times, in writes \< 512.

And what were the results of that?

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 10:48am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/7 "2020-05-21T10:48:32Z")

</div>

I tested with this:  
`for (int i = 0; i<1024;i++){ udp.write("X");}`

And my packet data is still cut off at 512 bytes.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 10:52am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/8 "2020-05-21T10:52:32Z")

</div>

I also just tried small delays in between those individual writes (10us, 100us, 1ms) and still cut off at 512.

---

<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:** [May 21, 2020, 11:06am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/9 "2020-05-21T11:06:40Z")

</div>

Are you running `SYSTEM_THREAD(ENABLED)` - if not, try that.  
You can also try adding a `Particle.process()` in your loop to force the network stack to engage (although I’d think that shouldn’t be required).

BTW, I just stumbled over this (again 😊)  
[https://docs.particle.io/reference/device-os/firmware/photon/#write--5](https://docs.particle.io/reference/device-os/firmware/photon/#write--5)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/1/2/128267ca902f1e7fd1d7b2087ee145b2a4796a47.png)  
So my proposed “solutions” above won’t work either.

Alternatively you can use [`udp.sendPacket()`](https://docs.particle.io/reference/device-os/firmware/photon/#sendpacket-) to send unbuffered or use [`udp.setBuffer()`](https://docs.particle.io/reference/device-os/firmware/photon/#setbuffer-) to donate a bigger buffer.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 11:12am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/10 "2020-05-21T11:12:36Z")

</div>

With SYSTEM\_THREAD(ENABLED), I don’t see any packets at all. IT’s breathing cyan, but wireshark sees nothing. If I recompile witih that commented out, I get packets.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 11:13am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/11 "2020-05-21T11:13:33Z")

</div>

I’ll see if I can set the buffer. Maybe that’s it.

---

<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:** [May 21, 2020, 11:14am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/12 "2020-05-21T11:14:08Z")

</div>

> [@jed](#):
>
> With SYSTEM\_THREAD(ENABLED), I don’t see any packets at all.

Have you made sure to adhere to this?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/1/5/151c0b487482c65b36795fa438fe1b6186df5f08.png)

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 11:17am UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/13 "2020-05-21T11:17:30Z")

</div>

Dammit!!! Thank you! I read through all that last week, and of course didn’t think to refresh now that I’m knee deep.

The buffer worked. I guess you can only fill the buffer once per packet. Makes sense, I suppose… you can’t send a packet in pieces from app code, defeats the purpose, no?

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 5:02pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/14 "2020-05-21T17:02:31Z")

</div>

OK. So now…

I woke up this morning and there are no UDP packets. I suspect that the device disconnected from WiFi at some point.

How can I detect this and reset the udp with another udp.begin()?

udo.write() returns the bytes sent to the buffer; so that doesn’t tell me what was sent out.

Do I need to poll and catch the network at a point when it’s down to make this work? do I just reset udp on every loop (not a problem, since this one is just sending data one way).

Or do I use udp.sendPacket(), which sends it unbuffered, and hopefully the return count will be zero when this problem occurs?

I’ll try this.

---

<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:** [May 21, 2020, 5:16pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/15 "2020-05-21T17:16:43Z")

</div>

> [@jed](#):
>
> How can I detect this and reset the udp with another udp.begin()?

The docs for `udp.begin()` do show that too 😉

[https://docs.particle.io/reference/device-os/firmware/photon/#begin--7](https://docs.particle.io/reference/device-os/firmware/photon/#begin--7)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/c/9/c9b6e53c921cdc441651cbb040fd5301996a9f8f.png)

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 5:45pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/16 "2020-05-21T17:45:05Z")

</div>

I saw that, and I started testing.

I deliberately called WiFi.disconnect() in order to kill the connection for a few seconds… then reconnected and ran the startPacket/write/endPacket. But this was successful (packet sent). Note that the sendPacket() method also worked, without returning a zero, so I was unable to test if that fixed the issue.

I then repeated with WiFi.Off/On. I watched the device reconnect.

Is there some UDP timeout that kills the connection?

So I’m not sure I understand how this entire thing works.

---

<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:** [May 21, 2020, 5:49pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/17 "2020-05-21T17:49:59Z")

</div>

> [@jed](#):
>
> Is there some UDP timeout that kills the connection?

UDP is connectionless, so no.

But you may run out of sockets when not calling `udp.stop()` before requesting.  
Consequently I'm not sure whether that example in the docs is _"complete"_, but for further details I'd defer to @rickkas7 who knows more about the actual UDP stack.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 5:59pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/18 "2020-05-21T17:59:05Z")

</div>

Looking at other docs, I found some references to “UDP timeout”. But neither my mac or my PC seem to have this value listed in their configurations.

I’ll include the code and maybe program in a long, long wifi downtime to test it.

I was also wondering about calling udp.stop(), so I’ll put that in there ahead of the begin in that loop.

---

<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:** [May 21, 2020, 6:04pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/19 "2020-05-21T18:04:51Z")

</div>

Since the UDP object inherits from the Stream class there will be individual functions that have their own timeout but that’s only confined to that call and not to a (non-existent) UDP connection.

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 21, 2020, 6:21pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/20 "2020-05-21T18:21:24Z")

</div>

There is no timeout in UDP itself. You should use `SYSTEM_THREAD(ENABLED)` or `SYSTEM_MODE(SEMI_AUTOMATIC)` and monitor `WiFi.ready()` and when it goes from false to true call `UDP.begin()` to start the listener. When the network goes away, either because you called `WiFi.disconnect()` or the access point went away, the listeners are removed. It won’t hurt to call `UDP.stop()` before calling `UDP.begin()`but it’s not necessary.

---

<div class="post-metadata">

**Author:** ![jed](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jed](https://community.particle.io/u/jed)\
**Post date:** [May 21, 2020, 6:35pm UTC](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267/21 "2020-05-21T18:35:25Z")

</div>

Thank you both, I have incorporated those changes.

However, I tried to test it by explicitly calling WiFi.disconnect/connect and then tried WiF.Off/On, and waiting as much as 10s with the connection down.

That did not disrupt the connection, and I did not need to re-call udp.begin().

I don’t disbelieve you guys, I am just trying to wrap my head around it. 🙂

[Next page](https://community.particle.io/t/can-you-increase-the-size-of-a-udp-packet/56267.md?page=2)
