<#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <TFT_22_ILI9225.h>
#include <../fonts/FreeSans9pt7b.h>
#include <../fonts/FreeSans12pt7b.h>
#include <../fonts/FreeSerif9pt7b.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#if defined (ARDUINO_ARCH_STM32F1)
#define TFT_RST PA1
#define TFT_RS PA2
#define TFT_CS PA0 // SS
#define TFT_SDI PA7 // MOSI
#define TFT_CLK PA5 // SCK
#define TFT_LED 0 // 0 if wired to +5V directly
#elif defined(ESP8266)
#define TFT_RST 4 // D2
#define TFT_RS 5 // D1
#define TFT_CLK 14 // D5 SCK
//#define TFT_SDO 12 // D6 MISO
#define TFT_SDI 13 // D7 MOSI
#define TFT_CS 15 // D8 SS
#define TFT_LED 2 // D4 set 0 if wired to +5V directly -> D3=0 is not possible !!
#elif defined(ESP32)
#define TFT_RST 26 // IO 26
#define TFT_RS 25 // IO 25
#define TFT_CLK 14 // HSPI-SCK
//#define TFT_SDO 12 // HSPI-MISO
#define TFT_SDI 13 // HSPI-MOSI
#define TFT_CS 15 // HSPI-SS0
#define TFT_LED 0 // 0 if wired to +5V directly
SPIClass hspi(HSPI);
#else
#define TFT_RST 8
#define TFT_RS 9
#define TFT_CS 10 // SS
#define TFT_SDI 11 // MOSI
#define TFT_CLK 13 // SCK
#define TFT_LED 3 // 0 if wired to +5V directly
#endif
#define TFT_BRIGHTNESS 200 // Initial brightness of TFT backlight (optional)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
int BMEwaitT = 1000;
int delayTime = BMEwaitT;
// Use hardware SPI (faster - on Uno: 13-SCK, 12-MISO, 11-MOSI)
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS);
// Use software SPI (slower)
//TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_SDI, TFT_CLK, TFT_LED, TFT_BRIGHTNESS);
// Variables and constants
int16_t x = 0, y = 0, w, h;
int orientation ();
void setup() {
Serial.begin(115200);
Serial.println(F("BME280 Startup"));
if (! bme.begin(0x76, &Wire)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
Serial.println("normal mode, 16x oversampling for all, filter off,");
Serial.println("0.5ms standby period");
delayTime = BMEwaitT;
Serial.println();
#if defined(ESP32)
hspi.begin();
tft.begin(hspi);
#else
tft.begin();
#endif
tft.clear();
tft.setOrientation (3);
String s1 = "Temperature";
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s1, x, y, &w, &h); // Get string extents
y = h; // Set y position to string height
tft.drawGFXText(x, y, s1, COLOR_BLUE); // Print string
String s2 = "Humidity 48 %";
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s2, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height
tft.drawGFXText(x, y, s2, COLOR_BLUE); // Print string
String s3 = "Air Pressure Hpa";
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s3, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height
tft.drawGFXText(x, y, s3, COLOR_BLUE); // Print string
String s4 = "Air Pressure inHg";
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s4, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height
tft.drawGFXText(x, y, s4, COLOR_BLUE); // Print string
String s5 = "Air Pressure mmHg"; // Create string object
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s5, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height plus shift down 10 pixels
tft.drawGFXText(x, y, s5, COLOR_BLUE); // Print string
String s6 = "Altitude 1408m"; // Create string object
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s6, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height plus shift down 10 pixels
tft.drawGFXText(x, y, s6, COLOR_BLUE); // Print string
String s7 = "LAT -25.731340"; // Create string object
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s7, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height plus shift down 10 pixels
tft.drawGFXText(x, y, s7, COLOR_BLUE); // Print string
String s8 = "LON 28.218370"; // Create string object
tft.setGFXFont(&FreeSerif9pt7b); // Set current font
tft.getGFXTextExtent(s8, x, y, &w, &h); // Get string extents
y += h + 6; // Set y position to string height plus shift down 10 pixels
tft.drawGFXText(x, y, s8, COLOR_BLUE); // Print string
}
void loop() {
// Only needed in forced mode! In normal mode, you can remove the next line.
bme.takeForcedMeasurement(); // has no effect in normal mode
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Pressure = ");
Serial.print((bme.readPressure() / 100.0F) * 0.03);
Serial.println(" inHg");
Serial.print("Pressure = ");
Serial.print((bme.readPressure() / 100.0F) * 0.75006157584566);
Serial.println(" mmHg");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}>