Récupérer données shelly EM

Bonjour,

Je souhaiterais récupérer les données de puissance en W des phases (pinces ampèremétriques) d'un Shelly 2EM et les afficher sur un # ESP32-2432S028R.
Voici mon code :

#include <ArduinoJson.h>
#include <WiFi.h>
#include <SPI.h>
#include <TFT_eSPI.h>  // Bibliothèque pour l'écran TFT

// Paramètres Wi-Fi et Shelly
const char* ssid = "Livebox-****";
const char* password = "*****";
const char* shelly_ip = "192.168.1.3";

float power_1;
float power_2;

TFT_eSPI tft = TFT_eSPI();

void setup() {
  WiFi.begin(ssid, password);
  tft.init();
  tft.setRotation(1);  // Ajuster la rotation selon votre configuration
  tft.fillScreen(TFT_BLACK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    tft.print(".");
  }
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {  // Vérifier si connecté au WiFi
    WiFiClient client;
    String url = "/status";
    if (client.connect(shelly_ip, 80)) {  // Connexion à l'IP de Shelly sur le port 80
      client.print(String("GET") + url + " HTTP/1.1\r\n" + "host : " + shelly_ip + "\r\n" + "Connection : close\r\n\r\n");

      delay(500);  // Attendre la réponse

      String response = "";
      while (client.available()) {
        String line = client.readStringUntil('\r');
        response += line;
      }

      client.stop();

      // Extraire la partie JSON de la réponse HTTP
      int jsonIndex = response.indexOf('{');
      if (jsonIndex != -1) {
        String jsonResponse = response.substring(jsonIndex);

        // Analyser JSON
        DynamicJsonDocument doc(2048);
        deserializeJson(doc, jsonResponse);

        float power_1 = doc["emeters"][0]["power"];  // Puissance phase 1
        float power_2 = doc["emeters"][1]["power"];  // Puissance phase 2

        setPower1(power_1);
        setPower2(power_2);
      }
    } else {
      tft.println("Echec connexion Shelly");
      delay(10000);  // Attendez 10 secondes avant la prochaine lecture
    }
  } else {
    tft.println("WiFi non connecté");
  }

  tft.setTextSize(2);
  tft.setCursor(10, 60);
  tft.print("Consommation:");
  tft.println(getPower1());
  tft.setCursor(10, 120);
  tft.print("Production:");
  tft.println(getPower2());
}

float getPower1() {
  return power_1;
}

float getPower2() {

  return power_2;
}

void setPower1(float power) {
  power_1 = power;
}

void setPower2(float power) {
  power_2 = power;
}

L'affichage fonctionne mais pas les données...
Si une âme charitable aurait une idée svp ?
Merci beaucoup d'avance.

Post mis dans la mauvaise section, on parle anglais dans les forums généraux, je viens de déplacer le post dans la section francophone.

Merci de prendre en compte les recommandations listées dans "Les bonnes pratiques du Forum Francophone".

1 Like

Bonjour,

Tu as essayé de faire afficher jsonResponse avec un Serial.print() pour voir son contenu?

En fait, si. Mais je sèche quand même :rofl:

Ce serait plus pratique de le faire afficher par un Serial.print() car on peut alors le copier pour l'analyser.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.