# SLS Gateway to bridge mode
In this article you will setup SLS gateway. Then convert it to bridge mode and connect to Zigbee2mqtt daemon.
## SLS Firmware
First you need to install microcontroller firmware of the gateway. Prepare the gateway by setting switches `1` and `3` at the bottom part of SLS Gateway to `ON`, others must be `OFF`.

Connect gateway to your Raspberry Pi via USB type-C port on the gateway.

Clone the repository with firmware to your Raspberry Pi:
```bash!
git clone https://github.com/airalab/robonomics-hass-utils.git
```
Go to `robonomics-hass-utils/esp_firmware/linux`. To flash the SLS gateway you need to run `Clear` and `Flash_16mb` scripts.
```shell=
cd robonomics-hass-utils/esp_firmware/linux
sudo chmod +x Clear.sh
sudo chmod +x Flash_16mb.sh
./Clear.sh
./Flash_16mb.sh
```
#### Troubleshooting
If you are experiencing problems updating the gateway firmware, you need to take additional steps:
1. Make sure you have the pySerial module installed:
```shell!
pip install pyserial
```
2. Give your user access rights to the USB port and reboot computer:
```shell
sudo usermod -a -G dialout $USER
sudo reboot
```
3. In some cases, it is necessary to change the bandwidth setting in the script to update the firmware. Open the `Flash_16mb.sh` script with the `nano` editor and change the baud parameter from `921600` to a smaller value (for example, `115200`).
## SLS Configuration
1. Set the switches on the back of the gateway to the proper position. Switches `5` (RX Zigbee to ESP) and `6` (TX Zigbee to ESP) must be in the `ON` position, the others must be `OFF`.

2. Connect the type-C power cable. The indicator light in the center should turn green.

3. On the first startup, the gateway will start sharing Wi-Fi with the SSID `zgw****`. Connect to this network. Keep in mind that the signal may be quite weak, so it is better to keep the SLS gateway closer to your computer.

4. If the connection is successful, the web interface will open (or you can find it on 192.168.1.1 address).
5. You will see `Wi-Fi Settings` page. Select your Wi-Fi and enter the password. Press `Apply` button. The gateway will restart and connect to your Wi-Fi network.
6. Find the local IP of the SLS gateway to access the web interface. To find it you can use [Fing mobile app](https://www.fing.com/products) or [nmap CLI tool](https://vitux.com/find-devices-connected-to-your-network-with-nmap/). The gateway name should look like this: `zgw****`. Open the web interface of the gateway by pasting the gateway IP into a browser.
7. Go to `Setting` -> `Hardware` and make sure that the settings look like on the image. Correct the settings if necessary and click `Save` button:
The table with required values:
| Field | Value |
|--------------------------|:-------------------|
| Zigbee module | TI |
| Zigbee UART RX | 22 |
| Zigbee UART TX | 23 |
| Zigbee RST Pin | 18 |
| Zigbee BSL Pin | 19 |
| Service Button Pin | 33 (pullUP - true) |
| Number addressable leds | 0 |
| Led Red (or addr) | 21 |
| Led Green | 5 |
| Led Blue | 27 |
| I2C SDA | 255 |
| I2C SCL | 255 |
8. Then reboot the gateway. Choose `Actions` -> `Reboot system` at the right top corner.
9. Go to `Setings` -> `services`. Turn on `Zigbee bridge (s1)` and `Zigbee bridge (s2)` fields and `Save` button. Then reboot the gateway.

***
# Zigbee2mqtt module
## Software Install
Set up Node.js runtime environment repository and install it with required dependencies:
```shell!
sudo curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs git make g++ gcc
```
Verify that the correct versions of Node.js (v14.X, V16.x, V17.x or V18.X) and package manager npm (6.X, 7.X or 8.X) automatically installed with Node.js, have been installed:
```shell!
node --version
npm --version
```
Create a directory for Zigbee2MQTT and set your user as owner of it:
```shell
sudo mkdir /opt/zigbee2mqtt
sudo chown -R ${USER}: /opt/zigbee2mqtt
```
Clone Zigbee2MQTT repository:
```shell
git clone --depth 1 --branch 1.28.2 https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
```
Install dependencies. Note that the npm ci could produce some warning which can be ignored.
```shell
cd /opt/zigbee2mqtt
npm ci
```
## Configuration and Run
Before starting Zigbee2MQTT you need to edit the `configuration.yaml` file. This file contains the configuration which will be used by Zigbee2MQTT:
```bash
nano /opt/zigbee2mqtt/data/configuration.yaml
```
The basic configuration needs a few adjustments. Change the following statements:
- `homeassistant:` to `true`. It will automatically connect sensors to Home Assistant.
- uncomment `user` and `password`statements under `mqtt` and enter your username and password (as a string, with quotes) from MQTT Broker.
- change port in `serial`-> `port` to `/dev/DEVICE_CONNECTION_DIRECTORY>`. In this example — `/dev/ttyUSB0`.
Adjusted configuration file should look like:
```shell
# Home Assistant integration (MQTT discovery)
homeassistant: true
# allow new devices to join
permit_join: true
frontend: false
# MQTT settings
mqtt:
# MQTT base topic for zigbee2mqtt MQTT messages
base_topic: zigbee2mqtt
# MQTT server URL
server: 'mqtt://localhost'
# MQTT server authentication, uncomment if required:
user: <YOUR_USERNAME>
password: <YOUR_PASSWORD>
# Serial settings
serial:
port: tcp://<SLS_IP_ADDRESS>:8881
```
***
If you already have an active Zigbee adapter or gateway in your home, and you are now configuring another adapter, then they will conflict with each other. To solve this problem you need to change the channel on the new device. For this add the following strings to the end of configuration file:
```shell
advanced:
# Optional: ZigBee channel, changing requires re-pairing of all devices. (Note: use a ZLL channel: 11, 15, 20, or 25 to avoid Problems)
# (default: 11)
channel: 15
```
***
Now you can start Zigbee2MQTT:
```bash
cd /opt/zigbee2mqtt
npm start
```
If started successfully, you will see something like:
```shell
Building Zigbee2MQTT... (initial build), finished
Zigbee2MQTT:info 2022-07-29 14:36:36: Logging to console and directory: '/opt/zigbee2mqtt/data/log/2022-07-29.14-36-36' filename: log.txt
Zigbee2MQTT:info 2022-07-29 14:36:36: Starting Zigbee2MQTT version 1.26.0 (commit #bc4ffc0)
Zigbee2MQTT:info 2022-07-29 14:36:36: Starting zigbee-herdsman (0.14.40)
Zigbee2MQTT:info 2022-07-29 14:36:49: zigbee-herdsman started (resumed)
Zigbee2MQTT:info 2022-07-29 14:36:49: Coordinator firmware version: '{"meta":{"maintrel":1,"majorrel":2,"minorrel":7,"product":1,"revision":20211219,"transportrev":2},"type":"zStack3x0"}'
Zigbee2MQTT:info 2022-07-29 14:36:49: Currently 0 devices are joined:
Zigbee2MQTT:warn 2022-07-29 14:36:49: `permit_join` set to `true` in configuration.yaml.
Zigbee2MQTT:warn 2022-07-29 14:36:49: Allowing new devices to join.
Zigbee2MQTT:warn 2022-07-29 14:36:49: Set `permit_join` to `false` once you joined all devices.
Zigbee2MQTT:info 2022-07-29 14:36:49: Zigbee: allowing new devices to join.
Zigbee2MQTT:info 2022-07-29 14:36:49: Connecting to MQTT server at mqtt://localhost
Zigbee2MQTT:info 2022-07-29 14:36:49: Connected to MQTT server
Zigbee2MQTT:info 2022-07-29 14:36:49: MQTT publish: topic 'zigbee2mqtt/bridge/state', payload 'online'
Zigbee2MQTT:info 2022-07-29 14:36:49: MQTT publish: topic 'zigbee2mqtt/bridge/config', payload '{"commit":"bc4ffc0","coordinator":{"meta":{"maintrel":1,"majorrel":2,"minorrel":7,"product":1,"revision":20211219,"transportrev":2},"type":"zStack3x0"},"log_level":"info","network":{"channel":11,"extendedPanID":"0x00124b0020cd133d","panID":6754},"permit_join":true,"version":"1.26.0"}'
Zigbee2MQTT:info 2022-07-29 14:36:49: MQTT publish: topic 'zigbee2mqtt/bridge/state', payload 'online
```
### systemctl service
To make the Zigbee2MQTT run after reboot, make a service. Create the file:
```shell
sudo nano /etc/systemd/system/zigbee2mqtt.service
```
Add the following to this file:
```shell
[Unit]
Description=zigbee2mqtt
After=network.target
[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/opt/zigbee2mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=<YOUR_USER_HERE>
[Install]
WantedBy=multi-user.target
```
***
If you don't know your username, use `whoami` command.
***
Save file and verify that the configuration works:
```shell
sudo systemctl enable zigbee2mqtt.service
sudo systemctl start zigbee2mqtt
systemctl status zigbee2mqtt.service
```
Output should look like:
```
pi@raspberry:/opt/zigbee2mqtt $ systemctl status zigbee2mqtt.service
● zigbee2mqtt.service - zigbee2mqtt
Loaded: loaded (/etc/systemd/system/zigbee2mqtt.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2018-06-07 20:27:22 BST; 3s ago
Main PID: 665 (npm)
CGroup: /system.slice/zigbee2mqtt.service
├─665 npm
├─678 sh -c node index.js
└─679 node index.js
Jun 07 20:27:22 raspberry systemd[1]: Started zigbee2mqtt.
Jun 07 20:27:23 raspberry npm[665]: > zigbee2mqtt@1.6.0 start /opt/zigbee2mqtt
Jun 07 20:27:23 raspberry npm[665]: > node index.js
Jun 07 20:27:24 raspberry npm[665]: Zigbee2MQTT:info 2019-11-09T13:04:01: Logging to directory: '/opt/zigbee2mqtt/data/log/2019-11-09.14-04-01'
Jun 07 20:27:25 raspberry npm[665]: Zigbee2MQTT:info 2019-11-09T13:04:01: Starting Zigbee2MQTT version 1.6.0 (commit #720e393)
```