ESP32 (WiFi)

Met onderstaande code kun je met een ESP32 gegevens uit de DSMR-logger32 ophalen en verder verwerken.

//==== edit "myCredentials_org.h" ========
//==== and save it as "myCredentials.h" ==
#include "myCredentials.h"

#include <WiFi.h>
#include <HTTPClient.h>

const char *ssid      = _WIFI_SSID;
const char *password  = _WIFI_PASSWRD;

//------ [ArduinoJson@6.19.3]
#include <ArduinoJson.h>
//-- if you need more fields make this JsonDocument bigger
StaticJsonDocument<350> dsmrDoc;
//-- if you need more fields make this JsonDocument bigger
StaticJsonDocument<150> filter;

#define _READINTERVAL     30000

const char *DSMRprotocol  = "http://";
const char *DSMRserverIP  = _DSMR_IP_ADDRESS;
const char *DSMRrestAPI   = "/api/v2/sm/actual";
String      payload;
int         httpResponseCode;
uint32_t    lastRead = 0;

//--- catch specific fields for further processing -------
//--- these are just an example! see readDsmrLogger() ----
String  timeStamp;
float   energyDelivered, pwrDelivered; 
float   energyReturned,  pwrReturned;
float   gasDelivered;
bool    firstCall = true;

//--------------------------------------------------------------------------
bool dsmrGETrequest() 
{
  HTTPClient DSMRclient;
    
  // Your IP address with path or Domain name with URL path 
  DSMRclient.begin(String(DSMRprotocol) + String(DSMRserverIP)+String(DSMRrestAPI));
  
  // Send HTTP GET request
  httpResponseCode = DSMRclient.GET();
  
  payload = ""; 
  
  if (httpResponseCode > 0) 
  {
    deserializeJson(dsmrDoc, DSMRclient.getString(), DeserializationOption::Filter(filter));
    if (firstCall)
    {
      Serial.print("dsmrDoc::");
      serializeJsonPretty(dsmrDoc, Serial);
      Serial.println();
    }
    firstCall = false;
  }
  else 
  {
    payload = "{\"actual\":[{\"name\":\"httpresponse\", \"value\": "+String(httpResponseCode)+"}]}";
    // Free resources
    DSMRclient.end();
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    return false;
  }

  // Free resources
  DSMRclient.end();

  return true;
  
} // dsmrGETrequest()

//--------------------------------------------------------------------------
void readDsmrLogger()
{
  int fieldNr = 0;

  dsmrGETrequest();

  //-- store the fields you are interested in ----------
  timeStamp         = dsmrDoc["actual"]["timestamp"].as<const char*>();
  energyDelivered   = dsmrDoc["actual"]["energy_delivered_tariff1"].as<float>()
                    + dsmrDoc["actual"]["energy_delivered_tariff2"].as<float>();
  pwrDelivered      = dsmrDoc["actual"]["power_delivered"].as<float>();
  energyReturned    = dsmrDoc["actual"]["energy_returned_tariff1"].as<float>()
                    + dsmrDoc["actual"]["energy_returned_tariff2"].as<float>();
  pwrReturned       = dsmrDoc["actual"]["power_returned"].as<float>();
  gasDelivered      = dsmrDoc["actual"]["gas_delivered"].as<float>();

  if (firstCall)
  {
    Serial.println();
    Serial.println(F("==== Start parsing payload ======================="));

    for (JsonPair p : dsmrDoc["actual"].as<JsonObject>())
    {
      Serial.print(p.key().c_str()); Serial.print("\t: ");
      if (p.value().is<int>())              Serial.println(p.value().as<int>());
      else if (p.value().is<float>())       Serial.println(p.value().as<float>());
      else if (p.value().is<const char*>()) Serial.println(p.value().as<const char*>());
      else Serial.println("unknown type");
    }
    Serial.println(F("=================================================="));
  }
      
} // readDsmrLogger()


//--------------------------------------------------------------------------
void setup() 
{
  Serial.begin(115200);
  while(!Serial) { /* wait a bit */ }

  Serial.println("");
  Serial.println(F("\r\n***************************************"));
  Serial.println(F("And then it all begins ..."));
  Serial.println(F("***************************************\r\n"));

  //-- The filter: it contains "true" for each value we want to capture
  filter["actual"]["timestamp"] = true;
  filter["actual"]["power_delivered"] = true;
  filter["actual"]["power_returned"] = true;
  filter["actual"]["gas_delivered"] = true;
  filter["actual"]["energy_delivered_tariff1"] = true;
  filter["actual"]["energy_delivered_tariff2"] = true;
  filter["actual"]["energy_returned_tariff1"] = true;
  filter["actual"]["energy_returned_tariff2"] = true;
  Serial.print("filterDoc::");
  serializeJsonPretty(filter, Serial);
  Serial.println();

  WiFi.begin(ssid, password);
  Serial.println(F("Connecting"));
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print(F("Connected to WiFi network with IP Address: "));
  Serial.println(WiFi.localIP());

  lastRead = millis() + _READINTERVAL;

  Serial.println(F("\r\nStart reading ..."));

} // setup()


//--------------------------------------------------------------------------
void loop() 
{
  if ((millis() - lastRead) > _READINTERVAL)
  {
    lastRead = millis();
    Serial.print(F("\r\nread API/v2 from DSMR-logger @"));
    Serial.println(_DSMR_IP_ADDRESS);
    readDsmrLogger();
    Serial.println(F("\r\nCaptured fields ==============================="));
    Serial.print(F("timestamp       : ")); Serial.println(timeStamp);
    Serial.print(F("energyDelivered : ")); Serial.println(energyDelivered);
    Serial.print(F("pwrDelivered    : ")); Serial.println(pwrDelivered);
    Serial.print(F("energyReturned  : ")); Serial.println(energyReturned);
    Serial.print(F("pwrReturned     : ")); Serial.println(pwrReturned);
    Serial.print(F("gasDelivered    : ")); Serial.println(gasDelivered);
    Serial.println(F("\r\n"));
  }
  
} // loop()

Last updated