Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created November 13, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaeplin/1b346a6c0bf6e2b56b28 to your computer and use it in GitHub Desktop.
Save chaeplin/1b346a6c0bf6e2b56b28 to your computer and use it in GitHub Desktop.
esp8266-udp.ino
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define _IS_MY_HOME
// wifi
#ifdef _IS_MY_HOME
#include "/usr/local/src/ap_setting.h"
#else
#include "ap_setting.h"
#endif
IPAddress syslogServer(192, 168, 10, 10);
WiFiClient wifiClient;
WiFiUDP udp;
void wifi_connect() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int Attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Attempt++;
Serial.print(".");
if (Attempt == 200)
{
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(1000);
wifi_connect();
delay(500);
if (WiFi.status() == WL_CONNECTED) {
sendUdpSyslog("esp8266-02-syslog started");
}
String str1 = "AB";
String str2 = "CD";
for (int i = 1; i <= 700; i++) {
str2 = str1 + str2 ;
int msg_length = str2.length();
int all_length = String(msg_length).length();
str2 = String(msg_length + all_length) + str2 ;
if ( msg_length > 400 && msg_length <= 1430 ) {
Serial.print("----> ");
Serial.println(str2.length());
sendUdpSyslog(str2);
}
if ( msg_length > 1431 ) {
break;
}
delay(100);
}
}
void loop() {
}
void sendUdpSyslog(String msgtosend)
{
unsigned int msg_length = msgtosend.length();
byte* p = (byte*)malloc(msg_length);
memcpy(p, msgtosend.c_str(), msg_length);
udp.beginPacket(syslogServer, 515);
//udp.write(msgtosend.c_str(), msg_length);
udp.write(p, msg_length);
udp.endPacket();
Serial.print("----> ");
Serial.println(String(msgtosend.c_str()).length());
free(p);
}
https://github.com/esp8266/Arduino/blob/342c4ae6fb847bfc787f80b89a2bb888d942dc32/libraries/ESP8266WiFi/src/include/UdpContext.h#L308
private:
void _reserve(size_t size)
{
const size_t pbuf_unit_size = 512;
if (!_tx_buf_head)
{
_tx_buf_head = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
_tx_buf_cur = _tx_buf_head;
_tx_buf_offset = 0;
}
size_t cur_size = _tx_buf_head->tot_len;
if (size < cur_size)
return;
size_t grow_size = size - cur_size;
while(grow_size)
{
pbuf* pb = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
pbuf_cat(_tx_buf_head, pb);
if (grow_size < pbuf_unit_size)
return;
grow_size -= pbuf_unit_size;
}
}
@chaeplin
Copy link
Author

-changed to 1024
14:11:09.317019 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1008
14:11:09.418847 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1014
14:11:09.518973 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1020
14:11:09.620085 IP truncated-ip - 2 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1026
14:11:09.721469 IP truncated-ip - 8 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1032
14:11:09.822548 IP truncated-ip - 14 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1038
14:11:09.924998 IP truncated-ip - 20 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1044
14:11:10.025179 IP truncated-ip - 26 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1050
14:11:10.126405 IP truncated-ip - 32 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1056
14:11:10.225801 IP truncated-ip - 38 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1062

@chaeplin
Copy link
Author

-changed to 1500
14:14:12.081947 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1020
14:14:12.182026 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1026
14:14:12.283688 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1032
14:14:12.384505 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1038
14:14:12.485692 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 1044

@chaeplin
Copy link
Author

~changed to 512
15:58:26.916233 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 472
15:58:27.016917 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 482
15:58:27.117935 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 492
15:58:27.218756 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 502
15:58:27.320031 IP 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 512
15:58:27.420847 IP truncated-ip - 10 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 522
15:58:27.522546 IP truncated-ip - 20 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 532
15:58:27.623471 IP truncated-ip - 30 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 542
15:58:27.721857 IP truncated-ip - 40 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 552
15:58:27.824500 IP truncated-ip - 50 bytes missing! 192.168.10.112.4097 > 192.168.10.10.515: UDP, length 562

@mkeyno
Copy link

mkeyno commented May 13, 2016

hi @chaeplin , can we increase the UDP length much more than 1500, to be sure nothing lost , actually I should deliver about 7kb data from processing in my lap to the ESP module through out my wifi-router in time not exceeded than 40ms , do you have any suggestion what would be best and fast way to deliver this bug chunk of data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment