├── .editorconfig ├── .gitignore ├── .vscode └── extensions.json ├── LICENSE ├── README.md ├── doc ├── 3d │ ├── LICENSE.txt │ ├── README.txt │ ├── attribution_card.html │ ├── files │ │ ├── Base.STL │ │ ├── Separate_Base.STL │ │ ├── Separate_Lid.STL │ │ ├── Separate_LidDiffuser.STL │ │ ├── Separate_Side1.STL │ │ ├── Separate_Side2.STL │ │ └── Sides.STL │ └── images │ │ ├── 245c57981f4cddeab83cff3f846162f7_preview_featured.jpg │ │ ├── 271c0036ba8b6635415a84bfcd176f19_preview_featured.jpg │ │ ├── 3396dd9f41a952cdc6e37b2317db52cd_preview_featured.jpg │ │ ├── 43d0d756b721a23e1a7fb791ff80daa2_preview_featured.jpg │ │ ├── 4b1c59c7728e2b1cb65f6cb20aaf5cf9_preview_featured.jpg │ │ ├── 63014cc61306f34a37fa27f194163c88_preview_featured.jpg │ │ ├── 6bfc060cf82461a36f069b9f4187059b_preview_featured.jpg │ │ ├── 8b0db017a77d7f9b70b762376feb0774_preview_featured.jpg │ │ ├── b61681cbd25cb6c9a7b1af2591f67687_preview_featured.jpg │ │ ├── b66bf216e8f3e216b8c03a8b85245b85_preview_featured.jpg │ │ └── ebca68d2776899c292c068493eb053f9_preview_featured.jpg ├── img │ ├── SW-420-sensor.jpeg │ ├── mpu6050.jpeg │ └── wiring-diagram.png └── wiring │ └── sketch.fzz ├── platformio.ini └── src ├── main.cpp └── secrets_copy.h /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml}] 15 | indent_size = 4 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | src/secrets.h 7 | src/main_copy.cpp 8 | src/main_copy.off -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DIY version of the Aqara cube - DIY with ESP32 and Home-Assistant 2 | 3 | This DIY smart cube sits on your desk and provides ability to automate everything around you via quick shortcuts. 4 | 5 | ⚡️ COMPONENTS AND SUPPLIES 6 | -------------------------- 7 | 8 | 9 | 10 | * [ESP32 Board](https://amzn.to/3jmCpqx) 11 | * [MPU-6050](https://www.amazon.ca/gp/product/B07JPK26X2) 3 Axis Accelerometer Gyroscope 12 | * [SW-420](https://www.amazon.ca/gp/product/B01FDGTTY8) Vibration Sensor 13 | * [5 LiPo Batteries And Charger](https://www.amazon.ca/gp/product/B0795F139D) 14 | * [Breadboard](https://amzn.to/2Ei40tP) 15 | * [Solder Kit](https://www.amazon.ca/-/fr/gp/product/B01N46T138/) 16 | * [Helping Hands for soldering](https://www.amazon.ca/gp/product/B002PIA6Z4) 17 | * [Breadboard](https://amzn.to/2Ei40tP) - [Jumper Wires](https://amzn.to/2Ehh2ru) - [Male to Male Jumper Wires + Tweezer](https://amzn.to/3jcf9eX) 18 | 19 | 🚀APPS 20 | ------ 21 | 22 | * [VSCode](https://code.visualstudio.com/) 23 | * [Fritzing](https://fritzing.org/) 24 | * [PlatformIO](https://platformio.org/) 25 | * [Home Assistant](https://www.home-assistant.io//) 26 | 27 | 📦 Libraries 28 | ===== 29 | * [Adafruit_MPU6050](https://github.com/adafruit/Adafruit_MPU6050) 30 | * [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 31 | * [PubSubClient](https://www.arduinolibraries.info/libraries/pub-sub-client) 32 | * [Wire](https://github.com/espressif/arduino-esp32/tree/master/libraries/Wire) 33 | * [WiFi](https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFi) 34 | 35 | Hardware Overview: MPU-6050 3 Axis Accelerometer Gyroscope 36 | ---------------------- 37 | 38 | 39 | 40 | The MPU-6050 is a serious little piece of motion processing tech! By combining both a 3-Axis accelerometer and a 3-Axis gyroscope on a single chip, the MPU-6050 is capable of processing complex 9-axis MotionFusion algorithms, the MPU-6050 does away with the cross-axis alignment problems that can creep up on discrete parts. 41 | 42 | Hardware Overview: SW-420 Vibration Sensor 43 | ---------------------- 44 | 45 | 46 | 47 | This Vibration Sensor Module consists of an SW-420 Vibration Sensor, resistors, capacitor, potentiometer, comparator LM393 IC, Power, and status LED in an integrated circuit. It is useful for a variety of shocks triggering, theft alarm, smart car, an earthquake alarm, motorcycle alarm, etc. 48 | 49 | Hardware Overview: LiPo Battery 50 | ----------------- 51 | 52 | 53 | A Lithium Polymer battery, or more correctly Lithium-ion Polymer battery (abbreviated as **LiPo**, **LIP**, **Li-poly**, **lithium-poly** and others), is a rechargeable battery of lithium-ion technology using a polymer electrolyte instead of a liquid electrolyte. High conductivity semisolid (gel) polymers form this electrolyte. These batteries provide higher specific energy than other lithium battery types and are used in applications where weight is a critical feature, such as mobile devices, radio-controlled aircraft and some electric vehicles. 54 | 55 | I had purchased this set of [5 batteries with a charger](https://www.amazon.ca/gp/product/B0795F139D) from Amazon for under $15 and been using them for a [previous project](https://github.com/MecaHumArduino/wemos-water-leak-sensor) without a problem, so that shall be my go to battery for this project as well. 56 | 57 | 3D PRINTED CASE 58 | --------------- 59 | No one likes wires hanging around, and so I went ahead looking for a 3D case I can use for this project and luckily found [this one on Thingiverse](https://www.thingiverse.com/thing:2550726) so that's what I'll be using 60 | 61 | Schematic Diagram 62 | ----------------- 63 | 64 | Wire the **MPU6050** and the **SW-420** to the [**ESP32**](https://amzn.to/3jmCpqx) development board as shown in the following schematic diagram. 65 | 66 | 67 | 68 | 69 | Make sure you have installed an MQTT broker in your HomeAssistant setup beforehand. You can start here: https://www.home-assistant.io/docs/mqtt/broker#run-your-own 70 | 71 | Finally 72 | -------- 73 | All contribution to this project is appreciated 74 | -------------------------------------------------------------------------------- /doc/3d/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Alien Cube (http://www.thingiverse.com/thing:1741021) by 3DSage is licensed under the Creative Commons - Attribution - Non-Commercial license. 2 | http://creativecommons.org/licenses/by-nc/3.0/ 3 | 4 | Creative Commons Legal Code 5 | 6 | Attribution-NonCommercial 3.0 Unported 7 | 8 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 9 | LEGAL SERVICES. DISTRIBUTION OF THIS LICENSE DOES NOT CREATE AN 10 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 11 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 12 | REGARDING THE INFORMATION PROVIDED, AND DISCLAIMS LIABILITY FOR 13 | DAMAGES RESULTING FROM ITS USE. 14 | 15 | License 16 | 17 | THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE 18 | COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY 19 | COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS 20 | AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED. 21 | 22 | BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE 23 | TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY 24 | BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS 25 | CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND 26 | CONDITIONS. 27 | 28 | 1. Definitions 29 | 30 | a. "Adaptation" means a work based upon the Work, or upon the Work and 31 | other pre-existing works, such as a translation, adaptation, 32 | derivative work, arrangement of music or other alterations of a 33 | literary or artistic work, or phonogram or performance and includes 34 | cinematographic adaptations or any other form in which the Work may be 35 | recast, transformed, or adapted including in any form recognizably 36 | derived from the original, except that a work that constitutes a 37 | Collection will not be considered an Adaptation for the purpose of 38 | this License. For the avoidance of doubt, where the Work is a musical 39 | work, performance or phonogram, the synchronization of the Work in 40 | timed-relation with a moving image ("synching") will be considered an 41 | Adaptation for the purpose of this License. 42 | b. "Collection" means a collection of literary or artistic works, such as 43 | encyclopedias and anthologies, or performances, phonograms or 44 | broadcasts, or other works or subject matter other than works listed 45 | in Section 1(f) below, which, by reason of the selection and 46 | arrangement of their contents, constitute intellectual creations, in 47 | which the Work is included in its entirety in unmodified form along 48 | with one or more other contributions, each constituting separate and 49 | independent works in themselves, which together are assembled into a 50 | collective whole. A work that constitutes a Collection will not be 51 | considered an Adaptation (as defined above) for the purposes of this 52 | License. 53 | c. "Distribute" means to make available to the public the original and 54 | copies of the Work or Adaptation, as appropriate, through sale or 55 | other transfer of ownership. 56 | d. "Licensor" means the individual, individuals, entity or entities that 57 | offer(s) the Work under the terms of this License. 58 | e. "Original Author" means, in the case of a literary or artistic work, 59 | the individual, individuals, entity or entities who created the Work 60 | or if no individual or entity can be identified, the publisher; and in 61 | addition (i) in the case of a performance the actors, singers, 62 | musicians, dancers, and other persons who act, sing, deliver, declaim, 63 | play in, interpret or otherwise perform literary or artistic works or 64 | expressions of folklore; (ii) in the case of a phonogram the producer 65 | being the person or legal entity who first fixes the sounds of a 66 | performance or other sounds; and, (iii) in the case of broadcasts, the 67 | organization that transmits the broadcast. 68 | f. "Work" means the literary and/or artistic work offered under the terms 69 | of this License including without limitation any production in the 70 | literary, scientific and artistic domain, whatever may be the mode or 71 | form of its expression including digital form, such as a book, 72 | pamphlet and other writing; a lecture, address, sermon or other work 73 | of the same nature; a dramatic or dramatico-musical work; a 74 | choreographic work or entertainment in dumb show; a musical 75 | composition with or without words; a cinematographic work to which are 76 | assimilated works expressed by a process analogous to cinematography; 77 | a work of drawing, painting, architecture, sculpture, engraving or 78 | lithography; a photographic work to which are assimilated works 79 | expressed by a process analogous to photography; a work of applied 80 | art; an illustration, map, plan, sketch or three-dimensional work 81 | relative to geography, topography, architecture or science; a 82 | performance; a broadcast; a phonogram; a compilation of data to the 83 | extent it is protected as a copyrightable work; or a work performed by 84 | a variety or circus performer to the extent it is not otherwise 85 | considered a literary or artistic work. 86 | g. "You" means an individual or entity exercising rights under this 87 | License who has not previously violated the terms of this License with 88 | respect to the Work, or who has received express permission from the 89 | Licensor to exercise rights under this License despite a previous 90 | violation. 91 | h. "Publicly Perform" means to perform public recitations of the Work and 92 | to communicate to the public those public recitations, by any means or 93 | process, including by wire or wireless means or public digital 94 | performances; to make available to the public Works in such a way that 95 | members of the public may access these Works from a place and at a 96 | place individually chosen by them; to perform the Work to the public 97 | by any means or process and the communication to the public of the 98 | performances of the Work, including by public digital performance; to 99 | broadcast and rebroadcast the Work by any means including signs, 100 | sounds or images. 101 | i. "Reproduce" means to make copies of the Work by any means including 102 | without limitation by sound or visual recordings and the right of 103 | fixation and reproducing fixations of the Work, including storage of a 104 | protected performance or phonogram in digital form or other electronic 105 | medium. 106 | 107 | 2. Fair Dealing Rights. Nothing in this License is intended to reduce, 108 | limit, or restrict any uses free from copyright or rights arising from 109 | limitations or exceptions that are provided for in connection with the 110 | copyright protection under copyright law or other applicable laws. 111 | 112 | 3. License Grant. Subject to the terms and conditions of this License, 113 | Licensor hereby grants You a worldwide, royalty-free, non-exclusive, 114 | perpetual (for the duration of the applicable copyright) license to 115 | exercise the rights in the Work as stated below: 116 | 117 | a. to Reproduce the Work, to incorporate the Work into one or more 118 | Collections, and to Reproduce the Work as incorporated in the 119 | Collections; 120 | b. to create and Reproduce Adaptations provided that any such Adaptation, 121 | including any translation in any medium, takes reasonable steps to 122 | clearly label, demarcate or otherwise identify that changes were made 123 | to the original Work. For example, a translation could be marked "The 124 | original work was translated from English to Spanish," or a 125 | modification could indicate "The original work has been modified."; 126 | c. to Distribute and Publicly Perform the Work including as incorporated 127 | in Collections; and, 128 | d. to Distribute and Publicly Perform Adaptations. 129 | 130 | The above rights may be exercised in all media and formats whether now 131 | known or hereafter devised. The above rights include the right to make 132 | such modifications as are technically necessary to exercise the rights in 133 | other media and formats. Subject to Section 8(f), all rights not expressly 134 | granted by Licensor are hereby reserved, including but not limited to the 135 | rights set forth in Section 4(d). 136 | 137 | 4. Restrictions. The license granted in Section 3 above is expressly made 138 | subject to and limited by the following restrictions: 139 | 140 | a. You may Distribute or Publicly Perform the Work only under the terms 141 | of this License. You must include a copy of, or the Uniform Resource 142 | Identifier (URI) for, this License with every copy of the Work You 143 | Distribute or Publicly Perform. You may not offer or impose any terms 144 | on the Work that restrict the terms of this License or the ability of 145 | the recipient of the Work to exercise the rights granted to that 146 | recipient under the terms of the License. You may not sublicense the 147 | Work. You must keep intact all notices that refer to this License and 148 | to the disclaimer of warranties with every copy of the Work You 149 | Distribute or Publicly Perform. When You Distribute or Publicly 150 | Perform the Work, You may not impose any effective technological 151 | measures on the Work that restrict the ability of a recipient of the 152 | Work from You to exercise the rights granted to that recipient under 153 | the terms of the License. This Section 4(a) applies to the Work as 154 | incorporated in a Collection, but this does not require the Collection 155 | apart from the Work itself to be made subject to the terms of this 156 | License. If You create a Collection, upon notice from any Licensor You 157 | must, to the extent practicable, remove from the Collection any credit 158 | as required by Section 4(c), as requested. If You create an 159 | Adaptation, upon notice from any Licensor You must, to the extent 160 | practicable, remove from the Adaptation any credit as required by 161 | Section 4(c), as requested. 162 | b. You may not exercise any of the rights granted to You in Section 3 163 | above in any manner that is primarily intended for or directed toward 164 | commercial advantage or private monetary compensation. The exchange of 165 | the Work for other copyrighted works by means of digital file-sharing 166 | or otherwise shall not be considered to be intended for or directed 167 | toward commercial advantage or private monetary compensation, provided 168 | there is no payment of any monetary compensation in connection with 169 | the exchange of copyrighted works. 170 | c. If You Distribute, or Publicly Perform the Work or any Adaptations or 171 | Collections, You must, unless a request has been made pursuant to 172 | Section 4(a), keep intact all copyright notices for the Work and 173 | provide, reasonable to the medium or means You are utilizing: (i) the 174 | name of the Original Author (or pseudonym, if applicable) if supplied, 175 | and/or if the Original Author and/or Licensor designate another party 176 | or parties (e.g., a sponsor institute, publishing entity, journal) for 177 | attribution ("Attribution Parties") in Licensor's copyright notice, 178 | terms of service or by other reasonable means, the name of such party 179 | or parties; (ii) the title of the Work if supplied; (iii) to the 180 | extent reasonably practicable, the URI, if any, that Licensor 181 | specifies to be associated with the Work, unless such URI does not 182 | refer to the copyright notice or licensing information for the Work; 183 | and, (iv) consistent with Section 3(b), in the case of an Adaptation, 184 | a credit identifying the use of the Work in the Adaptation (e.g., 185 | "French translation of the Work by Original Author," or "Screenplay 186 | based on original Work by Original Author"). The credit required by 187 | this Section 4(c) may be implemented in any reasonable manner; 188 | provided, however, that in the case of a Adaptation or Collection, at 189 | a minimum such credit will appear, if a credit for all contributing 190 | authors of the Adaptation or Collection appears, then as part of these 191 | credits and in a manner at least as prominent as the credits for the 192 | other contributing authors. For the avoidance of doubt, You may only 193 | use the credit required by this Section for the purpose of attribution 194 | in the manner set out above and, by exercising Your rights under this 195 | License, You may not implicitly or explicitly assert or imply any 196 | connection with, sponsorship or endorsement by the Original Author, 197 | Licensor and/or Attribution Parties, as appropriate, of You or Your 198 | use of the Work, without the separate, express prior written 199 | permission of the Original Author, Licensor and/or Attribution 200 | Parties. 201 | d. For the avoidance of doubt: 202 | 203 | i. Non-waivable Compulsory License Schemes. In those jurisdictions in 204 | which the right to collect royalties through any statutory or 205 | compulsory licensing scheme cannot be waived, the Licensor 206 | reserves the exclusive right to collect such royalties for any 207 | exercise by You of the rights granted under this License; 208 | ii. Waivable Compulsory License Schemes. In those jurisdictions in 209 | which the right to collect royalties through any statutory or 210 | compulsory licensing scheme can be waived, the Licensor reserves 211 | the exclusive right to collect such royalties for any exercise by 212 | You of the rights granted under this License if Your exercise of 213 | such rights is for a purpose or use which is otherwise than 214 | noncommercial as permitted under Section 4(b) and otherwise waives 215 | the right to collect royalties through any statutory or compulsory 216 | licensing scheme; and, 217 | iii. Voluntary License Schemes. The Licensor reserves the right to 218 | collect royalties, whether individually or, in the event that the 219 | Licensor is a member of a collecting society that administers 220 | voluntary licensing schemes, via that society, from any exercise 221 | by You of the rights granted under this License that is for a 222 | purpose or use which is otherwise than noncommercial as permitted 223 | under Section 4(c). 224 | e. Except as otherwise agreed in writing by the Licensor or as may be 225 | otherwise permitted by applicable law, if You Reproduce, Distribute or 226 | Publicly Perform the Work either by itself or as part of any 227 | Adaptations or Collections, You must not distort, mutilate, modify or 228 | take other derogatory action in relation to the Work which would be 229 | prejudicial to the Original Author's honor or reputation. Licensor 230 | agrees that in those jurisdictions (e.g. Japan), in which any exercise 231 | of the right granted in Section 3(b) of this License (the right to 232 | make Adaptations) would be deemed to be a distortion, mutilation, 233 | modification or other derogatory action prejudicial to the Original 234 | Author's honor and reputation, the Licensor will waive or not assert, 235 | as appropriate, this Section, to the fullest extent permitted by the 236 | applicable national law, to enable You to reasonably exercise Your 237 | right under Section 3(b) of this License (right to make Adaptations) 238 | but not otherwise. 239 | 240 | 5. Representations, Warranties and Disclaimer 241 | 242 | UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR 243 | OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY 244 | KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, 245 | INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, 246 | FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF 247 | LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, 248 | WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION 249 | OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU. 250 | 251 | 6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE 252 | LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR 253 | ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES 254 | ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS 255 | BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 256 | 257 | 7. Termination 258 | 259 | a. This License and the rights granted hereunder will terminate 260 | automatically upon any breach by You of the terms of this License. 261 | Individuals or entities who have received Adaptations or Collections 262 | from You under this License, however, will not have their licenses 263 | terminated provided such individuals or entities remain in full 264 | compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will 265 | survive any termination of this License. 266 | b. Subject to the above terms and conditions, the license granted here is 267 | perpetual (for the duration of the applicable copyright in the Work). 268 | Notwithstanding the above, Licensor reserves the right to release the 269 | Work under different license terms or to stop distributing the Work at 270 | any time; provided, however that any such election will not serve to 271 | withdraw this License (or any other license that has been, or is 272 | required to be, granted under the terms of this License), and this 273 | License will continue in full force and effect unless terminated as 274 | stated above. 275 | 276 | 8. Miscellaneous 277 | 278 | a. Each time You Distribute or Publicly Perform the Work or a Collection, 279 | the Licensor offers to the recipient a license to the Work on the same 280 | terms and conditions as the license granted to You under this License. 281 | b. Each time You Distribute or Publicly Perform an Adaptation, Licensor 282 | offers to the recipient a license to the original Work on the same 283 | terms and conditions as the license granted to You under this License. 284 | c. If any provision of this License is invalid or unenforceable under 285 | applicable law, it shall not affect the validity or enforceability of 286 | the remainder of the terms of this License, and without further action 287 | by the parties to this agreement, such provision shall be reformed to 288 | the minimum extent necessary to make such provision valid and 289 | enforceable. 290 | d. No term or provision of this License shall be deemed waived and no 291 | breach consented to unless such waiver or consent shall be in writing 292 | and signed by the party to be charged with such waiver or consent. 293 | e. This License constitutes the entire agreement between the parties with 294 | respect to the Work licensed here. There are no understandings, 295 | agreements or representations with respect to the Work not specified 296 | here. Licensor shall not be bound by any additional provisions that 297 | may appear in any communication from You. This License may not be 298 | modified without the mutual written agreement of the Licensor and You. 299 | f. The rights granted under, and the subject matter referenced, in this 300 | License were drafted utilizing the terminology of the Berne Convention 301 | for the Protection of Literary and Artistic Works (as amended on 302 | September 28, 1979), the Rome Convention of 1961, the WIPO Copyright 303 | Treaty of 1996, the WIPO Performances and Phonograms Treaty of 1996 304 | and the Universal Copyright Convention (as revised on July 24, 1971). 305 | These rights and subject matter take effect in the relevant 306 | jurisdiction in which the License terms are sought to be enforced 307 | according to the corresponding provisions of the implementation of 308 | those treaty provisions in the applicable national law. If the 309 | standard suite of rights granted under applicable copyright law 310 | includes additional rights not granted under this License, such 311 | additional rights are deemed to be included in the License; this 312 | License is not intended to restrict the license of any rights under 313 | applicable law. 314 | 315 | 316 | Creative Commons Notice 317 | 318 | Creative Commons is not a party to this License, and makes no warranty 319 | whatsoever in connection with the Work. Creative Commons will not be 320 | liable to You or any party on any legal theory for any damages 321 | whatsoever, including without limitation any general, special, 322 | incidental or consequential damages arising in connection to this 323 | license. Notwithstanding the foregoing two (2) sentences, if Creative 324 | Commons has expressly identified itself as the Licensor hereunder, it 325 | shall have all rights and obligations of Licensor. 326 | 327 | Except for the limited purpose of indicating to the public that the 328 | Work is licensed under the CCPL, Creative Commons does not authorize 329 | the use by either party of the trademark "Creative Commons" or any 330 | related trademark or logo of Creative Commons without the prior 331 | written consent of Creative Commons. Any permitted use will be in 332 | compliance with Creative Commons' then-current trademark usage 333 | guidelines, as may be published on its website or otherwise made 334 | available upon request from time to time. For the avoidance of doubt, 335 | this trademark restriction does not form part of the License. 336 | 337 | Creative Commons may be contacted at https://creativecommons.org/. -------------------------------------------------------------------------------- /doc/3d/README.txt: -------------------------------------------------------------------------------- 1 | .: :, 2 | ,:::::::: ::` ::: ::: 3 | ,:::::::: ::` ::: ::: 4 | .,,:::,,, ::`.:, ... .. .:, .:. ..`... ..` .. .:, .. :: .::, .:,` 5 | ,:: ::::::: ::, ::::::: `:::::::.,:: ::: ::: .:::::: ::::: :::::: .:::::: 6 | ,:: :::::::: ::, :::::::: ::::::::.,:: ::: ::: :::,:::, ::::: ::::::, :::::::: 7 | ,:: ::: ::: ::, ::: :::`::. :::.,:: ::,`::`::: ::: ::: `::,` ::: ::: 8 | ,:: ::. ::: ::, ::` :::.:: ::.,:: :::::: ::::::::: ::` :::::: ::::::::: 9 | ,:: ::. ::: ::, ::` :::.:: ::.,:: .::::: ::::::::: ::` ::::::::::::::: 10 | ,:: ::. ::: ::, ::` ::: ::: `:::.,:: :::: :::` ,,, ::` .:: :::.::. ,,, 11 | ,:: ::. ::: ::, ::` ::: ::::::::.,:: :::: :::::::` ::` ::::::: :::::::. 12 | ,:: ::. ::: ::, ::` ::: :::::::`,:: ::. :::::` ::` :::::: :::::. 13 | ::, ,:: `` 14 | :::::::: 15 | :::::: 16 | `,,` 17 | 18 | 19 | http://www.thingiverse.com/thing:1741021 20 | Alien Cube by 3DSage is licensed under the Creative Commons - Attribution - Non-Commercial license. 21 | http://creativecommons.org/licenses/by-nc/3.0/ 22 | 23 | # Summary 24 | 25 | This is a new and smaller version of my alien cube design. It has a cool sci fi look. 26 | This video explains how to put this together. 27 | https://www.youtube.com/watch?v=J9K_DfxWLdw 28 | 29 | Thank you for your support! 30 | 31 | # Print Settings 32 | 33 | Printer Brand: 3D Systems 34 | Printer: Cube 35 | Rafts: No 36 | Supports: No 37 | Resolution: 0.2mm -------------------------------------------------------------------------------- /doc/3d/attribution_card.html: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 |
16 |
17 | 18 |
19 |

Alien Cube by 3DSage

20 |

Published on August 28, 2016

21 |

www.thingiverse.com/thing:1741021

22 |

23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | Creative Commons - Attribution - Non-Commercial

35 |
36 | 37 | 42 | -------------------------------------------------------------------------------- /doc/3d/files/Base.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Base.STL -------------------------------------------------------------------------------- /doc/3d/files/Separate_Base.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Separate_Base.STL -------------------------------------------------------------------------------- /doc/3d/files/Separate_Lid.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Separate_Lid.STL -------------------------------------------------------------------------------- /doc/3d/files/Separate_LidDiffuser.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Separate_LidDiffuser.STL -------------------------------------------------------------------------------- /doc/3d/files/Separate_Side1.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Separate_Side1.STL -------------------------------------------------------------------------------- /doc/3d/files/Separate_Side2.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Separate_Side2.STL -------------------------------------------------------------------------------- /doc/3d/files/Sides.STL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/files/Sides.STL -------------------------------------------------------------------------------- /doc/3d/images/245c57981f4cddeab83cff3f846162f7_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/245c57981f4cddeab83cff3f846162f7_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/271c0036ba8b6635415a84bfcd176f19_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/271c0036ba8b6635415a84bfcd176f19_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/3396dd9f41a952cdc6e37b2317db52cd_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/3396dd9f41a952cdc6e37b2317db52cd_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/43d0d756b721a23e1a7fb791ff80daa2_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/43d0d756b721a23e1a7fb791ff80daa2_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/4b1c59c7728e2b1cb65f6cb20aaf5cf9_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/4b1c59c7728e2b1cb65f6cb20aaf5cf9_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/63014cc61306f34a37fa27f194163c88_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/63014cc61306f34a37fa27f194163c88_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/6bfc060cf82461a36f069b9f4187059b_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/6bfc060cf82461a36f069b9f4187059b_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/8b0db017a77d7f9b70b762376feb0774_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/8b0db017a77d7f9b70b762376feb0774_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/b61681cbd25cb6c9a7b1af2591f67687_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/b61681cbd25cb6c9a7b1af2591f67687_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/b66bf216e8f3e216b8c03a8b85245b85_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/b66bf216e8f3e216b8c03a8b85245b85_preview_featured.jpg -------------------------------------------------------------------------------- /doc/3d/images/ebca68d2776899c292c068493eb053f9_preview_featured.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/3d/images/ebca68d2776899c292c068493eb053f9_preview_featured.jpg -------------------------------------------------------------------------------- /doc/img/SW-420-sensor.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/img/SW-420-sensor.jpeg -------------------------------------------------------------------------------- /doc/img/mpu6050.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/img/mpu6050.jpeg -------------------------------------------------------------------------------- /doc/img/wiring-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/img/wiring-diagram.png -------------------------------------------------------------------------------- /doc/wiring/sketch.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartThingsDIY/esp32-smart-cube/62a1c268f88789c0bca4af358e17e96b3f439dca/doc/wiring/sketch.fzz -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:esp32doit-devkit-v1] 12 | platform = espressif32 13 | board = esp32doit-devkit-v1 14 | framework = arduino 15 | lib_extra_dirs = ~/Documents/Arduino/libraries 16 | monitor_speed = 115200 17 | upload_port = /dev/cu.usbserial-0001 18 | monitor_port = /dev/cu.usbserial-0001 19 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "secrets.h" 3 | #include 4 | #include "WiFi.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define DEBUG true // switch to "false" for production 12 | #define NB_TRYWIFI 20 // WiFi connection retries 13 | #define durationSleep 30 // seconds 14 | 15 | float GyroX, GyroY, GyroZ; 16 | Adafruit_MPU6050 mpu; 17 | 18 | WiFiClient espClient; 19 | PubSubClient client(espClient); 20 | 21 | // ************** 22 | void loop(); 23 | void setup(); 24 | void doAction(String x, String y, String z); 25 | void goToSleep(); 26 | void connectToWiFi(); 27 | void connectToHass(); 28 | void initGyroComponent(); 29 | void printWakeupReason(); 30 | void sendActionToHass(String msg); 31 | // ************** 32 | 33 | /** 34 | * Establishes WiFi connection 35 | * @return 36 | */ 37 | void connectToWiFi() 38 | { 39 | int _try = 0; 40 | 41 | WiFi.mode(WIFI_STA); 42 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 43 | 44 | if (DEBUG == true) 45 | { 46 | Serial.println("Connecting to Wi-Fi"); 47 | } 48 | 49 | while (WiFi.status() != WL_CONNECTED) 50 | { 51 | delay(500); 52 | Serial.print("."); 53 | _try++; 54 | if (_try >= NB_TRYWIFI) 55 | { 56 | if (DEBUG == true) 57 | { 58 | Serial.println("Impossible to connect WiFi, going to deep sleep"); 59 | } 60 | 61 | ESP.deepSleep(durationSleep * 1000000); 62 | } 63 | } 64 | if (DEBUG == true) 65 | { 66 | Serial.println("Connected to Wi-Fi"); 67 | } 68 | } 69 | 70 | /** 71 | * Establishes connection to Home Assistant MQTT Broker 72 | * @return 73 | */ 74 | void connectToHass() 75 | { 76 | client.setServer(MQTT_SERVER, 1883); 77 | 78 | // Loop until we're reconnected 79 | while (!client.connected()) 80 | { 81 | if (DEBUG == true) 82 | { 83 | Serial.print("Attempting MQTT connection..."); 84 | } 85 | // Attempt to connect 86 | // If you do not want to use a username and password, change next line to 87 | // if (client.connect("ESP32Client")) { 88 | if (client.connect("ESP32Client", MQTT_USER, MQTT_PASSWORD)) 89 | { 90 | if (DEBUG == true) 91 | { 92 | Serial.println("connected"); 93 | } 94 | } 95 | else 96 | { 97 | if (DEBUG == true) 98 | { 99 | Serial.print("failed, rc="); 100 | Serial.print(client.state()); 101 | Serial.println(" try again in 5 seconds"); 102 | } 103 | // Wait 5 seconds before retrying 104 | delay(5000); 105 | } 106 | } 107 | } 108 | 109 | /** 110 | * Publishes notification to MQTT topic 111 | * @return 112 | */ 113 | void sendActionToHass(String msg) 114 | { 115 | if (DEBUG == true) 116 | { 117 | Serial.println("Publishing to Hass!"); 118 | } 119 | // publish the reading to Hass through MQTT 120 | client.publish(MQTT_PUBLISH_TOPIC, msg.c_str(), true); 121 | client.loop(); 122 | 123 | if (DEBUG == true) 124 | { 125 | Serial.print("Alarm "); 126 | Serial.print(msg); 127 | Serial.println(" sent to Hass!"); 128 | } 129 | } 130 | 131 | void setup(void) 132 | { 133 | Serial.begin(115200); 134 | 135 | // only print debug messages to serial if we're in debug mode 136 | if (DEBUG == true) 137 | { 138 | Serial.println("Waking up "); 139 | printWakeupReason(); // Print the wakeup reason for ESP32 140 | } 141 | 142 | while (!Serial) 143 | { 144 | delay(10); // will pause Zero, Leonardo, etc until serial console opens 145 | } 146 | 147 | initGyroComponent(); 148 | 149 | /* wait for the cube to be put down */ 150 | delay(2000); 151 | /* Get new sensor events with the readings */ 152 | sensors_event_t a, g, temp; 153 | mpu.getEvent(&a, &g, &temp); 154 | 155 | GyroX = g.gyro.x; 156 | GyroY = g.gyro.y; 157 | GyroZ = g.gyro.z; 158 | 159 | if (DEBUG == true) 160 | { 161 | Serial.println(""); 162 | Serial.print("Rotation X: "); 163 | Serial.print(GyroX); 164 | 165 | Serial.print(", Y: "); 166 | Serial.print(GyroY); 167 | 168 | Serial.print(", Z: "); 169 | Serial.print(GyroZ); 170 | 171 | Serial.println(""); 172 | } 173 | 174 | doAction(String(GyroX), String(GyroY), String(GyroZ)); 175 | 176 | delay(5000); // stay awake for 5 seconds 177 | goToSleep(); 178 | } 179 | 180 | void doAction(String x, String y, String z) 181 | { 182 | 183 | connectToWiFi(); 184 | connectToHass(); 185 | 186 | if (x == "-0.01" && y == "-0.01" && z == "-0.01") { 187 | sendActionToHass("action:open_front_door"); 188 | } else if (x == "-0.01" && y == "-0.01" && z == "-0.02") { 189 | sendActionToHass("action:close_front_door"); 190 | } else if (x == "-0.01" && y == "-0.02" && z == "-0.02") { 191 | sendActionToHass("action:enable_busy_mode"); 192 | } else if (x == "-0.01" && y == "-0.02" && z == "-0.01") { 193 | sendActionToHass("action:disable_busy_mode"); 194 | } else { 195 | sendActionToHass("action:catch_all"); 196 | } 197 | } 198 | 199 | void goToSleep() 200 | { 201 | if (DEBUG == true) 202 | { 203 | Serial.println("Going to sleep"); 204 | } 205 | 206 | // Configure GPIO33 as ext0 wake up source for HIGH logic level 207 | esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); 208 | 209 | // Go to sleep now 210 | esp_deep_sleep_start(); 211 | } 212 | /** 213 | * Function that prints the reason by which ESP32 has been awaken from sleep 214 | * 215 | **/ 216 | void printWakeupReason() 217 | { 218 | esp_sleep_wakeup_cause_t wakeupReason; 219 | wakeupReason = esp_sleep_get_wakeup_cause(); 220 | 221 | switch (wakeupReason) 222 | { 223 | case 1: 224 | Serial.println("Wakeup caused by external signal using RTC_IO"); 225 | break; 226 | case 2: 227 | Serial.println("Wakeup caused by external signal using RTC_CNTL"); 228 | break; 229 | case 3: 230 | Serial.println("Wakeup caused by timer"); 231 | break; 232 | case 4: 233 | Serial.println("Wakeup caused by touchpad"); 234 | break; 235 | case 5: 236 | Serial.println("Wakeup caused by ULP program"); 237 | break; 238 | default: 239 | Serial.println("Wakeup was not caused by deep sleep"); 240 | break; 241 | } 242 | } 243 | 244 | void initGyroComponent() 245 | { 246 | // Try to initialize! 247 | if (!mpu.begin()) 248 | { 249 | Serial.println("Failed to find MPU6050 chip"); 250 | while (1) 251 | { 252 | delay(10); 253 | } 254 | } 255 | 256 | Serial.println(""); 257 | Serial.println("MPU6050 Found!"); 258 | Serial.println(""); 259 | 260 | mpu.setGyroRange(MPU6050_RANGE_500_DEG); 261 | Serial.print("Gyro range set to: "); 262 | switch (mpu.getGyroRange()) 263 | { 264 | case MPU6050_RANGE_250_DEG: 265 | Serial.println("+- 250 deg/s"); 266 | break; 267 | case MPU6050_RANGE_500_DEG: 268 | Serial.println("+- 500 deg/s"); 269 | break; 270 | case MPU6050_RANGE_1000_DEG: 271 | Serial.println("+- 1000 deg/s"); 272 | break; 273 | case MPU6050_RANGE_2000_DEG: 274 | Serial.println("+- 2000 deg/s"); 275 | break; 276 | } 277 | 278 | mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); 279 | Serial.print("Filter bandwidth set to: "); 280 | switch (mpu.getFilterBandwidth()) 281 | { 282 | case MPU6050_BAND_260_HZ: 283 | Serial.println("260 Hz"); 284 | break; 285 | case MPU6050_BAND_184_HZ: 286 | Serial.println("184 Hz"); 287 | break; 288 | case MPU6050_BAND_94_HZ: 289 | Serial.println("94 Hz"); 290 | break; 291 | case MPU6050_BAND_44_HZ: 292 | Serial.println("44 Hz"); 293 | break; 294 | case MPU6050_BAND_21_HZ: 295 | Serial.println("21 Hz"); 296 | break; 297 | case MPU6050_BAND_10_HZ: 298 | Serial.println("10 Hz"); 299 | break; 300 | case MPU6050_BAND_5_HZ: 301 | Serial.println("5 Hz"); 302 | break; 303 | } 304 | 305 | delay(100); 306 | } 307 | 308 | void loop() {} 309 | -------------------------------------------------------------------------------- /src/secrets_copy.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SECRET 4 | 5 | const char WIFI_SSID[] = "XXXXXXXX"; 6 | const char WIFI_PASSWORD[] = "XXXXXXXX"; 7 | 8 | // The MQTT topics that this device should publish/subscribe 9 | #define MQTT_PUBLISH_TOPIC "XXXXXXXX" 10 | 11 | #define MQTT_SERVER "XXX.XXX.XXX.XXX" 12 | #define MQTT_USER "XXXXXXXX" 13 | #define MQTT_PASSWORD "XXXXXXXX" 14 | --------------------------------------------------------------------------------