matthijskooijman / arduino-dsmr

Arduino library for interfacing with Dutch smart meters implementing DSMR
133 stars 110 forks source link

Baud rate check in header parsing too strict #47

Closed Hadramal closed 1 year ago

Hadramal commented 1 year ago

Hello!

I bought a Slimmelezer+ from Marcel Zuidwijk and it seems to use this parser in the firmware built upon ESPHome. It connects to the electricity meter just fine but can't read the data:

19:04:32 | [E] | [dsmr:265] | /ADN9 6534  
^ 
Invalid identification string

Now this seems to be because of this line in this library:

// The first identification line looks like:
// XXX5<id string>
// The DSMR spec is vague on details, but in 62056-21, the X's
// are a three-leter (registerd) manufacturer ID, the id
// string is up to 16 chars of arbitrary characters and the
// '5' is a baud rate indication. 5 apparently means 9600,
// which DSMR 3.x and below used. It seems that DSMR 2.x
// passed '3' here (which is mandatory for "mode D"
// communication according to 62956-21), so we also allow
// that.
if (line_start + 3 >= line_end || (line_start[3] != '5' && line_start[3] != '3'))
return res.fail(F("Invalid identification string"), line_start);

I.e. unless the fourth char (after the /, which is read before) is 3 or 5, the lib stops reading data. But, that fourth char is the baud rate according to IEC62056-21 (and your comment) and could be many things. In Sweden the standard is 115200 which is the number 9. My Aidon meter spits out data like this:

/ADN9 6534

0-0:1.0.0(213112235959W)
1-0:1.8.0(12345678.123*kWh)
1-0:2.8.0(12345678.123*kWh)
1-0:3.8.0(12345678.123*kVarh) 
....
1-0:71.7.0(123.1*A)!

It seems like this check is far too strict? Baud rate can be left to ESPHome or whatever other device uses this lib? It looks like without this header verification the device could parse "my" format as well, and 9 seems to be a valid number according to the spec.

matthijskooijman commented 1 year ago

Thanks for your report. It seems the check is indeed too strict, so I just removed the check entirely in git master.

Note that I'm not sure if esphome uses my library directly, I suspect there might be some forks floating around, so it might not start working immediately for you.

Also, could you maybe paste a complete telegraf from your meter here? I would like to collect some examples of different formats for future automated testing :-)

Hadramal commented 1 year ago

First of all thank you!

Yes - after making the issue I realised ESPHome is using a fork of your code, so I will raise an issue with the maintainers of that repo as well... I could have made a pullrequest myself but I'm not sure I fully understand the ESPHome build system so I'm not sure how I could test it! I normally write Java so everything's familiar yet strange.

As the reader I have stops reading data after the header I can't give you actual live data, but I got the example from this document: https://www.skekraft.se/wp-content/uploads/2022/10/SKEKRAFT1-1026973-v2-Aidon_HAN_Interface_RJ12.pdf

/Henrik

On Wed, Feb 8, 2023 at 12:44 PM Matthijs Kooijman @.***> wrote:

Thanks for your report. It seems the check is indeed too strict, so I just removed the check entirely in git master.

Note that I'm not sure if esphome uses my library directly, I suspect there might be some forks floating around, so it might not start working immediately for you.

Also, could you maybe paste a complete telegraf from your meter here? I would like to collect some examples of different formats for future automated testing :-)

— Reply to this email directly, view it on GitHub https://github.com/matthijskooijman/arduino-dsmr/issues/47#issuecomment-1422464932, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALRVG3AS5247C4GBEN56LZLWWOBKRANCNFSM6AAAAAAUUJXGI4 . You are receiving this because you authored the thread.Message ID: @.***>

matthijskooijman commented 1 year ago

Thanks! I've added the spec to the repo for future reference :-)

marcus-aa commented 1 year ago

Resolved, but here are some complete telegrams from a Swedish AIdon 6534.

/ADN9 6534

0-0:1.0.0(230302093810W) 1-0:1.8.0(00031157.405kWh) 1-0:2.8.0(00000000.000kWh) 1-0:3.8.0(00000857.557kVArh) 1-0:4.8.0(00000918.106kVArh) 1-0:1.7.0(0004.344kW) 1-0:2.7.0(0000.000kW) 1-0:3.7.0(0000.314kVAr) 1-0:4.7.0(0000.000kVAr) 1-0:21.7.0(0001.083kW) 1-0:22.7.0(0000.000kW) 1-0:41.7.0(0001.855kW) 1-0:42.7.0(0000.000kW) 1-0:61.7.0(0001.408kW) 1-0:62.7.0(0000.000kW) 1-0:23.7.0(0000.351kVAr) 1-0:24.7.0(0000.000kVAr) 1-0:43.7.0(0000.000kVAr) 1-0:44.7.0(0000.545kVAr) 1-0:63.7.0(0000.507kVAr) 1-0:64.7.0(0000.000kVAr) 1-0:32.7.0(231.9V) 1-0:52.7.0(232.6V) 1-0:72.7.0(232.7V) 1-0:31.7.0(004.9A) 1-0:51.7.0(008.3A) 1-0:71.7.0(006.4A) !7D79 /ADN9 6534

