ok hier mein Code.
#include <Arduino.h>
#include <WiFi.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
using namespace std;
#include <float.h>
#include <math.h>
#include <stdio.h>
#include "time.h"
#include <TFT_eSPI.h>
uint32_t delayMS;
#include "FS.h"
#include "SD.h"
#include <SPI.h>
//File root;
String Empf_str1; //Für Serielle
String Empf_str2; //Für Serielle
String rec_str = ""; //Für Serielle
int port = 23; //Port number
String ssid;
String pass;
WiFiServer server(port);
WiFiClient client;
int status = WL_IDLE_STATUS;
#define TFT_GREY 0x5AEB
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
// My Time and Date
#define MY_NTP_SERVER "at.pool.ntp.org"
#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03"
/* Globals */
time_t now; // this are the seconds since Epoch (1970) - UTC
tm tm; // the structure tm holds time information in a more convenient way *
char ntpDate[50]; //Datum
char ntpTime[50]; //Uhrzeit
//==============================================================
String empf_str; //Für WLAN Empfang
String Sende_str;
const char* empf_chr;
char Name_Chr[14];
char Memo_Chr[200];
String Name_Str;
String Memo_Str;
//==============================================================
void showTime() {
time(&now); // read the current time
localtime_r(&now, &tm); // update the structure tm with the current time
snprintf (ntpDate, 50, "%02d.%02d.%0002d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
snprintf (ntpTime, 50, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec + 1);
}
//===========================================
// Das ist mein erstes was ich mache
// Nach 2-3 mal Befehl senden bekomme ich true
void check_sd_card()
{
if(!SD.begin(5)){
client.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
client.println("No SD card attached");
return;
}
client.print("SD Card Type: ");
if(cardType == CARD_MMC){
client.println("MMC");
} else if(cardType == CARD_SD){
client.println("SDSC");
} else if(cardType == CARD_SDHC){
client.println("SDHC");
} else {
client.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
client.printf("SD Card Size: %lluMB\n", cardSize);
client.println("Ende Check SD Card");
}
//===================================================
// List Dir D
//===================================================
void listDir(fs::FS &fs, const char * dirname, uint8_t levels)
{
//client.printf("Listing directory: %s\n", dirname);
Sende_str.clear();
File root = fs.open(dirname);
if(!root){
client.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
client.println("Not a directory");
return;
}
File file = root.openNextFile();
Sende_str = "(";
while(file){
if(file.isDirectory()){
//client.print("DIR : ");
client.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Sende_str += file.name();
Sende_str += ":";
Sende_str += file.size();
Sende_str += ":";
Sende_str += ")";
Sende_str += "(";
}
file = root.openNextFile();
}
client.println(Sende_str);
}
//=========================================
//nur zum testen vom originalen demo
void readFile(fs::FS &fs, const char * path){
client.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
client.println("Failed to open file for reading");
return;
}
client.print("Read from file: ");
while(file.available()){
client.write(file.read());
}
file.close();
}
//nur zum testen vom originalen demo
//==================================
// Datei lesen ist ok
void read_sd_demo()
{ //debug ausgabe, kommt raus
//client.println("file open " + Name_Str);
File file = SD.open("/" + Name_Str);
if (file) {
// aus der Datei lesen, bis nichts mehr darin ist:
while (file.available()) {
client.write(file.read());
}
// close the file:
//client.println("file close ");
file.close();
} else {
// if the file didn't open, print an error:
client.println("error opening " + Name_Str);
}
}
//=================================
// ok
void datei_erstellen_sd_demo()
{
client.println("Creating eine datei " + Name_Str);
File file = SD.open("/" + Name_Str , FILE_WRITE);
file.close();
// Check to see if the file exists:
if (SD.exists("/" + Name_Str)) {
client.println(Name_Str + " exists.");
} else {
client.println(Name_Str + " doesn't exist.");
}
}
//=============================
// ok delete the file:
void delete_sd_demo()
{
client.println("Removing example.txt...");
SD.remove("/" + Name_Str);
if (SD.exists(Name_Str)) {
client.println(Name_Str + " Datei wurde nicht geloescht");
} else {
client.println(Name_Str + " Datei wurde geloecht.");
}
}
//================================
// ok Write in Datei
void write_sd_demo()
{
//Debug Ausgabe
//client.println("Name fuer Datei " + Name_Str);
//client.println("Memo fuer Datei " + Memo_Str);
File file = SD.open(Name_Str, FILE_WRITE);
// if the file opened okay, write to it:
if (file) {
file.println(Memo_Str);
// close the file:
file.close();
client.println("Fertig. !");
} else {
// if the file didn't open, print an error:
client.println("Konnte nichts in Datei schreiben");
}
}
//=============================
// ok geht auch
void append_sd_demo()
{
File file = SD.open("/" + Name_Str, FILE_APPEND);
if(!file){
Serial.println(F("failed to open file"));
}
file.print(Memo_Str);
file.print("\n");
file.close();
}
//==============================================================
// SETUP
//==============================================================
void setup()
{
Serial.begin(115200);
ssid = "********";
pass = "************";
configTime(0, 0, MY_NTP_SERVER); // 0, 0 because we will use TZ in the next line
setenv("TZ", MY_TZ, 1); // Set environment variable with your time zone
tzset();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass); //WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin();
tft.init();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
//tft.fillScreen(TFT_RED);
tft.setTextColor(TFT_WHITE, TFT_BLACK); // Adding a background colour erases previous text automatically
}
//==============================================================
// LOOP
//==============================================================
void loop ()
{
showTime();
tft.drawCentreString(ntpTime,120,10,4);
tft.drawCentreString(ntpDate,120,35,4);
delay(200); // dirty delay
client = server.available();
while(client.connected())
{
// kommt später wieder rein
// showTime();
// tft.drawCentreString(ntpTime,120,10,4);
// tft.drawCentreString(ntpDate,120,35,4);
while (client.connected() && client.available())
{
empf_str.clear();
empf_str = client.readStringUntil(12); // \n
//======================================= INIT SD Card
if (empf_str[0] == 'C')
{
check_sd_card();
}
//======================================== List Dir
if (empf_str[0] == 'D')
{
listDir(SD, "/", 0);
client.println("ok1");
}
//
//======================================== "2" read datei Demo
if (empf_str[0] == '2')
{
empf_str.remove(0,1);
//Name_Str = empf_str;
//read_sd_demo();
//Zum testen die Original Demo datei
readFile(SD, "/MyTest.txt");
}
//
//======================================== "3" write Datei
if (empf_str[0] == '3')
{
empf_str.remove(0,1);
Name_Str = empf_str;
datei_erstellen_sd_demo();
}
//======================================== "4" delete Datei
if (empf_str[0] == '4')
{
empf_str.remove(0,1);
Name_Str = empf_str;
delete_sd_demo();
}
//======================================== "5" Schreiben in Datei
if (empf_str[0] == '5')
{
empf_str.remove(0,1);
String s = empf_str;
int pos = s.indexOf('{');
Name_Str = s.substring(0,pos);
s.remove(0,pos+1);
Memo_Str = s;
write_sd_demo();
}
//======================================== "6" append
if (empf_str[0] == '6')
{
empf_str.remove(0,1);
Name_Str = empf_str;
append_sd_demo();
}
}
}
}
mfg