-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmegaCode.ino
More file actions
283 lines (250 loc) · 10.8 KB
/
AtmegaCode.ino
File metadata and controls
283 lines (250 loc) · 10.8 KB
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
#include <Time.h>
#include <TimeLib.h>
#include <Arduino.h>
#include "RTClib.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ShiftedLCD.h>
#include <SPI.h>
#define DHTPIN 7
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
char TempBuf[8];
char HumdBuf[8];
bool LEDState;
bool FanState;
bool TempOR;
bool HumdOR;
LiquidCrystal lcd(10);
RTC_Millis rtc;
const uint8_t SPRINTF_BUFFER_SIZE = 32;
char inputBuffer[SPRINTF_BUFFER_SIZE];
bool NTPBegin = false;
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(115200); /* start serial for debug */
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
lcd.begin(16, 2);
while (!NTPBegin) {
delay(10);
}
}
void loop() {
uint16_t tokens, yr, mh, dy, hr, me, sd;
bool flag = false;
if (!flag) {
tokens = sscanf(inputBuffer, "%*s %d-%d-%d %d:%d:%d;", // Use sscanf() to parse the date/ //
&yr, &mh, &dy, &hr, &me, &sd); // time into variables //
flag = true;
}
else {
tokens = sscanf(inputBuffer, "%*s %d:%d:%d;", // Use sscanf() to parse the date/ //
&hr, &me, &sd); // time into variables //
}
static uint8_t secs; // store the seconds value //
if (secs != second()) { // Output if seconds have changed //
sprintf(inputBuffer, "%04d-%02d-%02d %02d:%02d:%02d", year(), // Use sprintf() to pretty print //
month(), day(), hour(), minute(), second()); // date/time with leading zeros //
Serial.println(inputBuffer); // Display the current date/time //
secs = second(); // Set the counter variable //
}
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
lcd.setCursor(0, 0);
lcd.print("Error reading temperature!");
}
else {
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(event.temperature);
lcd.print(" *C");
if (!TempOR) {
if (event.temperature >= 20) {
digitalWrite(6, HIGH);
}
}
else {
if (LEDState) {
digitalWrite(6, HIGH);
}
}
}
// Get humidity event and print its value.
sensors_event_t eventH;
dht.humidity().getEvent(&eventH);
if (isnan(eventH.relative_humidity)) {
Serial.println("Error reading humidity!");
lcd.setCursor(0, 1);
lcd.print("Error reading humidity!");
}
else {
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(eventH.relative_humidity);
lcd.print("%");
if (!HumdOR) {
if (eventH.relative_humidity >= 50) {
digitalWrite(5, HIGH);
}
}
else {
if (FanState) {
digitalWrite(5, HIGH);
}
}
}
dtostrf(event.temperature, 6, 2, TempBuf);
dtostrf(eventH.relative_humidity, 6, 2, HumdBuf);
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
NTPBegin = true;
String cache = "";
String epoch = "";
String TimeBuffer = "";
uint16_t yr, mh, dy, hr, me, sd;
while (0 < Wire.available()) {
char c = Wire.read(); /* receive byte as a character */
cache += c;
}
if (cache.startsWith("UTC", 0)) {
Serial.print("Cache length: ");
Serial.println(cache.length());
for (int T = 3; T <= cache.length(); T++) {
epoch.concat(cache[T]);
}
//Serial.print("The time is ");
//Serial.println(epoch);
TimeBuffer += (epoch.toInt() % 86400L) / 3600;
TimeBuffer += ":";
if (((epoch.toInt() % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
TimeBuffer += "0";
}
TimeBuffer += (epoch.toInt() % 3600) / 60;
TimeBuffer += ":";
if ((epoch.toInt() % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
TimeBuffer += "0";
}
TimeBuffer += epoch.toInt() % 60;
char outbuffer[32];
TimeBuffer.toCharArray(outbuffer, 32);
for ( int i = 0 ; i <= TimeBuffer.length() ; ++i ) {
inputBuffer[ i ] = outbuffer[ i ];
}
Serial.print("NTP Time : ");
Serial.println(TimeBuffer);
//Serial.println(TimeBuffer.length());
time_t t = epoch.toInt();
yr = year(t);
mh = month(t);
dy = day(t);
hr = (epoch.toInt() % 86400L) / 3600;
me = (epoch.toInt() % 3600) / 60;
sd = epoch.toInt() % 60;
rtc.begin(DateTime(yr, mh, dy, hr, me, sd));
rtc.adjust(DateTime(yr, mh, dy, hr, me, sd)); // Adjust the RTC date/time //
Serial.print(F("Date has been set.\n"));
cache = "";
}
char buffer[32];
cache.toCharArray(buffer, 32);
if (strcmp(buffer, "LEDOR") == 0) {
Serial.println("LED OR");
TempOR = true;
}
if (strcmp(buffer, "NOTLEDOR") == 0) {
Serial.println("LED OR OFF");
TempOR = false;
}
if (strcmp(buffer, "FANOR") == 0) {
Serial.println("FAN OR");
HumdOR = true;
}
if (strcmp(buffer, "NOTFANOR") == 0) {
Serial.println("FAN OR OFF");
HumdOR = false;
}
if (strcmp(buffer, "LEDON") == 0) {
Serial.println("Turn on LED");
digitalWrite(6, HIGH);
LEDState = true;
}
if (strcmp(buffer, "LEDOFF") == 0) {
Serial.print("Turn off LED");
digitalWrite(6, LOW);
LEDState = false;
}
if (strcmp(buffer, "FANON") == 0) {
Serial.println("Turn on Fan");
digitalWrite(5, HIGH);
FanState = true;
}
if (strcmp(buffer, "FANOFF") == 0) {
Serial.print("Turn off Fan");
digitalWrite(5, LOW);
FanState = false;
}
}
// function that executes whenever data is requested from master
void requestEvent() {
Wire.write("T");
Wire.write(TempBuf); /*send string on request */
Wire.write(" H");
Wire.write(HumdBuf);
}
void readCommand() { // //
static uint8_t inputBytes = 0; // Variable for buffer position //
while (Serial.available()) { // Loop while incoming serial data //
inputBuffer[inputBytes] = Serial.read(); // Get the next byte of data //
if (inputBuffer[inputBytes] != '\n' && inputBytes < SPRINTF_BUFFER_SIZE) // keep on reading until a newline //
inputBytes++; // shows up or the buffer is full //
else { // //
inputBuffer[inputBytes] = 0; // Add the termination character //
for (uint8_t i = 0; i < inputBytes; i++) // Convert the whole input buffer //
inputBuffer[i] = toupper(inputBuffer[i]); // to uppercase characters //
Serial.print(F("\nCommand \"")); // //
Serial.write(inputBuffer); // //
Serial.print(F("\" received.\n")); // //
enum commands { SetDate, Unknown_Command }; // of commands enumerated type //
commands command; // declare enumerated type //
char workBuffer[10]; // Buffer to hold string compare //
sscanf(inputBuffer, "%s %*", workBuffer); // Parse the string for first word //
if (!strcmp(workBuffer, "SETDATE" )) command = SetDate; // Set command number when found //
else command = Unknown_Command; // Otherwise set to not found //
uint16_t tokens, year, month, day, hour, minute, second; // Variables to hold parsed dt/tm //
switch (command) { // Action depending upon command //
/***********************************************************************************************************
** Set the device time and date **
***********************************************************************************************************/
case SetDate: // Set the RTC date/time //
tokens = sscanf(inputBuffer, "%*s %d-%d-%d %d:%d:%d;", // Use sscanf() to parse the date/ //
&year, &month, &day, &hour, &minute, &second); // time into variables //
if (tokens != 6) // Check to see if it was parsed //
Serial.print(F("Unable to parse date/time\n")); // //
else { // //
rtc.adjust(DateTime(year, month, day, hour, minute, second)); // Adjust the RTC date/time //
Serial.print(F("Date has been set.")); // //
} // of if-then-else the date could be parsed // //
break; // //
case Unknown_Command: // Show options on bad command //
default: // //
Serial.println(F("Unknown command. Valid commands are:")); // //
Serial.println(F("SETDATE yyyy-mm-dd hh:mm:ss")); // //
} // of switch statement to execute commands // //
inputBytes = 0; // reset the counter // //
}
}
}