RaspBar icon

ESP8266_BME280_ST7735.ino

RaspBar | PRO | 02/01/23 05:30:26 AM UTC (Edited) | 0 ⭐ | 319 👁️ | Never ⏰ | []
C++ |

5.29 KB

|

None

|

0 👍

/

0 👎

/**************************************************************************
 * 
 * ESP8266 NodeMCU (ESP-12E)mit ST7735S TFT Display (128x160 Pixel) 
 * und BME280 Luftdruck-, Temperatur- und Feuchtigkeitssensor.
 * 
 *************************************************************************/
 
#include <Wire.h>              // einbinden von Wire library (wird für I2C-Geräte benötigt)
#include <Adafruit_GFX.h>      // einbinden von Adafruit graphics library
#include <Adafruit_ST7735.h>   // einbinden von Adafruit ST7735 TFT library
#include <Adafruit_BME280.h>   // einbinden von Adafruit BME280 sensor library
 
// LED Setup
int LEDrot = D0; // -> GPIO16
int LEDblau = D3; // -> GPIO0
 
// ST7735 TFT Modul
#define TFT_RST   -1     // TFT-RST-Pin ist verbunden mit dem RST-Pin vom ESP8266
#define TFT_CS    D8     // TFT-CS-Pin ist verbunden mit dem Pin D8 (GPIO15)
#define TFT_DC    D4     // TFT-DC-Pin ist verbunden mit dem Pin D4 (GPIO2)
 
// Display-Objekt anlegen -> tft
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
 
// I2C-Adresse für BME280 festlegen: 0x76 oder 0x77 (Achtung: 0x77 ist die Standard-Adresse)
#define BME280_I2C_ADDRESS  0x76
// Sensor-Objekt anlegen -> bme280
Adafruit_BME280  bme280;
 
void setup(void)
{
  //LED initialisieren -> LED aus
  pinMode(LEDrot, OUTPUT);    // Pin D0 ist ein Ausgang.
  pinMode(LEDblau,OUTPUT);    // Pin D3 ist ein Ausgang.
  digitalWrite(LEDrot, LOW);  // Schalte die LED an Pin D0 aus. D0 ist beim Start des ESP8266 auf HIGH.
  digitalWrite(LEDblau, LOW); // Schalte die LED an Pin D3 aus.
  
  //TFT-Display initialisieren
  tft.initR(INITR_BLACKTAB);        // ST7735S initialisieren
  tft.fillScreen(ST7735_BLACK);     // Displayfläche mit Farbe füllen
  tft.drawFastHLine(0, 30,  tft.width(), ST7735_WHITE);   // horizontale Linie an Position (0, 30) zeichnen
 
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);  // Textfarbe (weiss auf schwarz)setzen
  tft.setTextSize(1);               // Textgröße = 1
  tft.setCursor(4, 1);              // Cursor auf Pixelposition (4, 1) setzen
  tft.print("ESP8266 + ST7735 TFT");
  tft.setCursor(19, 15);            // Cursor auf Pixelposition (19, 15) setzen
  tft.print("WETTER STATION");
 
  // BME280 Sensor initilisieren
  Wire.begin(D2, D1);               // I2C Pins setzen [SDA = D2, SCL = D1], Taktrate ist 100kHz
  if( bme280.begin(BME280_I2C_ADDRESS) == 0 )
  {  // Rückmeldung Verbindungsfehler oder falsche Geräteadresse!
    tft.setTextColor(ST7735_RED, ST7735_BLACK);   // Textfarbe (rot auf schwarz)setzen
    tft.setTextSize(2);             // Textgröße = 2
    tft.setCursor(5, 76);           // Cursor auf Pixelposition (5, 76) setzen
    tft.print("Connection");
    tft.setCursor(35, 100);         // Cursor auf Pixelposition (35, 100) setzen
    tft.print("Error");
    while(1)  // Warten
      delay(1000);
  }
 
  tft.drawFastHLine(0, 76,  tft.width(), ST7735_WHITE);   // horizontale Linie bei (0, 76)
  tft.drawFastHLine(0, 122,  tft.width(), ST7735_WHITE);  // horizontale Linie bei (0, 122)
  tft.setTextColor(ST7735_RED, ST7735_BLACK);             // Textfarbe (rot auf schwarz)setzen
  tft.setCursor(25, 39);                                  // Cursor auf Pixelposition (25, 39) setzen
  tft.print("TEMPERATUR =");
  tft.setTextColor(ST7735_CYAN, ST7735_BLACK);            // Textfarbe (cyan auf schwarz) setzen
  tft.setCursor(25, 85);                                  // Cursor auf Pixelposition (25, 85) setzen
  tft.print("LUFTFEUCHTE =");
  tft.setTextColor(ST7735_GREEN, ST7735_BLACK);           // Textfarbe (grün auf Schwarz) setzen
  tft.setCursor(25, 131);                                 // Cursor auf Pixelposition (25, 131) setzen
  tft.print("LUFTDRUCK =");
  tft.setTextSize(2);                                     // Textgröße = 2
  // print °C
  tft.drawCircle(89, 56, 2, ST7735_YELLOW);               // Kreis für Grad-Zeichen ( ° )
  tft.setCursor(95, 54);                                  // Cursor auf Pixelposition (95, 54) setzen
  tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);          // Textfarbe (gelb auf schwarz) setzen
  tft.print("C");
 
}
 
// Hauptprogramm
void loop()
{
  // Sensorwerte vom BME280 auslesen
  float temp = bme280.readTemperature();    // Temperatur in °C
  float humi = bme280.readHumidity();       // Luftfeuchte in %
  float pres = bme280.readPressure();       // Luftdruck in Pa
 
  // Daten auf Display ausgeben
  
  // 1: Temperatur (in °C)
  tft.setCursor(11, 54);
  tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);  // Textfarbe (gelb auf schwarz) setzen
  if(temp < 0)    // wenn temp < 0
    tft.printf( "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
  else            // temp >= 0
    tft.printf( " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );
 
  // 2: Luftfeuchte (in %)
  tft.setCursor(23, 100);
  tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK);  // Textfarbe (magenta auf schwarz) setzen
  tft.printf( "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
 
  // 3: Luftdruck (in hPa)
  tft.setCursor(3, 146);
  tft.setTextColor(0xFD00, ST7735_BLACK);  // Textfarbe (orange auf schwarz) setzen
  tft.printf( "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) );
  tft.setCursor(91, 146);
  tft.print("hPa");
  delay(5000);    // Display aktualisieren
}
 

Comments