Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created November 13, 2015 05:18
Show Gist options
  • 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;
}
}
@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