# Arduino MEGA met Ethernet shield

### Arduino Mega met Ethernet shield

Met onderstaande code kun je, met een Arduino MEGA met Ethernet shield, gegevens uit de DSMR-logger32 ophalen en verder verwerken.

```

//==== edit "myCredentials_org.h" ========
//==== and save it as "myCredentials.h" ==
#include "myCredentials.h"
​
#include <ArduinoHttpClient.h>    // tested with version 0.4.0
#include <Ethernet.h>
#include <SPI.h>
​
//------ [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() 
{
  EthernetClient ETHclient;
  HttpClient DSMRclient = HttpClient(ETHclient, DSMRserverIP, 80);
​
  payload = ""; 
   
  Serial.println(F("making GET request"));
  DSMRclient.get(DSMRrestAPI);
​
  // read the response code and body of the response
  httpResponseCode = DSMRclient.responseStatusCode();
​
  if (httpResponseCode <= 0)
  {
    Serial.print(F("http Response Code: "));
    Serial.println(httpResponseCode);
    return false;
  }
​
  payload    = DSMRclient.responseBody();
​
  deserializeJson(dsmrDoc, payload, DeserializationOption::Filter(filter));
​
  // Free resources
  DSMRclient.stop();
​
  if (firstCall)
  {
    Serial.print("dsmrDoc::");
    serializeJsonPretty(dsmrDoc, Serial);
    Serial.println();
  }
  firstCall = false;
​
  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();
​
  // Initialize Ethernet library
  byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
​
  if (!Ethernet.begin(mac)) 
  {
    Serial.println(F("Failed to configure Ethernet"));
    return;
  }
  delay(1000);
​
  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()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mrwheel-docs.gitbook.io/dsmrlogger32/beschrijving-restapis-v2/arduino-mega-met-ethernet-shield.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
