Kód: Vybrat vše
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <Wire.h>
#include <MCUFRIEND_kbv.h>
#include <Calibri12pt7b.h>
#include <calibrib18pt7b.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x9C, 0xB7};
char* server;
String addr;
EthernetServer svr(80);
IPAddress err(0,0,0,0);
MCUFRIEND_kbv tft;
String read =
R"(
<!DOCTYPE html>
<html>
<head>
<title>Arduino Mega 2560</title>
<meta charset="utf-8">
</head>
<body>
<h1>501 Internal Server Error</h1>
Error in server SD unit. Cannot read server file.
</body>
</html>
)";
uint16_t bg = tft.color565(220,150,0);
bool sd;
void setup(){
Serial.begin(9600);
tft.reset();
tft.begin(tft.readID());
tft.invertDisplay(true);
tft.setRotation(3);
tft.fillScreen(bg);
tft.setTextColor(0, bg);
tft.setCursor(0, 18);
tft.setFont(&calibri12pt7b);
tft.println("Inicializace Ethernetu...");
Ethernet.begin(mac);
tft.fillScreen(bg);
tft.setFont(&calibri12pt7b);
delay(1000);
tft.setFont(&calibrib18pt7b);
tft.setCursor(0, 22);
tft.print("ServerDuino V1");
tft.setFont(&calibri12pt7b);
tft.print('\n');
tft.println(Ethernet.localIP() != err ? "ETH v poradku" : "ETH nefunguje");
tft.print("IP ");
tft.println(Ethernet.localIP());
tft.print("SD karta ");
sd = SD.begin(4);
tft.println(sd?"v poradku" : "nefunguje");
}
String last;
int ypos = 90;
void state(String st){
if(last != st){
last = st;
tft.setCursor(0,ypos+20);
tft.fillRect(0,ypos, 480, 38, bg);
tft.print(st);
}
}
uint32_t lastConnection = millis();
bool cleared = true;
void loop(){
if(Serial.available()){
bg = tft.color565(Serial.parseInt(), Serial.parseInt(), Serial.parseInt());
tft.setCursor(0, 22);
tft.setFont(&calibrib18pt7b);
tft.print("ServerDuino V1");
tft.setFont(&calibri12pt7b);
tft.print('\n');
tft.println(Ethernet.localIP() != err ? "ETH v poradku" : "ETH nefunguje");
tft.print("IP ");
tft.println(Ethernet.localIP());
tft.print("SD karta ");
sd = SD.begin(4);
tft.println(sd?"v poradku" : "nefunguje");
tft.setCursor(0,ypos+20);
tft.fillRect(0,ypos, 480, 38, bg);
tft.print(last);
}
EthernetClient client = svr.available();
if(client.available() && client.connected()){
lastConnection = millis()+60000;
cleared = false;
state("Zadost o pripojeni...");
Serial.println("New Connection:");
client.readStringUntil('/');
String request = client.readStringUntil(' ');
File file = SD.open(request);
state("Soubor ");
if(file){
tft.print("otevren ");
client.println("HTTP/1.1 200 OK\n");
while(file.available()){
read.concat(file.read());
}
file.close();
client.println(read);
}
else{
tft.println("vadny ");
client.println("HTTP/1.1 200 OK\n");
client.println(read);
}
String response;
while(client.available()){
response.concat((char)client.read());
}
client.stop();
tft.fillRect(0,78, 480, 242, bg);
tft.print(response);
delay(500);
}
else {
state("System bezi...");
}
if((!cleared)&&millis() > lastConnection){
cleared = true;
tft.fillRect(0,78, 480, 242, bg);
}
}