Last active
May 29, 2017 00:20
-
-
Save me-no-dev/116e417ea6a3bbc98b08 to your computer and use it in GitHub Desktop.
Single Client Telnet Server example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern "C" { | |
enum sleep_type { | |
NONE_SLEEP_T = 0, | |
LIGHT_SLEEP_T, | |
MODEM_SLEEP_T | |
}; | |
bool wifi_set_sleep_type(enum sleep_type type); | |
void system_set_os_print(uint8 onoff); | |
void ets_install_putc1(void* routine); | |
} | |
void _u0_putc(char c){ | |
while(((U0S >> USTXC) & 0x7F) == 0x7F); | |
U0F = c; | |
} | |
const char* ap_ssid = "esp8266-uart-srv"; | |
const char* ssid = "abcdefgh"; | |
const char* password = "1234567890"; | |
AsyncServer server(23); | |
AsyncClient *client; | |
void onPoll(void *obj, AsyncClient* c){ | |
//os_printf("p:%lu\n", millis()); | |
} | |
void onError(void *obj, AsyncClient* c, int8_t error){ | |
os_printf("e:%d\n", error); | |
} | |
void onAck(void *obj, AsyncClient* c, size_t len, uint32_t time){ | |
os_printf("a:%u:%u\n", len, time); | |
} | |
void onDisconnect(void *obj, AsyncClient* c){ | |
os_printf("d\n"); | |
c->free(); | |
delete c; | |
client = NULL; | |
} | |
void onTimeout(void *obj, AsyncClient* c, uint32_t time){ | |
os_printf("t:%u\n", time); | |
client->close(); | |
} | |
void onData(void *obj, AsyncClient* c, void *buf, size_t len){ | |
os_printf("rx:%u\n", len); | |
size_t i; | |
for(i=0; i<len; i++){ | |
while(((U0S >> USTXC) & 0x7F) != 0); | |
U0F = ((uint8_t*)buf)[i]; | |
} | |
} | |
void onClient(void *obj, AsyncClient* c){ | |
if(client){ | |
os_printf("r\n"); | |
if(client->connected()) | |
client->close(); | |
} | |
os_printf("c\n"); | |
client = c; | |
client->onError(onError); | |
client->onAck(onAck); | |
client->onDisconnect(onDisconnect); | |
client->onTimeout(onTimeout); | |
client->onData(onData); | |
//client->onPoll(onPoll); | |
} | |
void setup() { | |
Serial.begin(115200); | |
ets_install_putc1((void *) &_u0_putc); | |
system_set_os_print(1); | |
WiFi.mode(WIFI_AP_STA); | |
WiFi.softAP(ap_ssid, password); | |
WiFi.begin(ssid, password); | |
os_printf("AP: %s\n", ap_ssid); | |
ArduinoOTA.begin(); | |
server.onClient(onClient, 0); | |
server.begin(); | |
} | |
static uint32_t lastTest = 0; | |
void loop() { | |
ArduinoOTA.handle(); | |
if(Serial.available()){ | |
uint8_t * sbuf = (uint8_t *)malloc(1460); | |
size_t len = 0; | |
while(Serial.available() && len < 1460){ | |
sbuf[len++] = Serial.read(); | |
if(!Serial.available()) yield(); | |
} | |
if(client && client->connected()){ | |
while(!client->canSend()) yield(); | |
os_printf("x:%u\n", len); | |
client->write((const char *)sbuf, len); | |
} else { | |
os_printf("m:%u\n", len); | |
} | |
free(sbuf); | |
} | |
uint32_t newTest = ESP.getFreeHeap(); | |
if(newTest != lastTest){ | |
lastTest = newTest; | |
os_printf("%u\r\n", newTest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment