|
| 1 | +# Zigbee Sensor (MQTT) |
| 2 | + |
| 3 | +This example describes the implementation of the [Standalone UCM](https://handbook.enapter.com/software/virtual_ucm/) concept using the opensource [Enapter python-sdk](https://github.com/Enapter/python-sdk) for monitoring Zigbee Sensor via MQTT protocol (Zigbee2Mqtt). |
| 4 | + |
| 5 | +In order to use this UCM you need to have [Zigbee2MQTT](https://www.zigbee2mqtt.io/guide/installation/) and some MQTT broker (for example [Mosquitto](https://mosquitto.org)) running. |
| 6 | + |
| 7 | +As an example in this guide we will use the following dummy settings for configuration: |
| 8 | + |
| 9 | +MQTT Broker Address: 192.168.192.190 |
| 10 | + |
| 11 | +MQTT Broker Port: 9883 |
| 12 | + |
| 13 | +MQTT User: mqtt_user |
| 14 | + |
| 15 | +MQTT Password: mqtt_password |
| 16 | + |
| 17 | +Device MQTT topic: zigbee2mqtt/MyDevice |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +It is recommended to run this UCM using Docker and Docker Compose. This will ensure that environment is correct. |
| 22 | + |
| 23 | +The MQTT broker must be reachable from the computer where the Docker Container will be running. |
| 24 | + |
| 25 | +## Step 1. Create Standalone UCM in Enapter Cloud |
| 26 | + |
| 27 | +Log in to the Enapter Cloud, navigate to the Site where you want to create Standalone UCM and click on `Add new` button in the Standalone Device section. |
| 28 | + |
| 29 | +After creating Standalone UCM, you need to Generate and save Configuration string also known as ENAPTER_VUCM_BLOB as well as save UCM ID which will be needed for the next step |
| 30 | + |
| 31 | +More information you can find on [this page](https://developers.enapter.com/docs/tutorial/software-ucms/standalone). |
| 32 | + |
| 33 | +## Step 2. Upload Blueprint into the Cloud |
| 34 | + |
| 35 | +The general case [Enapter Blueprint](https://marketplace.enapter.com/about) consists of two files - declaration in YAML format (manifest.yaml) and logic written in Lua. Howerver for this case the logic is written in Python as Lua implementation doesn't have SNMP integration. |
| 36 | + |
| 37 | +But for both cases we need to tell Enapter Cloud which telemetry we are going to send and store and how to name it. |
| 38 | + |
| 39 | +The easiest way to do that - using [Enapter CLI](https://github.com/Enapter/enapter-cli) to upload manifest.yaml into Cloud. The other option is to use [Web IDE](https://developers.enapter.com/docs/tutorial/uploading-blueprint). |
| 40 | + |
| 41 | +```bash |
| 42 | +user@pc zigbee2mqtt % enapter devices upload --blueprint-dir . --hardware-id REAL_UCM_ID |
| 43 | +upload started with operation id 25721 |
| 44 | +[#25721] 2023-07-20T16:27:33Z [INFO] Started uploading blueprint[id=dcb05efe-1618-4b01-877b-6105960690bc] on device[hardware_id=REAL_UCM_ID] |
| 45 | +[#25721] 2023-07-20T16:27:33Z [INFO] Generating configuration for uploading |
| 46 | +[#25721] 2023-07-20T16:27:33Z [INFO] Updating configuration in the cloud platform |
| 47 | +[#25721] 2023-07-20T16:27:33Z [INFO] Updating configuration on the gateway |
| 48 | +[#25721] 2023-07-20T16:27:35Z [INFO] Uploading blueprint finished successfully |
| 49 | +Done! |
| 50 | +``` |
| 51 | +
|
| 52 | +## Step 3. Configuring Standalone UCM |
| 53 | +
|
| 54 | +Open `docker-compose.yaml` in any editor. |
| 55 | +
|
| 56 | +Set environment variables according to your configuration settings. With dummy settings your file will look like this: |
| 57 | +
|
| 58 | +```yaml |
| 59 | +version: "3" |
| 60 | +services: |
| 61 | + zigbee2mqtt-ucm: |
| 62 | + build: . |
| 63 | + image: enapter-vucm-examples/zigbee2mqtt:latest |
| 64 | + environment: |
| 65 | + - ENAPTER_VUCM_BLOB: "REALENAPTERVUCMBLOBMUSTBEHERE=" |
| 66 | + - ZIGBEE_MQTT_HOST: "192.168.192.190" |
| 67 | + - ZIGBEE_MQTT_PORT: "9883" |
| 68 | + - ZIGBEE_MQTT_USER: "mqtt_user" |
| 69 | + - ZIGBEE_MQTT_PASSWORD: "mqtt_password" |
| 70 | + - ZIGBEE_MQTT_TOPIC: "zigbee2mqtt/MyDevice" |
| 71 | + - ZIGBEE_SENSOR_MANUFACTURER: "Device Manufacturer" |
| 72 | + - ZIGBEE_SENSOR_MODEL: "Device Model" |
| 73 | +``` |
| 74 | +
|
| 75 | +## Step 4. Build Docker Image with Standalone UCM |
| 76 | +
|
| 77 | +> You can you can skip this step and go directly to th Step 5. |
| 78 | +> Docker Compose will automatically build your image before starting container. |
| 79 | +
|
| 80 | +Build your Docker image by running `bash docker_build.sh` command in directory with UCM. |
| 81 | +
|
| 82 | +```bash |
| 83 | +user@pc zigbee2mqtt % bash docker_build.sh |
| 84 | +#0 building with "desktop-linux" instance using docker driver |
| 85 | + |
| 86 | +#1 [internal] load .dockerignore |
| 87 | +#1 transferring context: 2B done |
| 88 | +#1 DONE 0.0s |
| 89 | + |
| 90 | +#2 [internal] load build definition from Dockerfile |
| 91 | +#2 transferring dockerfile: 281B done |
| 92 | +#2 DONE 0.0s |
| 93 | + |
| 94 | +#3 [internal] load metadata for docker.io/library/python:3.10-alpine3.16 |
| 95 | +#3 DONE 2.0s |
| 96 | + |
| 97 | +#4 [1/7] FROM docker.io/library/python:3.10-alpine3.16@sha256:afe68972cc00883d70b3760ee0ffbb7375cf09706c122dda7063ffe64c5be21b |
| 98 | +#4 DONE 0.0s |
| 99 | + |
| 100 | +#5 [internal] load build context |
| 101 | +#5 transferring context: 66B done |
| 102 | +#5 DONE 0.0s |
| 103 | + |
| 104 | +#6 [3/7] RUN apk add build-base |
| 105 | +#6 CACHED |
| 106 | + |
| 107 | +#7 [2/7] WORKDIR /app |
| 108 | +#7 CACHED |
| 109 | + |
| 110 | +#8 [4/7] RUN python -m venv .venv |
| 111 | +#8 CACHED |
| 112 | + |
| 113 | +#9 [5/7] COPY requirements.txt requirements.txt |
| 114 | +#9 CACHED |
| 115 | + |
| 116 | +#10 [6/7] RUN .venv/bin/pip install -r requirements.txt |
| 117 | +#10 CACHED |
| 118 | + |
| 119 | +#11 [7/7] COPY script.py script.py |
| 120 | +#11 CACHED |
| 121 | + |
| 122 | +#12 exporting to image |
| 123 | +#12 exporting layers done |
| 124 | +#12 writing image sha256:92e1050debeabaff5837c6ca5bc26b0b966d09fc6f24e21b1d10cbb2f4d9aeec done |
| 125 | +#12 naming to docker.io/enapter-vucm-examples/zigbee2mqtt:latest done |
| 126 | +#12 DONE 0.0s |
| 127 | +``` |
| 128 | +
|
| 129 | +Your `enapter-vucm-examples/zigbee2mqtt` image is now built and you can see it by running `docker images` command: |
| 130 | +
|
| 131 | +```bash |
| 132 | +user@pc zigbee2mqtt % docker images enapter-vucm-examples/zigbee2mqtt |
| 133 | +REPOSITORY TAG IMAGE ID CREATED SIZE |
| 134 | +enapter-vucm-examples/zigbee2mqtt latest 92e1050debea 5 hours ago 285MB |
| 135 | +``` |
| 136 | +
|
| 137 | +## Step 5. Run your Standalone UCM Docker Container |
| 138 | +
|
| 139 | +Finally run your Standalone UCM with `docker-compose up` command: |
| 140 | +
|
| 141 | +On this step you can check that your UCM is now Online in the Cloud. |
0 commit comments