From 3fe2f6b248684338fce41f2352073dcef13dd867 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 26 Jan 2018 23:05:14 +0100 Subject: [PATCH] bme280 --- platformio.ini | 4 ++- src/BME280.cpp | 46 ++++++++++++++++++++++++ src/BME280.h | 30 ++++++++++++++++ src/main.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++-------- src/mp3.cpp | 2 +- 5 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 src/BME280.cpp create mode 100644 src/BME280.h diff --git a/platformio.ini b/platformio.ini index c956651..f8861ae 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,7 @@ board = heltec_wifi_lora_32 board_f_cpu = 240000000L board_f_flash = 80000000L framework = arduino -build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_INFO +;build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_INFO -DCONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL=y lib_deps = ; Basecamp @@ -26,4 +26,6 @@ lib_deps = ; https://github.com/earlephilhower/ESP8266Audio.git https://github.com/h3ndrik/ESP8266Audio.git https://github.com/Gianbacchio/ESP8266_Spiram.git + Adafruit Unified Sensor + Adafruit BME280 Library lib_ignore = ESPAsyncTCP diff --git a/src/BME280.cpp b/src/BME280.cpp new file mode 100644 index 0000000..83d6257 --- /dev/null +++ b/src/BME280.cpp @@ -0,0 +1,46 @@ +#include "BME280.h" + +#include +#include +#include +#include + +BME280::BME280() {} + +bool BME280::begin(void) { +// SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); + pinMode(18, OUTPUT); + digitalWrite(18, HIGH); // disable LoRa_CS + delay(50); + bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI + while (!(status = bme->begin())) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + delay(500); + } + return true; +} + +void BME280::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(); +} + +float BME280::readTemperature(void) { return bme->readTemperature(); } +float BME280::readPressure(void) { return bme->readPressure() / 100.0F; } +float BME280::readHumidity(void) { return bme->readHumidity(); } diff --git a/src/BME280.h b/src/BME280.h new file mode 100644 index 0000000..31e77e0 --- /dev/null +++ b/src/BME280.h @@ -0,0 +1,30 @@ +#ifndef _BME280_H +#define _BME280_H + +#include +#include +#include +#include +#include + +static constexpr uint8_t BME_SCK = 5; // SCL/SCK +static constexpr int BME_MISO = 19; //SDO +static constexpr int BME_MOSI = 27; //SDA/SDI +static constexpr int BME_CS = 23; // CS + +#define SEALEVELPRESSURE_HPA (1013.25) + +class BME280 { + public: + BME280(); + bool begin(void); + void printValues(void); + float readTemperature(void); + float readPressure(void); + float readHumidity(void); + private: + Adafruit_BME280 *bme; + bool status; +}; + +#endif /* _BME280_H */ diff --git a/src/main.cpp b/src/main.cpp index 0b9d01b..565aa3e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,28 @@ #include + +/* Wiring (Heltec OLED Lora) + * GND O O GND + * 5V O O 5V + * 3V3 O O 3V3 + * GND O < 36 + * RX * * 17 + * TX * < 38 + * RST * BUTTON < 39 + * 0 * < 34 + * 22 * < 35 + * BME280_SDO 19 * LoRa_MISO * 32 + * BME280_CS 23 * * 33 + * 18 x LoRa_CS * 25 MAX98_DIN + * BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26 + * 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI + * 2 * LoRa_RST * 14 + * 4 * OLED_SDA * 12 MAX98_BCLK + * 17 * * 13 MAX98_LRC + * 16 * OLED_RST * 21 + */ + //Include Basecamp in this sketch #include #include @@ -20,6 +42,7 @@ #include "main.h" #include "mp3.h" +#include "BME280.h" U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); @@ -30,9 +53,11 @@ WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000); char timeStr[20]; +BME280 bme280; MP3 mp3; //Variables for the sensor and the battery +static const int buttonPin = 0; static const int ResetPin = 17; static const int SensorPin = 32; static const int BatteryPin = 34; @@ -51,17 +76,46 @@ String batteryTopic; String batteryValueTopic; void setup() { +// #include "soc/rtc_io_reg.h" +// #include "soc/sens_reg.h" +// +// SET_PERI_REG_MASK(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_X32N_MUX_SEL | RTC_IO_X32P_MUX_SEL); +// CLEAR_PERI_REG_MASK(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_X32P_RDE | RTC_IO_X32P_RUE | RTC_IO_X32N_RUE | RTC_IO_X32N_RDE); +// CLEAR_PERI_REG_MASK(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_X32N_MUX_SEL | RTC_IO_X32P_MUX_SEL); +// SET_PERI_REG_BITS(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_DAC_XTAL_32K, 1, RTC_IO_DAC_XTAL_32K_S); +// SET_PERI_REG_BITS(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_DRES_XTAL_32K, 3, RTC_IO_DRES_XTAL_32K_S); +// SET_PERI_REG_BITS(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_DBIAS_XTAL_32K, 0, RTC_IO_DBIAS_XTAL_32K_S); +// SET_PERI_REG_MASK(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_XPD_XTAL_32K); +// REG_SET_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_MUX_SEL_M); +// REG_CLR_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_RDE_M | RTC_IO_PDAC1_RUE_M); +// REG_SET_FIELD(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_FUN_SEL, 1); +// REG_SET_FIELD(SENS_SAR_DAC_CTRL1_REG, SENS_DEBUG_BIT_SEL, 0); +// const uint8_t sel = 4; /* sel = 4 : 32k XTAL; sel = 5 : internal 150k RC */ +// REG_SET_FIELD(RTC_IO_RTC_DEBUG_SEL_REG, RTC_IO_DEBUG_SEL0, sel); +// + + //configuration of the battery and sensor pins - pinMode(ResetPin, INPUT_PULLDOWN); - pinMode(SensorPin, INPUT_PULLDOWN); - pinMode(BatteryPin, INPUT); +// pinMode(ResetPin, INPUT_PULLDOWN); +// pinMode(SensorPin, INPUT_PULLDOWN); +// pinMode(BatteryPin, INPUT); + pinMode(buttonPin, INPUT_PULLUP); + + pinMode(18, OUTPUT); + digitalWrite(18, HIGH); // disable LoRa_CS //read the status of the doorsensor as soon as possible to determine the state that triggered it - sensorValue = digitalRead(SensorPin); +// sensorValue = digitalRead(SensorPin); //Initialize Basecamp iot.begin(); + bme280.begin(); + for (int i=0; i<5; i++) { + delay(500); + bme280.printValues(); + } + u8g2.begin(); delay(50); u8g2.clearBuffer(); // clear the internal memory @@ -143,7 +197,7 @@ void transmitStatus() { } //Read the current analog battery value - sensorValue = analogRead(BatteryPin); +// sensorValue = analogRead(BatteryPin); //sensorC stores the battery value as a char char sensorC[6]; //convert the sensor value to a string @@ -200,15 +254,28 @@ void loop() timeClient.getFormattedTime().toCharArray(timeStr, 50); u8g2.clearBuffer(); // clear the internal memory - u8g2.setFont(u8g2_font_inb19_mf); - u8g2.drawStr(0, 20, timeStr); - char title1[32]; char title2[32]; - strncpy(title1, titleStr, 22); title1[22] = '\0'; - strncpy(title2, titleStr+22, 32); title2[31] = '\0'; - u8g2.setFont(u8g2_font_prospero_bold_nbp_tf); // choose a suitable font - u8g2.drawUTF8(0, 45, title1); - u8g2.drawUTF8(0, 55, title2); + if (digitalRead(buttonPin == HIGH)) { + u8g2.setFont(u8g2_font_inb19_mf); + u8g2.drawStr(0, 20, timeStr); + + char title1[32]; char title2[32]; + strncpy(title1, titleStr, 22); title1[22] = '\0'; + strncpy(title2, titleStr+22, 32); title2[31] = '\0'; + char weather[32]; + u8g2.setFont(u8g2_font_profont12_mf); // choose a suitable font + sprintf(weather, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); + u8g2.drawUTF8(0, 30, weather); + u8g2.setFont(u8g2_font_prospero_bold_nbp_tf); // choose a suitable font + u8g2.drawUTF8(0, 54, title1); + u8g2.drawUTF8(0, 64, title2); + } else { + u8g2.setFont(u8g2_font_inb19_mf); + u8g2.drawUTF8(0,20, "Hallo!"); + u8g2.drawUTF8(20,45, "test"); + u8g2.drawUTF8(50,64, "X"); + } u8g2.sendBuffer(); +// bme280.printValues(); - delay(500); + delay(100); } diff --git a/src/mp3.cpp b/src/mp3.cpp index 0e07be6..57b3e38 100644 --- a/src/mp3.cpp +++ b/src/mp3.cpp @@ -72,7 +72,7 @@ bool MP3::begin() { buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize); buff->RegisterStatusCB(StatusCallback, (void*)"buffer"); out = new AudioOutputI2S(I2S_NUM_0, false); - out->SetPinout(12, 13, 23); + out->SetPinout(12, 13, 25); out->SetOutputModeMono(false); out->SetRate(44100); out->SetBitsPerSample(16);