Lora服务器:Chirpstack连接Lora网关实战

Lora服务器:Chirpstack连接Lora网关实战

Chirpstack:一个开源的Lora服务项目,该项目包含Gateway Bridge,Network Server,Application Server,Gateway os等子项目,官网地址:https://www.chirpstack.io/

服务器环境:CentOS Linux release 7.8.2003 (Core)。

一,安装mosquitto。mosauitto是一个开源的MQTT消息代理软件。这个项目中,MQTT承担了各个组件之间互相通信的重任,贯穿了从网桥到AS的整个流程。

1,添加EPEL软件包安装源:yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2,安装mosquitto:yum install mosquitto

3, 配置mosquitto

1

2

3

4

5

6

7

8

9

10

11

12

13

#创建密码文件

touch /etc/mosquitto/pwfile

#创建策略文件

touch /etc/mosquitto/aclfile

#创建日志目录

mkdir /var/log/mosquitto

#创建日志文件

touch /var/log/mosquitto/mosquitto.log

#创建本地持久化文件目录

mkdir /var/lib/mosquitto

#创建chrip用户

mosquitto_passwd /etc/mosquitto/pwfile chrip

#输入chrip用户的密码

 vi /etc/mosquitto/mosquitto.conf

#持久化配置
persistence true
persistence_location /var/lib/mosquitto/
#日志文件配置
log_dest file /var/log/mosquitto/mosquitto.log
#用户名密码认证配置
allow_anonymous false
password_file /etc/mosquitto/pwfile
acl_file /etc/mosquitto/aclfile

   配置用户策略:vi /etc/mosquitto/aclfile

1

2

user chirp  //用户chirp

topic chirpstack/#   //可对主提chirpstack读写

   测试: mosquitto_sub -t chirpstack/# -u chirp -P 123456

   日志查看:tail -f /var/log/mosquitto/mosquitto.log

二,安装gateway-bridge

1,新建一个目录,下载redhat安装包到该目录  wget https://artifacts.chirpstack.io/downloads/chirpstack-gateway-bridge/chirpstack-gateway-bridge_3.9.2_linux_386.rpm 

 2,安装rpm:rpm -ivh chirpstack-gateway-bridge_3.9.2_linux_386.rpm

3,为getway-bridge创建一个mqtt用户及密码:mosquitto_passwd chirp_gateway,并为该用户配置主题为“gateway/#”的读写策略.配置后重启Mosquitto

4,配置MQTT认证信息: vi /etc/chirpstack-gateway-bridge/chirpstack-gateway-bridge.toml

#配置MQTT payloads采用的协议,默认为protobuf
marshaler="json" 
#配置MQTT 用户名
username="chirp_gateway"
password="123456"
#如果需要更改udp的监听端口可以改写udp_bind值,默认1700

--我的环境实际内容如下

# Integration configuration.
[integration]
# Payload marshaler.
#
# This defines how the MQTT payloads are encoded. Valid options are:
# * protobuf:  Protobuf encoding
# * json:      JSON encoding (easier for debugging, but less compact than 'protobuf')
marshaler="protobuf"

  # MQTT integration configuration.
  [integration.mqtt]
  # Event topic template.
  event_topic_template="gateway/{{ .GatewayID }}/event/{{ .EventType }}"

  # State topic template.
  #
  # States are sent by the gateway as retained MQTT messages (by default)
  # so that the last message will be stored by the MQTT broker. When set to
  # a blank string, this feature will be disabled. This feature is only
  # supported when using the generic authentication type.
  state_topic_template="gateway/{{ .GatewayID }}/state/{{ .StateType }}"

  # Command topic template.
  command_topic_template="gateway/{{ .GatewayID }}/command/#"

。。。。

    # Generic MQTT authentication.
    [integration.mqtt.auth.generic]
    # MQTT servers.
    #
    # Configure one or multiple MQTT server to connect to. Each item must be in
    # the following format: scheme://host:port where scheme is tcp, ssl or ws.
    servers=[
      "tcp://127.0.0.1:1883",
    ]

    # Connect with the given username (optional)
    username=""

    # Connect with the given password (optional)
    password=""

 5, 启动网桥: sudo systemctl start chirpstack-gateway-bridge。查看日志:journalctl -f -n 100 -u chirpstack-gateway-bridge

三,安装配置redis

1,yum install redis

2, vi /etc/redis.conf。找到requirepass,去掉注释并设置新的密码:requirepass redis123456

3,启动redis:systemctl start redis

4,验证密码:redis-cli -h 127.0.0.1 -p 6379 -a redis123456

四,安装配置postgresql

1,rpm源: yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm。安装源见:https://yum.postgresql.org/repopackages/

2,安装:yum install postgresql95-server postgresql95-contrib

