-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.ino
200 lines (169 loc) · 4.58 KB
/
clock.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
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include "secrets.h"
#include <WiFi.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define BUTTON 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int i = 0;
const char* ssid = SSID;
const char* password = WIFI_PASSWORD;
const int httpsPort = 443;
const String url_time = "https://www.timeapi.io/api/Time/current/zone?timeZone=" + String(ZONE);
const String url_weather = "https://api.openweathermap.org/data/2.5/weather?lat=" + String(LAT) + "&lon=" + String(LON) + "&appid=" + String(APPID);
WiFiClient client;
HTTPClient http;
void setup()
{
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Connecting to WiFi...");
display.display();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
display.println("Connected to: ");
display.print(ssid);
display.display();
delay(1500);
display.clearDisplay();
display.display();
Serial.print("Connecting to ");
Serial.println(url_time);
http.begin(url_time);
int httpCode = http.GET();
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
int hour = doc["hour"];
int minute = doc["minute"];
int second = doc["seconds"];
int day = doc["day"];
int month = doc["month"];
int year = doc["year"];
http.end();
setTime(hour, minute, second, day, month, year);
}
void loop()
{
if (i < 60) {
timen();
i++;
} else if (i < 80) {
daten();
i++;
} else {
weather();
i = 0;
}
}
void weather(void)
{
Serial.print("Connecting to ");
Serial.println(url_weather);
display.clearDisplay();
http.begin(url_weather);
int httpCode = http.GET();
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String main = doc["weather"][0]["description"].as<String>();
double temp = doc["main"]["temp"];
int humidity = doc["main"]["humidity"];
double wind = doc["wind"]["speed"];
int pressure = doc["main"]["pressure"];
String weather[5];
weather[0] = "Main: " + main;
weather[1] = "Temp: " + String((int)(temp - 273.15)) + "C";
weather[2] = "Hum: " + String(humidity) + "%";
weather[3] = "Wind: " + String(wind) + "m/s";
weather[4] = "Pres: " + String((int)(pressure/1.33322)) + "mm";
for (uint8_t i = 0; i < 5; i++) {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 15);
display.clearDisplay();
center(weather[i], 5, 5);
display.display();
delay(3000);
}
http.end();
}
void timen(void)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 15);
digits(hour());
display.print(F(":"));
digits(minute());
display.print(F(":"));
digits(second());
display.display();
delay(250);
}
void daten(void)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5, 15);
digits(day());
display.print(F("."));
digits(month());
display.print(F("."));
digits(year());
display.display();
delay(250);
}
void digits(int digits)
{
if (digits < 10) {
display.print("0");
display.print(digits);
}
else
display.print(digits);
}
void center(const String buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
display.setCursor((x - w / 2) + (128 / 2), y);
display.print(buf);
}