-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmClock.ino
243 lines (203 loc) · 6.29 KB
/
AlarmClock.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
/* Digital Alarmclock Project
*
* Gets the current weather and time of Houston, Texas
* Also allows for functionality for an Alarm Clock. Currently must be hard-coded
*
* Sometimes the NTP server refreshes and you get a different server & time. Edit the value in calculateTime() to fix this
*
* Board used: Arduino Uno Wifi Rev 2
* Parts used: LCD1602, Potentiometer 10K, Active Buzzer
*
* This was done as a project for ELET 2303 (Digital Systems)
*
* Time Service Used: Network Time Protocol
* Weather Web Service Used: https://openweathermap.org/
*
* Setup
* Wire up the board correctly
* Configure ssid and pass with your wifi settings
* Set alarm time if needed in loop()
* Configure NTP settings if needed in calculateTime()
* Get API key from openweathermap and paste it in httpRequest()
*/
// Wifi modules needed
#include <WiFiNINA.h>
#include <SPI.h>
#include <ArduinoJson.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
// LCD display modules
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// Wifi info
char ssid[] = ""; //WiFi name
char pass[] = ""; //WiFi password
// Other wifi variable initialization
int status = WL_IDLE_STATUS;
unsigned long lastConnectionTime = 0; // last time you are connected to the server, in milliseconds
const unsigned long postingInterval = 30L * 1000L; // delay between updates, in milliseconds
// NTP stuff for time calculation
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Initialize the Wifi client library
WiFiSSLClient client;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Second;
int Minute;
int Hour;
int BuzzerID;
float weatherTemperature;
// Connect to wifi, get time from NTP server, and assign buzzer id
void setup(){
Serial.begin(9600);
// Wifi setup
while(status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.print(ssid);
Serial.print(" ");
status = WiFi.begin(ssid, pass);
delay(10000);
}
String ssid = WiFi.SSID();
IPAddress ip = WiFi.localIP();
IPAddress subnet = WiFi.subnetMask();
IPAddress gateway = WiFi.gatewayIP();
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("IP: ");
Serial.println(ip);
// Time setup
timeClient.update();
Serial.println(timeClient.getFormattedTime());
Second = timeClient.getSeconds();
Minute = timeClient.getMinutes();
Hour = timeClient.getHours();
// Digital Port which the buzzer is attached to
BuzzerID = 8;
pinMode(BuzzerID, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
Serial.println("Ready");
Serial.println("");
}
// Clear screen, get time, connect to weather and get temp, check alarm, and write to screen
void loop(){
lcd.clear();
lcd.setCursor(4, 0);
calculateTime();
lcd.setCursor(0, 1);
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
lcd.print("Temp: ");
lcd.print(weatherTemperature);
lcd.print(" F");
// Not enought space on the lcd :(
//lcd.print(" Run-time:");
//lcd.print(millis() / 1000);
//Alarm Time
//These values are currently hardcoded
if(Hour== 10 && Minute==10){
BuzzerOn();
}
delay(1000);
if(Hour== 10 && Minute==10){
BuzzerOff();
}
}
void calculateTime(){
Second = timeClient.getSeconds();
Minute = timeClient.getMinutes();
Hour = timeClient.getHours();
Hour -= 2; // I subtracted x hours because my ntp was off x hours
if(Hour < 0){
Hour = 24 + Hour; //(Because it'd be negative)
}
if(Hour < 10){
lcd.print("0");
lcd.print(Hour);
} else {
lcd.print(Hour);
}
lcd.print(":");
if(Minute < 10){
lcd.print("0");
lcd.print(Minute);
} else {
lcd.print(Minute);
}
lcd.print(":");
if(Second < 10){
lcd.print("0");
lcd.print(Second);
} else {
lcd.print(Second);
}
// Had some manual time calculation but removed it due to ntp service
}
void BuzzerOn(){
digitalWrite(BuzzerID, HIGH);
}
void BuzzerOff(){
digitalWrite(BuzzerID, LOW);
}
void httpRequest(){
// if there's a successful connection:
if (client.connect("api.openweathermap.org", 443)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /data/2.5/weather?q=houston,us&units=imperial&APPID=<API KEY> HTTP/1.1"); // Need to set API key here!
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
if (strcmp(status + 9, "200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
return;
}
// Allocate the JSON document
// Use arduinojson.org/v6/assistant to compute the capacity.
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) + 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(14) + 280;
DynamicJsonDocument doc(capacity);
// Parse JSON object
DeserializationError error = deserializeJson(doc, client);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
int weatherId = doc["weather"][0]["id"].as<int>();
weatherTemperature = doc["main"]["temp"].as<float>();
int weatherHumidity = doc["main"]["humidity"].as<int>();
//Disconnect
client.stop();
Serial.println(F("Response:"));
Serial.print("Weather: ");
Serial.println(weatherId);
Serial.print("Temperature: ");
Serial.println(weatherTemperature);
Serial.print("Humidity: ");
Serial.println(weatherHumidity);
Serial.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}else{
// if you couldn't make a connection:
Serial.println("connection failed");
}
}