-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
286 lines (234 loc) · 8.08 KB
/
sketch.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
/* Sketch - CBYG Temp Sensor Poject - V. 1.0
Author: Gonzo - [email protected]
*/
// Libraries to connect to SPI, Wifi, WiFiUDP, Time
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Time.h>
#include <Timezone.h>
#include <LiquidCrystal.h>
// ledPin setup
const int ledPin = 9;
// PIR setup
byte sensorPin = 6;
// LCD setup
LiquidCrystal lcd(10, 8, 5, 4, 3, 2);
// Web service
char server[] = "www.google.com";
// Wifi setup
char ssid[] = "cbyg"; // Wifi SSID name
char pass[] = "1019goN$44"; // Wifi password
int status = WL_IDLE_STATUS; // Wifi status
// Initialize the Wifi client library
WiFiClient client;
// Network setup
IPAddress ip(192, 168, 1, 156);
IPAddress dns(192, 168, 1, 254);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress checkIp; // Variable to store IP address
IPAddress checkGateway; // Variable to store IP gateway
// NTP setup
unsigned int localPort = 8888; // local port to listen for UDP packets
char timeServer[] = "192.168.1.254"; // ntp at cbyg.marcote.org NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
// --------- Functions ------------ //
// Function to send an NTP request to the time server at the given address
unsigned long sendNTPpacket(char* 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();
return 0;
}
// Function to get the NTP time in Unix time
unsigned long getNtpTime() {
// After connecting to wifi we update date and time with NTP protocol
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
// the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract the two words
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900)
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into everyday time
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years
unsigned long epoch = secsSince1900 - seventyYears;
Serial.println("Time synced with NTP server!");
Serial.println("");
Serial.println("Updating local Time with NTP...");
// print Unix time
return epoch;
}
Serial.println("ERROR. NTP update failed");
return 0; // return 0 if unable to get the time
}
// Function to add one 0 to correct display time
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void setup() {
// Initialize serial
Serial.begin(115200);
// Set ledPin pin out
pinMode(ledPin, OUTPUT);
// Set PIR pin in
pinMode(sensorPin,INPUT);
// Set LCD
lcd.begin(16, 2);
// Welcome message
Serial.println("== Welcome to CBYG Temp sensor 1.0 ==");
Serial.println("");
// We connect to Wifi only on Arduino boot
// Wifi setup
WiFi.config(ip, dns, gateway, subnet); // Configure network parameters
Serial.println("Connecting to Wifi ...");
status = WiFi.begin(ssid, pass); // Trying to connect to wifi using WPA2 encryption
// Failed wifi connection
if ( status != WL_CONNECTED) {
Serial.println("ERROR: Failed to connect to Wifi!");
Serial.println("");
while(true);
}
// Succesfully connected:
else {
Serial.println("Connected to Wifi network!");
// Print IP & Gateway addresses:
checkIp = WiFi.localIP();
checkGateway = WiFi.gatewayIP();
Serial.print("IP address is: ");
Serial.println(checkIp);
Serial.print("Gateway address is: ");
Serial.println(checkGateway);
// If connected to the Wifi, we initializate Udp on specified port
Serial.print("Initializing UDP on port: ");
Serial.println(localPort);
Serial.println("");
Udp.begin(localPort);
}
// After connecting to wifi we check time with NTP protocol
Serial.println("Connecting to NTP server ...");
setSyncProvider(getNtpTime);
if (timeStatus() == 2) {
Serial.println("Local time updated with NTP!");
Serial.print("Update status is: ");
Serial.println(timeStatus());
} else {
Serial.println("ERROR. Local time not updated with NTP");
Serial.print("Update status is: ");
Serial.println(timeStatus());
}
/*
Serial.print("Local Date and Time updated with NTP is: ");
time_t t = now();
Serial.print(day(t));
Serial.print(+ "/") ;
Serial.print(month(t));
Serial.print(+ "/") ;
Serial.print(year(t));
Serial.print( " ") ;
Serial.print(hour(t));
Serial.print(+ ":") ;
Serial.print(minute(t));
Serial.print(":") ;
Serial.println(second(t));
*/
// We update the current Time and Date with DST
time_t central, utc;
TimeChangeRule esCEST = {"CEST", Last, Sun, Mar, 2, +120}; // UTC + 2 hours
TimeChangeRule esCET = {"CET", Last, Sun, Oct, 3, +60}; // UTC + 1 hours
Timezone esCentral(esCEST, esCET);
utc = now(); // Current time from the Time Library
central = esCentral.toLocal(utc);
Serial.print("Local Date and Time updated with DST is: ");
Serial.print(day(central));
Serial.print(+ "/") ;
Serial.print(month(central));
Serial.print(+ "/") ;
Serial.print(year(central));
Serial.print( " ") ;
Serial.print(hour(central));
Serial.print(+ ":") ;
Serial.print(minute(central));
Serial.print(":") ;
Serial.println(second(central));
// End setup
Serial.println("");
Serial.println("===========================");
Serial.println("");
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("Connected to server");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
}
void loop() {
// Read motion sensor
byte state = digitalRead(sensorPin);
digitalWrite(ledPin, state);
if(state == 1) {
Serial.println("Door open. Turn on LCD and light");
digitalWrite(ledPin, state);
// Turn on the display
lcd.display();
delay(500);
lcd.setCursor(0, 0);
lcd.print(" CBYG - Rack");
lcd.setCursor(0, 1);
lcd.print("R:40 B:42 P:32");
delay(30000);
}
else if(state == 0) {
Serial.println("Door closed. Turn off LCD and light");
// Turn off the display
lcd.noDisplay();
delay(500);
}
// if there are incoming bytes available
// from the server, read them and print them:
//while (client.available()) {
// char c = client.read();
// Serial.write(c);
//}
// if the server's disconnected, stop the client:
//if (!client.connected()) {
// Serial.println();
// Serial.println("disconnecting from server.");
// client.stop();
// do nothing forevermore:
// while (true);
//}
}