|
| 1 | +#include <ArduinoJson.h> |
| 2 | +#include <ESP8266WiFi.h> |
| 3 | +#include <ESP8266HTTPClient.h> |
| 4 | +#include <TM1637Display.h> |
| 5 | + |
| 6 | +TM1637Display display = TM1637Display(4, 0); |
| 7 | +TM1637Display display2 = TM1637Display(14, 12); |
| 8 | + |
| 9 | +const int BRIGHTNESS = 1; |
| 10 | + |
| 11 | +const uint8_t C[] = { |
| 12 | + SEG_A | SEG_D | SEG_E | SEG_F // C |
| 13 | +}; |
| 14 | + |
| 15 | +const uint8_t F[] = { |
| 16 | + SEG_A | SEG_E | SEG_F | SEG_G // F |
| 17 | +}; |
| 18 | + |
| 19 | +const char* WIFI_SSID = "ssid"; |
| 20 | +const char* WIFI_PASS = "password"; |
| 21 | + |
| 22 | +const int HTTPS_PORT = 443; |
| 23 | +const char* HOST = "alarm-clock-api.vercel.app"; |
| 24 | +const String DATA_URL = "/api/info?mini=true"; |
| 25 | + |
| 26 | +BearSSL::WiFiClientSecure client; |
| 27 | + |
| 28 | +const int secondsBetweenChecks = 45000; |
| 29 | +int timeSinceLastRead = 60001; |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial.begin(115200); |
| 33 | + Serial.println(); |
| 34 | + Serial.println("Running..."); |
| 35 | + |
| 36 | + client.setInsecure(); |
| 37 | + |
| 38 | + display.setBrightness(BRIGHTNESS); |
| 39 | + display2.setBrightness(BRIGHTNESS); |
| 40 | + |
| 41 | + connect(); |
| 42 | +} |
| 43 | + |
| 44 | +void connect() { |
| 45 | + Serial.println(); |
| 46 | + Serial.println(); |
| 47 | + Serial.print("Connecting to "); |
| 48 | + Serial.println(WIFI_SSID); |
| 49 | + |
| 50 | + if (WiFi.status() != WL_CONNECTED) { |
| 51 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 52 | + } |
| 53 | + |
| 54 | + while (WiFi.status() != WL_CONNECTED) { |
| 55 | + if (WiFi.status() == WL_CONNECT_FAILED) { |
| 56 | + Serial.println("Failed to connect to WIFI. Please verify credentials: "); |
| 57 | + Serial.println(); |
| 58 | + Serial.print("SSID: "); |
| 59 | + Serial.println(WIFI_SSID); |
| 60 | + Serial.print("Password: "); |
| 61 | + Serial.println(WIFI_PASS); |
| 62 | + Serial.println(); |
| 63 | + Serial.println("Trying again..."); |
| 64 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 65 | + delay(10000); |
| 66 | + } |
| 67 | + |
| 68 | + delay(500); |
| 69 | + Serial.println("..."); |
| 70 | + } |
| 71 | + |
| 72 | + Serial.println(); |
| 73 | + Serial.println("WiFi connected"); |
| 74 | + Serial.print("IP address: "); |
| 75 | + Serial.println(WiFi.localIP()); |
| 76 | + Serial.println(); |
| 77 | +} |
| 78 | + |
| 79 | +void loop() { |
| 80 | + bool toReconnect = false; |
| 81 | + |
| 82 | + if (WiFi.status() != WL_CONNECTED) { |
| 83 | + Serial.println("Disconnected from WiFi"); |
| 84 | + toReconnect = true; |
| 85 | + } |
| 86 | + |
| 87 | + if (toReconnect) { |
| 88 | + connect(); |
| 89 | + } |
| 90 | + |
| 91 | + if (timeSinceLastRead > secondsBetweenChecks) { |
| 92 | + getWeatherAndTime(); |
| 93 | + |
| 94 | + timeSinceLastRead = 0; |
| 95 | + } |
| 96 | + |
| 97 | + delay(1000); |
| 98 | + timeSinceLastRead += 1000; |
| 99 | +} |
| 100 | + |
| 101 | +void getWeatherAndTime() { |
| 102 | + if (client.connect(HOST, HTTPS_PORT)) { |
| 103 | + client.print(String("GET ") + DATA_URL + " HTTP/1.1\r\n" + |
| 104 | + "Host: " + HOST + "\r\n" + |
| 105 | + "User-Agent: rorpage_clock_ESP8266\r\n" + |
| 106 | + "Connection: close\r\n\r\n"); |
| 107 | + |
| 108 | + delay(100); |
| 109 | + |
| 110 | + String response = client.readString(); |
| 111 | + int body_position = response.indexOf("\r\n\r\n") + 4; |
| 112 | + String response_body = response.substring(body_position); |
| 113 | + |
| 114 | + StaticJsonDocument<256> jsonDocument; |
| 115 | + deserializeJson(jsonDocument, response_body); |
| 116 | + |
| 117 | + display.clear(); |
| 118 | + display2.clear(); |
| 119 | + |
| 120 | + int digits[4]; |
| 121 | + digits[0] = jsonDocument["time_digits"][0]; |
| 122 | + digits[1] = jsonDocument["time_digits"][1]; |
| 123 | + digits[2] = jsonDocument["time_digits"][2]; |
| 124 | + digits[3] = jsonDocument["time_digits"][3]; |
| 125 | + |
| 126 | + // Time |
| 127 | + for (int i = 0; i < 4; i++) { |
| 128 | + display.showNumberDecEx(digits[i], 0b11100000, false, 1, i); |
| 129 | + } |
| 130 | + |
| 131 | + // Temperature |
| 132 | + int temperature = jsonDocument["temp_c"]; |
| 133 | + display2.showNumberDec(temperature, false, 3, 0); |
| 134 | + display2.setSegments(C, 1, 3); |
| 135 | + } else { |
| 136 | + Serial.print("Error getting data"); |
| 137 | + } |
| 138 | +} |
0 commit comments