0-0:1.0.0(230302093820W) 1-0:1.8.0(00031157.405kWh) 1-0:2.8.0(00000000.000kWh) 1-0:3.8.0(00000857.557kVArh) 1-0:4.8.0(00000918.106kVArh) 1-0:1.7.0(0004.320kW) 1-0:2.7.0(0000.000kW) 1-0:3.7.0(0000.313kVAr) 1-0:4.7.0(0000.000kVAr) 1-0:21.7.0(0001.063kW) 1-0:22.7.0(0000.000kW) 1-0:41.7.0(0001.853kW) 1-0:42.7.0(0000.000kW) 1-0:61.7.0(0001.411kW) 1-0:62.7.0(0000.000kW) 1-0:23.7.0(0000.329kVAr) 1-0:24.7.0(0000.000kVAr) 1-0:43.7.0(0000.000kVAr) 1-0:44.7.0(0000.545kVAr) 1-0:63.7.0(0000.505kVAr) 1-0:64.7.0(0000.000kVAr) 1-0:32.7.0(231.7V) 1-0:52.7.0(232.4V) 1-0:72.7.0(232.8V) 1-0:31.7.0(004.8A) 1-0:51.7.0(008.3A) 1-0:71.7.0(006.4A) !8D80 /ADN9 6534

0-0:1.0.0(230302093830W) 1-0:1.8.0(00031157.405kWh) 1-0:2.8.0(00000000.000kWh) 1-0:3.8.0(00000857.557kVArh) 1-0:4.8.0(00000918.106kVArh) 1-0:1.7.0(0005.984kW) 1-0:2.7.0(0000.000kW) 1-0:3.7.0(0000.332kVAr) 1-0:4.7.0(0000.000kVAr) 1-0:21.7.0(0001.057kW) 1-0:22.7.0(0000.000kW) 1-0:41.7.0(0002.688kW) 1-0:42.7.0(0000.000kW) 1-0:61.7.0(0002.233kW) 1-0:62.7.0(0000.000kW) 1-0:23.7.0(0000.356kVAr) 1-0:24.7.0(0000.000kVAr) 1-0:43.7.0(0000.000kVAr) 1-0:44.7.0(0000.960kVAr) 1-0:63.7.0(0000.963kVAr) 1-0:64.7.0(0000.000kVAr) 1-0:32.7.0(231.8V) 1-0:52.7.0(231.8V) 1-0:72.7.0(231.5V) 1-0:31.7.0(004.8A) 1-0:51.7.0(012.3A) 1-0:71.7.0(010.4A) !806A /ADN9 6534

0-0:1.0.0(230302093840W) 1-0:1.8.0(00031157.405kWh) 1-0:2.8.0(00000000.000kWh) 1-0:3.8.0(00000857.557kVArh) 1-0:4.8.0(00000918.106kVArh) 1-0:1.7.0(0004.327kW) 1-0:2.7.0(0000.000kW) 1-0:3.7.0(0000.302kVAr) 1-0:4.7.0(0000.000kVAr) 1-0:21.7.0(0001.058kW) 1-0:22.7.0(0000.000kW) 1-0:41.7.0(0001.859kW) 1-0:42.7.0(0000.000kW) 1-0:61.7.0(0001.410kW) 1-0:62.7.0(0000.000kW) 1-0:23.7.0(0000.347kVAr) 1-0:24.7.0(0000.000kVAr) 1-0:43.7.0(0000.000kVAr) 1-0:44.7.0(0000.540kVAr) 1-0:63.7.0(0000.508kVAr) 1-0:64.7.0(0000.000kVAr) 1-0:32.7.0(232.1V) 1-0:52.7.0(232.9V) 1-0:72.7.0(232.9V) 1-0:31.7.0(004.8A) 1-0:51.7.0(008.3A) 1-0:71.7.0(006.4A) !120C /ADN9 6534

0-0:1.0.0(230302093850W) 1-0:1.8.0(00031157.405kWh) 1-0:2.8.0(00000000.000kWh) 1-0:3.8.0(00000857.557kVArh) 1-0:4.8.0(00000918.106kVArh) 1-0:1.7.0(0004.319kW) 1-0:2.7.0(0000.000kW) 1-0:3.7.0(0000.309kVAr) 1-0:4.7.0(0000.000kVAr) 1-0:21.7.0(0001.055kW) 1-0:22.7.0(0000.000kW) 1-0:41.7.0(0001.856kW) 1-0:42.7.0(0000.000kW) 1-0:61.7.0(0001.409kW) 1-0:62.7.0(0000.000kW) 1-0:23.7.0(0000.331kVAr) 1-0:24.7.0(0000.000kVAr) 1-0:43.7.0(0000.000kVAr) 1-0:44.7.0(0000.541kVAr) 1-0:63.7.0(0000.507kVAr) 1-0:64.7.0(0000.000kVAr) 1-0:32.7.0(232.1V) 1-0:52.7.0(232.7V) 1-0:72.7.0(232.8V) 1-0:31.7.0(004.7A) 1-0:51.7.0(008.3A) 1-0:71.7.0(006.4A) !1C56

matthijskooijman commented 1 year ago

Thanks!