本地构建和自动化构建

by Amir Off

由Amir Off

如何构建最强大,最安全的家庭自动化系统 (How to build the most robust and secure home automation system)

In this article, I’ll discuss how I built a Smart Home Automation System with Angular and Node.js on a Raspberry Pi without relying on any external cloud services.

在本文中,我将讨论如何在Raspberry Pi上不依赖任何外部云服务的情况下,使用Angular和Node.js构建智能家居自动化系统。

介绍 (Intro)

Over the last few days, I spent some nights designing and developing a home automation system based on JavaScript, with the use of Angular and Node.js. And, like with any other project, the planning involved some deep research on the internet.

在过去的几天里,我花了一些晚上使用Angular和Node.js设计和开发基于JavaScript的家庭自动化系统。 并且,与任何其他项目一样,该计划涉及对互联网的一些深入研究。

It turned out that there are plenty of fish in the sea —plenty of solutions on how to implement a home automation system. Some offer paid services in “the cloud” and others explain how to build your own using a technology called MQTT.

事实证明,海中有很多鱼-有关如何实施家庭自动化系统的大量解决方案。 一些提供“云”中的付费服务,另一些解释如何使用称为MQTT的技术构建自己的服务。

None of the solutions made any sense to me. All options were either expensive, or had inconvenient implementations or even security flaws.

没有一种解决方案对我有意义。 所有选项要么昂贵,要么实现不便,甚至存在安全漏洞。

But, before we go any further, let’s explain what MQTT is. MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol. MQTT is designed for constrained devices and low-bandwidth, high-latency, or unreliable networks.

但是,在进一步介绍之前,让我们解释一下MQTT是什么。 MQTT代表MQ遥测传输 。 它是一种发布/订阅,极其简单和轻便的消息传递协议。 MQTT专为受限设备和低带宽,高延迟或不可靠的网络而设计。

The design principles are to minimise network bandwidth and device resource requirements whilst attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal for the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.

设计原则是在尝试确保可靠性和一定程度的交付保证的同时,最小化网络带宽和设备资源要求。 这些原则也使该协议成为新兴的连接设备的“机器对机器”(M2M)或“物联网”世界以及带宽和电池电量极为宝贵的移动应用的理想选择 。

Why wasn’t I convinced about using MQTT, or by any of the solutions I found on the internet? Two reasons:

为什么我对使用MQTT或我在互联网上找到的任何解决方案都不满意? 两个原因:

  1. While the MQTT technology seems very convenient for IoT devices, I still thought it was unnecessary. The system which I will be showing in the following tutorial operates in the same medium where the IoT devices live. All benefits that MQTT has for being “fast” and having “low-bandwidth” become irrelevant. Plus there’s all the hassle that is involved in its implementation and all the extra overhead with the additional npm packages that are required for it to work in a JavaScript environment. Instead, I will be using generic JavaScript and Node.js libraries only, nothing more!尽管MQTT技术对于物联网设备非常方便,但我仍然认为这是不必要的。 我将在以下教程中显示的系统在IoT设备所在的相同介质上运行。 MQTT所具有的“快速”和“低带宽”的所有优点变得无关紧要。 另外,它的实现还涉及所有麻烦,并且在JavaScript环境中运行它需要额外的npm软件包带来的所有额外开销。 相反,我将仅使用通用JavaScript和Node.js库,仅此而已!
  2. What about the security part? Well, I’m not a big fan of “the cloud” or cloud computing in general. In some cases it can be very beneficial, but in most cases it’s just unnecessary. Think about it: why would you have a service that is required for controlling your home appliances to be hosted somewhere else in “the cloud” and not in your own network?安全部分呢? 好吧,我一般都不喜欢“云”或云计算。 在某些情况下,这可能是非常有益的,但在大多数情况下,这是不必要的。 想一想:为什么您需要一项服务,以控制您的家用电器托管在“云”中而不是您自己的网络中?

One might think that “the cloud” provides the ability to access your home appliances from anywhere in the world via the internet.

可能有人认为“云”提供了通过互联网从世界任何地方访问您的家用电器的能力。

But think about this: when your home network doesn’t have internet connection, “the cloud” becomes redundant. More importantly, you can still make your home automation system accessible from the internet using port-forwarding even if it’s hosted on your local network.

但是请考虑一下:当您的家庭网络没有Internet连接时,“云”将变得多余。 更重要的是,即使端口托管在本地网络上,您仍然可以使用端口转发从互联网访问家庭自动化系统。

This is when it “clicked” for me, and I thought about hosting the whole system on a Raspberry Pi and keeping it in my local network.

