The DlmsCosemLib is a Arduino decoder Library for austrian el. energy meters with the P1 DLMS/COSEM interface.
Input is the already decrypted payload plaintext of the M-Bus message. The decoded records will be returned as a JSON.
Live test of the DlmsCosemLib Example at wokwi.com
A working P1 --> MQTT gateway with this library MBusinoP1
Thanks to DomiStyle for https://github.com/DomiStyle/esphome-dlms-meter where some ideas for decoding come from.
Thanks to HWHardsoft for the code base of this Library https://github.com/HWHardsoft/DLMS-MBUS-Reader
Include and instantiate the DlmsCosemLib class.
#include <DlmsCosemLib.h>
DlmsCosemLib dlmsCosem;Decodes an already decrypted payload plaintext of the M-Bus message. The decoded records will be written as a JsonArray (requires ArduinoJson library). The result is an array of objects. The method call returns the number of decoded fields or negative numbers if error.
uint8_t decode(uint8_t *plaintext, uint8_t size, JsonArray& root);same line from the example
fields = DlmsCosem.decode(&plaintext[0], sizeof(plaintext), root);Example JSON output:
[{"timestamp":"27.09.2021 09:47:15"},
{"code":10,"obis":"1.0.1.8.0","name":"ActiveEnergyPlus","value_scaled":12937,"units":"Wh"},
{"code":11,"obis":"1.0.2.8.0","name":"ActiveEnergyMinus","value_scaled":0,"units":"Wh"},
{"code":8,"obis":"1.0.1.7.0","name":"ActivePowerPlus","value_scaled":0,"units":"W"},
{"code":9,"obis":"1.0.2.7.0","name":"ActivePowerMinus","value_scaled":0,"units":"W"},
{"code":1,"obis":"1.0.32.7.0","name":"VoltageL1","value_scaled":233.7,"units":"V"},
{"code":2,"obis":"1.0.52.7.0","name":"VoltageL2","value_scaled":0,"units":"V"},
{"code":3,"obis":"1.0.72.7.0","name":"VoltageL3","value_scaled":0,"units":"V"},
{"code":4,"obis":"1.0.31.7.0","name":"CurrentL1","value_scaled":0,"units":"A"},
{"code":5,"obis":"1.0.51.7.0","name":"CurrentL2","value_scaled":0,"units":"A"},
{"code":6,"obis":"1.0.71.7.0","name":"CurrentL3","value_scaled":0,"units":"A"},
{"code":7,"obis":"1.0.13.7.0","name":"PowerFactor","value_scaled":1,"units":""},
{"meter_number":"181220000009"}]
Example extract the JSON
for (uint8_t i = 1; i < fields; i++) {
const char *obisString = root[i]["obis"];
const char *name = root[i]["name"];
const char *units = root[i]["units"];
double value = root[i]["value_scaled"].as<double>();
send or process the records
}You will find more details in the example
only contained records will be sended
- ["obisString"] contains the OBIS Code as ASCII array.
- ["value_scaled"] contains the value of the record as 64 bit real
- ["units"] contains the unit of the value as ASCII array
- ["name"] contains the name of the value as ASCII array
- ["meterNumber"] countains the serial Number of the meter
- ["timestamp"] countains the timestamp of the records
Returns the last error as ASCII array.
Unsupported OBIS header typeUnsupported OBIS header lengthUnsupported OBIS header mediumUnsupported OBIS data type
const char * getError(int8_t code);The returned units are Home Assistant compatible. There are two methods to get the right device- and state-classes for use as Home Assistant sensor.
const char* getDeviceClass(uint8_t code);const char* getStateClass(uint8_t code);The DlmsCosemLib library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
The DlmsCosemLib library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the MBusinoLib library. If not, see http://www.gnu.org/licenses/.