野火科技
直播中

范成功

12年用户 127经验值
擅长:嵌入式技术
私信 关注
[经验]

【鲁班猫0W】搭建一个LoraWAN集成网关

简介

硬件

LoraWAN网关硬件上需要一个网关模组,比如SX1301、SX1302等,我使用的是SX1302,某鱼入手还是挺便宜的还带树莓派转接板,到手后就直接可以用了,连接图如下:
672c2c9d66bbb034343eef9329ea65a.jpg

开发板和模块的通讯

SPI -- 与SX1302通讯,使用的是SPI3.0;
I2C -- 与温度传感器通讯,使用的是I2C-3;
串口 -- 接收GPS数据,使用的是ttyS8;
还有需要一个GPIO口,接到了板子的83引脚;

涉及到的系统配置

修改/boot/uEnv/uEnvLubanCatZW.txt
下面是设备树插件部分的内容,开启了一下相关的硬件:

#network
dtoverlay=/dtb/overlay/rk356x-lubancat-gmac1-disabled-overlay.dtbo
#40pin
dtoverlay=/dtb/overlay/rk356x-lubancat-i2c3-m0-overlay.dtbo
dtoverlay=/dtb/overlay/rk3566-lubancat-0-spi3-m1-gpio-cs-overlay.dtbo
dtoverlay=/dtb/overlay/rk356x-lubancat-spi3-m1-overlay.dtbo
dtoverlay=/dtb/overlay/rk356x-lubancat-uart8-m0-overlay.dtbo

软件

软件上需要用到SX1302的hal库,chirpstack的bridge软件包和chirpstack服务器软件包。

安装依赖

  1. 通过以下命令安装mosquitto

    
    
  2. 通过以下命令安装PostgreSQL配置
    安装:

    sudo apt install postgresql
    

    配置:

    sudo -u postgres psql
    
    create role chirpstack with login password 'chirpstack';
    create database chirpstack with owner chirpstack;
    \c chirpstack
    create extension pg_trgm;
    \q
    

    上面的账号密码及数据库名字都可以自定义,在下面配置chirpstack时保持一致就行。

  3. 通过以下命令安装redis-server

    sudo apt install redis-server
    
  4. 安装emqx
    可以选择docker部署或者直接安装emqx服务。
    直接安装参考Ubuntu 安装 EMQX 5.0 文档
    docker部署参考Docker 部署 EMQX 5.0 文档

sx1302_hal

可以在github上面下载到:
地址是:https://github.com/Lora-net/sx1302_hal.git
使用的是release版本,V2.1.0
里面需要修改的文件有:

// libloragw/inc/loragw_i2c.h
// 将I2C_DEVICE中的i2c-0改为i2c-3
#define I2C_DEVICE          "/dev/i2c-3"

复制tools/reset_lgw.sh到sx1302的根目录
修改如下

SX1302_RESET_PIN=83 #23     # SX1302 reset
SX1302_POWER_EN_PIN=18  # SX1302 power enable
SX1261_RESET_PIN=22     # SX1261 reset (LBT / Spectral Scan)
AD5338R_RESET_PIN=13    # AD5338R reset (full-duplex CN490 reference design)

WAIT_GPIO() {
    sleep 0.1
}

init() {
    # setup GPIOs
    echo "$SX1302_RESET_PIN" > /sys/class/gpio/export; WAIT_GPIO
    #echo "$SX1261_RESET_PIN" > /sys/class/gpio/export; WAIT_GPIO
    #echo "$SX1302_POWER_EN_PIN" > /sys/class/gpio/export; WAIT_GPIO
    #echo "$AD5338R_RESET_PIN" > /sys/class/gpio/export; WAIT_GPIO

    # set GPIOs as output
    echo "out" > /sys/class/gpio/gpio$SX1302_RESET_PIN/direction; WAIT_GPIO
    #echo "out" > /sys/class/gpio/gpio$SX1261_RESET_PIN/direction; WAIT_GPIO
    #echo "out" > /sys/class/gpio/gpio$SX1302_POWER_EN_PIN/direction; WAIT_GPIO
    #echo "out" > /sys/class/gpio/gpio$AD5338R_RESET_PIN/direction; WAIT_GPIO
}

