Last active
January 2, 2022 19:41
-
-
Save SimedruF/12f478224f5747f12e263f6eec2536e5 to your computer and use it in GitHub Desktop.
ESP32_BMP280_withdisplay.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Florin Simedru | |
Complete project details at https://automatic-house.blogspot.com | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <Adafruit_ST7735.h> | |
#include <Adafruit_GFX.h> | |
// These pins will also work for the 1.8" TFT shield | |
#define TFT_MOSI 23 // SDA Pin on ESP32 | |
#define TFT_SCLK 18 // SCL Pin on ESP32 | |
#define TFT_CS 5 // Chip select control pin | |
#define TFT_DC 2 // Data Command control pin | |
#define TFT_RST 4 // Reset pin (could connect to RST pin) | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 160 // OLED display height, in pixels | |
/* TFT ST7735 configuration */ | |
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
void printValues(); | |
void PrintInfoServer(); | |
Adafruit_BME280 bme; // I2C | |
unsigned long delayTime; | |
unsigned long previousMillis = 0; | |
const long interval = 1000; | |
void setup() { | |
Serial.begin(115200); | |
while(!Serial); // time to get serial running | |
Serial.println(F("BME280 test")); | |
unsigned status; | |
// default settings | |
status = bme.begin(); | |
// You can also pass in a Wire library object like &Wire2 | |
status = bme.begin(0x76); | |
if (!status) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); | |
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16); | |
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); | |
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); | |
Serial.print(" ID of 0x60 represents a BME 280.\n"); | |
Serial.print(" ID of 0x61 represents a BME 680.\n"); | |
while (1) delay(10); | |
} | |
Serial.println("-- Default Test --"); | |
delayTime = 1000; | |
// Use this initializer if you're using a 1.8" TFT | |
display.initR(INITR_GREENTAB); // Init ST7735 display 128x160 pixel | |
delay(100); | |
display.fillScreen(ST77XX_RED); | |
delay(100); | |
display.fillScreen(ST77XX_GREEN); | |
delay(100); | |
display.fillScreen(ST77XX_BLUE); | |
delay(100); | |
display.fillScreen(ST77XX_BLACK); | |
display.setTextSize(1); | |
display.setTextColor(ST77XX_WHITE); | |
display.setCursor(0, 0); | |
display.setRotation(3); | |
display.fillScreen(ST77XX_BLACK); | |
display.setTextSize(1); | |
display.setCursor(10, 10); | |
display.print(" ST7735 TFT display initiated!"); | |
Serial.println(); | |
} | |
void loop() { | |
printValues(); | |
delay(delayTime); | |
PrintInfoServer(); | |
} | |
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("Approx. Altitude = "); | |
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); | |
Serial.println(" m"); | |
Serial.print("Humidity = "); | |
Serial.print(bme.readHumidity()); | |
Serial.println(" %"); | |
Serial.println(); | |
} | |
void PrintInfoServer() { | |
unsigned long currentMillis = millis(); | |
if((currentMillis - previousMillis) >= interval) { | |
previousMillis = millis(); | |
#if (DHT_DEBUG==1) | |
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " % - Pressure: " + pressure + " hPa"); | |
#endif | |
display.fillScreen(ST77XX_BLACK); | |
// display temperature | |
display.setTextSize(1); | |
display.setTextColor(ST77XX_WHITE); | |
display.setCursor(10,10); | |
display.print("Temperature: "); | |
display.print(bme.readTemperature()); | |
display.print(" "); | |
display.setTextSize(1); | |
display.write(248); | |
display.setTextSize(1); | |
display.print(" C"); | |
// display humidity | |
display.setTextSize(1); | |
display.setCursor(10, 20); | |
display.print("Humidity: "); | |
display.print(bme.readHumidity()); | |
display.print(" %"); | |
// display altitude | |
display.setTextSize(1); | |
display.setCursor(10, 30); | |
display.print("Altitude:"); | |
display.print( bme.readAltitude(SEALEVELPRESSURE_HPA)); | |
display.print(" m"); | |
// display pressure | |
display.setTextSize(1); | |
display.setTextColor(ST77XX_WHITE); | |
display.setCursor(10,40); | |
display.print("Pressure: "); | |
display.print(bme.readPressure() / 100.0F); | |
display.print(" hPa"); | |
display.setTextSize(1); | |
display.setTextColor(ST77XX_GREEN); | |
display.setCursor(10,50); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment