/*
Hier werden Code-Schnipsel für das TEG-Projekt gesammelt
26.03.2024 der aktuelle Stand ist funktionstüchtig
HW: ESP32-WROOM, ESP32 D0WD V3
offene Punkte
- x Suche nach einem bekannten Netzwerk, ansonsten AP erstellen -> 27.03.2024 erledigt
- x statische IP -> 27.03.2024 erledigt
- Messdaten auf SD-Karte loggen,
- Zugriff auf Daten von überall, -> MySQL-Datenbank
- periodisches Update der Messdaten auf MySQL-DB bei bestehender Web-Verbindung
- Webpage auf SD-Karte auslagern, nicht mehr im Programmquellcode
- Darstellung der Messdaten im Browser
- Darstellung der Messdaten auf Display (TFT,OLED)
- Programmmodule in Bibliotheken auslagern
Autor: Christian Köppen
letzte Änderung: 27.03.2024
*/
// --- SPI Kommunikation einfügen ---
#include "SPI.h"
// --- I2C Kommunikation einfügen---
#include <Wire.h>
#define I2C_SDA 21
#define I2C_SCL 22
// --- INA219 einfügen ---
// Strom und Leistungsmessung
#include <INA219_WE.h>
#define I2C_ADDRESS 0x40
INA219_WE ina219 = INA219_WE(I2C_ADDRESS);
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
// --- SD-Card einfügen ---
#include "FS.h"
#include "SD.h"
// --- WLAN einfügen ---
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#include <HTTPClient.h>
const char* ssid_AP = "TEG-AP_lokal";
const char* password_AP = "123456789";
const char* ssid0 = "Redmi Note 12 Pro 5G";
const char* password0 = "012345678";
const char* ssid1 = "HUAWEI_E5785_9C01";
const char* password1 = "E2NTBJ9YTHG";
const char* ssid2 = "TP-Link_5D21";
const char* password3 = "Sandra+Christian";
// Set your Static IP address
IPAddress local_IP(192, 168, 1, 199);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
// Set web server port number to 80
AsyncWebServer server(80); // ggf. gegen die Zeile drunter WebServer auskommentieren...Stand 27.03.
// WiFiServer server(80); // ggf. gegen die Zeile drüber AsyncWebServer auskommentieren... Stand 26.03.
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
// --- den ADS1115 einfügen ---
//#include<ADS1115_WE.h>
#include<Adafruit_ADS1x15.h>
#define I2C_ADDRESS 0x48
Adafruit_ADS1115 ads1;
float V_TKaltIn_raw = 0.0; // Spannung am ADS für PT1000 des TKaltIn
float V_TWarmIn_raw = 0.0; // Spannung am ADS für PT1000 des TWarmIn
// --- den BME280 einfügen ---
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme280;
#define bme280_adr 0x76
#define SEALEVELPRESSURE_HPA (1013.25)
float amb_Druck = 1013.25;
float amb_Feuchte = 50.0;
float amb_Temperatur = 20.0;
int amb_Hoehe = 0;
// --- den MCP23017 einfügen ---
#include <MCP23017.h>
#define MCP_ADDRESS 0x20 // (A2/A1/A0 = LOW)
#define relay1 0 //A0 // 1. Relais an A0
#define relay2 8 //B0 // 2. Relais an B0
MCP23017 mcp = MCP23017(MCP_ADDRESS);
// --- den RTC einfügen ---
#include "RTClib.h"
RTC_DS3231 rtc;
// --- das TFT einfügen ---
#include <TFT_eSPI.h>
#include <Adafruit_GFX.h>
TFT_eSPI tft = TFT_eSPI();
// --- das OLED einfügen ---
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define BACK_COLOR 0x0000 //Hintergrundfarbe
// --- Programmteil -----
//---------------------------------------------------------------------------------
void WebAusgabe (){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("</style></head><body><h1>ESP32 with BME280</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
client.println(bme280.readTemperature());
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
client.println(1.8 * bme280.readTemperature() + 32);
client.println(" *F</span></td></tr>");
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println(bme280.readPressure() / 100.0F);
client.println(" hPa</span></td></tr>");
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
client.println(bme280.readAltitude(SEALEVELPRESSURE_HPA));
client.println(" m</span></td></tr>");
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
client.println(bme280.readHumidity());
client.println(" %</span></td></tr>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
void StromMessen(){
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV/1000);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
if(!ina219_overflow){
Serial.println("Values OK - no overflow");
}
else{
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
}
void Temperatur_messen(){
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3, faktor;
faktor = ads1.toVoltage(1);
Serial.print("Faktor: "); Serial.println(faktor,4);
adc0 = ads1.readADC_SingleEnded(0);
adc1 = ads1.readADC_SingleEnded(1);
adc2 = ads1.readADC_SingleEnded(2);
adc3 = ads1.readADC_SingleEnded(3);
volts0 = ads1.computeVolts(adc0);
volts1 = ads1.computeVolts(adc1);
volts2 = ads1.computeVolts(adc2);
volts3 = ads1.computeVolts(adc3);
Serial.print("adc0: "); Serial.print(adc0); Serial.print(" U0: "); Serial.print(volts0); Serial.println(" V");
Serial.print("adc1: "); Serial.print(adc1); Serial.print(" U1: "); Serial.print(volts1); Serial.println(" V");
Serial.print("adc2: "); Serial.print(adc2); Serial.print(" U2: "); Serial.print(volts2); Serial.println(" V");
Serial.print("adc3: "); Serial.print(adc3); Serial.print(" U3: "); Serial.print(volts3); Serial.println(" V");
}
void ZeitLesen (){
// tft.fillRect(0,0,320,10,BACK_COLOR);
DateTime now = (rtc.now() + TimeSpan(0,1,0,0)); // 1h Zeitverschiebung (Berlin)
//buffer can be defined using following combinations:
//hh - the hour with a leading zero (00 to 23)
//mm - the minute with a leading zero (00 to 59)
//ss - the whole second with a leading zero where applicable (00 to 59)
//YYYY - the year as four digit number
//YY - the year as two digit number (00-99)
//MM - the month as number with a leading zero (01-12)
//MMM - the abbreviated English month name ('Jan' to 'Dec')
//DD - the day as number with a leading zero (01 to 31)
//DDD - the abbreviated English day name ('Mon' to 'Sun')
// tft.setTextColor(ILI9341_WHITE);
// tft.setCursor(0,0);
char Datum[] = " C DD.MM.YYYY hh:mm:ss";
// tft.print(rtc.getTemperature(),1);
Serial.println(now.toString(Datum));
}
void setup() {
// serielle Kommunikation starten
Serial.begin(115200);
Serial.println("Initialisierung läuft.......");
Serial.println("");
Serial.println("01 Serielle Kommunikation gestartet: 115200 baud");
// 2Drahtkommunikation starten
Wire.begin(I2C_SDA, I2C_SCL);
Serial.print("02 I2C Kommunikation gestartet über - SDA:");
Serial.print(I2C_SDA);
Serial.print(" SCL:");
Serial.println(I2C_SCL);
// OLED starten
if(!oled1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("03 !!! ... OLED nicht gefunden"));
for(;;);
}
else {
Serial.println("03 OLED on");
}
// SD-Card starten
if(!SD.begin()){
Serial.println("04 !!! ... keine SD-Modul gefunden");
// return;
}
else{
Serial.println("04 SD-Modul on");
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("05 !!! ... keine SD-Karte gefunden");
// return;
}
else {
Serial.println("05 SD-Karte gefunden:");
}
Serial.print(" SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf(" SD Card Size: %lluMB\n", cardSize);
// WIFI starten
Serial.print("06 mit WLAN verbinden... ");
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { // statische iP vergeben
Serial.println("statische IP nicht konfigurierbar");
}
wifiMulti.addAP(ssid0, password0);
wifiMulti.addAP(ssid1, password1);
wifiMulti.addAP(ssid2, password2);
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (wifiMulti.run() == WL_CONNECTED){
// Print local IP address and start web server
Serial.println("");
Serial.println(" WLAN verbunden");
Serial.print(" IP Addresse: ");
Serial.println(WiFi.localIP());
server.on("/html", HTTP_GET, [](AsyncWebServerRequest *request){request->send(SD, "/index.html", "text/html");}); ///NEU!!!
server.begin();
}
else {
Serial.println("");
Serial.println(" !!! ... kann nicht mit bekannten WLANs verbinden");
Serial.println(" lokaler AP wird eingerichtet...")
WiFi.softAP(ssid_AP, password_AP);
IPAddress IP = WiFi.softAPIP();
Serial.print(" AP IP address: ");
Serial.println(IP);
}
// den INA219 starten
if(!ina219.init()){
Serial.println("07 !!! ... INA219 nicht verbunden");
}
else{
Serial.println("07 INA219 on");
}
//BME280 starten
if (!bme280.begin(bme280_adr)) {
Serial.println("08 !!! ... BME280 Sensor nicht gefunden");
}
else{
Serial.println("08 BME280 on");
}
// den ADS1115 starten
if (!ads1.begin(I2C_ADDRESS)) {
Serial.println("09 !!! ... ADS1115 nicht gefunden");
}
else{
Serial.println("09 ADS1115 on");
}
ads1.setGain(0);
// den MCP23017 starten
if (!mcp.Init()) {
Serial.println("10 !!! ... MCP23017 nicht gefunden");
}
else{
Serial.println("10 MCP23017 on");
}
// configure pin as outputs
mcp.setAllPins(A, HIGH);
mcp.setAllPins(B, HIGH);
// den RTC starten
if (!rtc.begin()) {
Serial.println("11 !!! ... RTC nicht gefunden");
}
else{
Serial.println("11 RTC on");
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // nimmt und setzt die Systemzeit, falls Batterie getrennt wurde
// das TFT starten
tft.init();
Serial.println("12 TFT gestartet");
tft.setRotation(1); // Querformat
tft.fillScreen(BACK_COLOR); // Hintergrundfarbe setzen
// Initialisierung abgeschlossen
Serial.println("");
Serial.println("... Initialisierung abgeschlossen");
Serial.println("");
}
void loop() {
ZeitLesen();
// WebAusgabe();
// amb_Druck = bme.readPressure();
// amb_Feuchte = bme.readHumidity();
// amb_Temperatur = bme.readTemperature();
// amb_Hoehe = bme.readAltitude(SEALEVELPRESSURE_HPA);
// mcp.digitalWrite(relay1,LOW); // relay1 ist an
// mcp.digitalWrite(relay2,LOW); // relay1 ist an
delay(1000);
// mcp.digitalWrite(relay1,HIGH); // relay1 ist aus
// mcp.digitalWrite(relay2,HIGH); // relay1 ist aus
}
Loading
ili9341-cap-touch
ili9341-cap-touch