-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeStation.ino
149 lines (125 loc) · 3.35 KB
/
homeStation.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
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT11 // DHT 11
DHT dht(7, DHT11);
int DHTmoisture = 0;
int DHThumidity = 0;
/* LIGHT */
int LIGHTpin = 2;
int LIGHTval;
int LIGHTadjustedVal;
int LIGHTmin;
int LIGHTmax;
/* GAS SENSOR */
const int GasAOUTpin = 0; //the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int GasDOUTpin = 8; //the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
int GasLimit;
int GasValue;
/* LCD */
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int LCDcols = 20;
int LCDrows = 4;
/* HODINY */
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Ne", "Po", "Ut", "St", "Ct", "Pa", "So"};
void setup()
{
Serial.begin(9600);
/* HODINY */
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
/* GAS SENSOR */
pinMode(GasDOUTpin, INPUT); //sets the pin as an input to the arduino
/* DHT */
dht.begin();
/* LIGHT */
LIGHTval = analogRead(LIGHTpin);
LIGHTmin = LIGHTval;
LIGHTmax = LIGHTval;
/* LCD */
lcd.begin(20, 4); //define o LCD 20 cols and 4 lines
lcd.setCursor(0, 1);
lcd.print("Svetlo: ");
lcd.setCursor(0, 2);
lcd.print("Plyn: ");
}
void loop()
{
/* GAS SENSOR */
GasValue = analogRead(GasAOUTpin); //reads the analaog value from the CO sensor's AOUT pin
GasLimit = digitalRead(GasDOUTpin); //reads the digital value from the CO sensor's DOUT pin
/* LCD - GAS */
clearRowFrom(6, 2);
lcd.setCursor(6, 2);
lcd.print(String(GasValue) + " (" + String(GasLimit ? "OK":"PROBLEM") + ")");
/* LCD HODINY */
clearRow(0);
lcd.setCursor(0, 0);
lcd.print(getDateTimeString());
/* LIGHT */
int LIGHTval = analogRead(LIGHTpin);
if (LIGHTval > LIGHTmax) LIGHTmax = LIGHTval;
if (LIGHTval < LIGHTmin) LIGHTmin = LIGHTval;
LIGHTval = constrain(LIGHTval, LIGHTmin, LIGHTmax);
LIGHTadjustedVal = map(LIGHTval, LIGHTmin, LIGHTmax, 100, 0);
clearRowFrom(8, 1);
lcd.setCursor(8, 1);
lcd.print(String(LIGHTval) + "|" + String(LIGHTadjustedVal));
/* DHT */
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
/* LOOP */
delay(1000);
/* DHT */
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
lcd.setCursor(0, 3);
lcd.print("H: "+ String(h) + "% " + "T: " + String(t) + "'C");
}
}
void clearRow(int row)
{
clearRowFrom(0, row);
}
void clearRowFrom(int col, int row)
{
lcd.setCursor(col, row);
for (int i = 0; i < LCDcols - col; i++){
lcd.print(" ");
}
}
String zeroLeading(int num)
{
if (num >= 10) return "";
return "0";
}
String getDateTimeString() {
DateTime now = rtc.now();
String string = String("");
string += daysOfTheWeek[now.dayOfTheWeek()];
string += " ";
string += now.day();
string += '.';
string += now.month();
string += '.';
string += now.year() - 2000;
string += " ";
string += zeroLeading(now.hour()) + now.hour();
string += ':';
string += zeroLeading(now.minute()) + now.minute();
string += ':';
string += zeroLeading(now.second()) + now.second();
return string;
}