reset() {
    echo "CoreCell reset through GPIO$SX1302_RESET_PIN..."
    #echo "SX1261 reset through GPIO$SX1302_RESET_PIN..."
    #echo "CoreCell power enable through GPIO$SX1302_POWER_EN_PIN..."
    #echo "CoreCell ADC reset through GPIO$AD5338R_RESET_PIN..."

    # write output for SX1302 CoreCell power_enable and reset
    #echo "1" > /sys/class/gpio/gpio$SX1302_POWER_EN_PIN/value; WAIT_GPIO

    echo "1" > /sys/class/gpio/gpio$SX1302_RESET_PIN/value; WAIT_GPIO
    echo "0" > /sys/class/gpio/gpio$SX1302_RESET_PIN/value; WAIT_GPIO

    #echo "0" > /sys/class/gpio/gpio$SX1261_RESET_PIN/value; WAIT_GPIO
    #echo "1" > /sys/class/gpio/gpio$SX1261_RESET_PIN/value; WAIT_GPIO

    #echo "0" > /sys/class/gpio/gpio$AD5338R_RESET_PIN/value; WAIT_GPIO
    #echo "1" > /sys/class/gpio/gpio$AD5338R_RESET_PIN/value; WAIT_GPIO
}
# 再往下的代码不变

复制packet_forwarder下的global_conf.json.sx1250.CN490到sx1302根目录,然后重命名为global_conf.json。修改为如下:

{
    "SX130x_conf": {
        "com_type": "SPI",
        "com_path": "/dev/spidev3.0",
        "lorawan_public": true,
        "clksrc": 0,
        "antenna_gain": 0, /* antenna gain, in dBi */
        "full_duplex": false,
        "fine_timestamp": {
            "enable": false,
            "mode": "all_sf" /* high_capacity or all_sf */
        },
        "radio_0": {
            "enable": true,
            "type": "SX1250",
            "single_input_mode": true,
            "freq": 470600000,
            "rssi_offset": -207.0,
            "rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0},
            "tx_enable": true,
            "tx_freq_min": 500000000,
            "tx_freq_max": 510000000,
            "tx_gain_lut":[
                {"rf_power": -6, "pa_gain": 0, "pwr_idx":  0},
                {"rf_power": -3, "pa_gain": 0, "pwr_idx":  1},
                {"rf_power":  0, "pa_gain": 0, "pwr_idx":  2},
                {"rf_power":  3, "pa_gain": 1, "pwr_idx":  3},
                {"rf_power":  6, "pa_gain": 1, "pwr_idx":  4},
                {"rf_power": 10, "pa_gain": 1, "pwr_idx":  5},
                {"rf_power": 11, "pa_gain": 1, "pwr_idx":  6},
                {"rf_power": 12, "pa_gain": 1, "pwr_idx":  7},
                {"rf_power": 13, "pa_gain": 1, "pwr_idx":  8},
                {"rf_power": 14, "pa_gain": 1, "pwr_idx":  9},
                {"rf_power": 16, "pa_gain": 1, "pwr_idx": 10},
                {"rf_power": 20, "pa_gain": 1, "pwr_idx": 11},
                {"rf_power": 23, "pa_gain": 1, "pwr_idx": 12},
                {"rf_power": 25, "pa_gain": 1, "pwr_idx": 13},
                {"rf_power": 26, "pa_gain": 1, "pwr_idx": 14},
                {"rf_power": 27, "pa_gain": 1, "pwr_idx": 15}
            ]
        },
        "radio_1": {
            "enable": true,
            "type": "SX1250",
            "single_input_mode": true,
            "freq": 471400000,
            "rssi_offset": -207.0,
            "rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0},
            "tx_enable": false
        },
        "chan_multiSF_All": {"spreading_factor_enable": [ 5, 6, 7, 8, 9, 10, 11, 12 ]},
        "chan_multiSF_0": {"enable": true, "radio": 0, "if": -300000},
        "chan_multiSF_1": {"enable": true, "radio": 0, "if": -100000},
        "chan_multiSF_2": {"enable": true, "radio": 0, "if":  100000},
        "chan_multiSF_3": {"enable": true, "radio": 0, "if":  300000},
        "chan_multiSF_4": {"enable": true, "radio": 1, "if": -300000},
        "chan_multiSF_5": {"enable": true, "radio": 1, "if": -100000},
        "chan_multiSF_6": {"enable": true, "radio": 1, "if":  100000},
        "chan_multiSF_7": {"enable": true, "radio": 1, "if":  300000},
        "chan_Lora_std":  {"enable": true, "radio": 1, "if": -200000, "bandwidth": 250000, "spread_factor": 7,
                           "implicit_hdr": false, "implicit_payload_length": 17, "implicit_crc_en": false, "implicit_coderate": 1},
        "chan_FSK":       {"enable": true, "radio": 1, "if":  300000, "bandwidth": 125000, "datarate": 50000}
    },

    "gateway_conf": {
        "gateway_ID": "AA555A0000000000",
        /* change with default server address/ports */
        "server_address": "localhost",
        "serv_port_up": 1700,
        "serv_port_down": 1700,
        /* adjust the following parameters for your network */
        "keepalive_interval": 10,
        "stat_interval": 30,
        "push_timeout_ms": 100,
        /* forward only valid packets */
        "forward_crc_valid": true,
        "forward_crc_error": false,
        "forward_crc_disabled": false,
        /* GPS configuration */
        "gps_tty_path": "/dev/ttyS8",
        /* GPS reference coordinates */
        "ref_latitude": 0.0,
        "ref_longitude": 0.0,
        "ref_altitude": 0,
        /* Beaconing parameters */
        "beacon_period": 0,
        "beacon_freq_hz": 869525000,
        "beacon_datarate": 9,
        "beacon_bw_hz": 125000,
        "beacon_power": 14,
        "beacon_infodesc": 0
    },

    "debug_conf": {
        "ref_payload":[
            {"id": "0xCAFE1234"},
            {"id": "0xCAFE2345"}
        ],
        "log_file": "loragw_hal.log"
    }
}

