You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32
Copyright (c) 2017 Hristo Gochkov. All rights reserved.
This file is part of the ESP32 WiFi library for the Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* This program act as a Telnet server and will allow monitoring of a serial stream on
* Serial2. It gathers all char into a buffer until it receives a Line-Feed 0X0A,
* add a carriage return, 0X0D, and a string terminator, 0X00. then send
* data out to Telnet client.
*
* This will also send the data out to a SYSLOG server.
*
CHANGE LOG:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
* 20-Feb-2021 1.0 TRL - First test
* 21-Feb-2021 1.0a TRL - Added Syslog and serial buffering
*
*
* Notes: 1) Tested with Arduino 1.8.13
* 2) ESP32 Feather
* 3) If RX line is open, it will cause chatter and overflow buffer
* 4) Will not send buffer to syslog if less that 3 char, ie: an empty line|
* 5)
*
*
* Todo: 1)
*
* TRL --> Tom Lafleur --> [email protected]
*/
#include <WiFi.h>
#include <Syslog.h> // https://github.com/arcao/Syslog
#include <WiFiUdp.h>
//how many clients should be able to telnet to this ESP32
#define MAX_SRV_CLIENTS 4
const char* ssid = "MySSID";
const char* password = "MyPassword";
// Syslog server connection info
#define SYSLOG_SERVER "192.168.1.32" // or FQDN
#define SYSLOG_PORT 514
// This device info
#define DEVICE_HOSTNAME "WS4a"
#define APP_NAME "WS4b"
#define ANOTHER_APP_NAME "my-another-app1"
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udpClient;
// Create a new empty syslog instance using IETF format
Syslog syslog(udpClient, SYSLOG_PROTO_IETF);
#define bufferSize 1024
char buffer[bufferSize];
byte c;
uint32_t bufferIndex = 0;
bool bufferFull = false;
/* ************************************************************* */
/* ************************************************************* */
/* ************************************************************* */
void setup()
{
Serial.begin(115200);
//while (!Serial)
Serial.print("\nSerial to Telnet and SYSLOG\n");
WiFi.begin(ssid, password);
Serial.print("** Connecting Wifi\n");
for (int loops = 10; loops > 0; loops--)
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println ("");
Serial.print ("WiFi connected ");
Serial.print ("IP address: ");
Serial.println (WiFi.localIP());
break;
}
else
{
Serial.printf("WiFi Connect attemps: %u\n", loops);
delay(1000);
}
}
if (WiFi.status() != WL_CONNECTED)
{
Serial.printf ("WiFi connect failed\n");
delay(1000);
ESP.restart();
}
// start UART and the server
Serial2.begin(115200);
server.begin();
server.setNoDelay(true);
Serial.print ("Use --> 'telnet ");
Serial.print (WiFi.localIP());
Serial.println (" 23' to connect");
memset(buffer, 0, bufferSize); // clear buffer...
// prepare syslog configuration here (can be anywhere before first call of
// log/logf method)
syslog.server(SYSLOG_SERVER, SYSLOG_PORT);
syslog.deviceHostname(DEVICE_HOSTNAME);
syslog.appName(APP_NAME);
syslog.defaultPriority(LOG_KERN);
syslog.logMask(LOG_MASK (LOG_INFO) );
syslog.log(LOG_INFO, "Done with setup");
Serial.print ("Done with setup\n");
}
/* ************************************************************* */
/* ************************************************************* */
/* ************************************************************* */
void loop()
{
uint8_t i;
if (Serial2.available())
{
if ( bufferIndex > bufferSize-3)
{
bufferIndex = 0; // this will fix crash if RX line is open
Serial.print("Buffer Overflow!, reseting buffer..\n");
}
c = Serial2.read();
buffer[bufferIndex] = c;
bufferIndex++;
if ( c == 0x0a) // check for a lf 0x0a
{
buffer[bufferIndex] = 0x0d; // add a CR 0x0d
bufferIndex++;
buffer[bufferIndex] = 0x00; // add a end of string
bufferFull = true;
// check for an empty message, don't send it to syslog
if (bufferIndex >= 3) syslog.logf(LOG_INFO, "%s", buffer);
}
if ( c == 0x0d) // flush any extra CR
{
bufferIndex--;
buffer[bufferIndex] = 0x00;
}
}
// check if we have a telnet connection
if (WiFi.status() == WL_CONNECTED)
{
//check if there are any new clients
if (server.hasClient())
{
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
// find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected())
{
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
if (!serverClients[i]) Serial.println("available broken");
Serial.print ("New client: ");
Serial.print (i); Serial.print(' ');
Serial.println(serverClients[i].remoteIP());
break;
}
}
if (i >= MAX_SRV_CLIENTS)
{
// no free slot, so disconnected stop, to reject connection
server.available().stop();
}
}
// check clients for RX data to us
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i] && serverClients[i].connected())
{
if(serverClients[i].available())
{
// get data from the telnet client and push it to the UART
while(serverClients[i].available()) Serial2.write(serverClients[i].read());
}
}
else
{
if (serverClients[i])
{
serverClients[i].stop();
}
}
} // end of for...
// check serial UART for data to send to client
if (bufferFull)
{
// push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i] && serverClients[i].connected())
{
serverClients[i].write(buffer, strlen(buffer));
delay(1);
}
}
} // end of if (serial2 avilable...)
} // end of if wifi status is connected
else
{
Serial.println ("WiFi not connected!");
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i]) serverClients[i].stop();
}
delay(1000);
}
if (bufferFull) // if bufferFull flag is true, we are now done with buffer
{
bufferIndex = 0;
bufferFull = false;
}
} // end of loop
/* ****************** End of Program *************** */
The text was updated successfully, but these errors were encountered:
trlafleur
changed the title
Example that receive serial stream data to SysLog server with intergrated Telnet client
Example that receive serial stream data to SysLog server with integrated Telnet client
Feb 23, 2021
The text was updated successfully, but these errors were encountered: