Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding errors #3

Open
HWHardsoft opened this issue Aug 30, 2023 · 4 comments
Open

Decoding errors #3

HWHardsoft opened this issue Aug 30, 2023 · 4 comments

Comments

@HWHardsoft
Copy link

HWHardsoft commented Aug 30, 2023

Hi,

First of all, thank you for this great library.

Based on your library, I wrote a demo program for a small consumption meter (MBUS slave) for the ESP32. That works fine too. For tests I used LorusFree and LibMBUS (on the Raspberry Pi). I can read my meter correctly with both programs.
Lorusfree_test_meter

I am currently trying to write a similar program for a master that decodes the data on the ESP32 with your library. Unfortunately, there seems to be a couple of problems. MBUSPayload recognizes the 7 data fields of the meter, but only displays 5 as JSON code??

`
#define DEBUG true

#include "MBUSPayload.h"
#include "ArduinoJson.h"

// Serial interface used for mbus to allow use of 8E1 encoding
#include <HardwareSerial.h>
HardwareSerial *customSerial;

#define MBUS_BAUD_RATE 2400
#define MBUS_ADDRESS 2
#define MBUS_TIMEOUT 1000 // milliseconds
#define MBUS_DATA_SIZE 255
#define MBUS_GOOD_FRAME true
#define MBUS_BAD_FRAME false

unsigned long loop_start = 0;
unsigned long last_loop = 0;
bool firstrun = true;
int Startadd = 0x13; // Start address for decoding

void setup() {
Serial.begin(115200);
Serial.println(F("emonMbus startup"));
Serial.println(F("mbus:"));
Serial.print(F(" slave address: "));
Serial.println(MBUS_ADDRESS);
Serial.print(F(" baud rate: "));
Serial.println(MBUS_BAUD_RATE);
customSerial = &Serial1;
customSerial->begin(MBUS_BAUD_RATE, SERIAL_8E1); // mbus uses 8E1 encoding
delay(1000); // let the serial initialize, or we get a bad first frame
}

void loop() {
loop_start = millis();

/************************

  • DATA COLLECTION LOOP *
    ************************/
    if ((loop_start-last_loop)>=9800 || firstrun) { // 9800 = ~10 seconds
    last_loop = loop_start; firstrun = false;
/*************
 * MBUS DATA *
 *************/
bool mbus_good_frame = false;
byte mbus_data[MBUS_DATA_SIZE] = { 0 };

if (DEBUG) Serial.print(F("mbus: requesting data from address: "));
if (DEBUG) Serial.println(MBUS_ADDRESS);
mbus_request_data(MBUS_ADDRESS);
mbus_good_frame = mbus_get_response(mbus_data, sizeof(mbus_data));
if (mbus_good_frame) {
  if (DEBUG) Serial.println(F("mbus: good frame: "));
  if (DEBUG) print_bytes(mbus_data, sizeof(mbus_data));

  int packet_size = mbus_data[1] + 6; 
  Serial.println(F("Creating payload buffer..."));
  MBUSPayload payload(255);
  Serial.print(F("Packet size: ")); Serial.println(packet_size);

  Serial.print(F("Start Address: ")); Serial.println(Startadd);
  
  Serial.println(F("Decoding..."));
  DynamicJsonDocument jsonBuffer(512);
  JsonArray root = jsonBuffer.createNestedArray();  
  uint8_t fields = payload.decode(&mbus_data[Startadd], packet_size - Startadd - 2, root); 
  serializeJsonPretty(root, Serial);

  Serial.println();
  Serial.print("Detected data fields: ");
  Serial.println(fields);
  
  Serial.print("Detected errors: ");
  Serial.println(payload.getError());

  Serial.println();
  for (uint8_t i=0; i<fields; i++) {
      float value = root[i]["value_scaled"].as<float>();
      uint8_t code = root[i]["code"].as<int>();
      Serial.print("Field "); Serial.print(i+1); 
      Serial.print(" ("); Serial.print((char *) payload.getCodeName(code)); 
      Serial.print("): ");
      Serial.print(value); Serial.print(" "); Serial.print((char *) payload.getCodeUnits(code));
      Serial.println();
  }

  Serial.println();
} else {
  Serial.print(F("mbus: bad frame: "));
  print_bytes(mbus_data, sizeof(mbus_data));
}

}
}
`

output

So the binary inputs and outputs are not decoded correctly.

@HWHardsoft
Copy link
Author

HWHardsoft commented Aug 30, 2023

Addendum:

When I try to decode the data from a commercial M-BUS device (whose payload is not encoded with MBUSPayload), things look even worse. I tried to decode the data of an elvaco MBUS thermostat. No problem with LorusFree. MBusPaypal does not find any data fields and reports 4 errors:

output2

Data from Thermostat (payload starts at address 19):
68 53 53 68 8 2 72 11 82 0 24 96 15 16 0 20 0 0 0 1 fd 1b 0 2 fc 3 48 52 25 74 62 17 22 fc 3 48 52 25 74 5c 17 12 fc 3 48 52 25 74 68 17 2 65 a2 8 22 65 9e 8 12 65 a2 8 1 72 0 72 65 0 0 b2 1 65 0 0 c 78 11 82 0 24 3 fd f 5 9 3 1f 9f 16

And here the decoding by LorusFree:

grafik

@Zeppelin500
Copy link

Hello,

Thank you for the libary!

I have the same issue. Do you find a solution?

Try to develop a M-Bus MQTT gateway.
It work, but only with the Engelmann Sensostar

Now i try to write a new version with the mbus-payload libary to enable more M-Bus devices, but there are some problems.

The M-Bus data part of the telegram is right received.
I get error code 4, interpreted as "UNSUPPORTED_VIF" Right?
The first 5 fields are decoded and inside the json string. The variable "fields" is 0.
Next VIF (#6) is 0h3C and should be supported as I understood the libary.

I also tried your code above HWHardsoft, but exactly the same.

@Zeppelin500
Copy link

Zeppelin500 commented Oct 21, 2023

Answer to myself:

It is just an issue of the size of the involved variables.
In my case a M-Bus telegram with (L-Byte) 193 uses around 2k! byte more then 4k byte for serialize the json doc.

Now I have different issues, but step by step.

@HWHardsoft
Copy link
Author

@Zeppelin500

I've tried your new version of MBUS Payload library today. Also I've increased the size of the JSON buffer to 4k but no change. The errors during decoding of data of elvaco thermostat are still the same.
Did you've tried to decode my sampe data with your MBUS Payload version too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants