From 8897226d9a2b13918e356be4803ccc528f57138b Mon Sep 17 00:00:00 2001 From: hans-jurgen Date: Wed, 4 Dec 2024 22:32:31 +0100 Subject: [PATCH] =?UTF-8?q?2024-12-0400=20Ich=20habe=20Touchscreen=20oder?= =?UTF-8?q?=20SD=20Card=20einrichten=20k=C3=B6nnen.=20Beides=20zusammen=20?= =?UTF-8?q?geht=20nicht.=20Schade=20!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/mess_htu21.h | 22 +++-- platformio.ini | 2 +- src/main.cpp | 211 ++++++++++++++++++++++++++++++++----------- 3 files changed, 170 insertions(+), 65 deletions(-) diff --git a/include/mess_htu21.h b/include/mess_htu21.h index 7978c44..357619e 100644 --- a/include/mess_htu21.h +++ b/include/mess_htu21.h @@ -15,6 +15,8 @@ float K_HTU= -0.00; struct { char temperature[15] = {0}; char humity[15] = {0}; + float t = 0.0; + float h = 0.0; } htuData; void init_HTU21(){ @@ -31,29 +33,29 @@ void init_HTU21(){ } -float read_HTU21D() { - float t = htu.readTemperature(); - //t = t + KorrekturTemperaturHTU; - dtostrf(t,7,1,htuData.temperature); - float h = htu.readHumidity(); - dtostrf(h,7,1,htuData.humity); +void read_HTU21D() { + htuData.t = htu.readTemperature(); + //htuData.t = htuData.t + KorrekturTemperaturHTU; + dtostrf(htuData.t,7,1,htuData.temperature); + htuData.h = htu.readHumidity(); + dtostrf(htuData.h,7,1,htuData.humity); Serial.print("Temperature (HTU21D):\t"); Serial.print(htuData.temperature); Serial.print(" °C\t"); tft.drawString("Temperatur: ", 1, x, 4); - tft.drawFloat(t, 1.0,190, x,4); + tft.drawFloat(htuData.t, 1.0,190, x,4); tft.drawString("o", 252, x, 1); tft.drawString("C", 260, x, 4); - Serial.println(t,8); + Serial.println(htuData.t,8); Serial.print("Luftfeuchtigkeit:\t"); Serial.print(htuData.humity); Serial.println(" %"); tft.drawString("Luftfeuchtigkeit: ", 1, x +=25, 4); - tft.drawFloat(h, 1.0,190, x, 4); + tft.drawFloat(htuData.h, 1.0,190, x, 4); tft.drawString("%", 252, x, 4); x = 100; - return t; + } void M2M_HTU21D(String deviceId = "4711") { diff --git a/platformio.ini b/platformio.ini index 400a2db..fc0e4fa 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ framework = arduino lib_deps = bodmer/TFT_eSPI@^2.5.33 ;nitek/XPT2046_Bitbang_Slim@^2.0.0 - ;https://github.com/PaulStoffregen/XPT2046_Touchscreen.git#v1.4 + https://github.com/PaulStoffregen/XPT2046_Touchscreen.git#v1.4 adafruit/Adafruit HTU21DF Library @ 1.0.5 ;wollewald/ADS1115_WE @ 1.4.3 adafruit/Adafruit MCP9808 Library @ 2.0.0 diff --git a/src/main.cpp b/src/main.cpp index 45775a1..d26a600 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,8 @@ #include +#include + #include //#include //#include @@ -26,9 +28,27 @@ int x = 100; char sID[100]; char ausgabe[100]; +// The CYD touch uses some non default +// SPI pins + +#define XPT2046_IRQ 36 +#define XPT2046_MOSI 32 +#define XPT2046_MISO 39 +#define XPT2046_CLK 25 +#define XPT2046_CS 33 + +#define SD_SCK 18 +#define SD_MISO 19 +#define SD_MOSI 23 +#define SD_CS 5 + TFT_eSPI tft = TFT_eSPI(); SPIClass spi2 = SPIClass(VSPI); +SPIClass mySpi = SPIClass(VSPI); +XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ); + + #include #include #include @@ -200,8 +220,16 @@ void testFileIO(fs::FS &fs, const char * path) { } // ENDE TEST - +int WiFiError = 36; +int Versuche; + void setup() { + + //spi2.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); + /* ts.begin(spi2); + ts.setRotation(1); */ + mySpi.begin(SD_SCK, SD_MISO, SD_MOSI); + // Start the tft display and set it to black tft.init(); tft.setRotation(1); //This is the display in landscape @@ -217,17 +245,32 @@ void setup() { x = 320 /2; y += 16; tft.setTextColor(TFT_RED, TFT_BLACK); - tft.drawCentreString("Warte auf verbindung", x, y, fontNum *= 2);printf(""); + tft.drawCentreString("Warte auf verbindung", x, y, fontNum *= 2); Serial.begin(115200); - WiFi.reconnect();printf(""); - delay( 1 ); - -Serial.print("Connecting to "); + WiFi.reconnect(); + delay(10); + WiFi.disconnect(); + delay(10); + Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); + Versuche = 0; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); + Versuche += 1; + if(Versuche >= WiFiError){ + Serial.println(); + tft.fillScreen(TFT_RED); + tft.setTextColor(TFT_BLACK, TFT_WHITE); + tft.drawCentreString(" Wlan Stoerung ", 160, 110, fontNum = 4); + for(int a = 0; a < 20; a++){ + tft.invertDisplay(a % 2); + delay(500); + } + WiFi.disconnect(); + ESP.restart(); + } } // Print local IP address and start web server tft.fillScreen(TFT_BLACK); @@ -235,47 +278,8 @@ Serial.print("Connecting to "); Serial.println("WiFi connected."); Serial.print("IP address: \t"); Serial.println(WiFi.localIP()); - tft.setTextColor(TFT_YELLOW, TFT_BLACK); - tft.setTextSize(2); - tft.println(WiFi.localIP()); - tft.setTextSize(1); - delay(5000); -// Initialize a NTPClient to get time - //timeClient.begin(); - // Set offset time in seconds to adjust for your timezone, for example: - // GMT +1 = 3600 - // GMT +8 = 28800 - // GMT -1 = -3600 - // GMT 0 = 0 - //timeClient.setTimeOffset(3600); - - configTime(0, 0, NTP_SERVER); - // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region - setenv("TZ", TZ_INFO, 1); - - if (getNTPtime(10)) { // wait up to 10sec to sync - } else { - Serial.println("Time not set"); - delay(5000); - ESP.restart(); - } - lastNTPtime = time(&now); - - -//-------------------------------------------------------- - - init_HTU21(); - init_MCP9808(); - Init_BMP280(); - if (F_MCP9808 == true){ - valTemp = getTemperature_MCP9808(); - } - - x = 320 /2; - y += 32; - tft.setTextColor(TFT_BLUE, TFT_BLACK); - - if (!SD.begin(SS, spi2, 80000000)) { + // SD Karte ---------------------------------------- + if (!SD.begin(SS, mySpi, 80000000)) { Serial.println("Card Mount Failed"); return; } @@ -297,7 +301,7 @@ Serial.print("Connecting to "); Serial.println("UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); - Serial.printf("SD Card Size: %lluMB\n", cardSize); + Serial.printf("SD Card Size: %llu MB\n", cardSize); listDir(SD, "/", 0); /* createDir(SD, "/mydir"); @@ -305,29 +309,128 @@ Serial.print("Connecting to "); removeDir(SD, "/mydir"); listDir(SD, "/", 2); testFileIO(SD, "/hello.txt"); - deleteFile(SD, "/hello.txt"); */ + deleteFile(SD, "/hello.txt"); */ + // ---------------------------------End SD Karte ------------------- + tft.setTextColor(TFT_YELLOW, TFT_BLACK); + tft.setTextSize(2); + tft.println(WiFi.localIP()); + tft.setTextSize(1); + delay(5000); +// Initialize a NTPClient to get time + configTime(0, 0, NTP_SERVER); + // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region + setenv("TZ", TZ_INFO, 1); + if (getNTPtime(10)) { // wait up to 10sec to sync + } else { + Serial.println("Time not set"); + tft.fillScreen(TFT_RED); + tft.setTextColor(TFT_BLACK, TFT_WHITE); + tft.drawCentreString(" Stoerung NTP Server ", 160, 110, fontNum = 4); + for(int a = 0; a < 20; a++){ + tft.invertDisplay(a % 2); + delay(500); + } + WiFi.disconnect(); + ESP.restart(); + } + lastNTPtime = time(&now); + + +//-------------------------------------------------------- + + init_HTU21(); + init_MCP9808(); + Init_BMP280(); + if (F_MCP9808 == true){ + valTemp = getTemperature_MCP9808(); + } + + x = 320 /2; + y += 32; + tft.setTextColor(TFT_BLUE, TFT_BLACK); } +void printTouchToDisplay(TS_Point p) { + + // Clear screen first + tft.fillScreen(TFT_BLACK); + tft.setTextColor(TFT_WHITE, TFT_BLACK); + + int x = 320 / 2; // center of display + int y = 100; + int fontSize = 2; + + String temp = "Pressure = " + String(p.z); + tft.drawCentreString(temp, x, y, fontSize); + + y += 16; + temp = "X = " + String(p.x); + tft.drawCentreString(temp, x, y, fontSize); + + y += 16; + temp = "Y = " + String(p.y); + tft.drawCentreString(temp, x, y, fontSize); +} + + +void printTouchToSerial(TS_Point p) { + Serial.print("Pressure = "); + Serial.print(p.z); + Serial.print(", x = "); + Serial.print(p.x); + Serial.print(", y = "); + Serial.print(p.y); + Serial.println(); +} + +void printTouchToSerial2(int x, int y, int z) { + Serial.print("Pressure = "); + Serial.print(z); + Serial.print(", x = "); + Serial.print(x); + Serial.print(", y = "); + Serial.print(y); + Serial.println(); +} int Minuten, MintenOld = 99; float test; +int ts_x, ts_y, ts_z; +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 void loop() { + + if (ts.tirqTouched() && ts.touched()) { + TS_Point p = ts.getPoint(); + //printTouchToSerial(p); + ts_x = map(p.x, 200, 3700, 1, SCREEN_WIDTH); + ts_y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT); + ts_z = p.z; + printTouchToSerial2(ts_x, ts_y, ts_z); + + if((ts_x >= 300) && (ts_y >= 220)){ + Serial.printf(" Gefunden bei: %i : %i : %i \t", p.y, p.x, p.z); + } else { + printTouchToDisplay(p); + } + + } getNTPtime(10); tft.setTextColor(TFT_WHITE, TFT_BLACK); - zeigeZeit(timeinfo, 210, 0); + zeigeZeit(timeinfo, 215, 0); Minuten = timeinfo.tm_min; if (Minuten != MintenOld){ MintenOld = Minuten; tft.setTextColor(TFT_BLUE, TFT_BLACK); x = 100; - test = read_HTU21D(); + read_HTU21D(); getTemperature_MCP9808(); read_BMP_280(); if (Minuten % SAVE_INTERVAL == 0){ - sprintf(ausgabe, "%s;%f\n", szZeit, test); + sprintf(ausgabe, "%s;%f;%f\n", szZeit, htuData.t, htuData.h); appendFile(SD, "/daten.csv", ausgabe); - Serial.print("SAVE ----> "); Serial.println(szZeit); + Serial.print("SAVE ----> "); Serial.println(ausgabe); } } @@ -363,7 +466,7 @@ void showTime(tm localTime) { } void zeigeZeit(tm localTime, int x = 0, int y = 0){ - sprintf(szZeit,"%02d:%02d:%02d\n", + sprintf(szZeit,"%02d:%02d:%02d", localTime.tm_hour, localTime.tm_min, localTime.tm_sec);