#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define HOST "tcp.alerts.com.ua"
#define PORT 1024
#define API_KEY "YOUR_API_KEY_HERE"
WiFiClient client;
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);
}
}
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() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to WiFi...");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
if (!client.connected()) {
Serial.print("Connecting to Air Raid Alert API...");
while (!client.connect(HOST, PORT))
{
Serial.println(" Failed.");
delay(1000);
}
Serial.println(" Connected!");
client.write(API_KEY);
}
String data;
while (client.available() > 0)
{
data += (char)client.read();
}
if (data.length()) {
handleData(data);
}
delay(10);
}