3,初始化数据库:/usr/pgsql-9.5/bin/postgresql95-setup initdb  

4,修改postgres用户密码: passwd postgres

5,修改postgresql监听地址:vi /var/lib/pgsql/9.5/data/postgresql.conf。修改listen_addresses值为“*”

6,修改postgresql认证方式:vi /var/lib/pgsql/9.5/data/pg_hba.conf。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     postgres                                peer
host    replication     postgres        127.0.0.1/32            ident
host    replication     postgres        ::1/128                 ident

5,启动:systemctl enable postgresql-9.5.service     systemctl start postgresql-9.5.service

五,安装network-server

补充一下NS的功能,NS功能很多,

Network Server component is the de-duplication of received LoRaWAN frames by the LoRa® gateways(去重复是很重要的功能) and for the collected frames handle the:

  • Authentication--MAC层密钥在这
  • LoRaWAN mac-layer (and mac-commands)--很复杂
  • Communication with the ChirpStack Application Server--不是MQTT?
  • Scheduling of downlink frames--ACK帧、数据帧、命令帧

分解如下特性更容易理解

1,添加mqtt账户:mosquitto_passwd /etc/mosquitto/pwfile chirp_network。并配置主题:“gateway/#” 的读写权限。配置后重启Mosquitto

2,添加postgresql账户及数据库

     1),进入postgres用户:su postgres,输入psql指令

     2),创建角色及密码:create role chirpstack_ns with login password 'dbpassword';

     3),创建数据库:create database chirpstack_ns with owner chirpstack_ns;

     4),测试:psql -h localhost -U chirpstack_ns -W chirpstack_ns。输入密码。

3,下载chirpstack-network-server:wget https://artifacts.chirpstack.io/downloads/chirpstack-network-server/chirpstack-network-server_3.10.0_linux_386.rpm

4,安装rpm:rpm -ivh chirpstack-network-server_3.10.0_linux_386.rpm

5,配置:vi /etc/chirpstack-network-server/chirpstack-network-server.toml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#postgresql连接字符串

[postgresql]

dsn="postgres://chirpstack_ns:dbpassword@localhost/chirpstack_ns?sslmode=disable"

#redis连接字符串

[redis]

url="redis://requirepass:redis123456@localhost:6379"

#中国lora频点设置

[network_server]

 name="CN470"

#注释掉[[network_server.network_settings.extra_channels]]节点

# [[network_server.network_settings.extra_channels]]

   # frequency=867100000

   # min_dr=0

   # max_dr=5

#mqtt设置

[network_server.gateway.backend]

username="chirp_network"

password="123456"

6,启动network-server:systemctl start chirpstack-network-server。查看日志:journalctl -u chirpstack-network-server -f -n 50

--我的环境实际如下:

  [network_server.gateway.backend]
    # Backend
    #
    # This defines the backend to use for the communication with the gateways.
    # Use the section name of one of the following gateway backends.
    # Valid options are:
    #  * mqtt
    #  * amqp
    #  * gcp_pub_sub
    #  * azure_iot_hub
    type="mqtt"

    # MQTT gateway backend settings.
    # Event topic template.
    event_topic="gateway/+/event/+"

    # Command topic template.---这些topic要跟上面的bridge对应上
    #
    # Use:
    #   * "{{ .GatewayID }}" as an substitution for the LoRa gateway ID
    #   * "{{ .CommandType }}" as an substitution for the command type
    command_topic_template="gateway/{{ .GatewayID }}/command/{{ .CommandType }}"

    # MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
    server="tcp://localhost:1883"

    # Connect with the given username (optional)
    username=""

    # Connect with the given password (optional)
    password=""

六,安装Chirpstack-Application-server

补充一下ChirpStack Application Server功能

(1)Payload encryption / decryption

(2)web-interface (built on top of the provided RESTful API).

(3)User authorization--多组织、多用户管理

(4)Payloads and device events--provides different ways of sending and receiving device payloads (e.g. MQTT, HTTP, InfluxDB, ...)---解密之后的数据

(5)Gateway discovery--By sending out periodical "pings" through each gateway,

(6)Live frame-logging---you are able to inspect all raw and encrypted LoRaWAN frames per gateway or device. When opening the LoRaWAN frames tab on the gateway or device detail page, you will see all frames passing in realtime. 加密数据

(7)Live event-logging--you are able to inspect all events from the web-interface, without the need to use a MQTT client or build an integration. When opening the Live event logs tab on the device detail pace, you will see all uplink, ack, join and error events in realtime.--事件,不涉及数据

1,新建Mosquitto用户和策略:mosquitto_passwd /etc/mosquitto/pwfile chrip_application。并配置主题"gateway/#"的读写权限。配置后重启Mosquitto

