-
Notifications
You must be signed in to change notification settings - Fork 0
/
display-clock-sensor.ino
126 lines (107 loc) · 3.18 KB
/
display-clock-sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//I2C device found at address 0x3C --> OLED-Display
//I2C device found at address 0x57 --> EEPROM on RTC
//I2C device found at address 0x68 --> RTC3231
//I2C device found at address 0x76 --> BME280
#include <Arduino.h>
#include <U8g2lib.h>
#include <Seeed_BME280.h>
#include <Wire.h>
#include <RtcDS3231.h>
#define SERIAL_BAUD 57600
/* Constructor */
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R1, /* reset=*/ U8X8_PIN_NONE);
BME280 bme;
RtcDS3231<TwoWire> Rtc(Wire);
void setup() {
Serial.begin(SERIAL_BAUD);
while(!Serial) {} // Wait
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Wire.begin();
u8g2.begin();
u8g2.setFlipMode(0);
u8g2.enableUTF8Print();
if(!bme.init()) {
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
void loop() {
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
delay(60000);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt) {
float pressure;
char datestring[11];
char timestring[9];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u.%02u.%04u"),
dt.Day(),
dt.Month(),
dt.Year());
snprintf_P(timestring,
countof(timestring),
PSTR("%02u:%02u"),
dt.Hour(),
dt.Minute());
//Serial.print(datestring);
RtcTemperature temp = Rtc.GetTemperature();
u8g2.firstPage();
do {
//u8g2.setFontMode(1);
u8g2.setFont(u8g2_font_6x13_mf);
u8g2.setCursor(0,15);
u8g2.print(datestring);
u8g2.setCursor(0,30);
u8g2.print(timestring);
u8g2.setCursor(0,45);
u8g2.print(bme.getTemperature()); //+temp.AsFloatDegC()
u8g2.setCursor(0,60);
u8g2.print(bme.getHumidity());
u8g2.setCursor(0,75);
u8g2.print(pressure = bme.getPressure()/100);
//u8g2.print(" / ");
//u8g2.print(bme.calcAltitude(pressure));
//u8g2.sendBuffer();
} while ( u8g2.nextPage() );
}