From ebea5823f1ba466fb801859931571dafb4f3167b Mon Sep 17 00:00:00 2001 From: chweber Date: Sun, 13 Jan 2019 13:25:38 +0100 Subject: [PATCH] new file: BME280_with_OLED.ino /first try to play with BME280 sensor and an oled display --- BME280_with_OLED.ino | 143 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 BME280_with_OLED.ino diff --git a/BME280_with_OLED.ino b/BME280_with_OLED.ino new file mode 100644 index 0000000..ce2084b --- /dev/null +++ b/BME280_with_OLED.ino @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include + +#define OLED_RESET 4 +Adafruit_SSD1306 display(OLED_RESET); + +#define BME_SCK 13 +#define BME_MISO 12 +#define BME_MOSI 11 +#define BME_CS 10 + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; // I2C +//Adafruit_BME280 bme(BME_CS); // hardware SPI +//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); + + + +void setup() { + int i = 0; + int x_pos = 5; + int y_pos = 10; + + Serial.begin(9600); + Serial.println(F("BME280 test")); + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); + display.display(); + delay(2000); + display.clearDisplay(); + display.drawRect(0,0,128,64,WHITE); + + char zeile_1[18] = "Temp [C]:"; + char zeile_2[18] = "Druck [hPa]:"; + char zeile_3[18] = "Feuchte [%]:"; + + //Zeile 1 schreiben + while(i<12){ + display.drawChar(x_pos,y_pos,zeile_1[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + + //Zeile 2 schreiben + i = 0; + x_pos = 5; + + while(i<12){ + y_pos = 20; + display.drawChar(x_pos,y_pos,zeile_2[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + + // Zeile 3 schreiben + + i = 0; + x_pos = 5; + + while(i<12){ + y_pos = 30; + display.drawChar(x_pos,y_pos,zeile_3[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + + display.display(); + + if (!bme.begin()) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + while (1); + } +} + + +void loop() { + // Hilfsvariablen für das OLED initialisieren + int i = 0; + int x_pos = 0; + int y_pos = 0; + + // Temperatur auslesen und schreiben + + i = 0; // Zähler zurücksetzen + x_pos = 90; // X-Position für den Cursor auf dem OLED + char temp_s[5]; // Array für den Temperaturwert zur Ausgabe auf das OLED + dtostrf(bme.readTemperature(), 3, 2, temp_s); // Wert auslesen und in das Array schreiben + + // Wert aus dem Array auf das OLED ausgeben + + while(i<5){ + y_pos = 10; + display.drawChar(x_pos,y_pos,temp_s[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + +// Druck auslesen und schreiben + + i = 0; // Zähler zurücksetzen + x_pos = 78; // X-Position für den Cursor auf dem OLED + float pressure = bme.readPressure() / 100.0F; // Wert auslesen und Dezimalstelle für hPA setzen + char press_s[7]; // Array für den Luftdruck zur Ausgabe auf das OLED + dtostrf(pressure, 3, 2, press_s); // Wert auslesen und in das Array schreiben + + // Wert aus dem Array auf das OLED ausgeben + + while(i<7){ + y_pos = 20; + display.drawChar(x_pos,y_pos,press_s[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + +// Luftfeuchte auslesen und schreiben + + i = 0; // Zähler zurücksetzen + x_pos = 90; // X-Position für den Cursor auf dem OLED + char humi_s[5]; // Array für den Luftdruck zur Ausgabe auf das OLED + dtostrf(bme.readHumidity(), 3, 2, humi_s); // Wert auslesen und in das Array schreiben + + // Wert aus dem Array auf das OLED ausgeben + + while(i<5){ + y_pos = 30; + display.drawChar(x_pos,y_pos,humi_s[i],WHITE,BLACK,1); + display.display(); + i++; + x_pos = x_pos + 6; + } + + delay(2000); +}