forked from sfjuocekr/Growputer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrowputer.ino
301 lines (223 loc) · 6.01 KB
/
Growputer.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
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#define DHTPIN 3
#define DHTTYPE DHT22
#define ONE_WIRE_BUS 5
#define TEMPERATURE_PRECISION 9
#define NTP_PACKET_SIZE 48
#define TIMEZONE 2
#define LOCALPORT 1337
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float airHumids[10];
float airTemps[10];
float waterTemps[10];
unsigned int index = 0;
bool inited = false;
float deltaTime = 0;
tmElements_t tm;
byte mac[6] = { 0xBE, 0xEF, 0xED, 0xC0, 0xFF, 0xEE };
byte ip[4] = { 192, 168, 178, 254 };
byte dnsServer[4] = { 192, 168, 178, 1 };
byte gateway[4] = { 192, 168, 178, 1 };
byte subnet[4] = { 255, 255, 255, 0 };
IPAddress timeServer(5, 101, 105, 6);
EthernetUDP udp;
byte packetBuffer[NTP_PACKET_SIZE];
void setup()
{
Serial.begin(9600);
while (!Serial) delay(1000);
Serial.flush();
Serial.println("Growputer v0.1 initializing . . .");
Serial.println();
initHardware();
Serial.println("Init: Done!");
Serial.println();
/*readDate();
Serial.print(" ");
readTime();*/
Serial.println();
inited = true;
}
void initHardware()
{
init_DHT22();
init_DS18B20();
if (init_DS1307() && init_W5100()) NTP();
}
void init_DHT22()
{
Serial.println("Init: DHT22");
dht.begin();
}
void init_DS18B20()
{
Serial.println("Init: DS18B20");
sensors.begin();
}
bool init_DS1307()
{
Serial.println("Init: DS1307");
RTC.set(0);
if (RTC.read(tm)) return true;
else return false;
}
bool init_W5100()
{
Serial.println("Init: W5100");
Ethernet.begin(mac, ip, dnsServer, gateway, subnet);
/*if (Ethernet.begin(mac) == 0)
{
Serial.println(" Failed to configure ethernet using DHCP!");
return false;
}*/
Serial.print(" IP address from DHCP: "); // static right now
Serial.println(Ethernet.localIP());
return true;
}
void read_DHT22()
{
airHumids[index] = dht.readHumidity();
airTemps[index] = dht.readTemperature();
while (airHumids[index] || airTemps[index])
{
Serial.println("delay DHT22");
delay(2000);
break;
airHumids[index] = dht.readHumidity();
airTemps[index] = dht.readTemperature();
}
}
void read_DS18B20()
{
sensors.requestTemperatures();
waterTemps[index] = sensors.getTempCByIndex(0);
}
void readTime()
{
while (!RTC.read(tm)) delay(1000);
Serial.print("");
print2digits(tm.Hour);
Serial.print(":");
print2digits(tm.Minute);
Serial.print(":");
print2digits(tm.Second);
}
void readDate()
{
while (!RTC.read(tm)) delay(1000);
Serial.print("");
print2digits(tm.Day);
Serial.print("-");
print2digits(tm.Month);
Serial.print("-");
print2digits(tmYearToCalendar(tm.Year));
}
void print2digits(int number)
{
if (number >= 0 && number < 10) Serial.print("0");
Serial.print(number);
}
void NTP()
{
udp.begin(LOCALPORT);
setSyncProvider(getNtpTime);
RTC.set(getNtpTime());
}
time_t getNtpTime()
{
while (udp.parsePacket() > 0);
Serial.println(" Transmiting NTP Request . . .");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500)
{
int size = udp.parsePacket();
if (size >= NTP_PACKET_SIZE)
{
Serial.println(" Received NTP Response!");
udp.read(packetBuffer, NTP_PACKET_SIZE);
unsigned long secsSince1900;
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + TIMEZONE * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0;
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
udp.beginPacket(address, 123); //NTP requests are to port 123
udp.write(packetBuffer, NTP_PACKET_SIZE);
udp.endPacket();
}
void read_sensors()
{
float currentTime = millis();
read_DHT22();
read_DS18B20();
deltaTime += (millis() - currentTime);
index++;
if (index == 10)
{
index = 1;
airHumids[0] = (airHumids[0] + airHumids[1] + airHumids[2] + airHumids[3] + airHumids[4] + airHumids[5] + airHumids[6] + airHumids[7] + airHumids[8] + airHumids[9]) / 10;
airTemps[0] = (airTemps[0] + airTemps[1] + airTemps[2] + airTemps[3] + airTemps[4] + airTemps[5] + airTemps[6] + airTemps[7] + airTemps[8] + airTemps[9]) / 10;
waterTemps[0] = (waterTemps[0] + waterTemps[1] + waterTemps[2] + waterTemps[3] + waterTemps[4] + waterTemps[5] + waterTemps[6] + waterTemps[7] + waterTemps[8] + waterTemps[9]) / 10;
Serial.print("Air humidity: ");
Serial.print(airHumids[0]);
Serial.println("%");
Serial.print("Air temperature: ");
Serial.println(airTemps[0]);
Serial.print("Water temperature: ");
Serial.println(waterTemps[0]);
Serial.println();
Serial.print("Procrastinated for ");
Serial.print(deltaTime / 1000);
Serial.println(" seconds!");
Serial.println();
readDate();
readTime();
Serial.println();
deltaTime = 0;
}
}
void loop()
{
if (inited)
{
readDate();
Serial.print(" ");
readTime();
Serial.println();
read_sensors();
}
}