下载ChirpStack

下载Chiprstack代码

https://github.com/chirpstack/chirpstack-docker.git
cd chirpstack-docker

最新版本里面已经默认添加了CN470_10频段了,所以只需要修改MQTT前缀就可以了

diff --git a/docker-compose.yml b/docker-compose.yml
index 5d6a9f1..c0b2ebb 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -24,16 +24,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,
	"data": "1234",
	"object": {
		"temp": 25},
		"hum": 32
	}
}

data和object只能用一个。
如果device-profile中CODEC选择是NONE,可以使用data属性,值是base64编码的数据。
如果device-profile中选择JAVASCRIPT方式,才能使用object属性。

ClassC模式问题

ClassC模式时,节点在Join入网后,节点需要切换为ClassC模式,并且向上发送一个数据包后,ChirpStack才会同步为ClassC模式,后续ChirpStack可以持续向节点发送数据包。

官方issue
github.com/chirpstac...

LoRaWAN_AT_Slave代码需要在入网后切换为ClassC模式,建议使用OTAA模式。

AT+CLASS=C

配置后,后续接收数据包的前缀就是+EVT:RX_C,表示在ClassC模式下接收数据包。

作者:SteveChen  创建时间:2024-09-14 17:14
最后编辑:SteveChen  更新时间:2025-05-15 14:19