Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino BME680 & Waveshare 2.9 epaper via SPI - NOT WORKING?!
- #include <SPI.h>
- #include <GxEPD2_BW.h>
- #include <Fonts/FreeMonoBold9pt7b.h>
- #include <Adafruit_Sensor.h>
- #include "Adafruit_BME680.h"
- // SPI pin mapping (ESP32)
- #define BME_SCK 18
- #define BME_MISO 19
- #define BME_MOSI 23
- #define BME_CS 21
- #define DISP_CS 5
- #define DISP_DC 17
- #define DISP_RST 16
- #define DISP_BUSY 4
- #define SEALEVELPRESSURE_HPA (1013.25)
- // Display driver
- GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> display(
- GxEPD2_290_T94_V2(DISP_CS, DISP_DC, DISP_RST, DISP_BUSY)
- );
- // BME680 via SPI
- Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
- bool sensor_error = false;
- float temperature, humidity, pressure, gas, alt;
- // Chip select switching
- void selectSensor() {
- digitalWrite(DISP_CS, HIGH);
- digitalWrite(BME_CS, LOW);
- sensor_error = false;
- }
- void selectDisplay() {
- digitalWrite(BME_CS, HIGH);
- digitalWrite(DISP_CS, LOW);
- }
- // Read sensor data
- void readValues() {
- selectSensor();
- if (!bme.performReading()) {
- sensor_error = true;
- Serial.println("Error reading from sensor!");
- return;
- }
- delay(400);
- temperature = bme.temperature;
- humidity = bme.humidity;
- pressure = bme.pressure;
- alt = bme.readAltitude(SEALEVELPRESSURE_HPA);
- Serial.print("Sensor values: ");
- Serial.print(temperature); Serial.print(" °C, ");
- Serial.print(humidity); Serial.print(" %, ");
- Serial.print(pressure); Serial.println(" Pa");
- }
- // Update display
- void displayValues(int16_t x, int16_t y, int16_t sep) {
- if (sensor_error) return;
- selectDisplay();
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(GxEPD_BLACK);
- display.setFullWindow();
- display.firstPage();
- do {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(x, y);
- display.print("T: "); display.print(temperature, 1); display.println(" C");
- display.setCursor(x, y + sep);
- display.print("H: "); display.print(humidity, 1); display.println(" %");
- display.setCursor(x, y + sep*2);
- display.print("P: "); display.print(pressure / 100.0, 1); display.println(" hPa");
- display.setCursor(x, y + sep*3);
- display.print("Alt: "); display.print(alt, 1); display.println(" m");
- } while (display.nextPage());
- Serial.println("Display updated.");
- }
- // Setup
- void setup() {
- Serial.begin(115200);
- delay(100);
- // Start SPI manually
- SPI.begin(BME_SCK, BME_MISO, BME_MOSI);
- // Configure CS pins
- pinMode(BME_CS, OUTPUT);
- pinMode(DISP_CS, OUTPUT);
- digitalWrite(BME_CS, HIGH);
- digitalWrite(DISP_CS, HIGH);
- // Initialize display
- selectDisplay();
- display.init(115200, true, 2, false); // works for you!
- // Clear display once
- display.setRotation(1);
- display.setFullWindow();
- display.firstPage();
- do {
- display.fillScreen(GxEPD_WHITE);
- } while (display.nextPage());
- // Initialize sensor
- selectSensor();
- if (!bme.begin()) {
- Serial.println("BME680 not found!");
- while (1);
- }
- bme.setTemperatureOversampling(BME680_OS_4X);
- bme.setHumidityOversampling(BME680_OS_2X);
- bme.setPressureOversampling(BME680_OS_4X);
- bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
- Serial.println("Setup complete.");
- delay(2000);
- }
- // Loop
- void loop() {
- Serial.println("\nNew cycle...");
- readValues();
- displayValues(20, 20, 20);
- Serial.println("Waiting 30 seconds...\n");
- delay(30000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement