131 lines
2.9 KiB
C++
131 lines
2.9 KiB
C++
#include <ParallelPrinter.h> // https://github.com/RobTillaart/ParallelPrinter
|
|
#include <WiFi.h> // https://docs.platformio.org/en/latest/boards/espressif32/az-delivery-devkit-v4.html
|
|
|
|
#define wifi_ssid "dezentrale-2.4"
|
|
#define wifi_password "dezentrale"
|
|
|
|
#define STROBE 15
|
|
#define BUSY 17
|
|
#define OOP 18
|
|
|
|
#define D0 21
|
|
#define D1 22
|
|
#define D2 23
|
|
#define D3 25
|
|
#define D4 26
|
|
#define D5 27
|
|
#define D6 32
|
|
#define D7 33
|
|
|
|
WiFiServer server(21);
|
|
WiFiClient client;
|
|
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();
|
|
|
|
|
|
uint8_t datapins[] = { D0, D1, D2, D3, D4, D5, D6, D7 };
|
|
// create connection to printer
|
|
PP = ParallelPrinter(STROBE, BUSY, OOP, datapins); // uses default pins
|
|
PP.begin(80,255);
|
|
// uint8_t datapins[] = { D0, D1, D2, D3, D4, D5, D6, D7 };
|
|
// create connection to printer
|
|
// PP = new ParallelPrinter(STROBE, BUSY, OOP, datapins); // uses default pins
|
|
// strobe 13; busy 2; oop 12; data pins from 3 to 10
|
|
PP.println("Drucker wurde erfolgreich gestartet.");
|
|
PP.println("Warte auf Anweisungen...");
|
|
}
|
|
|
|
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.");
|
|
}
|
|
}
|