Advertisement
ross_s

ESP32_TTGO_LoRa_BMP280_DS3231

Aug 7th, 2021
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BME280.h>
  4. #include "RTClib.h"
  5.  
  6. // bme/p 280 stuff
  7. #define BUFF_SIZE 100
  8. #define SEALEVELPRESSURE_HPA (1013.25)
  9. char buff[BUFF_SIZE];
  10. unsigned long delayTime;
  11.  
  12. Adafruit_BME280 bme; // I2C
  13.  
  14. RTC_DS3231 rtc;
  15. char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  16.  
  17. void setup()  {
  18.  
  19.   Serial.begin(9600);
  20.   if (! rtc.begin()) {
  21.     Serial.println("Couldn't find RTC");
  22.     //while (1);
  23.   }
  24.   rtc.adjust(DateTime(__DATE__, __TIME__));
  25.  
  26.   unsigned status;
  27.   // default settings
  28.   status = bme.begin(0x76);
  29.   // You can also pass in a Wire library object like &Wire2
  30.   // status = bme.begin(0x76, &Wire2)
  31.   if (!status) {
  32.     Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  33.     Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  34.     Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  35.     Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  36.     Serial.print("        ID of 0x60 represents a BME 280.\n");
  37.     Serial.print("        ID of 0x61 represents a BME 680.\n");
  38.     while (1) delay(10);
  39.   }
  40.  
  41.   Serial.println("-- Default Test --");
  42.   Serial.print("SensorID is: 0x"); Serial.println(bme.sensorID(), 16);
  43.   delayTime = 1000;
  44.  
  45.   Serial.println();
  46. }
  47.  
  48. void loop()
  49. {
  50.   printBME();
  51.   printRTC();  
  52.   delay(3000);
  53.  
  54. }
  55. void printBME() {
  56.   for (int i = 0; i < BUFF_SIZE; i++) {
  57.     snprintf(buff, sizeof(buff), "T:%.2f, H:%.2f, P:%.2f, A:%.2f\n",
  58.     bme.readTemperature(), bme.readHumidity(), (bme.readPressure() / 100.0F),
  59.     bme.readAltitude(SEALEVELPRESSURE_HPA));
  60.   }
  61.   Serial.print(buff);
  62. }
  63.  
  64. void printRTC(void) {
  65.   DateTime now = rtc.now();
  66.   Serial.print("Current Time: ");
  67.   Serial.print(now.hour(), DEC);
  68.   Serial.print(":");
  69.   Serial.println(now.minute(), DEC);
  70.   Serial.print("Todays Date: ");
  71.   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  72.   Serial.print(" ");
  73.   Serial.print(now.day(), DEC);
  74.   Serial.print("/");
  75.   Serial.print(now.month(), DEC);
  76.   Serial.print("/");
  77.   Serial.println(now.year(), DEC);
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement