diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/README.md b/README.md index 97e7d98..4661f5b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ with other subscribers as well. ## Installation -1. Edit `flow3r_openhab/__init__.py` and set the variables `WIFI_SSID`, `WIFI_PSK` and `MQTT_BROKER_IP`. +1. Copy `flow3r_openhab/config.example.json` to `flow3r_openhab/config.json` and enter your configuration details. 1. Copy the `flow3r_openhab` directory to `/sys/apps/flow3r_openhab` on the flow3r's flash filesystem. 1. Reboot the flow3r and start the `OpenHAB` app diff --git a/flow3r_openhab/__init__.py b/flow3r_openhab/__init__.py index 8db641b..0b19b11 100644 --- a/flow3r_openhab/__init__.py +++ b/flow3r_openhab/__init__.py @@ -1,19 +1,18 @@ import gc +import json import network -import time +import os import socket +import time from st3m.application import Application, ApplicationContext from umqtt.robust import MQTTClient -WIFI_SSID = b'...' -WIFI_PSK = b'...' -MQTT_BROKER_IP = '...' -MQTT_CLIENT_ID = b'flow3r-{}' -MQTT_BASE_TOPIC = b'flow3r-{}/event' +with open(os.path.join(os.path.dirname(__file__), 'config.json'), 'r') as f: + CONFIG = json.load(f) class Flow3rOpenhabMqtt(Application): @@ -22,9 +21,9 @@ class Flow3rOpenhabMqtt(Application): super().__init__(app_ctx) self.state = 0 self.nic = network.WLAN(network.STA_IF) - client_id = MQTT_CLIENT_ID.format(self.nic.config('mac').hex()) - self.base_topic = MQTT_BASE_TOPIC.format(self.nic.config('mac').hex()) - self.mqtt = MQTTClient(client_id, MQTT_BROKER_IP) + client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex()) + self.base_topic = CONFIG['mqtt_topic'].format(self.nic.config('mac').hex()) + self.mqtt = MQTTClient(client_id, CONFIG['mqtt_broker_ip']) self.touch_time = [None]*10 self.touch_begin = [None]*10 self.touch_last = [None]*10 @@ -44,7 +43,7 @@ class Flow3rOpenhabMqtt(Application): self.nic.active(True) if not self.nic.isconnected(): print('connecting to network...') - self.nic.connect(WIFI_SSID, WIFI_PSK) + self.nic.connect(CONFIG['wifi_ssid'], CONFIG['wifi_psk']) while not self.nic.isconnected(): pass print('network config:', self.nic.ifconfig()) diff --git a/flow3r_openhab/config.example.json b/flow3r_openhab/config.example.json new file mode 100644 index 0000000..8f524a9 --- /dev/null +++ b/flow3r_openhab/config.example.json @@ -0,0 +1,7 @@ +{ + "wifi_ssid": "example", + "wifi_psk": "supersecurepassword", + "mqtt_broker_ip": "127.0.0.1", + "mqtt_client_id": "flow3r-{}", + "mqtt_topic": "flow3r-{}/event" +}