diff --git b/LICENSE a/LICENSE new file mode 100644 index 0000000..3c4e824 --- /dev/null +++ a/LICENSE @@ -0,0 +1,189 @@ +GNU GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright © 2007 Free Software Foundation, Inc. + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. +Preamble + +The GNU General Public License is a free, copyleft license for software and other kinds of works. + +The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. + +To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. + +For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. + +Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. + +For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. + +Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. + +Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. + +The precise terms and conditions for copying, distribution and modification follow. +TERMS AND CONDITIONS +0. Definitions. + +“This License” refers to version 3 of the GNU General Public License. + +“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. + +“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. + +To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work. + +A “covered work” means either the unmodified Program or a work based on the Program. + +To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. + +To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. + +An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. +1. Source Code. + +The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work. + +A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. + +The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. + +The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. +2. Basic Permissions. + +All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. +3. Protecting Users' Legal Rights From Anti-Circumvention Law. + +No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. +4. Conveying Verbatim Copies. + +You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. +5. Conveying Modified Source Versions. + +You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified it, and giving a relevant date. + b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”. + c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. + d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. + +A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. +6. Conveying Non-Source Forms. + +You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: + + a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. + b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. + c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. + d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. + e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. + +A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. + +“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). + +The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. +7. Additional Terms. + +“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or + b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or + c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or + d) Limiting the use for publicity purposes of names of licensors or authors of the material; or + e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or + f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. + +All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. +8. Termination. + +You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. +9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. +10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. + +An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. +11. Patents. + +A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”. + +A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. + +In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. + +If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. + +A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. +12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. +13. Use with the GNU Affero General Public License. + +Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. +14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. + +Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. +15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. +16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. + +END OF TERMS AND CONDITIONS diff --git b/README.md a/README.md new file mode 100644 index 0000000..71d2dc7 --- /dev/null +++ a/README.md @@ -0,0 +1,3 @@ +# OpenTherm Arduino ESP32/ESP8266 Library + + diff --git b/examples/Basic_Thermostat/Basic_Thermostat.ino a/examples/Basic_Thermostat/Basic_Thermostat.ino new file mode 100644 index 0000000..c8d2b3f --- /dev/null +++ a/examples/Basic_Thermostat/Basic_Thermostat.ino @@ -0,0 +1,225 @@ +/* + * https://github.com/Jeroen88/EasyOpenTherm + * https://www.tindie.com/products/Metriot/OpenTherm-adapter/ + * + * Basic_Thermostat is a program to demonstrate the main communication of a thermostat (master) with an OpenTherm compatible boiler (slave) + * Copyright (C) 2022 Jeroen Döll + * + * This program 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. + * + * This program 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 this program. If not, see . + * + * You need an OpenTherm controller that you can buy at my Tindie store, + * Connect the two boiler wires to the OpenTherm controller pins marked OT. The order is not important. + * Connect the OpenTherm controller to your microcontroller's power (3v3) and ground (GND) pins. + * Connect the OpenTherm TXD pin to the microcontroller's pin defined by #define OT_RX_PIN. + * Connect the OpenTherm RXD pin to the microcontroller's pin defined by #define OT_TX_PIN. + * + * Connect the BME280 temperature sensor SDA pin to the microcontroller's pin defined by #define I2C_SDA_PIN + * Connect the BME280 temperature sensor SCL pin to the microcontroller's pin defined by #define I2C_SCL_PIN + * Check your sensor's address, it may differ from the value defined by #define BME_ADDRESS (0x76) + * Install the Adafruit BME280 library + * Any other temperature sensor may be used, like a Dallas sensor, BME380, BMP380, BME680 if you adapt the program accordingly. + * + * Define the room temperature setpoint (desired room temperature) using #define ROOM_TEMPERATURE_SETPOINT. In a real application this should be settable. + * Eventually define the maximum central heating boiler temperature setpoint using #define CH_MAX_SETPOINT. + * + * Compile and upload the program as normal. If the temperature measured by your sensor is lower than the ROOM_TEMPERATURE_SETPOINT this thermostat program will actually begin to heat up your room +*/ + + +#include + +#include + +#define OT_RX_PIN (34) +#define OT_TX_PIN (17) + +#define BME_ADDRESS (0x76) +#define I2C_SDA_PIN (8) +#define I2C_SCL_PIN (9) + + +#define ROOM_TEMPERATURE_SETPOINT (18.5f) // The desired room temperature +#define CH_MAX_SETPOINT (60.0f) // If your house is well isolated and/or you have low temperature radiators this could be as low as 40.0f +#define CH_MIN_SETPOINT (10.0f) // If the boiler starts it will warm up untill at least this temperature (unless the desired room temperature is reached before) + +#define ENABLE_HEATING (true) // If the boiler supports Central Heating (CH), use the boiler for heating +#define ENABLE_COOLING (true) // If the boiler supports cooling, use the boiler for cooling +#define ENABLE_DOMESTIC_HOT_WATER (true) // If the boiler supports Domestic Hot Water (DHW), use the boiler for DHW + +#include +#include +#include + +Adafruit_BME280 bme; // I2C + +// Use a PID controller to calculate the CH setpoint. See e.g. https://en.wikipedia.org/wiki/PID_controller +float pid(float sp, + float pv, + float pv_last, + float & ierr, + float dt) { + float KP = 30; + float KI = 0.02; + + float error = sp - pv; + ierr = ierr + KI * error * dt; + float dpv = (pv - pv_last) / dt; + float P = KP * error; + float I = ierr; + float op = P + I; + // anti-reset windup + if ((op < CH_MIN_SETPOINT) || (op > CH_MAX_SETPOINT)) { + I = I - KI * error * dt; + op = max(CH_MIN_SETPOINT, min(CH_MAX_SETPOINT, op)); + } + ierr = I; + + return op; +} + + +void setup() { + Serial.begin(115200); + delay(5000); // For debug only: give the Serial Monitor some time to connect to the native USB of the MCU for output + Serial.println("\n\nStarted"); + + Serial.println(F("\n-- BME280 test --")); + Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); + + bool status = bme.begin(BME_ADDRESS); + if (!status) { + Serial.println("Could not find a valid BME280 sensor, check wiring and address!"); + while (1); + } + Serial.println("-- Temperature sensor present --"); + + bme.setSampling(); + + Serial.println("Setup done, start loop..."); +} + + +void loop() { + // Create an OpenTherm thermostat (boiler is slave) with OT_RX_PIN to receive data from boiler and OT_TX_PIN to send data to boiler + // Only one OpenTherm object may be created! + static OpenTherm thermostat(OT_RX_PIN, OT_TX_PIN); + + // static variables used by the PID controller + static time_t previousTimestamp = millis(); // Previous timestamp + static float previousTemperature = bme.readTemperature(); // Previous temperature + static float ierr = 0; // Integral error + + + time_t timestamp = millis(); + if(timestamp - previousTimestamp > 1000) { + float roomTemperature = bme.readTemperature(); // Read the sensor to get the current room temperature + Serial.printf("Room temperature is %.02f *C, room temperature setpoint is %.02f *C\n", roomTemperature, ROOM_TEMPERATURE_SETPOINT); + + float dt = (timestamp - previousTimestamp) / 1000.0; // Time between measurements in seconds + float CHSetpoint = pid(ROOM_TEMPERATURE_SETPOINT, roomTemperature, previousTemperature, ierr, dt); + previousTimestamp = timestamp; + previousTemperature = roomTemperature; + + Serial.printf("New CH setpoint computed by PID is %.02f\n", CHSetpoint); + + + // First try to connect to the boiler to read it's capabilities. The boiler returns an 8 bit slaveFlags and each bit has a meaning. The bits are defined in enum class OpenTherm::CONFIGURATION_FLAGS + // The slaveMemberIDCode identifies the manufacturer of the boiler + uint8_t slaveFlags; + uint8_t slaveMemberIDCode; + if(thermostat.read(OpenTherm::READ_DATA_ID::SLAVE_CONFIGURATION, slaveFlags, slaveMemberIDCode)) { // Mandatory support + Serial.printf("Slave configuration flags is 0x%02x, slave member ID is %d (0x%02x)\n", slaveFlags, slaveMemberIDCode, slaveMemberIDCode); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_DHW_PRESENT)) Serial.println("DHW present"); else Serial.println("DHW not present"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_CONTROL_TYPE)) Serial.println("Control type on/off"); else Serial.println("Control type modulating"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_COOLING)) Serial.println("Cooling supprted"); else Serial.println("Cooling not supported"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_DHW)) Serial.println("DHW storage tank"); else Serial.println("DHW instantaneous or not-specified"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_LOW_OFF_PUMP_CTRL)) Serial.println("Low off and pump control not allowed"); else Serial.println("Low off and pump control allowed"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_CH2_PRESENT)) Serial.println("CH2 present"); else Serial.println("CH2 not present"); + } else { + slaveFlags = 0; + + if(thermostat.error() == OpenTherm::ERROR_CODES::UNKNOWN_DATA_ID) { + // Valid data is received but the for boilers mandatory DATA-ID OpenTherm::READ_DATA_ID::SLAVE_CONFIGURATION is not recognised. This is not a boiler but another device! + Serial.println("Your remote device is not a boiler"); + } else { + // No data or invalid data received + Serial.println("Failed to get slave configuration; is a boiler connected?"); + } + + // Wait 5 secs and try again + delay(5000); + return; + } + + // Tell the boiler the desired CH boiler water temperature + // This is done by writing this value to DATA-ID OpenTherm::WRITE_DATA_ID::CONTROL_SETPOINT_CH + if(thermostat.write(OpenTherm::WRITE_DATA_ID::CONTROL_SETPOINT_CH, CHSetpoint)) { + Serial.printf("CH temperature setpoint set to %.01f *C\n", CHSetpoint); + } else { + Serial.printf("Failed to set CH temperature setpoint to %.01f *C\n", CHSetpoint); + } + + // Tell the boiler the current room temperature (optional?) + // This is done by writing this value to DATA-ID OpenTherm::WRITE_DATA_ID::ROOM_TEMPERATURE + if(thermostat.write(OpenTherm::WRITE_DATA_ID::ROOM_TEMPERATURE, roomTemperature)) { + Serial.printf("Room temperature set to %.02f *C\n", roomTemperature); + } else { + Serial.println("Failed to set room temperature to sensor value"); + } + + + // masterFlags is used to tell the slave (the boiler) what available services (Central heating, cooling, domestic hot water) it wants to make use of, if present + // The meaning of each bit is defined in enum class OpenTherm::STATUS_FLAGS + // ENABLE_DOMESTIC_HOT_WATER is a #define. If defined 'true' then domestic hot water is enabled if it is available in the boiler. The prevoiuosly read slaveFlags are used to detect this capability + uint8_t masterFlags; + if(ENABLE_DOMESTIC_HOT_WATER && (slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_DHW))) { + Serial.println("Enable DHW"); + masterFlags |= uint8_t(OpenTherm::STATUS_FLAGS::MASTER_DHW_ENABLE); + } + + // ENABLE_HEATING is a #define. If defined 'true' then central heating is enabled. +// if(roomTemperature < ROOM_TEMPERATURE_SETPOINT && ENABLE_HEATING) { + if(ENABLE_HEATING) { + Serial.println("Enable central heating"); + masterFlags |= uint8_t(OpenTherm::STATUS_FLAGS::MASTER_CH_ENABLE); + } + + // ENABLE_COOLING is a #define. If defined 'true' then cooling is enabled. slaveFlags is used to detect the capability +// if(roomTemperature > ROOM_TEMPERATURE_SETPOINT && ENABLE_COOLING && (slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_COOLING))) { + if(ENABLE_COOLING && (slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_COOLING))) { + Serial.println("Enable cooling"); + masterFlags |= uint8_t(OpenTherm::STATUS_FLAGS::MASTER_COOLING_ENABLE); + } + + // Enable Outside Temperature Compensation by default + masterFlags |= uint8_t(OpenTherm::STATUS_FLAGS::MASTER_OTC_ENABLE); + + // Send masterFlags to the boiler to request services. Using statusFlags the boiler returns if it is in fault status, if it is in central heating status or domestic hot water, if it's flame is burning, etc. + Serial.println("Request services from the boiler and check it's status..."); + uint8_t statusFlags; + if(thermostat.status(masterFlags, statusFlags)) { // Mandatory support + Serial.printf("Flags is 0x%02x\n", statusFlags); + + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_FAULT_INDICATION)) Serial.println("FAULT NOTIFICATION"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_CH_MODE)) Serial.println("CH MODE"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_DHW_MODE)) Serial.println("DHW MODE"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_FLAME_STATUS)) Serial.println("FLAME"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_COOLING_STATUS)) Serial.println("COOLING"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_CH2_MODE)) Serial.println("CH2 ACTIVE"); + if(statusFlags & uint8_t(OpenTherm::STATUS_FLAGS::SLAVE_DIAGNOSTIC_IND)) Serial.println("DIAGNOSTICS INDICATION"); + } else { + Serial.println("Failed to get status"); + } + } +} diff --git b/examples/Test_Boiler_Communication/Test_Boiler_Communication.ino a/examples/Test_Boiler_Communication/Test_Boiler_Communication.ino new file mode 100644 index 0000000..ee22669 --- /dev/null +++ a/examples/Test_Boiler_Communication/Test_Boiler_Communication.ino @@ -0,0 +1,87 @@ +/* + * https://github.com/Jeroen88/EasyOpenTherm + * https://www.tindie.com/products/Metriot/OpenTherm-adapter/ + * + * Test_Boiler_Communication is a program to test if your MCU can communicate with your OpenTherm compatible boiler + * Copyright (C) 2022 Jeroen Döll + * + * This program 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. + * + * This program 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 this program. If not, see . + * + * You need an OpenTherm controller that you can buy at my Tindie store, + * Connect the two boiler wires to the OpenTherm controller pins marked OT. The order is not important. + * Connect the OpenTherm controller to your microcontroller's power (3v3) and ground (GND) pins. + * Connect the OpenTherm TXD pin to the microcontroller's pin defined by #define OT_RX_PIN. + * Connect the OpenTherm RXD pin to the microcontroller's pin defined by #define OT_TX_PIN. +*/ + + + +#include + +#include + + +#define OT_RX_PIN (34) +#define OT_TX_PIN (17) + + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.println("\n\nStarted"); + delay(5000); +} + + +void loop() { + // put your main code here, to run repeatedly: + static OpenTherm master(OT_RX_PIN, OT_TX_PIN); // Create an OpenTherm master (is thermostat; boiler is slave) with GPIO34 to receive data from boiler and GPIO17 to send data to boiler + + // First try to connect to the boiler to read it's capabilities. The boiler returns an 8 bit slaveFlags and each bit has a meaning. The bits are defined in enum class OpenTherm::CONFIGURATION_FLAGS + // The slaveMemberIDCode identifies the manufacturer of the boiler + uint8_t slaveFlags; + uint8_t slaveMemberIDCode; + if(master.read(OpenTherm::READ_DATA_ID::SLAVE_CONFIGURATION, slaveFlags, slaveMemberIDCode)) { // Mandatory support + Serial.println("Your setup is working! A frame was send to the boiler and the boiler responded with a valid frame."); + Serial.printf("Slave configuration flags is 0x%02x, slave member ID is %d (0x%02x).\n", slaveFlags, slaveMemberIDCode, slaveMemberIDCode); + Serial.println("Here is the meanining of each bit in these flags:"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_DHW_PRESENT)) Serial.println("DHW present"); else Serial.println("DHW not present"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_CONTROL_TYPE)) Serial.println("Control type on/off"); else Serial.println("Control type modulating"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_COOLING)) Serial.println("Cooling supprted"); else Serial.println("Cooling not supported"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_DHW)) Serial.println("DHW storage tank"); else Serial.println("DHW instantaneous or not-specified"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_LOW_OFF_PUMP_CTRL)) Serial.println("Low off and pump control not allowed"); else Serial.println("Low off and pump control allowed"); + if(slaveFlags & uint8_t(OpenTherm::CONFIGURATION_FLAGS::SLAVE_CH2_PRESENT)) Serial.println("CH2 present"); else Serial.println("CH2 not present"); + } else { + slaveFlags = 0; + + if(master.error() == OpenTherm::ERROR_CODES::UNKNOWN_DATA_ID) { + // Valid data is received but the for boilers mandatory DATA-ID OpenTherm::READ_DATA_ID::SLAVE_CONFIGURATION is not recognised. This is not a boiler but another device! + Serial.println("Your setup is working correctly but the remote device is not a boiler."); + Serial.println("Look in EasyOpenTherm.h for the HVAC_SLAVE_CONFIGURATION or SOLAR_SLAVE_CONFIGURATION\n DATA-ID and the corresponding master and slave flags."); + } else { + // No data or invalid data received + Serial.println("Your setup is not working yet. Please check:"); + Serial.println("Is the OpenTherm controller connected to the boiler using two wires? The order of the wires is not important."); + Serial.println("Is the OpenTherm controller correctly powered? The GND pin should be connected to the GND pin of the\n microcontroller and the 3v3 pin to the 3v3 pin of the microcontroller."); + Serial.printf("Is the OpenTherm controller TxD pin connected to the microcontroller's Rx pin as specified by\n #define OT_RX_PIN? Currently this pin is defined as %d.\n", OT_RX_PIN); + Serial.printf("Is the OpenTherm controller RxD pin connected to the microcontroller's Tx pin as specified by\n #define OT_TX_PIN? Currently this pin is defined as %d.\n", OT_TX_PIN); + Serial.printf("Is the microcontroller's Tx pin (currently pin %d} not an 'input only' pin?\n", OT_TX_PIN); + Serial.printf("Is in your software the OpenTherm object defined as OpenTherm master(OT_RX_PIN, OT_TX_PIN);\n Currently this expands to OpenTherm master(%d, %d);\n", OT_RX_PIN, OT_TX_PIN); + Serial.println("\n"); + } + } + + // Wait 5 secs and try again + delay(5000); +} diff --git b/keywords.txt a/keywords.txt new file mode 100644 index 0000000..2e9e765 --- /dev/null +++ a/keywords.txt @@ -0,0 +1,255 @@ +####################################### +# Syntax Coloring Map For OpenTherm +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +OpenTherm KEYWORD1 +OTDataLinkLayer KEYWORD1 +OTPhysicalLayer KEYWORD1 +READ_WRITE_DATA_ID KEYWORD1 +READ_DATA_ID KEYWORD1 +WRITE_DATA_ID KEYWORD1 +STATUS_FLAGS KEYWORD1 +MEMBER_IDS KEYWORD1 +FAULT_FLAGS KEYWORD1 +CONFIGURATION_FLAGS KEYWORD1 +REMOTE_PARAMETER_FLAGS KEYWORD1 +REMOTE_OVERRIDE_FLAGS KEYWORD1 +REMOTE_COMMANDS KEYWORD1 +HVAC_STATUS_FLAGS KEYWORD1 +SOLAR_STATUS_FLAGS KEYWORD1 +SOLAR_MODE_FLAGS_AND_MASKS KEYWORD1 +SOLAR_SLAVE_CONFIGURATION_FLAGS KEYWORD1 +ERROR_CODES KEYWORD1 +MSG_TYPE KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +status KEYWORD2 +read KEYWORD2 +write KEYWORD2 +readWrite KEYWORD2 +error KEYWORD2 + +set KEYWORD2 +parity KEYWORD2 +type KEYWORD2 +dataID KEYWORD2 +value KEYWORD2 +valueMSB KEYWORD2 +valueLSB KEYWORD2 +frame KEYWORD2 +isValid KEYWORD2 +dataInvalid KEYWORD2 +unknownDataID KEYWORD2 +send KEYWORD2 +receive KEYWORD2 +reset KEYWORD2 +handleInterrupt KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + +STATUS LITERAL1 +COMMAND_CODE LITERAL1 +HVAC_STATUS LITERAL1 +SOLAR_STATUS LITERAL1 + +FAULT_FLAGS LITERAL1 +OEM_DIAGNOSTIC LITERAL1 +SLAVE_CONFIGURATION LITERAL1 +OPENTHERM_VERSION_SLAVE LITERAL1 +SLAVE_PRODUCT_VERSION LITERAL1 +RELATIVE_MODULATION_LEVEL LITERAL1 +CH_WATER_PRESSURE LITERAL1 +DHW_FLOW_RATE LITERAL1 +DAY_TIME LITERAL1 +DATE LITERAL1 +YEAR LITERAL1 +BOILER_WATER_TEMP LITERAL1 +DHW_TEMPERATURE LITERAL1 +OUTSIDE_TEMPERATURE LITERAL1 +RETURN_WATER_TEMPERATURE LITERAL1 +SOLAR_STORE_TEMPERATURE LITERAL1 +SOLAR_COLLECTOR_TEMPERATURE LITERAL1 +FLOW_TEMPERATURE_CH2 LITERAL1 +DHW2_TEMPERATURE LITERAL1 +EXHAUST_TEMPERATURE LITERAL1 +BURNER_STARTS LITERAL1 +CH_PUMP_STARTS LITERAL1 +DHW_PUMP_VALVE_STARTS LITERAL1 +DHW_BURNER_STARTS LITERAL1 +BURNER_OPERATION_HOURS LITERAL1 +CH_PUMP_OPERATION_HOURS LITERAL1 +DHW_PUMP_VALVE_OPERATION_HOURS LITERAL1 +DHW_BURNER_OPERATION_HOURS LITERAL1 +REMOTE_PARAMETER LITERAL1 +DHW_SETPOINT_BOUNDS LITERAL1 +CH_SETPOINT_BOUNDS LITERAL1 +OTC_CURVE_BOUNDS LITERAL1 +DHW_SETPOINT LITERAL1 +MAX_CH_WATER_SETPOINT LITERAL1 +OTC_CURVE_RATIO LITERAL1 +NUMBER_OF_TSPS LITERAL1 +TSP_COMMAND LITERAL1 +FAULT_BUFFER_SIZE LITERAL1 +FAULT_BUFFER_DATA LITERAL1 +MAX_BOILER_CAPACITY_MIN_MOD_LEV LITERAL1 +CH_SETPOINT_OVERRIDE LITERAL1 +REMOTE_OVERRIDE_FUNCTION LITERAL1 +BOILER_HEAT_EXCHANGER_TEMPERATURE LITERAL1 +BOILER_FAN_SPEED_SETPOINT_VALUE LITERAL1 +ELECTRICAL_CURRENT_BURNER_FLAME LITERAL1 +HVAC_RELATIVE_VENT_SETPOINT LITERAL1 +HVAC_FAULT_FLAGS LITERAL1 +HVAC_OEM_DIAGNOSTIC_CODE LITERAL1 +HVAC_SLAVE_CONFIGURATION LITERAL1 +HVAC_OPENTHERM_VERSION_SLAVE LITERAL1 +HVAC_SLAVE_PRODUCT_VERSION LITERAL1 +HVAC_RELATIVE_VENTILATION LITERAL1 +HVAC_RELATIVE_HUMIDITY_EXHAUST LITERAL1 +HVAC_CO2_LEVEL_EXHAUST_AIR LITERAL1 +HVAC_SUPPLY_INLET_TEMPERATURE LITERAL1 +HVAC_SUPPLY_OUTLET_TEMPERATURE LITERAL1 +HVAC_EXHAUST_INLET_TEMPERATURE LITERAL1 +HVAC_EXHAUST_OUTLET_TEMPERATURE LITERAL1 +HVAC_EXHAUST_FAN_SPEED LITERAL1 +HVAC_SUPPLY_FAN_SPEED LITERAL1 +HVAC_REMOTE_PARAMETER LITERAL1 +HVAC_NOMINAL_RELATIVE_VENTILATION LITERAL1 +HVAC_NUMBER_OF_TSPS LITERAL1 +HVAC_TSP_COMMAND LITERAL1 +HVAC_FAULT_BUFFER_SIZE LITERAL1 +HVAC_FAULT_BUFFER_DATA LITERAL1 +HVAC_RF_STRENGTH_BATTERY_LEVEL LITERAL1 +HVAC_OPERATING_MODE LITERAL1 +SOLAR_FAULT_FLAGS LITERAL1 +SOLAR_SLAVE_CONFIGURATION LITERAL1 +SOLAR_SLAVE_PRODUCT_VERSION LITERAL1 +SOLAR_NUMBER_OF_TSPS LITERAL1 +SOLAR_TSP_COMMAND LITERAL1 +SOLAR_FAULT_BUFFER_SIZE LITERAL1 +SOLAR_FAULT_BUFFER_DATA LITERAL1 +PRODUCER_STARTS LITERAL1 +PRODUCER_HOURS LITERAL1 +PRODUCTION LITERAL1 +CUMULATIVE_PRODUCTION LITERAL1 +UNSUCCESSFUL_BURNER_STARTS LITERAL1 +FLAME_SIGNAL_TOO_LOW LITERAL1 + +CONTROL_SETPOINT_CH LITERAL1 +CONTROL_SETPOINT_CH2 LITERAL1 +MASTER_CONFIGURATION LITERAL1 +OPENTHERM_VERSION_MASTER LITERAL1 +MASTER_PRODUCT_VERSION LITERAL1 +ROOM_SETPOINT LITERAL1 +DAY_TIME LITERAL1 +DATE LITERAL1 +YEAR LITERAL1 +ROOM_SETPOINT_CH2 LITERAL1 +ROOM_TEMPERATURE LITERAL1 +BURNER_STARTS LITERAL1 +CH_PUMP_STARTS LITERAL1 +DHW_PUMP_VALVE_STARTS LITERAL1 +DHW_BURNER_STARTS LITERAL1 +BURNER_OPERATION_HOURS LITERAL1 +CH_PUMP_OPERATION_HOURS LITERAL1 +DHW_PUMP_VALVE_OPERATION_HOURS LITERAL1 +DHW_BURNER_OPERATION_HOURS LITERAL1 +DHW_SETPOINT LITERAL1 +MAX_CH_WATER_SETPOINT LITERAL1 +OTC_CURVE_RATIO LITERAL1 +TSP_COMMAND LITERAL1 +COOLING_CONTROL LITERAL1 +MAX_MODULATION_LEVEL LITERAL1 +ROOM_TEMPERATURE_CH2 LITERAL1 + +MASTER_CH_ENABLE LITERAL1 +MASTER_DHW_ENABLE LITERAL1 +MASTER_COOLING_ENABLE LITERAL1 +MASTER_OTC_ENABLE LITERAL1 +MASTER_CH2_ENABLE LITERAL1 +SLAVE_FAULT_INDICATION LITERAL1 +SLAVE_CH_MODE LITERAL1 +SLAVE_DHW_MODE LITERAL1 +SLAVE_FLAME_STATUS LITERAL1 +SLAVE_COOLING_STATUS LITERAL1 +SLAVE_CH2_MODE LITERAL1 +SLAVE_DIAGNOSTIC_IND LITERAL1 + +REMEHA LITERAL1 + +SERVICE_REQUEST LITERAL1 +LOCKOUT_RESET LITERAL1 +LOW_WATER_PRESS LITERAL1 +GAS_FLAME LITERAL1 +AIR_PRESS LITERAL1 +WATER_OVER_TEMP LITERAL1 + +SLAVE_DHW_PRESENT LITERAL1 +SLAVE_CONTROL_TYPE LITERAL1 +SLAVE_COOLING LITERAL1 +SLAVE_DHW LITERAL1 +SLAVE_LOW_OFF_PUMP_CTRL LITERAL1 +SLAVE_CH2_PRESENT LITERAL1 + +TRANSFER_ENABLE_DHW_SETPOINT LITERAL1 +TRANSFER_ENABLE_MAX_CH_SETPOINT LITERAL1 +READ_WRITE_DHW_SETPOINT LITERAL1 +READ_WRITE_MAX_CH_SETPOINT LITERAL1 + +MANUAL_CHANGE_PRIORITY LITERAL1 +REMOTE_CHANGE_PRIORITY LITERAL1 + +BLOR LITERAL1 +CHWF LITERAL1 + +MASTER_VENTILATION_ENABLE LITERAL1 +MASTER_BYPASS_POSTION LITERAL1 +MASTER__BYPASS_MODE LITERAL1 +MASTER_FREE_VENTILATION_MODE LITERAL1 +SLAVE_FAULT_INDICATION LITERAL1 +SLAVE_VENTILATION_MODE LITERAL1 +SLAVE_BYPASS_STATUS LITERAL1 +SLAVE_BYPASS_AUTOMATIC_STATUS LITERAL1 +SLAVE_FREE_VENTILATION_MODE LITERAL1 +SLAVE_DIAGNOSTIC_IND LITERAL1 + +MASTER_MODE LITERAL1 +SLAVE_FAULT_INDICATION LITERAL1 +SLAVE_MODE LITERAL1 +SLAVE_STATUS LITERAL1 + +SYSTEM_TYPE LITERAL1 +BYPASS LITERAL1 +SPEED_CONTROL LITERAL1 + +SLAVE_SYSTEM_TYPE LITERAL1 + +OK LITERAL1 +UNKNOWN_DATA_ID LITERAL1 +INVALID_DATA LITERAL1 +SEND_TIMEOUT LITERAL1 +RECEIVE_TIMEOUT LITERAL1 +PARITY_ERROR LITERAL1 +UNKNOWN_ERROR LITERAL1 + +MASTER_TO_SLAVE_READ_DATA LITERAL1 +MASTER_TO_SLAVE_WRITE_DATA LITERAL1 +MASTER_TO_SLAVE_INVALID_DATA LITERAL1 +MASTER_TO_SLAVE_RESERVED LITERAL1 +SLAVE_TO_MASTER_READ_ACK LITERAL1 +SLAVE_TO_MASTER_WRITE_ACK LITERAL1 +SLAVE_TO_MASTER_DATA_INVALID LITERAL1 +SLAVE_TO_MASTER_UNKNOWN_DATA_ID LITERAL1 + diff --git b/library.properties a/library.properties new file mode 100644 index 0000000..263fd25 --- /dev/null +++ a/library.properties @@ -0,0 +1,10 @@ +name=EasyOpenTherm +version=0.0.1 +author=Jeroen Döll +maintainer=Jeroen Döll +sentence=OpenTherm Library to control Central Heating, HVAC or Solar systems using Arduino IDE and ESP32 / ESP8266 hardware. +paragraph=OpenTherm Library is based on OpenTherm protocol specification v2.2 and higher and works with all OpenTherm compatible boilers, HVACs and Solar systems +category=Device Control +url=https://github.com/Jeroen88/EasyOpenTherm +architectures=* +includes=EasyOpenTherm.h diff --git b/src/EasyOpenTherm.cpp a/src/EasyOpenTherm.cpp new file mode 100644 index 0000000..795956e --- /dev/null +++ a/src/EasyOpenTherm.cpp @@ -0,0 +1,472 @@ +/* + * https://github.com/Jeroen88/EasyOpenTherm + * https://www.tindie.com/products/Metriot/OpenTherm-adapter/ + * + * EasyOpenTherm is a library to communicate with OpenTherm compatible devices + * Copyright (C) 2022 Jeroen Döll + * + * This program 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. + * + * This program 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 this program. If not, see . +*/ + + +#include "EasyOpenTherm.h" + + + + + OpenTherm::OpenTherm(uint8_t rxPin, + uint8_t txPin, + time_t timeoutMs, + bool master): _rxPin(rxPin), _txPin(txPin), _timeoutMs(timeoutMs), _master(master) { + _OTP = new OTPhysicalLayer(_rxPin, _txPin, master); + + if(!_OTP) Serial.println("OpenTherm Out of Memory, fail on assert()"); + assert(_OTP != NULL); // check for Out of Memory +} + + + OpenTherm::~OpenTherm() { + delete _OTP; +} + + +bool OpenTherm::status(uint8_t & slaveFlags) { + uint8_t masterFlags = uint8_t(OpenTherm::STATUS_FLAGS::MASTER_CH_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::MASTER_DHW_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::MASTER_COOLING_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::MASTER_OTC_ENABLE); + + return status(masterFlags, slaveFlags); +} + + +bool OpenTherm::status(uint8_t masterFlags, + uint8_t & slaveFlags) { + slaveFlags = 0x00; // See OpenTherm Protocol Specification v2.2 page 25 + return readWrite(READ_WRITE_DATA_ID::STATUS, masterFlags, slaveFlags); +} + + +bool OpenTherm::read(READ_DATA_ID msgID, + uint16_t & value) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), 0x0000); + + if(_execute(data)) { + value = data.value(); + + return true; + } + + return false; +} + + +bool OpenTherm::read(READ_DATA_ID msgID, + int16_t & value) { // signed integer + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), 0x0000); + + if(_execute(data)) { + value = int16_t(data.value()); + + return true; + } + + return false; +} + + +bool OpenTherm::read(READ_DATA_ID msgID, + uint8_t & valueMSB, + uint8_t & valueLSB) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), 0x0000); + + if(_execute(data)) { + valueMSB = data.valueMSB(); + valueLSB = data.valueLSB(); + + return true; + } + + return false; +} + + +bool OpenTherm::read(READ_DATA_ID msgID, + int8_t & valueMSB, // signed intergers + int8_t & valueLSB) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), 0x0000); + + if(_execute(data)) { + valueMSB = int8_t(data.valueMSB()); + valueLSB = int8_t(data.valueLSB()); + + return true; + } + + return false; +} + + +bool OpenTherm::read(READ_DATA_ID msgID, + float & value) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), 0x0000); + + if(_execute(data)) { + value = float(data.value()) / 256.0; + + return true; + } + + return false; +} + + +bool OpenTherm::write(WRITE_DATA_ID msgID, + uint16_t value) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_WRITE_DATA, uint8_t(msgID), value); + + return _execute(data); +} + + +bool OpenTherm::write(WRITE_DATA_ID msgID, + uint8_t valueMSB, + uint8_t valueLSB) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_WRITE_DATA, uint8_t(msgID), valueMSB, valueLSB); + + return _execute(data); +} + + +bool OpenTherm::write(WRITE_DATA_ID msgID, + float value) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_WRITE_DATA, uint8_t(msgID), uint16_t(value * 256.0f)); + + return _execute(data); +} + + +bool OpenTherm::readWrite(READ_WRITE_DATA_ID msgID, + uint8_t valueMSB, + uint8_t & valueLSB) { + OTDataLinkLayer data; + data.set(OTDataLinkLayer::MSG_TYPE::MASTER_TO_SLAVE_READ_DATA, uint8_t(msgID), valueMSB, valueLSB); + + if(_execute(data)) { + valueLSB = data.valueLSB(); + + return true; + } + + return false; +} + + +OpenTherm::ERROR_CODES OpenTherm::error() { + return _lastError; +} + + +bool OpenTherm::_execute(OTDataLinkLayer & data) { + _lastError = ERROR_CODES::OK; + + time_t startMillis = millis(); + for(;;) { + if(millis() - startMillis >= _timeoutMs) { + _lastError = ERROR_CODES::SEND_TIMEOUT; + + _OTP->reset(); + + return false; + } + + if(_OTP->send(data.frame())) break; + } + + startMillis = millis(); + for(;;) { + if(millis() - startMillis >= _timeoutMs) { + _lastError = ERROR_CODES::RECEIVE_TIMEOUT; + _OTP->reset(); + + return false; + } + + uint32_t frame; + if(_OTP->receive(frame)) { + data.set(frame); + + if(data.isValid()) { + + return true; + } else { + if(data.parity()) _lastError = ERROR_CODES::PARITY_ERROR; + else if(data.dataInvalid()) _lastError = ERROR_CODES::INVALID_DATA; + else if(data.unknownDataID()) _lastError = ERROR_CODES::UNKNOWN_DATA_ID; + else _lastError = ERROR_CODES::UNKNOWN_ERROR; + + return false; + } + } + } + + return false; +} + + + + + OTDataLinkLayer::OTDataLinkLayer() { + _frame = 0; +} + + OTDataLinkLayer::OTDataLinkLayer(uint32_t frame) { + _frame = frame; +} + + +void OTDataLinkLayer::set(uint32_t frame) { + _frame = frame; +} + + +void OTDataLinkLayer::set(MSG_TYPE msgType, + uint8_t dataID, + uint16_t value) { + _frame = uint32_t(msgType) | (uint32_t(dataID) << 16) | uint32_t(value); + if(!_parity(_frame)) _frame |= 0x80000000; +} + + +void OTDataLinkLayer::set(MSG_TYPE msgType, + uint8_t dataID, + uint8_t valueMSB, + uint8_t valueLSB) { + _frame = uint32_t(msgType) | (uint32_t(dataID) << 16) | (uint32_t(valueMSB) << 8) | uint32_t(valueLSB); + if(!_parity(_frame)) _frame |= 0x80000000; +} + + +bool OTDataLinkLayer::parity() { + return _parity(_frame); +} + +OTDataLinkLayer::MSG_TYPE OTDataLinkLayer::type() { + return OTDataLinkLayer::MSG_TYPE(_frame & 0x70000000); +} + + +uint8_t OTDataLinkLayer::dataID() { + return uint8_t((_frame & 0xff0000) >> 16); +} + + +uint16_t OTDataLinkLayer::value() { + return uint16_t(_frame & 0xffff); +} + + +uint8_t OTDataLinkLayer::valueMSB() { + return uint8_t((_frame & 0xff00) >> 8); +} + + +uint8_t OTDataLinkLayer::valueLSB() { + return uint8_t(_frame & 0xff); +} + + +uint32_t OTDataLinkLayer::frame() { + return _frame; +} + + +bool OTDataLinkLayer::isValid() { + return parity() && (type() == MSG_TYPE::SLAVE_TO_MASTER_READ_ACK || type() == MSG_TYPE::SLAVE_TO_MASTER_WRITE_ACK || type() == MSG_TYPE::MASTER_TO_SLAVE_READ_DATA || type() == MSG_TYPE::MASTER_TO_SLAVE_WRITE_DATA); +} + + +bool OTDataLinkLayer::dataInvalid() { + return type() == MSG_TYPE::SLAVE_TO_MASTER_DATA_INVALID || type() == MSG_TYPE::MASTER_TO_SLAVE_INVALID_DATA; +} + + +bool OTDataLinkLayer::unknownDataID() { + return type() == MSG_TYPE::SLAVE_TO_MASTER_UNKNOWN_DATA_ID; // Slave does not select the DATA-ID, this function should not be called from a slave +} + + +// https://stackoverflow.com/questions/21617970/how-to-check-if-value-has-even-parity-of-bits-or-odd +bool OTDataLinkLayer::_parity(uint32_t frame) { + frame ^= frame >> 16; + frame ^= frame >> 8; + frame ^= frame >> 4; + frame ^= frame >> 2; + frame ^= frame >> 1; + return (~frame) & 1; +} + + + + +OTPhysicalLayer * OTPPtr = NULL; + +#if defined(ESP32) +void IRAM_ATTR OTPGenericISR() { +#elif defined(ESP8266) +void ICACHE_RAM_ATTR OTPGenericISR() { +#else +void OTPGenericISR() { +#endif + if(OTPPtr) OTPPtr->handleInterrupt(); +} + + + OTPhysicalLayer::OTPhysicalLayer(uint8_t rxPin, + uint8_t txPin, + bool master): _rxPin(rxPin), _txPin(txPin), _master(master) { + + pinMode(_rxPin, INPUT); + pinMode(_txPin, OUTPUT); + digitalWrite(_txPin, HIGH); // idle + + if(OTPPtr != NULL) { + Serial.println("Only one instance of OTPhysicalLayer() may be active at the time. Executing will fail on an assert(false)"); + } + assert(OTPPtr == NULL); // For now only one instance allowed, later a list OTPPtr could be a and the ISR a timer ISR (this is needed because now the ISR is attached to a specific pin) + OTPPtr = this; + + attachInterrupt(digitalPinToInterrupt(_rxPin), OTPGenericISR, CHANGE); +} + + + OTPhysicalLayer::~OTPhysicalLayer() { + OTPPtr = NULL; + + detachInterrupt(digitalPinToInterrupt(_rxPin)); +} + + +bool OTPhysicalLayer::send(uint32_t frame) { + if(_state != STATE::READY && _state != STATE::INVALID) { + return false; + } + + if(_state == STATE::READY && millis() - _lastReceivedTimestampMs < 100) { + + return false; // Wait at least 100 ms after receiving the final bit of the latest frame before sending a new frame + } + + sendBit(HIGH); // start bit + + uint32_t mask = 0x80000000UL; + while(mask) { // data bits + sendBit((frame & mask) ? HIGH : LOW); + mask >>= 1; + } + + sendBit(HIGH); // stop bit + + digitalWrite(_txPin, HIGH); // idle + + _frame = 0; + _state = STATE::WAITING; + + _lastSentTimestampMs = millis(); + + return true; +} + + +bool OTPhysicalLayer::receive(uint32_t & frame) { + if(_state == STATE::INVALID) return false; // ::send() will set _state to STATE::WAITING + + if(_state != STATE::READY) { // ::handleInterrupt() will set _state to STATE::READY upon receiving a complete frame (including start and stop bits) + if(_master && millis() - _lastSentTimestampMs > 800) { // ::send() will set _lastSentTimestampMs to after sending the final bit. A slave never times out, it keeps on listning to the master + _state = STATE::INVALID; // timeout + } + + return false; + } + + frame = _frame; // _state == STATE::READY so a frame is available. The frame may be retrieved by calling ::receive() until the next ::send() is called + + return true; +} + + +void OTPhysicalLayer::reset() { + _state = STATE::INVALID; +} + + +void OTPhysicalLayer::sendBit(uint8_t val) { + digitalWrite(_txPin, (val == HIGH) ? LOW : HIGH); + delayMicroseconds(500); + digitalWrite(_txPin, (val == HIGH) ? HIGH : LOW); + delayMicroseconds(500); +} + + + void OTPhysicalLayer::handleInterrupt() { + static volatile uint32_t lastTimestamp; + static volatile uint32_t mask; + + if(_state == STATE::INVALID) return; // Start reception after _send() has set _state to STATE::WAITING + // ::handleInterrupt() passes from STATE_WATING to STATE::START_BIT, to STATE::RECEIVING for the data bits, to _state STATE::READY after the stop bit + if(_state == STATE::READY) { + if(!_master && digitalRead(_rxPin) == HIGH) { + _state = STATE::WAITING; + _frame = 0; + } else { + + return; // Nothing to do for a master in _state is STATE::READY + } + } + + uint32_t timestamp = micros(); + if(_state == STATE::WAITING) { // First bit received after sending is the start bit. Init mask for first data bit (the most significant bit is send first) + if(digitalRead(_rxPin) == HIGH) { // start bit + lastTimestamp = timestamp; + mask = 0x80000000UL; + _state = STATE::START_BIT; + + } else { + _state = STATE::INVALID; + } + } else if(_state == STATE::START_BIT) { // First bit received after the start bit is the first data bit + if(timestamp - lastTimestamp < 750 && digitalRead(_rxPin) == LOW) { + lastTimestamp = timestamp; + _state = STATE::RECEIVING; + } else { + _state = STATE::INVALID; + } + } else if(_state == STATE::RECEIVING) { // Record all received data bits until mask == 0; then alle data bits are consumed and this must be the final stop bit. Set _state to STATE::READY signaliing that a frame is available + if(timestamp - lastTimestamp > 750) { + if(mask) { // data bit + if(digitalRead(_rxPin) == LOW) _frame |= mask; + lastTimestamp = timestamp; + mask >>= 1; + } else { // stop bit + _lastReceivedTimestampMs = millis(); + _state = STATE::READY; + } + } + } +} diff --git b/src/EasyOpenTherm.h a/src/EasyOpenTherm.h new file mode 100644 index 0000000..331b8ad --- /dev/null +++ a/src/EasyOpenTherm.h @@ -0,0 +1,504 @@ +/* + * https://github.com/Jeroen88/EasyOpenTherm + * https://www.tindie.com/products/Metriot/OpenTherm-adapter/ + * + * EasyOpenTherm is a library to communicate with OpenTherm compatible devices + * Copyright (C) 2022 Jeroen Döll + * + * This program 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. + * + * This program 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 this program. If not, see . +*/ + + +#pragma once + +#include + + +class OTDataLinkLayer; // Foreward declaration +class OTPhysicalLayer; // Foreward declaration + + +class OpenTherm { +public: + OpenTherm(uint8_t rxPin, + uint8_t txPin, + time_t timeoutMs = 900, + bool master = true); + + ~OpenTherm(); + + bool status(uint8_t & slaveFlags); + + bool status(uint8_t masterFlags, + uint8_t & slaveFlags); + + + enum class READ_WRITE_DATA_ID { + // Class 1 : Control and Status Information + STATUS = 0, // flag8, flag8 Mandatory: Master status, slave status + + // Class 3 : Remote Commands + COMMAND_CODE = 4, // u8, u8 Command code / Response to command + + // Incomplete information for all DATA-IDs below. If these DATA-IDs are R, W or R/W is unknown. Also the data type is unknown and deducted from the name. These DATA-IDs are not ordered by class + // Where applicable and available flags and masks are also defined below + + // HVAC specific DATA-IDs (ventilation / heat recovery) + HVAC_STATUS = 70, // flag8, flag8 Master status, slave status + + // Solar specific DATA-IDs + SOLAR_STATUS = 101, // flag8, flag8? MSB Master Solar Storage Mode, LSB Slave Solar Storage Mode + }; + + + enum class READ_DATA_ID { + // Class 1 : Control and Status Information + FAULT_FLAGS = 5, // flag8, u8 Application-specific fault flags and OEM fault code + OEM_DIAGNOSTIC = 115, // u16 An OEM-specific diagnostic/service code + + // Class 2 : Configuration Information + SLAVE_CONFIGURATION = 3, // flag8, u8 Mandatory: Slave Configuration Flags / MemberID Code of the slave + OPENTHERM_VERSION_SLAVE = 125, // f8.8 The implemented version of the OpenTherm Protocol Specification in the slave + SLAVE_PRODUCT_VERSION = 127, // u8, u8 The slave device product version number and type as defined by the manufacturer + + // Class 4 : Sensor and Informational Data + RELATIVE_MODULATION_LEVEL = 17, // f8.8 Mandatory: Percent modulation between min and max modulation levels. i.e. 0% = Minimum modulation level and 100% = Maximum modulation level (%) + CH_WATER_PRESSURE = 18, // f8.8 Water pressure of the boiler CH circuit (bar) + DHW_FLOW_RATE = 19, // f8.8 Water flow rate through the DHW circuit (l/min) + DAY_TIME = 20, // special, u8 Day of Week and Time of Day; special: bit 7,6,5 day of week (1=Monday, etc...., 0=no DoW info available); bit 4,3,2,1,0 hours, LSB is minutes + DATE = 21, // u8, u8 Calendar date: Month, Day of month + YEAR = 22, // u16 Calendar year; note : 1999-2099 will normally be sufficient + BOILER_WATER_TEMP = 25, // f8.8 Mandatory: Flow water temperature from boiler (°C) + DHW_TEMPERATURE = 26, // f8.8 Domestic hot water temperature (°C) + OUTSIDE_TEMPERATURE = 27, // f8.8 Outside air temperature (°C) + RETURN_WATER_TEMPERATURE = 28, // f8.8 Return water temperature to boiler (°C) + SOLAR_STORE_TEMPERATURE = 29, // f8.8 Solar storage temperature (°C) + SOLAR_COLLECTOR_TEMPERATURE = 30, // f8.8 Solar collector temperature (°C) + FLOW_TEMPERATURE_CH2 = 31, // f8.8 Flow water temperature of the second central heating circuit (°C) + DHW2_TEMPERATURE = 32, // f8.8 Domestic hot water temperature 2 (°C) + EXHAUST_TEMPERATURE = 33, // s16 Exhaust temperature (°C) + BURNER_STARTS = 116, // u16 Number of starts burner. Reset by writing zero is optional for slave + CH_PUMP_STARTS = 117, // u16 Number of starts CH pump. Reset by writing zero is optional for slave + DHW_PUMP_VALVE_STARTS = 118, // u16 Number of starts DHW pump/valve. Reset by writing zero is optional for slave + DHW_BURNER_STARTS = 119, // u16 Number of starts burner in DHW mode. Reset by writing zero is optional for slave + BURNER_OPERATION_HOURS = 120, // u16 Number of hours that burner is in operation (i.e. flame on). Reset by writing zero is optional for slave + CH_PUMP_OPERATION_HOURS = 121, // u16 Number of hours that CH pump has been running. Reset by writing zero is optional for slave + DHW_PUMP_VALVE_OPERATION_HOURS = 122, // u16 Number of hours that DHW pump has been running or DHW valve has been opened. Reset by writing zero is optional for slave + DHW_BURNER_OPERATION_HOURS = 123, // u16 Number of hours that burner is in operation during DHW mode. Reset by writing zero is optional for slave + + // Class 5 : Pre-Defined Remote Boiler Parameters + REMOTE_PARAMETER = 6, // flag8, flag8 Remote boiler parameter transfer-enable flags, read/write flags + DHW_SETPOINT_BOUNDS = 48, // s8, s8 Upper bound for adjustment of DHW setp (°C), Lower bound for adjustment of DHW setp (°C) + CH_SETPOINT_BOUNDS = 49, // s8, s8 Upper bound for adjustment of maxCH setp (°C), Lower bound for adjustment of maxCH setp (°C) + OTC_CURVE_BOUNDS = 50, // s8, s8 upp/low bnd + DHW_SETPOINT = 56, // f8.8 Domestic hot water temperature setpoint (Remote parameter 1) (°C) + MAX_CH_WATER_SETPOINT = 57, // f8.8 Maximum allowable CH water setpoint (Remote parameter 2) (°C) + OTC_CURVE_RATIO = 58, // f8.8 OTC heat curve ratio (Remote parameter 3) (°C) + + // Class 6 : Transparent Slave Parameters + NUMBER_OF_TSPS = 10, // u8, u8 Number of transparent-slave-parameter supported by the slave device, -Reserved- + TSP_COMMAND = 11, // u8, u8 Index number of following TSP, Value of the referenced TSP + + // Class 7 : Fault History Data + FAULT_BUFFER_SIZE = 12, // u8, u8 The size of the fault history buffer, -Reserved- + FAULT_BUFFER_DATA = 13, // u8, u8 Index number of Fault Buffer entry, Value of the referenced Fault Buffer entry + + // Class 8 : Control of Special Applications + MAX_BOILER_CAPACITY_MIN_MOD_LEV = 15, // u8, u8 MSB : max. boiler capacity (0..255kW), LSB : min. modulation level (0..100%) expressed as a percentage of the maximum capacity + CH_SETPOINT_OVERRIDE = 9, // f8.8 Remote override room setpoint (0 is no override, 1..30 is remote override room setpoint) + REMOTE_OVERRIDE_FUNCTION = 100, // flag8, u8 Bit 0: Manual change priority; bit 1: Program change priority, HSB -Reserved- + + + // Incomplete information for all DATA-IDs below. If these DATA-IDs are R, W or R/W is unknown. Also the data type is unknown and deducted from the name. These DATA-IDs are not ordered by class + // Where applicable and available flags and masks are also defined below + + // Boiler specific DATA-IDs + BOILER_HEAT_EXCHANGER_TEMPERATURE = 34, // f8.8 Boiler heat exchanger temperature (°C) + BOILER_FAN_SPEED_SETPOINT_VALUE = 35, // ? Boiler fan speed Setpoint and actual value + ELECTRICAL_CURRENT_BURNER_FLAME = 36, // ? Electrical current through burner flame (μA) + + // HVAC specific DATA-IDs (ventilation / heat recovery) + HVAC_RELATIVE_VENT_SETPOINT = 71, // f8.8 Relative ventilation position (0-100%) + HVAC_FAULT_FLAGS = 72, // flag8, u8 Application-specific fault flags and OEM fault code + HVAC_OEM_DIAGNOSTIC_CODE = 73, // u16 An OEM specific diagnostic/service code + HVAC_SLAVE_CONFIGURATION = 74, // flag8, u8 Slave Configuration Flags / MemberID Code of the slave + HVAC_OPENTHERM_VERSION_SLAVE = 75, // f8.8 The implemented version of the OpenTherm Protocol Specification in the slave + HVAC_SLAVE_PRODUCT_VERSION = 76, // u8, u8 The slave device product version number and type as defined by the manufacturer + HVAC_RELATIVE_VENTILATION = 77, // f8.8 Relative ventilation (0-100%) + HVAC_RELATIVE_HUMIDITY_EXHAUST = 78, // f8.8 Relative humidity exhaust air (0-100%) + HVAC_CO2_LEVEL_EXHAUST_AIR = 79, // ? CO2 level exhaust air (0-2000 ppm) + HVAC_SUPPLY_INLET_TEMPERATURE = 80, // f8.8 Supply inlet temperature (*C) + HVAC_SUPPLY_OUTLET_TEMPERATURE = 81, // f8.8 Supply outlet temperature (*C) + HVAC_EXHAUST_INLET_TEMPERATURE = 82, // f8.8 Exhaust inlet temperature (*C) + HVAC_EXHAUST_OUTLET_TEMPERATURE = 83, // f8.8 Exhaust outlet temperature (*C) + HVAC_EXHAUST_FAN_SPEED = 84, // u16? Exhaust fan speed (rpm) + HVAC_SUPPLY_FAN_SPEED = 85, // u16? Supply fan speed (rpm) + HVAC_REMOTE_PARAMETER = 86, // u8, u8? MSB Remote parameter transfer enable nominal ventilation value, LSB Remote parameter read/write nominal ventilation value + HVAC_NOMINAL_RELATIVE_VENTILATION = 87, // f8.8 Nominal relative value for ventilation (0-100%) + HVAC_NUMBER_OF_TSPS = 88, // u8, u8? Number of transparent-slave-parameter supported by the slave device, -Reserved- + HVAC_TSP_COMMAND = 89, // u8, u8 Index number of following TSP, Value of the referenced TSP + HVAC_FAULT_BUFFER_SIZE = 90, // u8, u8? The size of the fault history buffer + HVAC_FAULT_BUFFER_DATA = 91, // u8, u8 Index number of Fault Buffer entry, Value of the referenced Fault Buffer entry + HVAC_RF_STRENGTH_BATTERY_LEVEL = 98, // u8, u8? For a specific RF sensor RF strength and battery level + HVAC_OPERATING_MODE = 99, // ? Operating mode HC1, HC2 / Operating mode DHW + + // Solar specific DATA-IDs + SOLAR_FAULT_FLAGS = 102, // flag8, u8 Application-specific fault flags and OEM fault code + SOLAR_SLAVE_CONFIGURATION = 103, // flag8, u8 Slave Configuration Flags / MemberID Code of the slave + SOLAR_SLAVE_PRODUCT_VERSION = 104, // u8, u8 The slave device product version number and type as defined by the manufacturer + SOLAR_NUMBER_OF_TSPS = 105, // u8, u8? Number of transparent-slave-parameter supported by the slave device, -Reserved- + SOLAR_TSP_COMMAND = 106, // u8, u8 Index number of following TSP, Value of the referenced TSP + SOLAR_FAULT_BUFFER_SIZE = 107, // u8, u8? The size of the fault history buffer + SOLAR_FAULT_BUFFER_DATA = 108, // u8, u8 Index number of Fault Buffer entry, Value of the referenced Fault Buffer entry + + // Electricity specific DATA-IDs + PRODUCER_STARTS = 109, // u16 Electricity producer starts + PRODUCER_HOURS = 110, // u16 Electricity producer hours + PRODUCTION = 111, // u16 Electricity production + CUMULATIVE_PRODUCTION = 112, // u16 Electricity cumulative production + + // Boiler specific DATA-IDs + UNSUCCESSFUL_BURNER_STARTS = 113, // u16 Number of unsuccessful burner starts + FLAME_SIGNAL_TOO_LOW = 114, // u16 Number of times flame signal was too low + }; + + + enum class WRITE_DATA_ID { + // Class 1 : Control and Status Information + CONTROL_SETPOINT_CH = 1, // f8.8 Mandatory: Control setpoint (CH water temperature setpoint) (°C) + CONTROL_SETPOINT_CH2 = 8, // f8.8 Control setpoint for 2nd CH circuit (°C) + + // Class 2 : Configuration Information + MASTER_CONFIGURATION = 2, // flag8, u8 Master Configuration Flags / MemberID Code of the master + OPENTHERM_VERSION_MASTER = 124, // f8.8 The implemented version of the OpenTherm Protocol Specification in the master + MASTER_PRODUCT_VERSION = 126, // u8, u8 The master device product version number and type as defined by the manufacturer + + // Class 4 : Sensor and Informational Data + ROOM_SETPOINT = 16, // f8.8 Current room temperature setpoint (°C) + DAY_TIME = 20, // special, u8 Day of Week and Time of Day; special: bit 7,6,5 day of week (1=Monday, etc...., 0=no DoW info available); bit 4,3,2,1,0 hours, LSB is minutes + DATE = 21, // u8, u8 Calendar date: Month, Day of month + YEAR = 22, // u16 Calendar year; note : 1999-2099 will normally be sufficient + ROOM_SETPOINT_CH2 = 23, // f8.8 Current room Setpoint for 2nd CH circuit (°C) + ROOM_TEMPERATURE = 24, // f8.8 Current sensed room temperature (°C) + BURNER_STARTS = 116, // u16 Number of starts burner. Reset by writing zero is optional for slave + CH_PUMP_STARTS = 117, // u16 Number of starts CH pump. Reset by writing zero is optional for slave + DHW_PUMP_VALVE_STARTS = 118, // u16 Number of starts DHW pump/valve. Reset by writing zero is optional for slave + DHW_BURNER_STARTS = 119, // u16 Number of starts burner in DHW mode. Reset by writing zero is optional for slave + BURNER_OPERATION_HOURS = 120, // u16 Number of hours that burner is in operation (i.e. flame on). Reset by writing zero is optional for slave + CH_PUMP_OPERATION_HOURS = 121, // u16 Number of hours that CH pump has been running. Reset by writing zero is optional for slave + DHW_PUMP_VALVE_OPERATION_HOURS = 122, // u16 Number of hours that DHW pump has been running or DHW valve has been opened. Reset by writing zero is optional for slave + DHW_BURNER_OPERATION_HOURS = 123, // u16 Number of hours that burner is in operation during DHW mode. Reset by writing zero is optional for slave + + // Class 5 : Pre-Defined Remote Boiler Parameters + DHW_SETPOINT = 56, // f8.8 Domestic hot water temperature setpoint (Remote parameter 1) (°C) + MAX_CH_WATER_SETPOINT = 57, // f8.8 Maximum allowable CH water setpoint (Remote parameter 2) (°C) + OTC_CURVE_RATIO = 58, // f8.8 OTC heat curve ratio (Remote parameter 3) (°C) + + // Class 6 : Transparent Slave Parameters + TSP_COMMAND = 11, // u8, u8 Index number of following TSP, Value of the referenced TSP + + // Class 8 : Control of Special Applications + COOLING_CONTROL = 7, // f8.8 Signal for cooling plant (%) + MAX_MODULATION_LEVEL = 14, // f8.8 Mandatory: Maximum relative boiler modulation level setting for sequencer and off-low & pump control applications (%) + + + // Incomplete information for DATA-IDs below. If these DATA-IDs are R, W or R/W is unknown. Also the data type is unknown and deducted from the name + // Boiler DATA-ID + ROOM_TEMPERATURE_CH2 = 37, // f8.8 Room temperature for second CH unit(°C) + }; + + + enum class STATUS_FLAGS { + // Master is MSB + MASTER_CH_ENABLE = (0b00000001), // CH is Central Heating + MASTER_DHW_ENABLE = (0b00000010), // DHW is Domestic Hot Water + MASTER_COOLING_ENABLE = (0b00000100), + MASTER_OTC_ENABLE = (0b00001000), // OTC is Outside Temperature Compensation + MASTER_CH2_ENABLE = (0b00010000), + + // Slave is LSB + SLAVE_FAULT_INDICATION = (0b00000001), + SLAVE_CH_MODE = (0b00000010), + SLAVE_DHW_MODE = (0b00000100), + SLAVE_FLAME_STATUS = (0b00001000), + SLAVE_COOLING_STATUS = (0b00010000), + SLAVE_CH2_MODE = (0b00100000), + SLAVE_DIAGNOSTIC_IND = (0b01000000), + }; + + + enum MEMBER_IDS: uint8_t { // May be extended with other manufacturer OpenTherm member IDs. Maybe incorrect + AWB = 2, + BRINK = 2, + ATAG = 4, + BROTJE = 4, + ELCO = 4, + GEMINOX = 4, + ITHO_DAALDEROP = 5, + IDEAL = 6, + BOSCH = 8, + HOVAL = 8, + FERROLI = 9, + REMEHA = 11, + DE_DIETRICH = 11, + UNICAL = 16, + BULEX = 24, + VAILLANT = 24, + BAXI = 27, + DAALDEROP = 29, + VIESSMANN = 33, + NEFIT = 131, + INTERGAS = 173, + }; + + + enum class FAULT_FLAGS { + SERVICE_REQUEST = (0b00000001), + LOCKOUT_RESET = (0b00000010), + LOW_WATER_PRESS = (0b00000100), + GAS_FLAME = (0b00001000), + AIR_PRESS = (0b00010000), + WATER_OVER_TEMP = (0b00100000), + }; + + + enum class CONFIGURATION_FLAGS { + SLAVE_DHW_PRESENT = (0b00000001), + SLAVE_CONTROL_TYPE = (0b00000010), + SLAVE_COOLING = (0b00000100), + SLAVE_DHW = (0b00001000), + SLAVE_LOW_OFF_PUMP_CTRL = (0b00010000), + SLAVE_CH2_PRESENT = (0b00100000), + }; + + + enum class REMOTE_PARAMETER_FLAGS { + TRANSFER_ENABLE_DHW_SETPOINT = (0b00000001), + TRANSFER_ENABLE_MAX_CH_SETPOINT = (0b00000010), + + READ_WRITE_DHW_SETPOINT = (0b00000001), + READ_WRITE_MAX_CH_SETPOINT = (0b00000010), + }; + + + enum class REMOTE_OVERRIDE_FLAGS { + MANUAL_CHANGE_PRIORITY = (0b00000001), + REMOTE_CHANGE_PRIORITY = (0b00000010), + }; + + + enum class REMOTE_COMMANDS { + BLOR = (1), // Boiler Lock-out Reset command + CHWF = (2), // CH water filling + }; + + + enum class HVAC_STATUS_FLAGS { + // Master is MSB + MASTER_VENTILATION_ENABLE = (0b00000001), + MASTER_BYPASS_POSTION = (0b00000010), + MASTER__BYPASS_MODE = (0b00000100), + MASTER_FREE_VENTILATION_MODE = (0b00001000), + + // Slave is LSB + SLAVE_FAULT_INDICATION = (0b00000001), + SLAVE_VENTILATION_MODE = (0b00000010), + SLAVE_BYPASS_STATUS = (0b00000100), + SLAVE_BYPASS_AUTOMATIC_STATUS = (0b00001000), + SLAVE_FREE_VENTILATION_MODE = (0b00010000), + SLAVE_DIAGNOSTIC_IND = (0b00100000), + }; + + + enum class SOLAR_STATUS_FLAGS { + // Master is MSB + MASTER_MODE = (0b00000111), + + // Slave is LSB + SLAVE_FAULT_INDICATION = (0b00000001), + SLAVE_MODE = (0b00001110), + SLAVE_STATUS = (0b00110000), + }; + + + enum class SOLAR_MODE_FLAGS_AND_MASKS { + SYSTEM_TYPE = (0b00000001), + BYPASS = (0b00000010), + SPEED_CONTROL = (0b00000100), + }; + + + enum class SOLAR_SLAVE_CONFIGURATION_FLAGS { + SLAVE_SYSTEM_TYPE = (0b00000001), + }; + + + enum class ERROR_CODES { + OK, + UNKNOWN_DATA_ID, + INVALID_DATA, + SEND_TIMEOUT, + RECEIVE_TIMEOUT, + PARITY_ERROR, + UNKNOWN_ERROR, + }; + + bool read(READ_DATA_ID msgID, + uint16_t & value); + + bool read(READ_DATA_ID msgID, + int16_t & value); + + bool read(READ_DATA_ID msgID, + uint8_t & valueMSB, + uint8_t & valueLSB); + + bool read(READ_DATA_ID msgID, + int8_t & valueMSB, + int8_t & valueLSB); + + bool read(READ_DATA_ID msgID, + float & value); + + bool write(WRITE_DATA_ID msgID, + uint16_t value); + + bool write(WRITE_DATA_ID msgID, + uint8_t valueMSB, + uint8_t valueLSB); + + bool write(WRITE_DATA_ID msgID, + float value); + + bool readWrite(READ_WRITE_DATA_ID msgID, + uint8_t valueMSB, + uint8_t & valueLSB); + + ERROR_CODES error(); + +private: + bool _execute(OTDataLinkLayer & data); + + OTPhysicalLayer * _OTP; + + uint8_t _rxPin; + uint8_t _txPin; + + time_t _timeoutMs; + bool _master; + + ERROR_CODES _lastError; +}; + + +class OTDataLinkLayer { +public: + enum class MSG_TYPE { + MASTER_TO_SLAVE_READ_DATA = (0b0000000UL << 24), // value >> 28 := 0 + MASTER_TO_SLAVE_WRITE_DATA = (0b0010000UL << 24), // value >> 28 := 1 + MASTER_TO_SLAVE_INVALID_DATA = (0b0100000UL << 24), // value >> 28 := 2 + MASTER_TO_SLAVE_RESERVED = (0b0110000UL << 24), // value >> 28 := 3 + + SLAVE_TO_MASTER_READ_ACK = (0b1000000UL << 24), // value >> 28 := 4 + SLAVE_TO_MASTER_WRITE_ACK = (0b1010000UL << 24), // value >> 28 := 5 + SLAVE_TO_MASTER_DATA_INVALID = (0b1100000UL << 24), // value >> 28 := 6 + SLAVE_TO_MASTER_UNKNOWN_DATA_ID = (0b1110000UL << 24) // value >> 28 := 7 + }; + + OTDataLinkLayer(); + + OTDataLinkLayer(uint32_t frame); + + void set(uint32_t frame); + + void set(MSG_TYPE msgType, + uint8_t dataID, + uint16_t value); + + void set(MSG_TYPE msgType, + uint8_t dataID, + uint8_t valueMSB, + uint8_t valueLSB); + + bool parity(); + + MSG_TYPE type(); + + uint8_t dataID(); + + uint16_t value(); + + uint8_t valueMSB(); + + uint8_t valueLSB(); + + uint32_t frame(); + + bool isValid(); + + bool dataInvalid(); + + bool unknownDataID(); + +private: + uint32_t _frame; + + bool _parity(uint32_t frame); + +}; + + +class OTPhysicalLayer { +public: + OTPhysicalLayer(uint8_t rxPin, + uint8_t txPin, + bool master); + + ~OTPhysicalLayer(); + + bool send(uint32_t frame); + + bool receive(uint32_t & frame); + + void reset(); + +#if defined(ESP32) + void IRAM_ATTR handleInterrupt(); +#elif defined(ESP8266) + void ICACHE_RAM_ATTR handleInterrupt(); +#else + void handleInterrupt(); +#endif + +private: + void sendBit(uint8_t val); + + volatile uint32_t _frame; + uint32_t _lastSentTimestampMs; + volatile uint32_t _lastReceivedTimestampMs; + + uint8_t _rxPin; + uint8_t _txPin; + bool _master; + + enum class STATE { + INVALID, + READY, + WAITING, + START_BIT, + RECEIVING, + }; + + volatile STATE _state = STATE::INVALID; +};