2,添加postgresql账户及数据库

     1),进入postgres用户:su postgres,输入psql指令

     2),创建角色及密码:create role chirpstack_as with login password 'dbpassword';

     3),创建数据库:create database chirpstack_as with owner chirpstack_as;

     4)开启  trigram 和 hstore

           \c chirpstack_as

           create extension pg_trgm;

           create extension hstore;

     4),测试:psql -h localhost -U chirpstack_as -W chirpstack_as。输入密码。

3,下载application-server:wget https://artifacts.chirpstack.io/downloads/chirpstack-application-server/chirpstack-application-server_3.12.2_linux_386.rpm

4,安装:rpm -ivh chirpstack-application-server_3.12.2_linux_386.rpm

5,利用openssl生成一个密钥:openssl rand -base64 32

6,配置:

#postgresql连接字符串
[postgresql]
dsn="postgres:/chirpstack_as:dbpassword@localhost/chirpstack_as?sslmode=disable"
#redis连接字符串
[redis]
url="redis://requirepass:redis123456@localhost:6379"

 7,启动:systemctl start chirpstack-application-server。 查看日志:journalctl -f -n 100 -u chirpstack-application-server

七,开放端口

开放 UDP 1700 ,TCP 8080,1883

firewall-cmd --zone=public --add-port=1700/udp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --reload

 打开applicationServer管理页面,默认用户和密码为admin

八 配置application server

打开管理界面,登录

1,添加services:

2,添加网关配置

 3,配置网关,我手里有一个躬远的Lorawan网关。配置Lora服务器

 在application server添加该网关。

 查看gateway-bridge日志。可以看到已经可以收到网关的udp包,并发布到Mqtt:journalctl -u chirpstack-gateway-bridge -f -n 50

 application server上该网关已经是active了。

 九:添加设备。我手中有安志博的lora倾斜传感器终端,采用abp入网方式。按说明书激活后在application平台添加该设备

1,添加设备配置文件

 2,添加应用域

 3,在该应用域下添加设备

 4,配置设备的入网参数。该入网参数可以由设备终端提供,也可以自己定好后发给厂家由厂家出厂时设置好。

ABP入网主要有三个参数:1,Device address 2,Network session key,3,Application session key

 5,触发该lora终端,applcation平台设备上线

 十,从mqtt获取设备的上行数据

利用Mosquitto_sub工具订阅application/#主题:mosquitto_sub -t application/# -u chirp_application -P 123456 。可以新建一个Mosquitto用户和策略,也可以直接用之前部署Application-server时的用户

 红框中的数据即为设备所上传的数据,需要用base64解密后转为16进制。

十一,下发数据到设备

可以通过http接口或者mqtt发布二种方式,由于篇幅关系就不一一介绍。参考管网教程:

mqtt方式:https://www.chirpstack.io/application-server/integrations/mqtt/

http方式:https://www.chirpstack.io/application-server/api/http-examples/

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
E106 LoRa网关代码是用于管理和控制E106型号的LoRa网关设备的程序代码。这些代码负责与物联网设备通信并传输数据。在编写代码之前,需要了解以下几个关键概念和功能: 1. LoRa技术:LoRa是一种低功耗的长距离无线通信技术,适用于物联网应用。LoRa通过使用长距离、低速率和低功耗的调制方式,实现了在室内和室外环境下的长距离通信。 2. 网关网关连接物联网设备和云服务器的中间设备,负责将物联网设备发送的数据传输给云服务器,并将云服务器的命令传输给物联网设备。 3. E106:E106是一种常见的LoRa网关设备,具有低功耗、高性能和可靠性。它支持多种通信方式,如Wi-Fi和以太网,可以连接到云服务器,并与物联网设备进行双向通信。 在编写E106 LoRa网关代码时,需要实现以下功能: 1. 网络连接:代码需要确保LoRa网关与云服务器的网络连接可用。可以使用Wi-Fi或以太网连接来实现网络通信。 2. 数据接收:代码需要接收来自物联网设备的数据。E106 LoRa网关可以同时接收多个物联网设备发送的LoRa数据包。 3. 数据传输:代码需要将接收到的数据传输给云服务器。可以使用MQTT或HTTP等通信协议来实现数据传输。 4. 数据解析:代码需要解析接收到的数据,并将其格式化为可读性强的格式。可以使用JSON或其他数据格式来实现数据的解析和处理。 5. 云服务器通信:代码需要处理来自云服务器的命令,并将其传输给物联网设备。可以使用MQTT或HTTP等通信协议来实现与云服务器的通信。 总之,E106 LoRa网关代码是为了实现LoRa网关与物联网设备和云服务器之间的通信和数据传输而编写的程序代码。这些代码可以确保LoRa网关设备的正常运行,并可靠地将数据从物联网设备传输到云服务器,同时也能接收来自云服务器的命令,并将其传输给物联网设备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值