这是当它为我“点击”时,我考虑过将整个系统托管在Raspberry Pi上,并将其保留在我的本地网络中。

技术 (The technology)

  1. Software: The reason why I chose Angular and Node.js is because they’re based on JavaScript and I’m already familiar with it. After all, I wanted to design and develop a progressive web app that communicates with my IoT devices via HTTP — and JavaScript offered all the functionality that I needed.

    软件:之所以选择Angular和Node.js是因为它们基于JavaScript,并且我已经很熟悉。 毕竟,我想设计和开发一个渐进式Web应用程序,该应用程序可通过HTTP与我的IoT设备进行通信-JavaScript提供了我需要的所有功能。

  2. Hardware: The system works with microcontrollers like the Arduino Uno/Mega/Du/MKR1000, Adafruit HUZZAH CC3000, and any other microcontroller with a WiFi connection. I am using the ESP8266 as a base component for my home automation system. It’s a low-cost WiFi microchip with microcontroller capability. It has everything I need and for a cheap price! Lastly, we need to host the system somewhere on our local network — so what’s better than the Raspberry Pi?

    硬件:该系统可与Arduino Uno / Mega / Du / MKR1000,Adafruit HUZZAH CC3000和任何其他具有WiFi连接的微控制器一起使用。 我将ESP8266用作家庭自动化系统的基本组件。 这是一款具有微控制器功能的低成本WiFi微芯片。 它具有我需要的一切,而且价格便宜! 最后,我们需要将系统托管在本地网络上的某个地方–那么有什么比Raspberry Pi更好的呢?

This won’t be a coding tutorial where I dive deep into coding, since this project is open-source and I will be publishing everything on GitHub. I will only demonstrate how to implement your own home automation system and will be going through each step. If you’re a developer please fork the repository and get involved in improving it.

这不是我将深入研究编码的编码教程,因为该项目是开源的,我将在GitHub上发布所有内容。 我将仅演示如何实现自己的家庭自动化系统,并将逐步进行每个步骤。 如果您是开发人员,请分叉存储库并参与改进。

设置 (The setup)

I estimate that it will take about 40 minutes to finish this whole setup plus any time spent online searching for solutions for installation errors.

我估计大约需要40分钟才能完成整个安装过程,并且花费任何时间在线搜索安装错误的解决方案。

需要什么? (What is needed?)

A Raspberry Pi is required. In my example I’m using a Raspberry Pi 3, but it should work with most versions. The components needed are:

需要Raspberry Pi。 在我的示例中,我使用的是Raspberry Pi 3,但它适用于大多数版本。 所需的组件是:

  1. Raspberry Pi board树莓派板
  2. MicroSD card (A class 10 with 16 GB or higher is recommended)MicroSD卡(建议使用16 GB或更高的Class 10)
  3. A USB MicroSD card reader or SD card adapterUSB MicroSD读卡器或SD卡适配器
  4. HDMI monitor and a USB keyboard (only required temporarily for the first boot of the Raspberry Pi)HDMI监视器和USB键盘(仅在Raspberry Pi首次启动时临时需要)
  5. Ethernet cable (not needed for Raspberry Pi 3 as it has built in WiFi)以太网电缆(Raspberry Pi 3内置WiFi不需要)

在Raspberry Pi上安装Raspbian OS (Installing Raspbian OS on the Raspberry Pi)

Raspbian is a free operating system based on Debian Linux, and it is optimized for Raspberry Pi.

Raspbian是基于Debian Linux的免费操作系统,并且已针对Raspberry Pi进行了优化。

I recommend the headless “LITE” version. It has no desktop environment or any graphical user interface, and it’s remotely accessible from a computer or device on the same network via SSH. We’re keeping things simple since this is the only way we are going to access the Raspberry Pi. The LITE version has all the functionality we’re looking for.

我推荐无头“ LITE”版本。 它没有桌面环境或任何图形用户界面,并且可以通过SSH从同一网络上的计算机或设备进行远程访问。 我们将事情保持简单,因为这是我们访问Raspberry Pi的唯一方法。 LITE版本具有我们正在寻找的所有功能。

  1. Download the latest Raspbian image from the official Raspberry Pi website.

    从Raspberry Pi官方网站下载最新的 Raspbian映像。

  2. Flash the Raspbian OS image to the SD card with Etcher or any other OS image burning software of your choice.

    使用Etcher或您选择的任何其他OS映像刻录软件将Raspbian OS映像闪存到SD卡。

设置Raspberry Pi (Setting up the Raspberry Pi)

To get the Raspberry Pi ready to boot we need to:

要使Raspberry Pi准备启动,我们需要:

  1. Insert the MicroSD card into the Raspberry Pi将MicroSD卡插入Raspberry Pi
  2. Connect the USB keyboard and the HDMI cable连接USB键盘和HDMI电缆
  3. Connect the Ethernet cable or if you have a Raspberry Pi 3 and want to use WiFi you should set up the network in the next section连接以太网电缆,或者如果您有Raspberry Pi 3并想使用WiFi,则应在下一部分中设置网络

When the Raspberry Pi has finished booting up, log in using username pi and password raspberry

Raspberry Pi完成启动后,使用用户名pi和密码raspberry登录

启用WiFi并连接到网络 (Enabling WiFi and connecting to the network)

Skip this step if you chose to connect with an Ethernet cable.

如果您选择使用以太网电缆连接,请跳过此步骤

  1. Open the “wpa-supplicant” configuration file打开“ wpa-supplicant”配置文件
$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

2. add the following at the bottom of the file while adding your wifi name and password:

2.在文件底部添加以下内容,同时添加您的wifi名称和密码:

network={
ssid="your_networks_name"   psk="your_networks_password"
}

3. Press Ctrl+X to save the code. Confirm with Y then Enter

3.Ctrl+X保存代码。 确认与Y然后Enter

4. Reboot the Raspberry Pi with the following command:

4.使用以下命令重新启动Raspberry Pi:

$ sudo reboot

启用S​​SH并更改用户名和密码 (Enabling SSH and changing the username and password)

Now that the Raspberry Pi is connected to the internet, it’s recommended to change the default password.

现在Raspberry Pi已连接到Internet,建议更改默认密码。

  1. Open the Raspberry Pi configuration tool and click the second option “Change User Password” and follow the instructions打开Raspberry Pi配置工具,然后单击第二个选项“更改用户密码”,然后按照说明进行操作
$ sudo raspi-config

2. Select option 5 “Interfacing Options” then activate SSH

2.选择选项5“接口选项”,然后激活SSH

3. Reboot the Raspberry Pi. When it’s up, you have SSH enabled and it’s ready to be accessed remotely from your desktop computer

3.重新启动Raspberry Pi。 启动时,您已启用SSH,并且可以从台式计算机远程访问它

$ sudo reboot

配置对Raspberry Pi的远程访问 (Configuring remote access to the Raspberry Pi)

Now, finally, the part when we install the required software on the Raspberry Pi. This part can be executed directly on the Pi through the terminal using a HDMI monitor and a USB keyboard. For convenience — and since we enable remote SSH connection — we’re going to connect from another desktop environment. This is the best and easiest way to remotely access and control the Pi whenever changes and configurations are needed.

现在,最后是我们在Raspberry Pi上安装所需软件的部分。 这部分可以使用HDMI监视器和USB键盘通过终端直接在Pi上执行。 为了方便起见,并且由于我们启用了远程SSH连接,因此我们将从另一个桌面环境进行连接。 每当需要更改和配置时,这是远程访问和控制Pi的最佳和最简便的方法。

So, basically, this is how you can access the command line interface of a Raspberry Pi remotely from another computer or any device on the same network using SSH. This can be done in two ways:

因此,基本上,这就是您可以使用SSH从另一台计算机或同一网络上的任何设备远程访问Raspberry Pi的命令行界面的方法。 这可以通过两种方式完成:

  1. Using the Command Prompt or PowerShell (I’m using Windows on a Desktop computer), replace with your username and IP address使用命令提示符或PowerShell(我在台式计算机上使用Windows),替换为您的用户名和IP地址
$ ssh username@ipaddress

If you do not know the IP address, type “hostname -I" in the Raspberry Pi command line.

如果您不知道 IP地址,请在Raspberry Pi命令行中键入“ hostname -I"

2. The second method is using a client program like PuTTY or any other functioning client SSH software. Here’s an easy guide for using PuTTY.

2.第二种方法是使用客户端程序,例如PuTTY或任何其他程序 客户端SSH软件。 这是使用PuTTY的简单指南

在Raspberry Pi上安装所需的软件 (Installing the required software on the Raspberry Pi)

Before installing anything, it’s recommended to update the Raspberry Pi’s operating system and packages. Doing this regularly will keep it up-to-date.

在安装任何东西之前,建议先更新Raspberry Pi的操作系统和软件包。 定期执行此操作可以使它保持最新状态。

  1. Update the system packages list using the following command:使用以下命令更新系统软件包列表:
$ sudo apt-get update

2. Upgrade all your installed packages to their latest version:

2.将所有已安装的软件包升级到最新版本:

