ESP32ASCIIPrinter/src/main.cpp

110 lines
2.2 KiB
C++

#include <ParallelPrinter.h>
#include <WiFi.h>
#include <WebServer.h>
WiFiServer server(21);
WiFiClient client;
#define wifi_ssid "dezentrale-2.4"
#define wifi_password "dezentrale"
ParallelPrinter PP;
bool wifi_connect()
{
delay(5000);
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
static int timeout = 20;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
if(timeout == 0)
{
Serial.println("Failed to connect: Timeout");
return false;
}
timeout--;
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("Local IP address: ");
Serial.println(WiFi.localIP());
return true;
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
if(wifi_connect())
{
server.begin();
Serial.println("Telnet port = 21");
Serial.println("Password: drucker");
} else
{
Serial.println("Failed to connect.");
}
Serial.println();
Serial.println();
PP.begin(); // create connection to printer
}
bool clientAuthorized = false;
void loop()
{
// put your main code here, to run repeatedly:
size_t bytes = 0;
if (client.connected())
{
bytes = client.available();
if (bytes)
{
if (clientAuthorized)
{
char buf[32];
if (bytes > 32) bytes = 32;
client.readBytes(buf,bytes);
String line = client.readStringUntil('\n');
//PP.println(buf,bytes);
PP.println(line);
Serial.write(buf,bytes);
//Serial.print('\n');
} else
{
String line = client.readStringUntil('\n');
if(!strcmp("drucker",line.c_str())) // Passwordcheck
{
clientAuthorized = true;
client.println("You are now logged in.");
}
}
}
size_t bytes = Serial.available();
if(bytes)
{
char buf[32];
if(bytes > 32) bytes = 32;
Serial.readBytes(buf, bytes);
//String s = Serial.readString();
//client.print(s);
client.write(buf,bytes);
}
} else
{
clientAuthorized = false;
client = server.available();
//send out password info
client.println("Please enter password.");
}
}