下载ChirpStack

master分支commit,版本为24ce6fa78e7b757bcb128ff36e5ef67cf579e6c6
下载Chiprstack代码,并切换为master版本

git clone https://github.com/brocaar/chirpstack-docker.git
cd chirpstack-docker
git checkout 24ce6fa78e7b757bcb128ff36e5ef67cf579e6c6

添加CN470频段

diff --git a/configuration/chirpstack/chirpstack.toml b/configuration/chirpstack/chirpstack.toml
index 54c3e5d..399f171 100644
--- a/configuration/chirpstack/chirpstack.toml
+++ b/configuration/chirpstack/chirpstack.toml
@@ -70,7 +70,18 @@
     "as923_3",
     "as923_4",
     "au915_0",
     "cn470_10",
+    "cn470_11",
     "cn779",
     "eu433",
     "eu868",

配置频段为CN470_10

diff --git a/docker-compose.yml b/docker-compose.yml
index f15a6f7..49a643d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -27,16 +27,16 @@ services:
     volumes:
       - ./configuration/chirpstack-gateway-bridge:/etc/chirpstack-gateway-bridge
     environment:
-      - INTEGRATION__MQTT__EVENT_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/event/{{ .EventType }}
-      - INTEGRATION__MQTT__STATE_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/state/{{ .StateType }}
-      - INTEGRATION__MQTT__COMMAND_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/command/#
+      - INTEGRATION__MQTT__EVENT_TOPIC_TEMPLATE=cn470_10/gateway/{{ .GatewayID }}/event/{{ .EventType }}
+      - INTEGRATION__MQTT__STATE_TOPIC_TEMPLATE=cn470_10/gateway/{{ .GatewayID }}/state/{{ .StateType }}
+      - INTEGRATION__MQTT__COMMAND_TOPIC_TEMPLATE=cn470_10/gateway/{{ .GatewayID }}/command/#
     depends_on:
       - mosquitto

   chirpstack-gateway-bridge-basicstation:
     image: chirpstack/chirpstack-gateway-bridge:4
     restart: unless-stopped
-    command: -c /etc/chirpstack-gateway-bridge/chirpstack-gateway-bridge-basicstation-eu868.toml
+    command: -c /etc/chirpstack-gateway-bridge/chirpstack-gateway-bridge-basicstation-cn470_10.toml
     ports:
       - 3001:3001
     volumes:

如果需要多个频段区域在一个ChirpStack服务器上,可以配置为多个chirpstack-gateway-bridge。

启动ChirpStack服务

$ docker-compose up -d

Chirpstack默认的用户名密码都是admin。

运行后,可以打开浏览器访问127.0.0.1:8080,使用用户名和密码来登录。

在 Device Profile 中除了 LoRaWAN 参数之外,还有一个 codec 模块。

这个模块默认是未启用的,它支持两种协议解析,我们在 AS 的 WEB 界面中可以看到。

Cayenne Low Power Payload。卡宴的这个协议要求传感器按其协议上报的话,它则会解析出相应的数据。不够灵活,一般做简单的DEMO应用。
Custom JavaScript codec functions。这就是我们今天重点介绍的 JS 编解码函数。可将设备的原始数据转换为可读性更强的 JSON 格式。

下面是一个温度示例,取第一字节来做温度。

// Decode uplink function.
  //
  // Input is an object with the following fields:
  // - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
  // - fPort = Uplink fPort.
  // - variables = Object containing the configured device variables.
  //
  // Output must be an object with the following fields:
  // - data = Object representing the decoded payload.
  function decodeUplink(input) {
    return {
      data: {
        temp: input.bytes[0]
      }
    };
  }
  
  // Encode downlink function.
  //
  // Input is an object with the following fields:
  // - data = Object representing the payload that must be encoded.
  // - variables = Object containing the configured device variables.
  //
  // Output must be an object with the following fields:
  // - bytes = Byte array containing the downlink payload.
  function encodeDownlink(input) {
    return {
      bytes: [225, 230, 255, 0]
    };
  }
  

使用MQTT上传接收数据

使用MQTT客户端,连接Chirpstack服务器提供的MQTT服务,IP是Chirpstack的服务器IP,端口是1883,无需密码可以直接连接。

