Skip to content

Decoding errors #3

@HWHardsoft

Description

@HWHardsoft

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions