-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1mini_logger.ino
352 lines (314 loc) · 11.2 KB
/
d1mini_logger.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Hardware:
* - D1 mini (ESP8266)
* - Data logger shield
* * RTC (DS1307)
* D1 SCL
* D2 SDA
* * microSD
* D5 CLK
* D6 MISO
* D7 MOSI
* D8 CS
* - Temperatursensor BME280
* D1 SCL
* D2 SCA
* 6 polige BME Variante versorgt mit 3,3V
* CSB = 3,3V (pullup)
* SDO = 3,3V (pullup)
* - OLED 128x64
* D1 SCL
* D2 SCA
* D4 Versorgung
*/
#include <Wire.h> // fuer I2C
#include <ESP8266WiFi.h> // fuer WIFI aus- / einschalten
#include <Adafruit_BME280.h>
// lcd display include
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SD.h> // fuer SD karte
#include <RtcDS1307.h> // rtc include https://github.com/Makuna/Rtc/blob/master/examples/DS1307_Simple/DS1307_Simple.ino
// fuer sd
#define CS D8
RtcDS1307<TwoWire> Rtc(Wire);
RtcDateTime now;
Adafruit_BME280 bme1;
float temp1(NAN), hum1(NAN), press1(NAN);
bool SDKarteError = false; // sd karte nicht vorhanden oder nicht ok?
bool RTCError = false; // rtc nicht vorhanden oder nicht ok?
bool bme1SensorError = false; // bme sensor nicht vorhanden oder nicht ok?
bool displayError = false; // display initialisierung fehlgeschlagen?
// [ms] wie lange bleibt die anzeige an nach aktivierung ?
const unsigned long MS_ANZEIGEDAUER = 10000;
// [us] wie lange darf ich schlafen bevor ich wieder messe ?
const uint64_t US_SCHLAFINTERVAL = 120e6;
const String NAME_LOGDATEI = "loggerDaten";
const String NAME_LOGDATEI_CSV = NAME_LOGDATEI + String(".csv");
// lcd defines
#define OLED_RESET -1
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306* displayPtr = NULL;
/* ACHTUNG: der display konstruktor allokiert dynamischen speicher,
* da das display aber zur laufzeit an und ausgeschaltet werden soll
* um strom zu sparen muss der dynamische speicher wieder freigegeben
* werden. da die implementierung in der bibliothek fix ist muessen
* wir es bei uns loesen und da sind die new / delete operatoren
* die einzige moeglichkeit die ich aktuell sehe. */
// Achtung: D4 ist auch der Pin der User LED beim D1 mini
#define PIN_SCREEN_POWER D4
#define countof(a) (sizeof(a) / sizeof(a[0]))
/**
* diese setup funktion koennte man dynamisch zur Laufzeit aufrufen.
* damit koennte das display ein-/ausgeschaltet werden ohne dass
* der controller neugestartet / geresettet werden muss.
* in diesem fall muesste es mit periphBegin = false aufgerufen werden
* um das einfrieren durch doppelten aufruf von Wire.begin()
* zu vermeiden. */
void setupDisplay(bool periphBegin = false) {
digitalWrite(PIN_SCREEN_POWER, HIGH); // display einschalten
delay(100); // etwas zeit geben zum spannung stabilisieren (WICHTIG)
// etwas grosszuegig, aber wir habens ja nicht eilig :)
if (displayPtr) {
delete displayPtr;
displayPtr = NULL;
}
displayPtr = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
if (!displayPtr->begin(SSD1306_SWITCHCAPVCC, 0x3C, /*reset*/ true, /*periphBegin*/ periphBegin)){
Serial.println("display init error!");
displayError = true;
return;
}
displayPtr->setTextSize(2);
displayPtr->setTextColor(WHITE); // ohne Effekt -> das eingesetzte Display kann nur blau
displayPtr->clearDisplay();
displayPtr->setCursor(5, 0);
displayPtr->setTextSize(2);
displayPtr->println("Init");
displayPtr->display(); // Text zeigen
}
void setupSd()
{
Serial.print("Initialisiere SD-Karte...");
if (!SD.begin(CS)) {
Serial.println(" fehlgeschlagen!");
SDKarteError = true;
}
Serial.println(" fertig.");
// Datei mit Schreibzugriff oeffnen bzw erzeugen wenn nicht vorhanden.
File zielDatei = SD.open(NAME_LOGDATEI + String("_beschreibung.txt"), FILE_WRITE);
if (zielDatei) { // oeffnen erfolgreich?
Serial.print("Datei gefunden.");
// Spaltenueberschrift eintragen als beschreibung fuer csv datei
zielDatei.println("Datum;Uhrzeit;Temperatur;Feuchte;Druck;");
zielDatei.close();
} else { // fehler beim Zugriff die Datei...
Serial.println("Fehler beim Oeffnen der Datei auf SD Karte :(");
SDKarteError = true;
}
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u.%02u.%04u %02u:%02u:%02u"),
dt.Day(),
dt.Month(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
void setupRTC()
{
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
Serial.print("Kompilierzeitstempel: ");
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid()) {
if (Rtc.LastError() != 0) {
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
RTCError = true;
} else {
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
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.SetSquareWavePin(DS1307SquareWaveOut_Low);
}
void setupBME280()
{
int i;
for (i = 0; i < 5; i++) { // max 5x versuchen mit bme zu kommunizieren / initialisieren
if (!bme1.begin()) { // KEIN erfolg?
Serial.println("BME280 sensor nicht gefunden. Versuche es in 1s noch einmal.");
delay(1000); // dann 1s warten
} else {
break; // nicht weiter probieren
}
}
if (i >= 5) { // 5x ohne erfolg versucht zu kommunizieren?
bme1SensorError = true;
Serial.println("Konnte BME280 Sensor nicht finden!");
} else {
Serial.println("BME280 Sensor gefunden! ERFOLG!!");
}
if (!displayPtr) {
Serial.println("init display");
displayPtr = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
} else {
Serial.println("error display ptr not NULL");
}
}
void setup()
{
Serial.begin(74880); // optional: aber praktisch fuer debug ausgaben ;)
while (!Serial) ;
delay(200);
Serial.println(">>>>>>>>>>>>>>>>>>>> Starte Datenlogger...");
Serial.println(ESP.getFlashChipId(),HEX);
pinMode(PIN_SCREEN_POWER, OUTPUT); // spannungsversorgung vom display
digitalWrite(PIN_SCREEN_POWER, HIGH); // display einschalten
setupBME280();
setupSd();
setupRTC();
setupDisplay(true);
delay(200);
displayPtr->clearDisplay();
if (bme1SensorError) {
displayPtr->setCursor(5, 0);
displayPtr->println("Sensor-");
displayPtr->setCursor(5, 30);
displayPtr->print("fehler");
} else {
displayPtr->setCursor(5, 0);
displayPtr->println("Hallo");
displayPtr->setTextSize(2);
displayPtr->setCursor(5, 30);
displayPtr->print("Datenlogger");
}
displayPtr->display(); // Text zeigen
// WiFi.mode( WIFI_AP /*WIFI_OFF*/ );
// WiFi.forceSleepBegin();
// WiFi.forceSleepWake();
}
void readRTC() {
if (!Rtc.IsDateTimeValid()) {
if (Rtc.LastError() != 0) {
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
RTCError = true;
}
else {
// Common Causes:
// 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!");
}
}
now = Rtc.GetDateTime();
}
void writeDataToSD() {
// Zeilenaufbau CSV Datei (comma-sperated-value) => excel format
// Datum;Uhrzeit;Temperatur;Feuchte;Druck;
// 01.09.22;18:10:23;23.2;41.23;1023.22;
char buffer[80];
sprintf(buffer, "%02d.%02d.%d;%d:%d:%d;%4.1f;%.0f;%.0f;",
now.Day(), now.Month(), now.Year(),
now.Hour(), now.Minute(), now.Second(),
temp1, hum1, press1);
Serial.println(buffer);
File zielDatei = SD.open(NAME_LOGDATEI_CSV, FILE_WRITE);
if (zielDatei) { //existiert die Datei ?
zielDatei.println(buffer);
zielDatei.close();
} else { // Dateifehler
Serial.print("Fehler beim Schreiben auf SD-Karte");
}
}
void loop()
{
static unsigned long millisLastRead = 0; // letztes sensor auslesen
unsigned long curMillis = millis();
// anzeigezeit abgelaufen ?
if (curMillis > MS_ANZEIGEDAUER) {
// dann display ausschalten
//digitalWrite(PIN_SCREEN_POWER, LOW);
Serial.println("stoppe anzeige. lege mich schlafen...");
ESP.deepSleep(US_SCHLAFINTERVAL); // [us]
delay(100);
} else { // anzeigezeit laeuft ?
// dann alle 2s werte anzeigen/schreiben
if (curMillis - millisLastRead > 2000) {
readRTC();
if (!RTCError) {
printDateTime(now); // RTC Zeit
Serial.print(" | ");
}
if (!bme1SensorError) {
readAndPrintBME280Data();
}
if (!RTCError && !bme1SensorError && !SDKarteError) {
writeDataToSD();
}
millisLastRead = curMillis;
}
}
}
void readAndPrintBME280Data()
{
temp1 = bme1.readTemperature();
hum1 = bme1.readHumidity();
press1 = bme1.readPressure() / 100;
String Temperatur = String(temp1);
Serial.print("Temp = " + Temperatur + " C | ");
String feuchte = String(hum1);
Serial.print("Feuchte = " + feuchte + " %RF | ");
// ohne nachkommastellen darstellen und dabei richtig runden
String druck = String(int(press1 + 0.5f));
Serial.println("Druck = " + druck + " hPa");
// lcd
displayPtr->clearDisplay();
displayPtr->setTextSize(2);
displayPtr->setTextColor(WHITE);
displayPtr->setCursor(1, 1);
displayPtr->println(Temperatur + " C");
displayPtr->setCursor(1, 25);
displayPtr->println(feuchte + " % RF");
displayPtr->setCursor(1, 50);
displayPtr->println(druck + " hPa");
displayPtr->display(); // anzeige auftrag ausfuehren
}