$ sudo apt-get dist-upgrade

3. Download and install the latest version of Node.js:

3.下载并安装最新版本的Node.js:

// To download$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
// To install$ sudo apt-get install -y nodejs
// Check if the installation was successful:$ node -v

4. Install the Angular CLI globally:

4.全局安装Angular CLI:

$ npm install -g @angular/cli

5. Install the Git version-control system:

5.安装Git版本控制系统:

$ sudo apt-get install git

安装数据库(MongoDB) (Installing the database (MongoDB))

We need a database for storing the registered users and their credentials. Here are the required steps:

我们需要一个数据库来存储注册用户及其凭证。 这是必需的步骤:

  1. Install MongoDB安装MongoDB
$ sudo apt-get install mongodb

2. Start the MongoDB process

2.启动MongoDB进程

$ sudo service mongodb start

3. Start the mongo Shell

3.启动mongo Shell

$ mongo

3. Create a database called “smarthaus”

3.创建一个名为“ smarthaus”的数据库

$ use smarthaus

In MongoDB, default database is test. If you didn’t create any database, then collections will be stored in test database.

在MongoDB中,默认数据库为test。 如果您未创建任何数据库,则集合将存储在测试数据库中。

安装Smart Haus (Installing Smart Haus)

1. Check the current work directory using this command:

1.使用以下命令检查当前工作目录:

$ pwd
/* It will probably print "/home/pi"   where "pi" is the current user directory */

It’s recommended to clone the project’s repository under the pi’s user directory but you can navigate somewhere else if you’re sure.

建议在pi的用户目录下克隆项目的存储库,但如果确定的话,可以导航到其他地方。

2. Clone the repository from:

2.从以下位置克隆存储库:

$ git clone https://github.com/ameer157/smarthaus.git

Make sure to navigate inside the directory using:

确保使用以下内容浏览目录:

$ cd smarthaus

Before installing any npm packages using “npm install” please refer to the npm guide to fix permissions to learn how to fix any “EACCESS ”errors you might face during installation. This is very important since it will prevent any npm permission errors, and allows you to install packages globally without using sudo. Using sudo with npm is not recommended and should be avoided.

在使用“ npm install”安装任何npm软件包之前,请参考npm指南以修复权限,以了解如何解决在安装过程中可能遇到的任何“ EACCESS ”错误。 这非常重要,因为它将防止任何npm权限错误,并允许您在不使用sudo的情况下全局安装软件包。 不建议将sudo与npm一起使用, 应避免使用

3. Install all the required packages for the project:

3.安装项目所需的所有软件包:

$ npm install

启动Node.js服务器 (Starting the Node.js server)

Before starting the server we need to build the project using the Angular CLI tool. And lastly, we configure the Raspberry Pi so that it runs the server whenever it boots up.

在启动服务器之前,我们需要使用Angular CLI工具构建项目。 最后,我们配置Raspberry Pi,使其在启动时运行服务器。

  1. Build the project using:使用以下命令构建项目:
$ ng build --prod

2. Edit the rc.local file using nano:

2.使用nano编辑rc.local文件:

$ sudo nano /etc/rc.local

3. Add the following on the line before exit 0 then exit and save the file:

3.exit 0之前的行中添加以下内容,然后退出并保存文件:

su pi -c 'cd /home/pi/smarthaus/backend && sudo node server.js > log.txt &'

The Node.js server is now ready! It will run on every system boot up and save logs in under the same directory in a “log.txt” file.

Node.js服务器现在准备就绪! 它将在每次系统启动时运行,并将日志保存在同一目录下的“ log.txt”文件中。

Let’s run it now and see if it works using this command:

现在运行它,并使用以下命令查看它是否有效:

$ sudo node server

The system in now accessible from any device on your network via the Raspberry Pi’s IP address.

现在可以通过Raspberry Pi的IP地址从网络上的任何设备访问该系统。

Please go ahead and fork this project and get involved in developing the missing parts ?

请着手这个项目,并涉足发展中缺少的部分?

结束 (The end)

We got ourselves a working home automation system running safely on a Raspberry Pi in our local network without the use of “the cloud” or somebody else’s server.

我们获得了一个可以正常工作的家庭自动化系统,该系统可以在本地网络中的Raspberry Pi上安全运行,而无需使用“云”或其他人的服务器。

My Raspberry Pi sitting next to my Fingbox and router in the living room ?

我的Raspberry Pi坐在客厅的Fingbox和路由器旁边吗?

I hope you enjoyed reading,Please follow and share for more tech stuff ??

希望您喜欢阅读,请关注分享更多的技术资料?

