-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-temp-greenhouse.ino
162 lines (125 loc) · 3.25 KB
/
arduino-temp-greenhouse.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
/*-----( Import needed libraries )-----*/
#include <RCSwitch.h>
#include <OneWire.h>
#include <LiquidCrystal_I2C.h>
/*-----( Declare Constants )-----*/
const int DAY_TARGET_TEMP = 17;
const int NIGHT_TARGET_TEMP = 19;
const int DIVIATION_TEMP = 2;
const int DS18S20_Pin = 2;
const int RF433_Pin = 10;
const int SEND_RETRY = 3;
const int LDR_PIN = A0;
/*-----( Declare objects )-----*/
LiquidCrystal_I2C lcd(0x27, 16, 2);
OneWire ds(DS18S20_Pin);
RCSwitch mySwitch = RCSwitch();
/*-----( Declare Variables )-----*/
int heatingOn = false;
int ldrValue = 0;
int targetTemp = 0;
void setup()
{
Serial.begin(9600);
// initialize the LCD
lcd.begin();
// initilize the rf433 sender
mySwitch.enableTransmit(RF433_Pin);
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Vorzugsbeet");
lcd.setCursor(0,1);
lcd.print("TempSteuerung");
}
void loop()
{
ldrValue = analogRead(LDR_PIN);
if(ldrValue > 500)
{
// The sun is shining, target temp = 17
targetTemp = DAY_TARGET_TEMP;
} else {
targetTemp = NIGHT_TARGET_TEMP;
}
float curTemp = getTemp();
int minTemp = targetTemp - DIVIATION_TEMP;
int maxTemp = targetTemp + DIVIATION_TEMP;
if (curTemp<=(targetTemp-DIVIATION_TEMP) && heatingOn == false)
{
Serial.println("Please make heating on");
for(int i = 0; i<SEND_RETRY; i++)
{
mySwitch.switchOn('a', 1, 2);
delay(500);
}
heatingOn = true;
}
else if (curTemp>=(targetTemp+DIVIATION_TEMP) && heatingOn == true)
{
Serial.println("Please make heating off");
for(int i = 0; i<SEND_RETRY; i++)
{
mySwitch.switchOff('a', 1, 2);
delay(500);
}
heatingOn = false;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ist : ");
lcd.print(getTemp());
lcd.print(" C");
if(heatingOn == true)
{
lcd.print(" +");
}
lcd.setCursor(0,1);
lcd.print("Soll: ");
if(targetTemp == DAY_TARGET_TEMP)
{
lcd.print(DAY_TARGET_TEMP);
lcd.print(",00 C");
lcd.print(" T");
} else {
lcd.print(NIGHT_TARGET_TEMP);
lcd.print(",00 C");
lcd.print(" N");
}
Serial.print("HeatingON: ");
Serial.print(heatingOn);
Serial.println(heatingOn);
delay(10000);
}
float getTemp(){
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}