-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunriseSunset.ino
182 lines (158 loc) · 4.18 KB
/
sunriseSunset.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
/* SmartCoop: Sunrise and Sunset
* Connects to api.sunrise-sunset.org, fetches the sunrise and sunset times, and prints them out.
*
* Jason Keane
*/
/* Fetches the sunrise and sunset times */
int sunriseHours, sunriseMinutes;
int sunsetHours, sunsetMinutes;
void getSunrise()
{
/* FETCH */
// if there are incoming bytes available
// from the server, read them and print them:
sunrise = "";
sunset = "";
if (client.available()) {
char c = client.read();
currentLine += c;
if (c == '\n') {
if (currentLine.startsWith("{\"results\":")) {
int i;
for (i = 23; i < 30; i++) {
sunrise += currentLine[i];
}
for (i = 166; i < 173; i++) {
sunset += currentLine[i];
}
Serial.println("The sunrise time is: " + sunrise);
Serial.println("The sunset time is: " + sunset);
client.stop();
} else {
currentLine = "";
}
}
}
/* converts the strings into separate integers for hours and minutes (unfinished) */
// sunrise
String stringRiseHours = "", stringRiseMinutes = "";
int i;
for (i = 0; i < sunrise.indexOf(':'); i++) {
stringRiseHours += sunrise[i];
}
sunriseHours = stringRiseHours.toInt();
for (i = sunrise.indexOf(':') + 1; i < sunrise.lastIndexOf(':'); i++) {
stringRiseMinutes += sunrise[i];
}
sunriseMinutes = stringRiseMinutes.toInt();
// sunset (NEEDS TESTING!!)
String stringSetHours = "", stringSetMinutes = "";
for (i = 0; i < sunset.indexOf(':'); i++) {
stringSetHours += sunset[i];
}
sunsetHours = stringSetHours.toInt();
sunsetHours = convert12HourTo24Hour(sunsetHours);
for (i = sunset.indexOf(':') + 1; i < sunset.lastIndexOf(':'); i++) {
stringSetMinutes += sunset[i];
}
sunsetMinutes = stringSetMinutes.toInt();
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
Connected = false;
getTimes = false;
}
mySQLAddTimes();
}
int convert12HourTo24Hour(int twelveHour)
{
int twentyFourHour = twelveHour + 12;
if (twentyFourHour == 24)
{
twentyFourHour = 0;
}
return twentyFourHour;
}
/*
* Connects to the server api.sunrise-sunset.org
*/
void connectForGET()
{
// will need to run this block every day using RTC on Galileo
if (getTimes) {
if (Connected == false) {
/* CONNECT */
char serverToScrape[] = "104.131.2.15";
// if you get a connection, report back via serial:
if (client.connect(serverToScrape, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /json?lat=53.2706680&lng=-9.0567910&date=today HTTP/1.1");
client.println("Host: api.sunrise-sunset.org");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Connected = true;
} else {
getSunrise();
}
}
}
void getRealTime()
{
int currentHours, currentMinutes, currentSeconds;
time_t t;
time(&t);
struct tm *time = localtime(&t);
currentHours = time->tm_hour;
currentMinutes = time->tm_min;
currentSeconds = time->tm_sec;
// check sunrise
/*Serial.print("TIME: ");
Serial.print(currentHours);
Serial.print(":");
Serial.print(currentMinutes);
Serial.print(":");
Serial.print(currentSeconds);
Serial.print(":");
Serial.println();
Serial.print("Sunsett: ");
Serial.print(sunriseHours);
Serial.print(":");
Serial.print(sunriseMinutes);
Serial.println();*/
if (currentHours == sunriseHours)
{
if (currentMinutes == sunriseMinutes)
{
if (currentSeconds == 0)
{
// open door
doorStatus = 1;
saveDoorStatus();
}
}
}
else if (currentHours == sunsetHours+1) // delay by 1 hour
{
if (currentMinutes == sunsetMinutes)
{
if (currentSeconds == 0)
{
// close door
Serial.println("CLOSE");
doorStatus = 0;
saveDoorStatus();
}
}
}
if (currentHours == 0 && currentMinutes == 0 && currentSeconds == 0) // get sunrise at midnight
{
getTimes = true;
}
}