-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
138 lines (118 loc) · 3.44 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
#include <TM1637Display.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Define the NTP client and server details
WiFiUDP ntpUDP;
unsigned long _updateInterval = (1024*1000);
NTPClient timeClient(ntpUDP, "ntp.time.nl", _updateInterval);
// Pins for TM1637 display
#define CLK_PIN 12 // (D5)
#define DIO_PIN 14 // (D6)
// Create a TM1637Display object
TM1637Display display(CLK_PIN, DIO_PIN);
// Weather API
const char* host = "api.tomorrow.io";
const char* endpoint = "/v4/weather/realtime?location=LOCATION&apikey=";
const int httpsPort = 443;
const char* apiKey = "APIKEY";
int updateLoop = 0;
bool flip = false;
float internalTemperature = 1337;
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
// Connect to Wi-Fi
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize the NTP client and get the time
timeClient.begin();
timeClient.setTimeOffset(3600); // Set your timezone offset in seconds (e.g., GMT+1)
// Initialize the display
display.setBrightness(0x04); // Adjust the brightness (0x00 to 0x0f)
Serial.println("Clock setup complete");
internalTemperature = getTemperature();
}
void loop() {
timeClient.update();
// After 60 ticks get new temperature
if(updateLoop == 300)
{
updateLoop = 0;
internalTemperature = getTemperature();
}
if (updateLoop % 4 == 1)
{
flip = !flip;
}
int hours = timeClient.getHours();
int minutes = timeClient.getMinutes();
int seconds = timeClient.getSeconds();
/// Automatic brightness adjustmnent
if(hours == 21 && minutes == 0 && seconds == 0){
display.setBrightness(0x00);
}
else if(hours == 7 && minutes == 0 && seconds == 0){
display.setBrightness(0x02);
}
else if(hours == 10 && minutes == 0 && seconds == 0){
display.setBrightness(0x04);
}
else if(hours == 18 && minutes == 0 && seconds == 0){
display.setBrightness(0x02);
}
if(flip){
// Get the current time
// Display the time on the TM1637 display
display.showNumberDecEx((hours * 100) + minutes, 0b01000000, true);
}
else{
display.showNumberDecEx(internalTemperature * 100, 0b01000000, false);
}
delay(1000); // Update every second
// Display the temperature on the TM1637 display
updateLoop++;
}
float getTemperature(){
WiFiClientSecure client;
client.setInsecure();
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return 0;
}
// Make request
client.print(String("GET ") + endpoint + apiKey + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// Wait for response headers
while (!client.available()) {
delay(100);
}
// Read JSON response
String response = "";
bool bodyStarted = false;
while (client.available()) {
char c = client.read();
if (c == '{' || bodyStarted) {
response += c;
bodyStarted = true;
}
}
// Parse JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
// Extract temperature
float temperature = doc["data"]["values"]["temperature"];
Serial.println(temperature);
return temperature;
}