#include <Arduino.h>
#include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#include <map>
#include <vector>
const char *ssid = "WIFI_SSID";
const char *password = "WIFI_PASS";
const char *host = "tcp.alerts.com.ua";
const int port = 1024;
const char *apiKey = "b1fa9a784c5b4e44da94bf99a5ef021d2587a061";
#define PIN 12
#define NUMPIXELS 200
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
WiFiClient client;
String buffer;
std::map<int, std::vector<int>> led = {
{1, {1, 2, 3, 4, 5, 6, 7}},
{21, {8, 9, 10, 65, 66, 67}},
{8, {11, 12, 13, 59, 60, 61}},
{5, {14, 15, 16, 56, 57, 58}},
{12, {16, 17, 18, 19, 20, 21, 22, 53, 54, 55}},
{20, {23, 24, 25, 50, 51, 52}},
{18, {26, 27, 28, 41, 42, 43}},
{25, {29, 30, 31, 32, 33, 34}},
{10, {35, 36, 37, 92, 93, 94}},
{16, {38, 39, 40, 44, 45, 46}},
{4, {47, 48, 49, 62, 63, 64}},
{14, {68, 69, 70, 74, 75, 76}},
{15, {71, 72, 73, 110, 111, 112, 113, 114, 115}},
{11, {0, 78, 79, 80, 81, 82}},
{23, {83, 84, 85, 86, 87, 88}},
{2, {89, 90, 91, 107, 108, 109}},
{6, {95, 96, 97, 101, 102, 103}},
{17, {98, 99, 100, 137, 138}},
{22, {104, 105, 106, 149, 150}},
{24, {116, 117, 118}},
{9, {119, 120, 121, 144, 145, 146}},
{7, {122, 123, 124, 125, 126, 127}},
{13, {128, 129, 130, 139, 140, 141}},
{3, {131, 132, 133, 134, 135, 136}},
{19, {142, 143, 147, 148}}
};
void statusShow(int district_id, int status) {
// Знаходимо вектор LED для даного district_id
Serial.println(district_id);
Serial.println(status);
if (led.find(district_id) != led.end()) {
std::vector<int> leds = led[district_id];
for (int i = 0; i < leds.size(); ++i) {
int pixel = leds[i];
if (status == 1) {
strip.setPixelColor(pixel, strip.Color(255, 5, 5)); // Колір для статусу "1"
} else {
strip.setPixelColor(pixel, strip.Color(20, 255, 20)); // Колір для статусу "0"
}
}
strip.show();
} else {
// Не знайдено district_id в словнику
Serial.println("Unknown district ID");
}
}
void processPacket(String data) {
Serial.print("Got packet: ");
Serial.println(data);
if (data == "a:wrong_api_key") {
Serial.println("Please specify your API key!");
delay(5000);
}
else if (data.startsWith("s:")) {
int idx = data.indexOf('=');
if (idx != -1) {
int district_id = data.substring(2, idx).toInt();
int status = data.substring(idx + 1).toInt();
Serial.printf("Parsed: id=%d; status=%d\n", district_id, status);
statusShow(district_id, status);
}
}
}
void handleData(String data) {
static String buffer;
buffer += data;
while (1) {
int border = buffer.indexOf("\n");
if (border == -1) {
break;
}
processPacket(buffer.substring(0, border));
buffer = buffer.substring(border + 1);
}
}
void setup() {
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
if (!client.connected()) {
Serial.print("Connecting to server...");
if (client.connect(host, port)) {
Serial.println("Connected");
client.print(apiKey);
} else {
Serial.println("Failed");
delay(1000);
}
}
strip.begin();
strip.show();
}
void loop() {
String data;
while (client.available() > 0) {
data += (char)client.read();
}
if (data.length() > 0) {
handleData(data);
}
}