#include <Arduino.h>
#include <GyverHTTP.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiClientSecureBearSSL.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#endif
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#if defined(ESP8266)
BearSSL::WiFiClientSecure client;
#elif defined(ESP32)
WiFiClientSecure client;
#endif
#include <GSON.h>
ghttp::Client http(client, "dash.wqtt.ru", 443);
#define WQTT_TOKEN ""
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
client.setInsecure();
Text req = R"rawliteral(
{
"name": "Название устройства",
"type": 2,
"room": "Комната",
"on_off": [
{
"topic_cmd": "topic",
"cmd_on": "1",
"cmd_off": "0"
}
]
}
)rawliteral";
static Text headers =
"Content-Type: application/json\r\n"
"Authorization: Token " WQTT_TOKEN "\r\n";
http.request("/api/devices", "POST", headers, req);
static bool wqtt_update = false;
http.onResponse([](ghttp::Client::Response& resp) {
Serial.println(resp.type());
Serial.println(resp.body().length());
//resp.body().writeTo(Serial);
String s;
String rstr = resp.body().readString();
rstr.replace("null", "0");
s = su::unicode::decode(rstr);
//resp.body().readBytes(s, resp.body().length());
//
//Serial.println(s);
Serial.println(s[0]);
gson::Parser json(resp.body().length());
if (json.parse(s)) {
json.stringify(Serial);
} else {
Serial.println("Parse error");
Serial.println(json.errorIndex());
Serial.println(json.readError());
}
//Serial.println(json);
if(!wqtt_update) {
wqtt_update = true;
http.request("/api/devices/refresh", "GET", headers);
} else {
wqtt_update = false;
}
});
}
void loop() {
http.tick();
delay(10);
}