├── BLETOOL Commands Manual_V0.2.pdf ├── BLE_Bridge.md ├── README.md ├── gl-mqtt_3.0.37-1_mips_24kc.ipk └── zigbee ├── gl-zigbee_1.0.2_ipq806x.ipk ├── gl-zigbee_1.0.4_mips_24kc.ipk └── zigbee_user_guide.pdf /BLETOOL Commands Manual_V0.2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gl-inet/ble2mqtt/4d75d4e9222d8cec1dd4c1667d0a1276e3bb4905/BLETOOL Commands Manual_V0.2.pdf -------------------------------------------------------------------------------- /BLE_Bridge.md: -------------------------------------------------------------------------------- 1 | # BLE MQTT Bridge 2 | 3 | ## Introduction 4 | 5 | Bluetooth Low Energy (BLE) is widely used for smart home, wearable and IoT sensors. This guide provide a simple but useful tool that send your BLE data to MQTT servers, which is the underling mechanism for popular IoT solution. 6 | 7 | In this guide we will introduce two GL.iNet BLE gateway, `GL-X750 Spitz` and `GL-S1300 convexa S` which has built-in BLE modules and how to use them to forward your BLE data to the cloud based on MQTT protocol, including GL.iNet `GoodCloud` and `AWS IoT`. 8 | 9 | 10 | 11 | ## Hardware Required 12 | 13 | Both GL-X750 and GL-S1300 comes with a Silicon BLE 5.0 module with the following specs. It connects to the main board using `UART`. The same module support BLE and Zigbee. However you need to change the firmware on the module (not the gateway). There is no way to change the firmware from BLE to Zigbee or vice versa on the air. So you have to select the correct module when you purchase the correct product. 14 | 15 | | Item | Specification | 16 | | --------------------- | ----------------------------------------------------------- | 17 | | Product Name | Bluetooth & Zigbee Module | 18 | | Model | GL-EFR32S/GL-EFR32H | 19 | | CPU | Silicon Labs EFR32MG1P232F256GM48-CR0 | 20 | | Protocol | Support Zigbee、BLE | 21 | | Storage | RAM:32KB/ Flash:256KB | 22 | | Tx Power | 19 dBm | 23 | | Antenna | On-board antenna (external antenna IPEX reserved) | 24 | | Internal Interface | 17*GPIO (could be configured to UART、SPI、JTAG、I2C, etc.) | 25 | | Module Interface Type | Stamp interface/Pin interface | 26 | | Supply Voltage | 3.3 V | 27 | | Power Consumption | support low power consumption, standby power<50uW | 28 | | Module Dimenstion | 25mm*20mm | 29 | | Operation Temperature | 0℃~75℃ | 30 | | Storage Temperature | -40℃~85℃ | 31 | | Operating Humidity | 10%~90% | 32 | 33 | You must use the gateway with BLE devices. 34 | 35 | There are two kinds of BLE devices. One kind of BLE device use beacons to send data, e.g a BLE thermometer. Another kind of device need to pair first, e.g. BLE smart watch. 36 | 37 | For BLE beacon device, you can just scan the beacon and get the data. The data is generally encrypted. It is depends on you to decrypted the data. You can decrypt locally on the gateway or just send the encrypted data to your cloud to decrypt there. 38 | 39 | For BLE devices that needs to pair first, you need to know how they can pair with the gateway. Generally this is provided by the device maker. There are a lot of devices that accept standard connection so you can pair without problem. 40 | 41 | A list of compatible devices will be collected and posted here. 42 | 43 | ***TBD: a list of compatible BLE device.*** 44 | 45 | ## BLE Tools 46 | 47 | Both GL-X750 and GL-S1300 come with OpenWrt and our BLE tools. 48 | 49 | In your current firmware for S1300, there may be a BLE tool which comes with a UI. But we are further developing this and you need to use the new BLE tool. 50 | 51 | ```shell 52 | root@GL-S1300:~# opkg remove gl-ble-daemon gl-ble-api 53 | root@GL-S1300:~# opkg install bletool 54 | Installing bletool (1-2) to root... 55 | Downloading http://download.gl-inet.com/releases/packages-3.x/ipq40xx/glinet/bletool_1-2_ipq806x.ipk. 56 | Configuring bletool. 57 | ``` 58 | 59 | Now you can use the bletool to test your BLE function. 60 | 61 | ```shell 62 | root@GL-S1300:~# bletool on 63 | root@GL-S1300:~# bletool discovery_type 1 1 64 | OK 65 | root@GL-S1300:~# bletool discovery 1 1 0 66 | OK 67 | af000045d1e43df801ff1f0201061bff570100a0c931e781473f364824a724d7d3380402f83de4d1 4500 68 | af040045d1e43df801ff0f0a094d492042616e6420320302e0fe 69 | d0009238c444f07001ff0e02011a0aff4c001005031802dcbd 70 | d0049238c444f07001ff00 71 | 72 | ``` 73 | 74 | You can find a detailed documentation about `bletool` ***https://github.com/gl-inet/ble2mqtt/blob/master/BLETOOL%20Commands%20Manual_V0.2.pdf***. 75 | 76 | 77 | 78 | ## MQTT 79 | 80 | MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. You can find official document in [mqtt.org](mqtt.org). 81 | 82 | This guide will give you a step to step guide of how to send message to GL.iNet GoodCloud and AWS. 83 | 84 | ### Send Data to GoodCloud 85 | 86 | GoodCloud is our Cloud based on MQTT under development. Now you can use it for demo purpose related to BLE. You can contact us if you have any suggestions by sending an email to admin@goodcloud.xyz. 87 | 88 | All you need to do is to register an account in GoodCloud.xyz and bind your X750 or S1300 to your account. 89 | 90 | **Register your device on [GoodCloud.xyz](https://www.goodcloud.xyz)** 91 | 92 | Follow [this docs](https://docs.gl-inet.com/en/3/app/cloud/#setup) to add devices on GoodCloud. 93 | 94 | 95 | 96 | **Where is the config file** 97 | 98 | The config file for cloud is in UCI. You will need to modify this directly if you want to connect to your own MQTT cloud. 99 | 100 | ```shell 101 | root@GL-S1300:~# cat /etc/config/glconfig 102 | .... 103 | config service 'cloud' 104 | option enable '1' 105 | option check_status '1' 106 | option serverzone 'gslb-jp.goodcloud.xyz' 107 | option username 'your name' 108 | option email 'your@email.address' 109 | option bindtime '1558928701947' 110 | 111 | ``` 112 | 113 | 114 | 115 | **Forward data to GoodCloud** 116 | 117 | To forward data to GoodCloud, you can use simple shell script. 118 | 119 | After your device is connected to your cloud account, you can [SSH](https://docs.gl-inet.com/en/3/app/ssh/) to the router, try the following command. 120 | 121 | ```shell 122 | root@GL-S1300:~# ubus call mqtt pub '{"api":"/user/data", "data":"hello from S1300" 123 | }' 124 | { 125 | "code": 0 126 | } 127 | root@GL-S1300:~# 128 | 129 | ``` 130 | 131 | Login the GoodCloud, https://www.goodcloud.xyz,find the registered device in"device list", click the device name and you will find your message in timeline. 132 | 133 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/find-device.png) 134 | 135 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/find-log.jpg) 136 | 137 | 138 | 139 | 140 | 141 | ### Send Data to AWS IoT 142 | 143 | Amazon AWS IoT is a good choice for business applications. AWS seems to have good security and privacy protection. Here we will give an example of how to send your data to AWS IoT platform. It is a little complicated but still OK for IT specialist. 144 | 145 | ####1. Setting up your AWS IoT environment 146 | 147 | 1) On the **Welcome to the AWS IoT Console** page, in the navigation pane, choose **Manage->Things** , then **Register a thing**. 148 | 149 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image001.png) 150 | 151 | 2) On the **Creating AWS IoT things** page, choose **Create a single thing**. 152 | 153 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image002.png) 154 | 155 | 3) On the **Create a thing** page, in the **Name** field, type a name for your thing, such as **x750**. Choose **Next**. 156 | 157 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image003.png) 158 | 159 | 4) On the **Add a certificate for your thing** page, choose **Create certificate**. This generates an X.509 certificate and key pair. 160 | 161 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image004.png) 162 | 163 | 5) On the **Certificate created!** page, download your public and private keys, certificate, and root certificate authority (CA): 164 | 165 | - Choose **Download** for your certificate. 166 | 167 | - Choose **Download** for your private key. 168 | 169 | - Choose **Download** for the Amazon root CA. This will display a new web page. Choose **RSA 2048 bit key: Amazon Root CA 1**. This opens another web page with the text of the root CA certificate. Copy this text and paste it into a file named `Amazon_Root_CA_1.pem`. 170 | 171 | Most web browsers save downloaded files into a Downloads directory. You will copy these files to a different directory when you run the sample applications. Choose **Activate** to activate the X.509 certificate, and then choose **Attach a policy**. 172 | 173 | 174 | ![](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image005.png) 175 | 176 | The **RSA 2048 bit key: Amazon Root CA1** download web page: 177 | 178 | ![ ](https://static.gl-inet.com/docs/en/3/app/BLE_bridge/image007.png) 179 | 180 | 181 | 182 | 6) Active your thing 183 | 184 | Back to the previous site, click "**Activate**", then click "**Done**". 185 | 186 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image008.png) 187 | 188 | **2. Attach Policy** 189 | 190 | 1) After you register your thing, you will need to create and attach a new **policy** to the **certificate**.![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image009.png) 191 | 192 | 193 | 194 | 2) Enter a Name for the policy,such as x750_policy, then **Advance mode** 195 | 196 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image010.png) 197 | 198 | 3) Delete the automatically created policy code, then paste the policy below. 199 | 200 | ``` 201 | { 202 | "Version": "2012-10-17", 203 | "Statement": [ 204 | { 205 | "Action": [ 206 | "iot:Publish", 207 | "iot:Subscribe", 208 | "iot:Connect", 209 | "iot:Receive" 210 | ], 211 | "Effect": "Allow", 212 | "Resource": [ 213 | "*" 214 | ] 215 | } 216 | ] 217 | } 218 | ``` 219 | 220 | 4) **Create** Policy after you pasted policy code. 221 | 222 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image012.png) 223 | 224 | 225 | 226 | 5) On the left side, Secure --> Certificates, Select the certificate you just created, Actions 227 | --> Attach policy 228 | 229 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image013.png) 230 | 231 | 6) Chose the policy you created(x750_policy) and choose **Attach**. 232 | 233 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image014.png) 234 | 235 | 236 | 237 | ####2. Config GL.iNet devices to connect to AWS IoT 238 | 239 | **1) Upload your certificate to the router** 240 | 241 | Rename the downloaded certificate files : 242 | 243 | AmazonRootCA1.pem --> root_CA.crt 244 | 245 | xxxxxxxxxx.private.pem.key --> client.key 246 | 247 | xxxxxxxxxx. certificate.pem.crt --> client.crt 248 | 249 | Windows User use [SSH and WinScp](https://docs.gl-inet.com/en/3/app/ssh/) to transfer these three certificate files to `/etc/aws/` on router, please create new folder if you cannot find `/etc/aws/`. 250 | 251 | ```shell 252 | mkdir /etc/aws/ 253 | ``` 254 | 255 | **2) Update mqtt package on GL.iNet routers.** 256 | 257 | You can check if your router comes with updated MQTT package. If it is not, please upgrade it if you want to use AWS. 258 | 259 | Download the updated [mqtt package]( https://1drv.ms/u/s!AgvZCliGPjk-saQ_3HbKiRGo_WsSSQ), file name is '**gl-mqtt_3.0.37-1_mips_24kc.ipk**‘ 260 | 261 | Use WinSCP/SSH to transfer the package to /tmp/ , then delete the old package. 262 | 263 | ```shell 264 | opkg remove gl-mqtt --force-depends 265 | ``` 266 | 267 | Then install the package: 268 | 269 | ```shell 270 | opkg install gl-mqtt_3.0.37-1_mips_24kc.ipk 271 | ``` 272 | 273 | **3) Setup config file for your router** 274 | 275 | Edit `/etc/config/glconfig ` 276 | 277 | Add the following code to your **glconfig** file. 278 | 279 | ```shell 280 | config service 'cloud' 281 | option enable '1' 282 | option sub_topic '/a1rLNyG2VK5/mqtt_demo/user/get' 283 | option pub_topic '/sys/a1rLNyG2VK5/mqtt_demo/thing/event/property/post' 284 | option service_pride '2' 285 | option address 'ssl://xxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com:8883' 286 | option clientid 'iotconsole-xxxxxxxxxxxxxx' 287 | option log '1' 288 | ``` 289 | 290 | The sample of glconfig file: 291 | 292 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image016.png) 293 | 294 | If you already setup GoodCloud on router before, just add the code at the end of **config service 'cloud'** 295 | 296 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image017.png) 297 | 298 | ```shell 299 | option log '1' 300 | ``` 301 | 302 | must be contained in the config file. 303 | 304 | Note** 305 | 306 | The topic ARN follows this format: 307 | 308 | ```shell 309 | arn:aws:iot:*your-region*:*your-aws-account*:topic/** 310 | ``` 311 | 312 | For example: 313 | 314 | ```shell 315 | arn:aws:iot:us-east-1:123456789012:topic/my/topic 316 | ``` 317 | 318 | **Topics** 319 | 320 | The message broker uses topics to route messages from publishing clients to subscribing clients. The forward slash (/) is used to separate topic hierarchy. 321 | 322 | **Note** 323 | 324 | We do not recommend using personally identifiable information in your topics. 325 | 326 | The following table lists the wildcards that can be used in the topic filter when you subscribe. 327 | 328 | **Topic Wildcards** 329 | 330 | | Wildcard | Description | 331 | | :------- | :----------------------------------------------------------- | 332 | | # | Must be the last character in the topic to which you are subscribing. Works as a wildcard by matching the current tree and all subtrees. For example, a subscription to `Sensor/#` receives messages published to `Sensor/`, `Sensor/temp`, `Sensor/temp/room1`, but not the messages published to `Sensor`. | 333 | | + | Matches exactly one item in the topic hierarchy. For example, a subscription to `Sensor/+/room1` receives messages published to `Sensor/temp/room1`,`Sensor/moisture/room1`, and so on. | 334 | 335 | **4) HTTP: API Endpoints add to router config file** 336 | 337 | The message broker supports clients connecting with the HTTP protocol using a REST API. Clients can publish by sending a POST message to `**/topics/**?qos=1"`. 338 | 339 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image019.png) 340 | 341 | 342 | 343 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image020.png) 344 | 345 | 346 | 347 | 348 | 349 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image021.png) 350 | 351 | 352 | 353 | 354 | 355 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image022.png) 356 | 357 | 358 | 359 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image023.png) 360 | 361 | 362 | 363 | 364 | 365 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image024.png) 366 | 367 | 368 | 369 | 370 | 371 | ![](https://static.gl-inet.com./docs/en/3/app/BLE_bridge/image025.png) 372 | 373 | 374 | 375 | 376 | 377 | ## Application and Demo 378 | 379 | ### Read BLE Thermal Sensor 380 | 381 | The following demo show how to read data from a thermal sensor, pack the data in JSON format and send to the GoodCloud. 382 | 383 | ```shell 384 | #!/usr/bin/sh 385 | . /usr/share/libubox/jshn.sh 386 | while true 387 | do 388 | sleep 5 389 | bletool on 390 | bletool discovery_type 1 1 391 | bletool discovery 1 1 0 | awk '/0d09536d6172745f426561636f6e02/{print $0; exit}' > 392 | /tmp/blesniffer.log 393 | bletool end_procedure 394 | adv_data=`awk '{print $1}' /tmp/blesniffer.log` 395 | date=`date +"%Y%m%d%H%M%S"` 396 | json_init 397 | json_add_string "api" "/user/data" 398 | json_add_object "data" 399 | json_add_string "date" "$date" 400 | json_add_string "advertising data" "$adv_data" 401 | json_close_object 402 | json_close_object 403 | data=`json_dump` 404 | ubus call mqtt pub "$data" 405 | rm /tmp/blesniffer.log 406 | done 407 | ``` 408 | 409 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BLE MQTT Bridge 2 | 3 | ## Introduction 4 | 5 | Bluetooth Low Energy (BLE) is widely used for smart home, wearable and IoT sensors. This guide provide a simple but useful tool that send your BLE data to MQTT servers, which is the underling mechanism for popular IoT solution. 6 | 7 | In this guide we will introduce two GL.iNet BLE gateway, `GL-X750 Spitz` and `GL-S1300 convexa S` which has built-in BLE modules and how to use them to forward your BLE data to the cloud based on MQTT protocol, including GL.iNet `GoodCloud` and `AWS IoT`. 8 | 9 | 10 | 11 | ## Hardware Required 12 | 13 | Both GL-X750 and GL-S1300 comes with a Silicon BLE 5.0 module with the following specs. It connects to the main board using `UART`. The same module support BLE and Zigbee. However you need to change the firmware on the module (not the gateway). There is no way to change the firmware from BLE to Zigbee or vice versa on the air. So you have to select the correct module when you purchase the correct product. 14 | 15 | | Item | Specification | 16 | | --------------------- | ----------------------------------------------------------- | 17 | | Product Name | Bluetooth & Zigbee Module | 18 | | Model | GL-EFR32S/GL-EFR32H | 19 | | CPU | Silicon Labs EFR32MG1P232F256GM48-CR0 | 20 | | Protocol | Support Zigbee、BLE | 21 | | Storage | RAM:32KB/ Flash:256KB | 22 | | Tx Power | 19 dBm | 23 | | Antenna | On-board antenna (external antenna IPEX reserved) | 24 | | Internal Interface | 17*GPIO (could be configured to UART、SPI、JTAG、I2C, etc.) | 25 | | Module Interface Type | Stamp interface/Pin interface | 26 | | Supply Voltage | 3.3 V | 27 | | Power Consumption | support low power consumption, standby power<50uW | 28 | | Module Dimenstion | 25mm*20mm | 29 | | Operation Temperature | 0℃~75℃ | 30 | | Storage Temperature | -40℃~85℃ | 31 | | Operating Humidity | 10%~90% | 32 | 33 | You must use the gateway with BLE devices. 34 | 35 | There are two kinds of BLE devices. One kind of BLE device use beacons to send data, e.g a BLE thermometer. Another kind of device need to pair first, e.g. BLE smart watch. 36 | 37 | For BLE beacon device, you can just scan the beacon and get the data. The data is generally encrypted. It is depends on you to decrypted the data. You can decrypt locally on the gateway or just send the encrypted data to your cloud to decrypt there. 38 | 39 | For BLE devices that needs to pair first, you need to know how they can pair with the gateway. Generally this is provided by the device maker. There are a lot of devices that accept standard connection so you can pair without problem. 40 | 41 | A list of compatible devices will be collected and posted here. 42 | 43 | ***TBD: a list of compatible BLE device.*** 44 | 45 | ## BLE Tools 46 | 47 | Both GL-X750 and GL-S1300 come with OpenWrt and our BLE tools. 48 | 49 | In your current firmware for S1300, there may be a BLE tool which comes with a UI. But we are further developing this and you need to use the new BLE tool. 50 | 51 | ```shell 52 | root@GL-S1300:~# opkg remove gl-ble-daemon gl-ble-api 53 | root@GL-S1300:~# opkg install bletool 54 | Installing bletool (1-2) to root... 55 | Downloading http://download.gl-inet.com/releases/packages-3.x/ipq40xx/glinet/bletool_1-2_ipq806x.ipk. 56 | Configuring bletool. 57 | ``` 58 | 59 | Now you can use the bletool to test your BLE function. 60 | 61 | ```shell 62 | root@GL-S1300:~# bletool on 63 | root@GL-S1300:~# bletool discovery_type 1 1 64 | OK 65 | root@GL-S1300:~# bletool discovery 1 1 0 66 | OK 67 | af000045d1e43df801ff1f0201061bff570100a0c931e781473f364824a724d7d3380402f83de4d1 4500 68 | af040045d1e43df801ff0f0a094d492042616e6420320302e0fe 69 | d0009238c444f07001ff0e02011a0aff4c001005031802dcbd 70 | d0049238c444f07001ff00 71 | 72 | ``` 73 | 74 | You can find a detailed documentation about `bletool` ***https://github.com/gl-inet/ble2mqtt***. 75 | 76 | 77 | 78 | ## MQTT 79 | 80 | MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. You can find official document in [mqtt.org](mqtt.org). 81 | 82 | This guide will give you a step to step guide of how to send message to GL.iNet GoodCloud and AWS. 83 | 84 | ### Send Data to GoodCloud 85 | 86 | GoodCloud is our Cloud based on MQTT under development. Now you can use it for demo purpose related to BLE. You can contact us if you have any suggestions by sending an email to admin@goodcloud.xyz. 87 | 88 | All you need to do is to register an account in GoodCloud.xyz and bind your X750 or S1300 to your account. 89 | 90 | **Register your device on [GoodCloud.xyz](https://www.goodcloud.xyz)** 91 | 92 | Follow [this docs](https://docs.gl-inet.com/en/3/app/cloud/#setup) to add devices on GoodCloud. 93 | 94 | 95 | 96 | **Where is the config file** 97 | 98 | The config file for cloud is in UCI. You will need to modify this directly if you want to connect to your own MQTT cloud. 99 | 100 | ```shell 101 | root@GL-S1300:~# cat /etc/config/glconfig 102 | .... 103 | config service 'cloud' 104 | option enable '1' 105 | option check_status '1' 106 | option serverzone 'gslb-jp.goodcloud.xyz' 107 | option username 'your name' 108 | option email 'your@email.address' 109 | option bindtime '1558928701947' 110 | 111 | ``` 112 | 113 | 114 | 115 | **Forward data to GoodCloud** 116 | 117 | To forward data to GoodCloud, you can use simple shell script. 118 | 119 | After your device is connected to your cloud account, you can [SSH](https://docs.gl-inet.com/en/3/app/ssh/) to the router, try the following command. 120 | 121 | ```shell 122 | root@GL-S1300:~# ubus call mqtt pub '{"api":"/user/data", "data":"hello from S1300" 123 | }' 124 | { 125 | "code": 0 126 | } 127 | root@GL-S1300:~# 128 | 129 | ``` 130 | 131 | Login the GoodCloud, https://www.goodcloud.xyz,find the registered device in"device list", click the device name and you will find your message in timeline. 132 | 133 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/find-device.png) 134 | 135 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/find-log.jpg) 136 | 137 | 138 | 139 | 140 | 141 | ### Send Data to AWS IoT 142 | 143 | Amazon AWS IoT is a good choice for business applications. AWS seems to have good security and privacy protection. Here we will give an example of how to send your data to AWS IoT platform. It is a little complicated but still OK for IT specialist. 144 | 145 | ####1. Setting up your AWS IoT environment 146 | 147 | 1) On the **Welcome to the AWS IoT Console** page, in the navigation pane, choose **Manage->Things** , then **Register a thing**. 148 | 149 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image001.png) 150 | 151 | 2) On the **Creating AWS IoT things** page, choose **Create a single thing**. 152 | 153 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image002.png) 154 | 155 | 3) On the **Create a thing** page, in the **Name** field, type a name for your thing, such as **x750**. Choose **Next**. 156 | 157 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image003.png) 158 | 159 | 4) On the **Add a certificate for your thing** page, choose **Create certificate**. This generates an X.509 certificate and key pair. 160 | 161 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image004.png) 162 | 163 | 5) On the **Certificate created!** page, download your public and private keys, certificate, and root certificate authority (CA): 164 | 165 | - Choose **Download** for your certificate. 166 | 167 | - Choose **Download** for your private key. 168 | 169 | - Choose **Download** for the Amazon root CA. This will display a new web page. Choose **RSA 2048 bit key: Amazon Root CA 1**. This opens another web page with the text of the root CA certificate. Copy this text and paste it into a file named `Amazon_Root_CA_1.pem`. 170 | 171 | Most web browsers save downloaded files into a Downloads directory. You will copy these files to a different directory when you run the sample applications. Choose **Activate** to activate the X.509 certificate, and then choose **Attach a policy**. 172 | 173 | 174 | ![](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image005.png) 175 | 176 | The **RSA 2048 bit key: Amazon Root CA1** download web page: 177 | 178 | ![ ](http://static.gl-inet.com/docs/en/3/app/BLE_bridge/image007.png) 179 | 180 | 181 | 182 | 6) Active your thing 183 | 184 | Back to the previous site, click "**Activate**", then click "**Done**". 185 | 186 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image008.png) 187 | 188 | **2. Attach Policy** 189 | 190 | 1) After you register your thing, you will need to create and attach a new **policy** to the **certificate**.![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image009.png) 191 | 192 | 193 | 194 | 2) Enter a Name for the policy,such as x750_policy, then **Advance mode** 195 | 196 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image010.png) 197 | 198 | 3) Delete the automatically created policy code, then paste the policy below. 199 | 200 | ``` 201 | { 202 | "Version": "2012-10-17", 203 | "Statement": [ 204 | { 205 | "Action": [ 206 | "iot:Publish", 207 | "iot:Subscribe", 208 | "iot:Connect", 209 | "iot:Receive" 210 | ], 211 | "Effect": "Allow", 212 | "Resource": [ 213 | "*" 214 | ] 215 | } 216 | ] 217 | } 218 | ``` 219 | 220 | 4) **Create** Policy after you pasted policy code. 221 | 222 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image012.png) 223 | 224 | 225 | 226 | 5) On the left side, Secure --> Certificates, Select the certificate you just created, Actions 227 | --> Attach policy 228 | 229 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image013.png) 230 | 231 | 6) Chose the policy you created(x750_policy) and choose **Attach**. 232 | 233 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image014.png) 234 | 235 | 236 | 237 | ####2. Config GL.iNet devices to connect to AWS IoT 238 | 239 | **1) Upload your certificate to the router** 240 | 241 | Rename the downloaded certificate files : 242 | 243 | AmazonRootCA1.pem --> root_CA.crt 244 | 245 | xxxxxxxxxx.private.pem.key --> client.key 246 | 247 | xxxxxxxxxx. certificate.pem.crt --> client.crt 248 | 249 | Windows User use [SSH and WinScp](https://docs.gl-inet.com/en/3/app/ssh/) to transfer these three certificate files to `/etc/aws/` on router, please create new folder if you cannot find `/etc/aws/`. 250 | 251 | ```shell 252 | mkdir /etc/aws/ 253 | ``` 254 | 255 | **2) Update mqtt package on GL.iNet routers.** 256 | 257 | You can check if your router comes with updated MQTT package. If it is not, please upgrade it if you want to use AWS. 258 | 259 | Download the updated [mqtt package]( https://1drv.ms/u/s!AgvZCliGPjk-saQ_3HbKiRGo_WsSSQ), file name is '**gl-mqtt_3.0.37-1_mips_24kc.ipk**‘ 260 | 261 | Use WinSCP/SSH to transfer the package to /tmp/ , then delete the old package. 262 | 263 | ```shell 264 | opkg remove gl-mqtt --force-depends 265 | ``` 266 | 267 | Then install the package: 268 | 269 | ```shell 270 | opkg install gl-mqtt_3.0.37-1_mips_24kc.ipk 271 | ``` 272 | 273 | **3) Setup config file for your router** 274 | 275 | Edit `/etc/config/glconfig ` 276 | 277 | Add the following code to your **glconfig** file. 278 | 279 | ```shell 280 | config service 'cloud' 281 | option enable '1' 282 | option sub_topic '/a1rLNyG2VK5/mqtt_demo/user/get' 283 | option pub_topic '/sys/a1rLNyG2VK5/mqtt_demo/thing/event/property/post' 284 | option service_pride '2' 285 | option address 'ssl://xxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com:8883' 286 | option clientid 'iotconsole-xxxxxxxxxxxxxx' 287 | option log '1' 288 | ``` 289 | 290 | The sample of glconfig file: 291 | 292 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image016.png) 293 | 294 | If you already setup GoodCloud on router before, just add the code at the end of **config service 'cloud'** 295 | 296 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image017.png) 297 | 298 | ```shell 299 | option log '1' 300 | ``` 301 | 302 | must be contained in the config file. 303 | 304 | Note** 305 | 306 | The topic ARN follows this format: 307 | 308 | ```shell 309 | arn:aws:iot:*your-region*:*your-aws-account*:topic/** 310 | ``` 311 | 312 | For example: 313 | 314 | ```shell 315 | arn:aws:iot:us-east-1:123456789012:topic/my/topic 316 | ``` 317 | 318 | **Topics** 319 | 320 | The message broker uses topics to route messages from publishing clients to subscribing clients. The forward slash (/) is used to separate topic hierarchy. 321 | 322 | **Note** 323 | 324 | We do not recommend using personally identifiable information in your topics. 325 | 326 | The following table lists the wildcards that can be used in the topic filter when you subscribe. 327 | 328 | **Topic Wildcards** 329 | 330 | | Wildcard | Description | 331 | | :------- | :----------------------------------------------------------- | 332 | | # | Must be the last character in the topic to which you are subscribing. Works as a wildcard by matching the current tree and all subtrees. For example, a subscription to `Sensor/#` receives messages published to `Sensor/`, `Sensor/temp`, `Sensor/temp/room1`, but not the messages published to `Sensor`. | 333 | | + | Matches exactly one item in the topic hierarchy. For example, a subscription to `Sensor/+/room1` receives messages published to `Sensor/temp/room1`,`Sensor/moisture/room1`, and so on. | 334 | 335 | **4) HTTP: API Endpoints add to router config file** 336 | 337 | The message broker supports clients connecting with the HTTP protocol using a REST API. Clients can publish by sending a POST message to `**/topics/**?qos=1"`. 338 | 339 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image019.png) 340 | 341 | 342 | 343 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image020.png) 344 | 345 | 346 | 347 | 348 | 349 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image021.png) 350 | 351 | 352 | 353 | 354 | 355 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image022.png) 356 | 357 | 358 | 359 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image023.png) 360 | 361 | 362 | 363 | 364 | 365 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image024.png) 366 | 367 | 368 | 369 | 370 | 371 | ![](http://static.gl-inet.com./docs/en/3/app/BLE_bridge/image025.png) 372 | 373 | 374 | 375 | 376 | 377 | ## Application and Demo 378 | 379 | ### Read BLE Thermal Sensor 380 | 381 | The following demo show how to read data from a thermal sensor, pack the data in JSON format and send to the GoodCloud. 382 | 383 | ```shell 384 | #!/usr/bin/sh 385 | . /usr/share/libubox/jshn.sh 386 | while true 387 | do 388 | sleep 5 389 | bletool on 390 | bletool discovery_type 1 1 391 | bletool discovery 1 1 0 | awk '/0d09536d6172745f426561636f6e02/{print $0; exit}' > 392 | /tmp/blesniffer.log 393 | bletool end_procedure 394 | adv_data=`awk '{print $1}' /tmp/blesniffer.log` 395 | date=`date +"%Y%m%d%H%M%S"` 396 | json_init 397 | json_add_string "api" "/user/data" 398 | json_add_object "data" 399 | json_add_string "date" "$date" 400 | json_add_string "advertising data" "$adv_data" 401 | json_close_object 402 | json_close_object 403 | data=`json_dump` 404 | ubus call mqtt pub "$data" 405 | rm /tmp/blesniffer.log 406 | done 407 | ``` 408 | 409 | -------------------------------------------------------------------------------- /gl-mqtt_3.0.37-1_mips_24kc.ipk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gl-inet/ble2mqtt/4d75d4e9222d8cec1dd4c1667d0a1276e3bb4905/gl-mqtt_3.0.37-1_mips_24kc.ipk -------------------------------------------------------------------------------- /zigbee/gl-zigbee_1.0.2_ipq806x.ipk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gl-inet/ble2mqtt/4d75d4e9222d8cec1dd4c1667d0a1276e3bb4905/zigbee/gl-zigbee_1.0.2_ipq806x.ipk -------------------------------------------------------------------------------- /zigbee/gl-zigbee_1.0.4_mips_24kc.ipk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gl-inet/ble2mqtt/4d75d4e9222d8cec1dd4c1667d0a1276e3bb4905/zigbee/gl-zigbee_1.0.4_mips_24kc.ipk -------------------------------------------------------------------------------- /zigbee/zigbee_user_guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gl-inet/ble2mqtt/4d75d4e9222d8cec1dd4c1667d0a1276e3bb4905/zigbee/zigbee_user_guide.pdf --------------------------------------------------------------------------------