最后通过下面命令启动:

sudo ./packet_forwarder/lora_pkt_fwd -c global_conf.json

chirpstack_gateway_bridge

可以去chirpstack官网下载,地址如下:
chirpstack-gateway-bridge下载地址
下载deb安装包进行安装,安装完成后修改配置文件:/etc/chirpstack-gateway-bridge/chirpstack-gateway-bridge.toml,内容如下:

marshaler="json"

主要为了调试的时候可以更直观的看到数据,也可以不改,只要与后面的chirpstack配置对应就行。

通过一下命令启动

sudo systemctl start chirpstack-gateway-bridge

chirpstack

也在chirpstack官网下载,地址如下:
ChirpStack下载地址

复制配置文件,从/etc/chirpstack中的chirpstack.toml和region_cn470_0.toml复制到~/.config/chirpstack(没有目录就创建),修改配置文件:
chirpstack.toml:

#以上内容不变
# Network related configuration.
[network]

  # Network identifier (NetID, 3 bytes) encoded as HEX (e.g. 010203).
  net_id="000000"

  # Enabled regions.
  #
  # Multiple regions can be enabled simultaneously. Each region must match
  # the 'name' parameter of the region configuration in '[[regions]]'.
  enabled_regions=[
    "cn470_0",
  ]

# API interface configuration.
[api]

  # interface:port to bind the API interface to.
  bind="0.0.0.0:28080"

  # Secret.
  #
  # This secret is used for generating login and API tokens, make sure this
  # is never exposed. Changing this secret will invalidate all login and API
  # tokens. The following command can be used to generate a random secret:
  #   openssl rand -base64 32
  secret="you-must-replace-this"


[integration]
  enabled=["mqtt"]

  [integration.mqtt]
    server="tcp://localhost:1883/"
    json=true

region_cn470_0.toml:

# 以上内容不变
# MQTT configuration.
      [regions.gateway.backend.mqtt]

        # Topic prefix.
        #
        # The topic prefix can be used to define the region of the gateway.
        # Note, there is no need to add a trailing '/' to the prefix. The trailing
        # '/' is automatically added to the prefix if it is configured.
        #topic_prefix= "cn470_0"
        event_topic="gateway/+/event/+"
        command_topic="gateway/{{gateway_id}}/command/{{command}}"

        # 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:

chirpstack -c ~/.config/chirpstack

至此,安装部署已经完成,接下去是通过控制面板来添加、配置lorawan。请参考下一节吧。

更多回帖

发帖
×
20
完善资料,
赚取积分