68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
#define PIN_LED 2
|
|
|
|
|
|
const char* ssid = "Livebox-37cc";
|
|
const char* pass = "8A6060920A8A86896F770F2C47";
|
|
|
|
void setup() {
|
|
pinMode(PIN_LED, OUTPUT);
|
|
|
|
Serial.begin(115200);
|
|
// Connectez-vous au réseau WiFi
|
|
WiFi.begin(ssid, pass);
|
|
Serial.println("Connexion au WiFi en cours.");
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
digitalWrite(PIN_LED, LOW);
|
|
delay(500);
|
|
Serial.print(".");
|
|
digitalWrite(PIN_LED, HIGH);
|
|
delay(500);
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connecté au réseau WiFi");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void loop() {
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
HTTPClient http;
|
|
|
|
http.begin("http://192.168.1.3:5000/control"); // URL de votre microservice
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
String json = "{\"action\":\"on\"}"; // ou "{\"action\":\"off\"}"
|
|
|
|
int httpResponseCode = http.POST(json);
|
|
|
|
if (httpResponseCode > 0) {
|
|
String response = http.getString();
|
|
Serial.println(httpResponseCode);
|
|
Serial.println(response);
|
|
|
|
StaticJsonDocument<200> doc;
|
|
|
|
// Parse le JSON
|
|
DeserializationError error = deserializeJson(doc, response);
|
|
if (error) {
|
|
Serial.print(F("Failed to parse JSON: "));
|
|
Serial.println(error.f_str());
|
|
return;
|
|
}
|
|
double conso = doc["conso_apparente"]; //[0];
|
|
Serial.println(conso);
|
|
} else {
|
|
Serial.print("Erreur lors de l'envoi de la requête POST: ");
|
|
Serial.println(httpResponseCode);
|
|
}
|
|
|
|
http.end();
|
|
}
|
|
|
|
delay(10000); // Attente de 10 secondes avant de répéter l'action
|
|
}
|