{"id":534,"date":"2015-07-05T15:36:15","date_gmt":"2015-07-05T14:36:15","guid":{"rendered":"http:\/\/www.sevenwatt.com\/main\/?p=534"},"modified":"2016-01-10T14:07:04","modified_gmt":"2016-01-10T13:07:04","slug":"rfm69-energy-count-3000-elv-cost-control","status":"publish","type":"post","link":"https:\/\/www.sevenwatt.com\/main\/rfm69-energy-count-3000-elv-cost-control\/","title":{"rendered":"RFM69 &#8211; Energy Count 3000 \/ ELV Cost Control"},"content":{"rendered":"<p>Conrad Energy Count 3000 and ELV Cost Control are wireles 230V electricity consumption measuring plugs Thomas Schulz has cracked the RF protocol of these device back in 2011 on the <a href=\"http:\/\/forum.jeelabs.net\/comment\/4972.html#comment-4972\" target=\"_blank\">JeeLabs forum<\/a>. The signals are FSK encoded and use the 868Mhz band. Thomas was able to receive them with a RFM12B module. Together with a JeeLink V3, it has found popular use with the German home automation software <a href=\"http:\/\/www.fhemwiki.de\/wiki\/JeeLink\" target=\"_blank\">FEHM<\/a>.<\/p>\n<p>In this post I describe how the RFM69 can take over a task that earlier had to be performed in software.<\/p>\n<p>One of the complications of the protocol is that the preamble of a series of 0x55 is scramble (whitened), and therefore more difficult to recognize. However, after scrambling it is still identical for all packets.<\/p>\n<pre>raw data 25 8A F9 59 DA 02 7E 15 ED 67 13 F1 85 D3 AC D2   [payload]   13 E6 84 A8 3B \r\ndescramb 48 BB 55 55 55 55 55 55 55 55 55 55 55 55 55 7E   [payload]   7E 55 55 55 55 \r\n               -------------------------------------- --   ---------   -- -----------\r\n                             preamble                 HDLC  payload  HDLC  postamble\r\n<\/pre>\n<p>The original Jeelib-based RFM12B programs detection of valid packets was clumsy.<!--more--><\/p>\n<p>It started matching the last 4 bytes of the preamble with the received signals<br \/>\n<code>13 F1 85 D3 AC<\/code><br \/>\nThe problem was that it is undetermined which is the first bit received from the preamble, as the receiver first has to detect a signal and then tune its gain and frequency. Some bits are lost. So Thomas Schulz had to not only check for the given series of bytes, but also for all 7 bit shifted variants. The code checks on:<br \/>\n<code>13 F1 85 D3 | F8 C2 E9 D6 | FC 61 74 EB | 7E 30 BA 75 | 3F 18 5D 3A | 9F 8C 2E 9D | 4F C6 17 4E | 27 E3 0B A7<br \/>\n<\/code><br \/>\nAfter detecting one of these pattern, the complete receive buffer gets bit-shifted the proper amount so that the buffer shows the last 5 preamble bytes as <code>13 F1 85 D3 AC<\/code><\/p>\n<h3>Sync words<\/h3>\n<p>Here comes the RFM69 to the rescue. Where the RFM12B can only program one user defined syncword-byte, the RFM69 can have 8. When programming those last five preamble bytes as sync-words, the task of detecting a proper packet and bit-aligning it is fully left to the RFM69.<br \/>\nThe received signal now no longer contains the preamble, but starts at the HDLC control word 0x7E.<\/p>\n<pre>raw data    D2   [payload]   13 E6 84 A8 3B \r\ndescrambled 7E   [payload]   7E 55 55 55 55 \r\n            --   ---------   -- -----------\r\n            HDLC  payload  HDLC  postamble\r\n<\/pre>\n<p>Effortless!<br \/>\n&nbsp;<\/p>\n<h3>Descrambling<\/h3>\n<p>But before this will start working one has to realize that the preamble bits are also scrambled. In other words, the descrambler needs to be primed if one wants to start descrambling at the payload.<br \/>\nThe actual AX5051 \/ AX5042 scrambling is this polynomial: <em>1+X<sup>12<\/sup>+X<sup>17<\/sup><\/em>.<br \/>\n<a href=\"\/\/www.sevenwatt.com\/main\/wp-content\/uploads\/2015\/07\/AX5051-descrambler.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-545\" src=\"\/\/www.sevenwatt.com\/main\/wp-content\/uploads\/2015\/07\/AX5051-descrambler.png\" alt=\"AX5051-descrambler\" width=\"733\" height=\"119\" srcset=\"https:\/\/www.sevenwatt.com\/main\/wp-content\/uploads\/2015\/07\/AX5051-descrambler.png 733w, https:\/\/www.sevenwatt.com\/main\/wp-content\/uploads\/2015\/07\/AX5051-descrambler-300x49.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/a><br \/>\nPriming needs to be done for 17 bits at most, So the last three bytes of the preamble could be send through the descrambling algorithm and discarded before descrambling HDLC+payload. As the preamble is always constant, the priming value can also be determine one time, which I did.<br \/>\nThe magic number is <code>scramshift = 0xF185D3AC;<\/code><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic void descramb(uint8_t* buff, uint16_t len) {\r\n\tuint8_t ibit, obit;\r\n\tuint8_t bit;\r\n\tuint8_t inpbyte, outbyte;\r\n\tuint32_t scramshift;\r\n\r\n\t\/\/scramshift = 0xFFFFFFFF;\r\n\tscramshift = 0xF185D3AC; \/\/descrambler primed at end of preanble\r\n\twhile (len--) {\r\n\t\tinpbyte = *buff;\r\n\t\tfor (bit = 0; bit &lt; 8; ++bit) {\r\n\t\t\t\/\/ RFM69 receives MSBit first into bytes:\r\n\t\t\tibit = (inpbyte &amp; 0x80) &gt;&gt; 7;\r\n\t\t\tobit = ibit ^ (count1bits(scramshift &amp; SCRAMPOLY) &amp; 0x01);\r\n\t\t\tscramshift = scramshift &lt;&lt; 1 | ibit;\r\n\t\t\tinpbyte &lt;&lt;= 1;\r\n\t\t\toutbyte = outbyte &lt;&lt; 1 | obit;\r\n\t\t}\r\n\t\t*buff++ = outbyte ^ 0xFF;\r\n\t}\r\n}\r\n<\/pre>\n<h3>Code<\/h3>\n<p>The software is developed on a LPC824. It can be used on the complete LPC8xx family as it fits the tiny LPC810. Hardware that can be used is for example a Jeelabs <a href=\"http:\/\/jeelabs.org\/book\/1523c\/\" target=\"_blank\">Tinker Pico LPC824 board<\/a>.<br \/>\nThe software can be found in my github <a href=\"https:\/\/github.com\/SevenW\/embapps\/tree\/master\/embapps\/costcontrol\" target=\"_blank\">embapps<\/a> repository<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conrad Energy Count 3000 and ELV Cost Control are wireles 230V electricity consumption measuring plugs Thomas Schulz has cracked the RF protocol of these device back in 2011 on the JeeLabs forum. The signals are FSK encoded and use the 868Mhz band. Thomas was able to receive them with a RFM12B module. Together with a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,27,15],"tags":[6,53,55,54,8,49],"class_list":["post-534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jeenode","category-picos-arm","category-rfm69","tag-868mhz","tag-ec3k","tag-elv-cost-control","tag-energy-count-3000","tag-fsk","tag-rfm69"],"_links":{"self":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/comments?post=534"}],"version-history":[{"count":19,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/534\/revisions\/577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/media\/535"}],"wp:attachment":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}