订阅Topic如下

  • application下的所有设备application/APPLICATION_ID/#

  • application下的指定设备的所有事件的信息application/APPLICAION_ID/device/DEV_EUI/#

  • application下的指定设备的上行数据application/APPLICAION_ID/device/DEV_EUI/event/up

下面以订阅指定设备的所有事件信息为例

{
  "deduplicationId": "19952bb9-42a2-4448-8736-a70dc2f26be7",
  "time": "2025-04-22T08:57:14.413846259+00:00",
  "deviceInfo": {
    "tenantId": "52f14cd4-c6f1-4fbd-8f87-4025e1d49242",
    "tenantName": "ChirpStack",
    "applicationId": "b3525049-5fea-4f22-a60d-6510ce7004e1",
    "applicationName": "i2som-lm401",
    "deviceProfileId": "2e320a4c-f9be-42b6-afcb-89052e8875f4",
    "deviceProfileName": "lm401otaa",
    "deviceName": "lm401b",
    "devEui": "eb03290421c69c6a",
    "deviceClassEnabled": "CLASS_C",
    "tags": {}
  },
  "devAddr": "01290607",
  "adr": true,
  "dr": 0,
  "fCnt": 1,
  "fPort": 2,
  "confirmed": false,
  "data": "EjQ=",
  "rxInfo": [
    {
      "gatewayId": "1172bdd0cd032a42",
      "uplinkId": 12952,
      "nsTime": "2025-04-22T08:57:14.196388934+00:00",
      "rssi": -56,
      "snr": 6.5,
      "location": {},
      "context": "HvR7rA==",
      "metadata": {
        "region_config_id": "cn470_10",
        "region_common_name": "CN470"
      },
      "crcStatus": "CRC_OK"
    }
  ],
  "txInfo": {
    "frequency": 486300000,
    "modulation": {
      "lora": {
        "bandwidth": 125000,
        "spreadingFactor": 12,
        "codeRate": "CR_4_5"
      }
    }
  }
}

这里是一个收到的节点上传的数据,其中data数据是编码的,需要配置device-profile里面的codec后,就能看到原始数据了。

下面是按照前面配置codec为temp后,data字段后多了object对象,里面有个temp数据。

{
  "deduplicationId": "11fbe164-86df-4c5e-9038-45cf21cc542b",
  "time": "2025-04-22T08:58:17.345164243+00:00",
  "deviceInfo": {
    "tenantId": "52f14cd4-c6f1-4fbd-8f87-4025e1d49242",
    "tenantName": "ChirpStack",
    "applicationId": "b3525049-5fea-4f22-a60d-6510ce7004e1",
    "applicationName": "i2som-lm401",
    "deviceProfileId": "2e320a4c-f9be-42b6-afcb-89052e8875f4",
    "deviceProfileName": "lm401otaa",
    "deviceName": "lm401b",
    "devEui": "eb03290421c69c6a",
    "deviceClassEnabled": "CLASS_A",
    "tags": {}
  },
  "devAddr": "01290607",
  "adr": true,
  "dr": 0,
  "fCnt": 2,
  "fPort": 2,
  "confirmed": true,
  "data": "q80=",
  "object": {
    "temp": 171
  },
  "rxInfo": [
    {
      "gatewayId": "1172bdd0cd032a42",
      "uplinkId": 28862,
      "nsTime": "2025-04-22T08:58:17.137770886+00:00",
      "rssi": -56,
      "snr": 7.5,
      "channel": 1,
      "location": {},
      "context": "IrT7Yg==",
      "metadata": {
        "region_common_name": "CN470",
        "region_config_id": "cn470_10"
      },
      "crcStatus": "CRC_OK"
    }
  ],
  "txInfo": {
    "frequency": 486500000,
    "modulation": {
      "lora": {
        "bandwidth": 125000,
        "spreadingFactor": 12,
        "codeRate": "CR_4_5"
      }
    }
  }
}

MQTT下发数据到节点

使用MQTT下发数据给节点,需要对指定Topic来发送JSON数据。
application/APPLICATION_ID/device/DEV_EUI/command/down

{     
	"devEui": "0102030405060708",
	"confirmed": true,
	"fPort": 10,
	"object": {
		"temp": 25},
		"hum": 32
	}
}

device-profile中要选择使用JAVASCRIPT方式,这里才能使用object方式来发送数据。

作者:SteveChen  创建时间:2024-09-14 17:14
最后编辑:SteveChen  更新时间:2025-04-22 22:34