翻译自: https://www.freecodecamp.org/news/the-most-robust-and-secure-home-automation-system-6d0ddbb39f29/

本地构建和自动化构建

本地构建和自动化构建_如何构建最强大,最安全的家庭自动化系统相关推荐

  1. 构建嵌入式linux系统_用于构建嵌入式Linux系统的4种工具

    构建嵌入式linux系统 Linux正在被部署到比Linus Torvalds在他的宿舍里工作的设备更多的设备中. 受支持的各种芯片架构令人震惊,并导致各种大小的设备都使用Linux. 从庞大的IBM ...

  2. 构建meteor应用程序_我构建了一个渐进式Web应用程序并将其发布在3个应用程序商店中。 这是我学到的。...

    构建meteor应用程序 by JudahGabriel Himango 犹大(Gabriel Himango) 我构建了一个渐进式Web应用程序并将其发布在3个应用程序商店中. 这是我学到的. (I ...

  3. 如何构建顶部导航条_如何构建导航栏

    如何构建顶部导航条 导航栏 (Navigation Bars) Navigation bars are a very important element to any website. They pr ...

  4. 构建maven项目插件_如何构建一个Maven插件

    构建maven项目插件 使用Okta的身份管理平台轻松部署您的应用程序 使用Okta的API在几分钟之内即可对任何应用程序中的用户进行身份验证,管理和保护. 今天尝试Okta. 由于其插件生态系统的普 ...

  5. 软件构建发布自动化[摘抄]

    ******************************************************************************************** Softwar ...

  6. 本地构建和自动化构建_构建自动化面板

    本地构建和自动化构建 上周二,我作为持续讨论(#c9d9)的一部分,参加了一个关于Build Automation主题的在线讨论会,这是一系列有关敏捷,持续交付和DevOps的社区讨论会. 自动化构建 ...

  7. jenkins修改pom文件_自动化部署—Jenkins+Docker构建工程

    ================= 交作业:hello-world级的自动化部署(Maven+Git+Docker+Jenkins),因为内容稍微有点儿多,分两篇,这是第二篇--构建工程. ===== ...

  8. Gulp构建前端自动化工作流

    ## Gulp构建前端自动化工作流 ### Gulp构建前端自动化工作流之:常用插件介绍及使用 在对Gulp有了一个初步的了解之后,我们开始构建一个较为完整的Gulp开发环境. 本文主要分为6个段落: ...

  9. java 构建是什么意思_构建的概念

    [Maven 简介] Maven 是 Apache 软件基金会组织维护的一款自动化构建工具,专注服务于 Java 平台的项目构建和依赖管理.Maven 这个单词的本意是:专家,内行. Make-> ...

最新文章

  1. PaddleHub 视频人脸检测
  2. Spring MVC与表单日期提交的问题
  3. python点击屏幕_Python Appium 滑动、点击等
  4. C++:19---重载与模板、模板特例化
  5. 更正日期(洛谷P5690题题解,Java语言描述)
  6. C#LeetCode刷题之#83-删除排序链表中的重复元素(Remove Duplicates from Sorted List)
  7. access vba 用recordset读取表中数据的简单方法
  8. c语言:猴子吃桃问题
  9. ubuntu linux网关不通,Ubuntu 8.04不能上网等问题的解决
  10. ios 图片合成 处理合成模糊 水印 模板图片合成
  11. 深圳dotnet俱乐部2006年1月7日WebPart交流活动
  12. 创建触发器报错_Oracle行级触发器的使用
  13. Netty自带的心跳机制——IdleStateHandler
  14. linux扫描仪如何使用方法,怎样在linux下设置和使用扫描仪.doc
  15. djyvp计算机电缆参数,DJYVP计算机电缆2x2x1.5型号规格含义
  16. ZigBee技术[转]
  17. 每秒50W笔交易,阿里双十一,架构如何优化到极致!
  18. 如果你是我眼中一滴泪,那么我永远不会哭
  19. Obsidian 插件(一):DataView 的使用
  20. 火狐浏览器如何导入和导出书签收藏夹

热门文章

  1. 第一节:ASP.NET开发环境配置
  2. Linux记录-TCP状态以及(TIME_WAIT/CLOSE_WAIT)分析(转载)
  3. DBCP连接池配置常用参数说明
  4. 加密算法—MD5、RSA、DES
  5. UVA 10600 ACM Contest and Blackout (次小生成树)
  6. 快速排序——算法系列
  7. 【转】ORACLE中的子查询 ---OCP--047--46
  8. Python docs
  9. 在windows storage server 2008上创建iscsi磁盘
  10. caffe路径正确,却读不到图片