#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
String URL = "http://81.163.30.77:3215/api/state";
String TOKEN = "AGp4maYMXgBcYandexMicrocontrollers5WiThBblGQQJl";
String UUID = "454dcf08-b24c-4b8f-96c1-8e83c8dc6d55";
HTTPClient client;
void sendState(String state) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected");
return;
}
// Формируем JSON с помощью ArduinoJson
StaticJsonDocument<200> doc;
doc["id"] = UUID;
doc["state"] = state;
String payload;
serializeJson(doc, payload);
client.begin(URL);
client.addHeader("Content-Type", "application/json");
client.addHeader("Authorization", "Bearer " + TOKEN);
int httpResponseCode = client.POST(payload);
if (httpResponseCode == 200) {
String response = client.getString();
StaticJsonDocument<200> resDoc;
DeserializationError error = deserializeJson(resDoc, response);
if (!error) {
Serial.println("Статус лампы : " + String(resDoc["state"]));
}
} else {
Serial.println("Ошибка при отправке запроса. Код ошибки : " + String(httpResponseCode));
}
client.end();
}
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
sendState("on");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}