#include <WiFi.h>
#include <time.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apiUrl = "https://api.alerts.in.ua/v1/iot/active_air_raid_alerts/15.json";
const char* token = "8e9ca4f81345b2386e1af7b93dd5987c31a3e115ab2203";
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 18
#define DATA_PIN 23
#define CS_PIN 5
MD_Parola matrix = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
const long gmtOffset_sec = 2 * 3600;
const int daylightOffset_sec = 3600;
unsigned long lastUpdate = 0;
unsigned long displayDuration = 20000;
bool showAlert = false;
int alertStatus = -1;
int getAlertStatus() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String fullUrl = String(apiUrl) + "?token=" + token;
http.begin(fullUrl);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
Serial.println("Отримані дані:");
Serial.println(payload);
DynamicJsonDocument doc(4096);
auto error = deserializeJson(doc, payload);
if (error) {
Serial.print("Помилка розбору JSON: ");
Serial.println(error.c_str());
return -1;
}
if (payload == "\"N\"") {
Serial.println("Немає інформації про повітряну тривогу.");
return 0;
} else if (payload == "\"A\"") {
Serial.println("ТРИВОГА!!!");
return 1;
} else {
Serial.println("Невідомий статус.");
return 2;
}
} else {
Serial.printf("Помилка HTTP: %d\n", httpResponseCode);
}
http.end();
} else {
Serial.println("Немає з'єднання з Wi-Fi.");
}
return -1;
}
void setup() {
Serial.begin(115200);
matrix.begin();
matrix.setIntensity(5);
matrix.displayClear();
Serial.print("Підключення до Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(" Підключено!");
configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org", "time.nist.gov");
Serial.println("Час налаштовано через NTP!");
lastUpdate = millis(); // Ініціалізація таймера
}
void loop() {
unsigned long currentMillis = millis();
// Оновлення статусу тривоги кожні 20 секунд
if (currentMillis - lastUpdate >= displayDuration) {
alertStatus = getAlertStatus();
showAlert = true; // Увімкнути показ тривоги
lastUpdate = currentMillis;
}
if (showAlert) {
// Показ повідомлення про тривогу
if (alertStatus == 0) {
matrix.displayText("!Alarm", PA_CENTER, 50, 2000, PA_PRINT, PA_SCROLL_UP_LEFT);
} else if (alertStatus == 1) {
matrix.displayText("Alarm!", PA_CENTER, 50, 2000, PA_PRINT, PA_SCROLL_UP_LEFT);
} else {
matrix.displayText("...", PA_CENTER, 50, 2000, PA_PRINT, PA_SCROLL_UP_LEFT);
}
while (!matrix.displayAnimate());
showAlert = false; // Після показу повертаємося до часу
} else {
// Показ поточного часу
struct tm timeInfo;
if (!getLocalTime(&timeInfo)) {
Serial.println("Не вдалося отримати час!");
return;
}
char timeStr[6];
strftime(timeStr, sizeof(timeStr), "%H:%M", &timeInfo);
matrix.displayText(timeStr, PA_CENTER, 50, 1000, PA_PRINT, PA_NO_EFFECT);
while (!matrix.displayAnimate());
}
}