树莓派3B+基于domoticz搭建智能家居服务器之记录

主要是在无界面树莓派raspberry系统上部署【宝塔面板】、【frp内网穿透】、【domoticz智能家居系统】、【DSP8266】、【Arduino】、【DHT11温湿度传感器】、【手机domoticz客户端显示数据及控制】

*******************now 20201031 16:22**************************

一、树莓派写入新系统

1、这个页面是描述《树莓派各类操作系统大全》,下载32 位 Lite 版(无桌面),考虑是服务器应用。(官网下载,国内慢的很,我传到了天翼云盘https://cloud.189.cn/t/zYFzyq3uIBv2

https://shumeipai.nxez.com/raspberry-pi-os-collection

2、下载对应Raspberry Pi Imager,用来烧录下载好的系统文件(官网下载,国内慢的很,我传到了天翼云盘https://cloud.189.cn/t/nMzMru73yMnq

https://www.raspberrypi.org/downloads/

3、打开boot文件夹,新增两个文件  wpa_supplicant.conf、ssh

wpa_supplicant.conf中写入以下内容

country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="你的WiFi名称"
    psk="你的WiFi密码"
    key_mgmt=WPA-PSK
    priority=1
}

ssh中不写内容直接建立一个无后缀名的文件支持ssh连接

  1. 打开路由器管理 查看树莓派IP地址
  2. 用ssh工具连接树莓派 (xshell、putty)都可以
  3. 默认用户名密码(raspbian系统)
    用户名:pi
    密码:raspberry

(参考:https://blog.csdn.net/thewaiting/article/details/105975582

【链接上了记得修改pi的默认密码sudo passwd pi】用用户pi进入系统之后,可以先修改root密码sudo passwd root,然后su root来切换成root用户

二、树莓派系统安装软件

1、修改apt-get源为国内(清华)

sudo apt-get install vim

修改软件源:(sudo vim /etc/apt/sources.list   sudo vim /etc/apt/sources.list.d/raspi.list)
 

# 编辑 `/etc/apt/sources.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi

# 编辑 `/etc/apt/sources.list.d/raspi.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui

 

再sudo apt-get update更新下源

2、安装宝塔面板5.9(https://blog.csdn.net/no1xium/article/details/107694406

wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && bash install.sh

å¨è¿éæå¥å¾çæè¿°

å¨è¿éæå¥å¾çæè¿°

å¨è¿éæå¥å¾çæè¿°

å¨è¿éæå¥å¾çæè¿°

如果忘记了登录密码也没事我们可以
cat /www/server/panel/default.pl

折腾完了想卸载也很方便
rm -f /etc/init.d/bt && rm -rf /www/server/panel

3、安装穿透frp (https://github.com/fatedier/frp/releases

安装这个版本的在树莓派上(客户端):frp_X.XX.X_linux_arm.tar.gz

解压:tar -zxfv 

进入目录之后编辑 sudo vim frpc.ini

[common]
server_addr = ***.网址.***
server_port = 服务器端口
[ssh]
type=tcp
local_ip=127.0.0.1
local_port=22
remote_port=远程ssh端口
[web]
type=http
local_ip=127.0.0.1
local_port=80
custom_domains=***.网址.***
[webpicam]这个是第二个网址
type=http
local_ip=127.0.0.1
local_port=***
custom_domains=***.网址.***
locations=/路径/
[domoticz]
type = http
custom_domains = ***.***.网址.***
local_ip = 192.168.*****
local_port = ****
use_compression = false
use_encryption = false

在宝塔面板中开启端口80,22,你需要的端口

设置frp开机启动(https://www.cnblogs.com/zinan/p/10943781.html

添加systemd配置文件:

vim /usr/lib/systemd/system/frp.service

文件内容如下:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit

[Install]
WantedBy=multi-user.target

ExecStart的内容请根据自己frp安装目录修改。

设置开机启动

systemctl daemon-reload
systemctl enable frp

启动 frp

systemctl start frp

查看frp是否启动

ps aux | grep frps

centos7 查看启动服务项

使用 systemctl list-unit-files 可以查看启动项 

左边是服务名称,右边是状态,enabled是开机启动,disabled是开机不启动

可以使用 systemctl status xxx.service查看服务状态

****宝塔面板域名解析配置,如果增加其他域名的话直接就在下面再加一个Server

server
{
    listen 80;
    server_name ***.网址.***;
  
  
location ~ .* {
              proxy_pass http://***.网址.***:端口;
              proxy_set_header Host $http_host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }

 
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-72.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/***.网址.***.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off; 
    }
    access_log  /www/wwwlogs/***.网址.***.log;
    error_log  /www/wwwlogs/***.网址.***.error.log;
}

4、树莓派安装Domoticz智能家居

最简单的方法安装Domoticz: sudo curl -L install.domoticz.cn | bash

安装完成了进入http://IP:8080,设置里面修改语言为中文,还需要添加位置信息,才能够保存。【网站保护】https://www.domoticz.com/forum/viewtopic.php?t=14383、【本地网络】必须添加

在设置-》硬件 中 添加硬件类型DUMMY,名称随意,在然后【创建虚拟传感器】,选择【Temp & Hum】

这时候我们再回到树莓派SSH进去,修改时区

更改时区 and Expand file system

raspi-config

登录后输入以下命令:

 sudo raspi-config
  • 选择选项以扩展root文件系统
  • 建议更改密码
  • 更改为你当地的时区,时区设置在"Internationalization Options"中(不同版本不一定)

然后回到温度界面可以看到传感器

然后是ESP8266链接到domoticz,官网wiki:https://www.domoticz.com/wiki/ESP8266_WiFi_module

结合DHT11在Arduino中的代码(需要知道domoticz中虚拟设备的idx,在设置-》设备中可以看到):

#include <SimpleDHT.h>
#include <ESP8266WiFi.h>

int pinDHT11 = 2; // GPIO2 of ESP8266
const char* ssid = "*****";//手机Wi-Fi热点
const char* password = "******";//手机Wi-Fi密码
WiFiClient client;
SimpleDHT11 dht11(pinDHT11);
// domoticz
 const char * domoticz_server = "***.***.网址.***"; //Domoticz port
 int port = ***; //Domoticz port
 int idx = 2; //IDX for this virtual sensor, found in Setup -> Devices

 float humidity = 0;
 float temp = 0;
 
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  setup_wifi();
}
void setup_wifi() {

  delay(10);
  WiFi.begin(ssid, password);     // 我们从连接到WiFi网络开始
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("WiFi connected");
}
void loop() {
  // put your main code here, to run repeatedly:
  printInfo();
  delay(60000);
温度湿度 
}

void printInfo()
 {
    // Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
温度湿度
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
    return;
  }
  Serial.println(String(temperature)+" - "+String(humidity));
    if (client.connect(domoticz_server,port)) {

        client.print("GET /json.htm?type=command&param=udevice&idx=");
        client.print(idx);
        client.print("&nvalue=0&svalue=");
        client.print(String(temperature));
        client.print(";");
        client.print(String(humidity));
        client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
        
        client.stop();
     }
  }

数据接收成功后,下载domoticz的手机客户端:https://www.domoticz.cn/forum/viewtopic.php?f=4&t=3&sid=11f057035d38b21ad6f695aa802c21ee

免费版:https://play.google.com/store/apps/deta ... s.domoticz
墙内下载(最新版20200619):https://www.jianguoyun.com/p/DUd26BYQh66pBhi0q7QD

高级版:https://play.google.com/store/apps/deta ... cz.premium

高级版支持 地理围栏、Android Wear 手表、桌面小工具,体验后我表示没什么用处。。。有能力的可以买,支持下作者。

客户端时候需要设置远程domoticz服务器:其实就是在frp中配置的domoticz的dashboard地址

*******************now 20201101 19:32**************************

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值