After two day,s of wrestling with my TFT display and various pinout sugestions and all I was able to find on the internet I must admit no succes.
At the moment I have a working set-up so realy the motivation to solve the problem is not there howe
Code: Select all
// *************** AsyncWebserver *******************
#include <AsyncEventSource.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
//**************** WIFI *****************************
#include <WiFi.h>
#include <WiFiUdp.h> // nodig voor de tijd NTP
AsyncWebServer server(80);
//**************** TIME ******************************
#include <time.h>
long timezone = 1;
byte daysavetime = 1;
//**************** SPIFFS ****************************
#include <SPIFFS.h> // SPIFFS file flashen
//**************** ESPmDNS **************************
#include <ESPmDNS.h>
//**************** FS *******************************
#include <FS.h>
#include <FSImpl.h>
#include <vfs_api.h>
File fsUploadFile; // a File variable to temporarily store the received file
//**************** OTA ******************************
#include <ArduinoOTA.h> // Over the air programming
const char* ssid = "mmm"; // Replace with your network credentials
const char* password = "nnnn";
//************** Display ****************************
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#define TFT_CS 15 // Chip Select
#define TFT_RST 12 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 4 // D/C
#define TFT_BACKLIGHT 25 // Backlight
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// *************** SD ******************************
#include <SD.h>
#include <SPI.h>
#define CS_PIN 2 // chipselect pin gedefinieerd SD card
//**************** BME280 ****************************
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C SDA=21 en SCL=22
// *************** DALLAS temp. *********************
#include <OneWire.h>
#include <DallasTemperature.h>
#define TEMP_SENSOR_PIN 5
OneWire oneWire(TEMP_SENSOR_PIN); // Set up a OneWire instance to communicate with OneWire devices
DallasTemperature sensors(&oneWire); // Create an instance of the temperature sensor class
const int ledPin = 13; // Set LED GPIO13
String ledState; // Stores LED state
String yy;
String mm;
String dd;
String hr;
String mi;
String se;
String dataMessage; // met gegevens om te loggen
String headerMessage; // file header met colom gegevens datum, tijd, tempbi,hum,press,tempbu
String tempbi; // van BME
String humidity; // van BME
String pressure; // van BME
String tempbu;
String temperature;
long previousMillis = 0; // tbv display wissel
long interval = 120000; // tbv display wissel
// *************** BME-280 ***************************
String getTemperature() { // Variable to store the HTTP request String header;
float temperature = bme.readTemperature();
Serial.println(temperature);
return String(temperature);
}
String getHumidity() {
float humidity = bme.readHumidity();
Serial.println(humidity);
return String(humidity);
}
String getPressure() {
float pressure = bme.readPressure()/ 100.0F;
Serial.println(pressure);
return String(pressure);
}
//**************** DS18B20 ***************************
String readDSTemperatureC() { // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
delay(20);
float tempbu = sensors.getTempCByIndex(0);
delay(20);
Serial.println(tempbu);
return String(tempbu);
}
//**************** LED etc.**************************
String processor(const String& var){ // Replaces placeholder with LED state value
Serial.println(var);
if(var == "STATE"){ // var in index.html page (id)
if(digitalRead(ledPin)){
ledState = "ON";
}
else{
ledState = "OFF";
}
Serial.print(ledState);
return ledState;
}
else if (var == "TEMPERATURE"){ // var in index.html page (id)
return getTemperature(); // BME-280 temperature binnen
}
else if (var == "HUMIDITY"){ // var in index.html page (id)
return getHumidity();
}
else if (var == "PRESSURE"){ // var in index.html page (id)
return getPressure();
}
else if (var == "TEMPBU"){ // var in index.html page (id)
return readDSTemperatureC(); // DS18B20 buiten
}
}
//**************** SET-UP ****************************
void setup(){
Serial.begin(115200);
pinMode(TFT_BACKLIGHT, OUTPUT);
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
pinMode(ledPin, OUTPUT); // bool status;
pinMode(2, OUTPUT);
SPI.begin();
sensors.begin(); // Start up the DS18B20 library
bme.begin();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
initSD();
startWIFI();
initTFT();
startOTA();
if(!SPIFFS.begin()){ // Initialize SPIFFS
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ // Route for root / web page
request->send(SPIFFS, "/index.html", String(), false, processor);
});
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){ // was /favicon.ico
request->send(SPIFFS, "/favicon.png", "image/png");
});
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to load style.css file
request->send(SPIFFS, "/style.css", "text/css");
});
server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to set GPIO to HIGH
digitalWrite(ledPin, HIGH);
request->send(SPIFFS, "/index.html", String(), false, processor);
});
server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to set GPIO to LOW
digitalWrite(ledPin, LOW);
request->send(SPIFFS, "/index.html", String(), false, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to the BME Temperature
request->send_P(200, "text/plain", getTemperature().c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to the BME Humidity
request->send_P(200, "text/plain", getHumidity().c_str());
});
server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to the BME Pressure
request->send_P(200, "text/plain", getPressure().c_str());
});
server.on("/tempbu", HTTP_GET, [](AsyncWebServerRequest *request){ // Route to the DS18B20 Temperature
request->send_P(200, "text/plain", readDSTemperatureC().c_str());
});
server.begin(); // Start server
}
//**************** LOOP ****************************
void loop(){
ArduinoOTA.handle();
unsigned long currentMillis = millis();
Serial.println("Contacting Time Server");
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
struct tm tmstruct ;
delay(200);
tmstruct.tm_year = 0;
getLocalTime(&tmstruct, 5000);
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct.tm_year)+1900,( tmstruct.tm_mon)+1, tmstruct.tm_mday,tmstruct.tm_hour , tmstruct.tm_min, tmstruct.tm_sec);
Serial.println("");
yy = (tmstruct.tm_year)+1900;
mm = ( tmstruct.tm_mon)+1;
dd = tmstruct.tm_mday;
hr = (tmstruct.tm_hour);
mi = (tmstruct.tm_min);
se = (tmstruct.tm_sec);
getReadings();
delay(20);
getBME();
delay(20);
displayText();
String path = "/" + mm + "-" + dd + ".csv"; // format maand dag
headerMessage = "Tijd, temperature,humidity,pressure,tempbu \n"; // tijd,temperature, humidity, pressure, tempbu
dataMessage = String(hr) + ":" + String(mi) + ":" + String(se) + "," + String(temperature) + "," + String(humidity) + "," + String(pressure) + "," + String(tempbu)+ "\r\n"; // uur:minuut:sec,temperature, humidity, pressure, tempbu
File file = SD.open(path.c_str());
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, path.c_str(), headerMessage.c_str());
}
else {
Serial.println("File already exists");
}
file.close();
if (dd != dd) { // nieuwe dag
writeFile(SD, path.c_str(), headerMessage.c_str());
}
if (currentMillis - previousMillis > interval) {
appendFile(SD, path.c_str(), dataMessage.c_str());// SD
previousMillis = currentMillis;
}
}
//**************** VOIDS *****************************
void startOTA(){
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type); // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void initTFT(){
tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
tft.setTextWrap(false); // Allow text to run off right edge
tft.fillScreen(ST77XX_BLACK);
}
void displayText() {
int x;
x=random(0,100);
tft.setRotation(-45);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, x);
tft.setTextColor(ST77XX_RED);
tft.setTextSize(2);
tft.print("Temp:");
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(2);
tft.println(temperature);
delay(1500);
tft.fillScreen(ST77XX_BLACK);
x=random(0,100);
tft.setCursor(0,x);
tft.setTextColor(ST77XX_GREEN);
tft.setTextSize(2);
tft.print("Hum :");
tft.setTextColor(ST77XX_GREEN);
tft.setTextSize(2);
tft.println(humidity);
delay(1500);
tft.fillScreen(ST77XX_BLACK);
x=random(0,100);
tft.setCursor(0,x);
tft.setTextColor(ST77XX_BLUE);
tft.setTextSize(2);
tft.print("Pres:");
tft.setTextColor(ST77XX_BLUE);
tft.println(pressure);
delay(1500);
tft.fillScreen(ST77XX_BLACK);
x=random(0,100);
tft.setCursor(0,x);
tft.setTextColor(ST77XX_RED);
tft.setTextSize(2);
tft.print("Tempbu:");
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(2);
tft.println(tempbu);
delay(2500);
tft.fillScreen(ST77XX_GREEN);
x=random(0,70);
tft.setCursor(20, x);
tft.setTextSize(2);
tft.setTextColor(ST77XX_BLACK);
tft.println("Datum:");
tft.println("");
tft.print(yy);
tft.print(":");
tft.print(mm);
tft.print(":");
tft.println(dd);
delay(2500);
tft.fillScreen(ST77XX_GREEN);
x=random(0,70);
tft.setCursor(30, x);
tft.println("Time:");
tft.println("");
tft.print(hr);
tft.print("-");
tft.print(mi);
tft.print("-");
tft.println(se);
delay(2500);
}
void initSD() {
SD.begin(2);
Serial.print("Initializing SD card...");
if (!SD.begin(2))
{
Serial.println("Card failed, or not present"); // see if the card is present and can be initialized:
while (1) ; // don't do anything more: nu mag door ook als er geen kaart is
}
Serial.println("card initialized.");
delay(10);
Serial.println("\r\n");
}
void startWIFI() { // Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); {
while (WiFi.waitForConnectResult() != WL_CONNECTED) { // while (WiFi.status() != WL_CONNECTED)
delay(1000);
Serial.println("Connecting to WiFi..");
Serial.println(WiFi.localIP()); // Print ESP32 Local IP Address
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
}
}
void getReadings(){ // Function to get DS18B20 temperature
sensors.requestTemperatures();
delay(30);
tempbu = sensors.getTempCByIndex(0); // Temperature in Celsius
delay(30);
}
void getBME() { // BME-280 readings
temperature = bme.readTemperature();
delay(20);
humidity = bme.readHumidity();
delay(20);
pressure = bme.readPressure()/ 100.0F;
delay(20);
}
void startmDNS() {
if (!MDNS.begin("esp32")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(100);
}
}
Serial.println("mDNS responder started");
server.begin(); // Start TCP (HTTP) server
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80); // Add service to MDNS-SD
}
void appendFile(fs::FS &fs, const char * path, const char * message) { // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
Serial.println("");
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message");
Serial.print(dataMessage.c_str());
Serial.println("Appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message) { // Write to the SD card (DON'T MODIFY THIS FUNCTION)
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
ever it still bodders me and sorry for your effort.
Confusing pin-out.
The TFT: VSPI RST 19, CS 5, D/C 22 (conflict with my sensor on 21 and 22 SCL/SDA) , DIN 23, clk 18
as far as I understand no pins should be used by both.
For now I,am happy.
Thanks .