<p>Based on Home Assisstant, build a smart home with integrated home lighting system, multimedia system and water flow system. Technical points: Tmall Genie interaction, LoRa communication, MQTT protocol, FRP intranet penetration, NAS storage, and toggle switch circuit transformation.</p>
</span>
<aid="more"></a>
<script>
session.onload(function(){
if(page.tran.getLang() == 'en'){
tips.warning({
title: 'Caution',
position: 'topRight',
message: 'This page was translated by Machine!!',
buttons: [['<button>Show Original Page</button>', function (instance, toast) {
<h2id="过程综述:"><ahref="#过程综述:"class="headerlink"title="过程综述:"></a>过程综述:</h2><h3id="一轮开发"><ahref="#一轮开发"class="headerlink"title="一轮开发"></a>一轮开发</h3><p>大一暑假,开始尝试使用WiFiduino在家中控制一些物体,实现了对(坏了的)太阳能加水(通过电磁阀)的控制(修改了原先家里的管道,加入了电磁阀。跑了好多趟五金店,收获了不少关于水管的知识和实践经验。之后做surf的水泵也有用到。)以及对走廊灯(通过光电耦合器)的控制。找到一台废弃的10年前的我爸的IBM笔记本,装上了win server 2008, 并配置好了mysql数据库,并定期使用http从单片机采集数据。我用php写了一个简单的web界面,来展示相关信息,并实现网页控制。由于家里被分配的是中国移动分配的大NAT内网IP,因此使用frp来实现内网穿透,使外界可以访问家里服务器上的web网页。</p>
<p>Combining the knowledge I have learned and using cutting-edge technology to benefit life has been my unremitting pursuit for a long time. In order to let my family experience a more stable, comfortable and intelligent lifestyle, and to consolidate the technical foundation and architecture experience accumulated in the school smartfarm project and my own ushio system, I decided to use 20 to 23 before going abroad. All day long, reconstruct the old IoT system at home. Adopt a layered architecture and interface-oriented, test-oriented, and <strong>document-oriented</strong> development principles, with <strong>Available</strong> and <strong>Reliable</strong> as the highest priority indicators to build an operational target 3. A highly modular, easy-to-remote control, open source home software and hardware intelligent sensor control system for more than years.</p>
<h2id="Background"><ahref="#Background"class="headerlink"title="Background:"></a>Background:</h2><p>In the first semester of the freshman year, I was new to MCU and used Arduino Uno to build a smart curtain system in the dormitory (for details, please refer to <ahref="https://www.eee.dog/tech/build-236-iot-platform">this blog</a> -on-wifiduino.html)), has accumulated a certain amount of experience.</p>
<li>Ask experts (for water pipe problems, ask the owner of the hardware store, and LoRa for advice from Oulang R&D department)</li>
<li>Be a wheel, keep iterating, optimize the system in refactoring, and gain experience</li>
<li>Investigate the needs of customers (family) carefully, pay more attention to customer needs in design, rather than communicate with customers</li>
<li>When designing a system, it must be assumed that other systems that interact with it are unstable. Let the interaction between systems be like gentlemen intersect, like water. Thereby improving the margin of the whole system.</li>
<li>Continuous operation and maintenance. For example, it happened before the three rounds of development. My mother touched a human sensor while cleaning, causing the entire light control system to be disordered. At that time, I was in Suzhou connected to the server at home via remote ssh, kicked the broken sensor out of the control system and solved the problem. Therefore, you must allow yourself to be connected to the system for operation and maintenance at any time. The future operation and maintenance must be fully considered in the design.</li>
</ol>
<h2id="Process-overview"><ahref="#Process-overview"class="headerlink"title="Process overview:"></a>Process overview:</h2><h3id="First-round-of-development"><ahref="#First-round-of-development"class="headerlink"title="First round of development"></a>First round of development</h3><p>Freshman summer vacation, I began to try to use WiFiduino to control some objects at home, and realized the control of (broken) solar water (through solenoid valve) (modified the original home pipes and added solenoid valves. I went to the hardware store many times. , I have gained a lot of knowledge and practical experience on water pipes. Later, I will also be useful to do surf pumps.) and control the corridor lights (through photocouplers). I found an abandoned IBM laptop of my dad 10 years ago, installed it with win server 2008, and configured the mysql database, and regularly used http to collect data from the microcontroller. I wrote a simple web interface in php to display relevant information and implement web control. Since the home is assigned a large NAT intranet IP allocated by China Mobile, frp is used to achieve intranet penetration, so that the outside world can access the web pages on the home server.</p>
<h3id="Second-round-of-development"><ahref="#Second-round-of-development"class="headerlink"title="Second round of development"></a>Second round of development</h3><p>During the sophomore winter vacation, the previous firmware was improved, wifiduino was used to control the lights in the kitchen, living room, and dining room one after another, and a series of sensors were deployed and connected to the network. Because the WiFi signal of the single-chip microcomputer of the solar energy pipeline was weak, a router was used nearby to bridge. It is worth mentioning that on the night of New Year’s Eve, I was still at home with a power outage 2 minutes before the start of the Spring Festival Gala. I don’t know how my parents feel. .</p>
<h3id="Third-round-of-development"><ahref="#Third-round-of-development"class="headerlink"title="Third round of development"></a>Third round of development</h3><p>Sophomore summer vacation, based on the experience of this semester, reconstruct the system. On the server, the reinstallation system provides UI and other services for Centos7 to use the home assistant open source project, using mqtt as the communication protocol. Use nodeJS to write the control logic of light control automation and solar water adding. In the single chip microcomputer, the firmware is updated, and the ota remote firmware update is supported (referring to some Wiot experience, but more tailored to local conditions). In addition, two new flow meters have been added to fully automate the solar water addition (previously, the water addition time was set through the web page). Due to the weak WiFi signal of the flowmeter, we used Arduino uno + LoRa as the communication means. This design, combined with the opinions of parents, fully considered the stability of the overall system. For example, how to handle various types of errors and loss of connection, how to restart after a power failure, how to ensure that the MCU is not disordered when the server is unresponsive, and how to physically switch back to manual after the system is out of control (such as lights, and Solar energy plus water).</p>
<h3id="Fourth-round-of-development"><ahref="#Fourth-round-of-development"class="headerlink"title="Fourth round of development"></a>Fourth round of development</h3><p>In the summer vacation of the junior year, based on the sensor data accumulated in the past, the LSTM model is used to train the neural network through keras+tensorflow to realize the prediction of the light control state. The optimization is still ongoing and is expected to be completed before September. In addition, there are plans to connect to the Tmall Wizard (still in the queue, waiting for the development of the ushio oauth system, the ushio oauth system needs to wait for the development of the ushio user system, and the ushio user system needs stable js-session as support, js-session Need to use fp to accurately identify user equipment.. Currently fp is still in the third test stage. So, let’s do it step by step,,).</p>
<h2id="Hardware-design"><ahref="#Hardware-design"class="headerlink"title="Hardware design"></a>Hardware design</h2><h3id="Single-Chip-Microcomputer-Material-Selection"><ahref="#Single-Chip-Microcomputer-Material-Selection"class="headerlink"title="Single Chip Microcomputer Material Selection"></a>Single Chip Microcomputer Material Selection</h3><p>Use Arduino UNO as the calculation unit. On the one hand, Arduino Uno has high reliability. My previous smart curtain system used Arduino Uno as the main control board, and it ran smoothly for one and a half years without hardware problems. On the other hand, Arduino Uno has a larger RAM, so there is no need to bother to program the microcontroller in order to compete for RAM.</p>
<p>The main disadvantage of Arduino UNO is its high price, but this cost is necessary to ensure long-term stable operation.</p>
<h3id="WiFi-communication-module-selection"><ahref="#WiFi-communication-module-selection"class="headerlink"title="WiFi communication module selection"></a>WiFi communication module selection</h3><p>Use ESP-01 as the WiFi communication module. The main reason is that the ESP-01 has been used to build a remote control car in the previous school courses, and I have a better understanding of its working methods and performance to reduce development costs.</p>
<h3id="LoRa-communication-module-selection"><ahref="#LoRa-communication-module-selection"class="headerlink"title="LoRa communication module selection"></a>LoRa communication module selection</h3><p>Use <strong>Anxinke LORA RA-02 LORA</strong> as the communication module between LoRa node and LoRa gateway. The reason is that this module is used in the surf project, so I understand it better.</p>
<h3id="Smart-speaker-selection"><ahref="#Smart-speaker-selection"class="headerlink"title="Smart speaker selection"></a>Smart speaker selection</h3><p>As the most important interface for human-computer interaction in this IoT system, smart speakers play a pivotal role. After research, I use Tmall Elf Sugar R as a home smart speaker. On the one hand, the price/performance ratio of Tmall Genie is very cheap. Second, unlike Xiaomi’s development of Xiao Ai, the purpose of Ali’s development of Tmall Genie is to provide a platform that allows developers to access its network in various forms, which is very important to me.</p>
<h3id="Light-toggle-switch"><ahref="#Light-toggle-switch"class="headerlink"title="Light toggle switch"></a>Light toggle switch</h3><p>Initial idea: one-way control + reset device.<br>Actual realization: The original single-opening is transformed into double-opening, one is connected in series with the relay, and the other is connected with two pin ports.</p>
<p>Two communication modes of WiFi+LoRa are adopted. In scenarios that require high-speed and high-quality communication, such as Tmall Genie, mobile phones, and notebooks, use WiFi as a means of communication. In places where the WiFi signal is unstable, use LoRa for communication. Use MQTT as the application layer protocol.</p>
<h2id="Main-control-system-design"><ahref="#Main-control-system-design"class="headerlink"title="Main control system design"></a>Main control system design</h2><p>Use the python3 open source project Home Assistant as the basis to build the main control system of this project. The effect is as follows:</p>
<h2id="Multimedia-system-design"><ahref="#Multimedia-system-design"class="headerlink"title="Multimedia system design"></a>Multimedia system design</h2><p>Multimedia is mainly provided by Tmall Genie. In addition, through the Xiaomi box, the video, pictures, and music in the home network disk can be played on the TV and stereo.</p>
<h2id="Cloud-support-system-design"><ahref="#Cloud-support-system-design"class="headerlink"title="Cloud support system design"></a>Cloud support system design</h2><p>dns.yimian.xyz provides dns resolution service.</p>
<h2id="NAS-storage-system-design"><ahref="#NAS-storage-system-design"class="headerlink"title="NAS storage system design"></a>NAS storage system design</h2><p>By mounting the idle 500G mechanical hard disk removed from the old computer to the old IBM server, the storage system is built.<br>In the local area network, since the home is a win system, smb is used as a sharing protocol to realize file transfer and supports mounting in the local area network. The speed is stable, and the whole network reads and writes about 10MB/s. Access to the external network is realized through http and frp reverse generation, but the speed is slow.</p>
<p>The following is a screenshot of the web interface:</p>
<p><imgsrc="https://api.yimian.xyz/img/?path=imgbed/img_352da66_1866x814_8_null_normal.jpeg"alt="NAS Web Page"></p>
<li>Use WiFi, LoRa dual protocol. WiFi is used to connect the single-chip microcomputer closer to the router, and LoRa connects to the single-chip microcomputer in the original place where the WiFi signal is weaker.</li>
<li>I have equipped each switch with double insurance, so that each switch can be returned to manual state after easy operation. If you want to return to manual mode, just scan the QR code on the switch and follow the instructions. For details, see <ahref="https://sola.yimian.xyz/#/home?id=%E5%BA%94%E6%80%A5%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97"target="_blank"rel="noopener">Emergency Operation Guide</a></li>
<li>In the first round of development, the light control is determined by the microcontroller. There is no essential difference with the sound-controlled lights in the corridor, they are all sensors -> single chip microcomputer -> lights. In the second round of development, this judgment logic was transferred to the server. In the third round of development, we introduced night mode, day mode, midnight mode, etc., and used mathematical models in the decision-making process to determine the probability of people using the data of each sensor to determine the control of the lights. However, this method is not always timely. Family members always need to press the toggle switch when it is just dark. Therefore, in the fourth round of development, I hope to use machine learning to let myself learn the switching habits of my family. Currently, data is being continuously collected and trained, and the night accuracy is around 0.75.</li>
<li>Each module and the single-chip microcomputer use mqtt to interact. Each microcontroller has an online mode and an offline mode. The light control decision module and the solar control module are two separate nodeJS modules, guarded by pm2. In order to ensure uninterrupted services, they use redis (configured persistence) as real-time data storage.</li>
</ol>
<hr>
<p><strong>2020.6 update</strong></p>
<h2id="RNN-realizes-light-control"><ahref="#RNN-realizes-light-control"class="headerlink"title="RNN realizes light control"></a>RNN realizes light control</h2><p>Based on the previously accumulated human sensor data, the LSTM model is used to train the neural network using keras to realize the prediction of the light state.</p>
<hr>
<p><strong>2020.8 update</strong></p>
<h2id="Tmall-Elf-Access"><ahref="#Tmall-Elf-Access"class="headerlink"title="Tmall Elf Access"></a>Tmall Elf Access</h2><p>Self-built OAuth server has realized the access of Tmall Wizard.</p>