114 lines
2.8 KiB
C++
114 lines
2.8 KiB
C++
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
// Remplacez par vos informations réseau
|
|
const char* ssid = "Votre_SSID";
|
|
const char* password = "Votre_MotDePasse";
|
|
|
|
// Informations API Tuya
|
|
const char* access_id = "Votre_Access_ID";
|
|
const char* access_secret = "Votre_Access_Secret";
|
|
const char* device_id = "Votre_Device_ID";
|
|
const char* api_endpoint = "https://openapi.tuyaeu.com"; // Point de terminaison européen
|
|
|
|
// Stockage des informations d'authentification
|
|
String token;
|
|
String refresh_token;
|
|
unsigned long token_expiration;
|
|
|
|
// Fonction pour se connecter au WiFi
|
|
void connectToWiFi() {
|
|
Serial.print("Connexion au WiFi");
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("Connecté !");
|
|
}
|
|
|
|
// Fonction pour obtenir un jeton d'accès Tuya
|
|
bool getTuyaToken() {
|
|
HTTPClient http;
|
|
String url = String(api_endpoint) + "/v1.0/token?grant_type=1";
|
|
|
|
http.begin(url);
|
|
http.addHeader("client_id", access_id);
|
|
http.addHeader("sign_method", "HMAC-SHA256");
|
|
|
|
int httpCode = http.GET();
|
|
if (httpCode > 0) {
|
|
String payload = http.getString();
|
|
Serial.println(payload);
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
deserializeJson(doc, payload);
|
|
|
|
token = doc["result"]["access_token"].as<String>();
|
|
refresh_token = doc["result"]["refresh_token"].as<String>();
|
|
token_expiration = millis() + (doc["result"]["expire_time"].as<long>() * 1000);
|
|
|
|
Serial.println("Token obtenu: " + token);
|
|
return true;
|
|
} else {
|
|
Serial.println("Erreur lors de l'obtention du jeton");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Fonction pour obtenir l'état d'un appareil Tuya
|
|
void getDeviceStatus() {
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
connectToWiFi();
|
|
}
|
|
|
|
if (millis() > token_expiration) {
|
|
if (!getTuyaToken()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
HTTPClient http;
|
|
String url = String(api_endpoint) + "/v1.0/devices/" + device_id + "/status";
|
|
|
|
http.begin(url);
|
|
http.addHeader("client_id", access_id);
|
|
http.addHeader("access_token", token);
|
|
http.addHeader("sign_method", "HMAC-SHA256");
|
|
|
|
int httpCode = http.GET();
|
|
if (httpCode > 0) {
|
|
String payload = http.getString();
|
|
Serial.println(payload);
|
|
|
|
// Analyse du JSON pour extraire l'état de l'appareil
|
|
DynamicJsonDocument doc(1024);
|
|
deserializeJson(doc, payload);
|
|
|
|
for (JsonObject item : doc["result"].as<JsonArray>()) {
|
|
Serial.print("Code: ");
|
|
Serial.print(item["code"].as<String>());
|
|
Serial.print(" - Valeur: ");
|
|
Serial.println(item["value"].as<String>());
|
|
}
|
|
} else {
|
|
Serial.println("Erreur lors de la récupération de l'état de l'appareil");
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
connectToWiFi();
|
|
|
|
if (getTuyaToken()) {
|
|
getDeviceStatus();
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
// Vérifiez l'état de l'appareil toutes les 60 secondes
|
|
delay(60000);
|
|
getDeviceStatus();
|
|
}
|