aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/mqtt_listener.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/mqtt_listener.py')
-rw-r--r--scripts/mqtt_listener.py60
1 files changed, 30 insertions, 30 deletions
diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py
index 1920824..6c617a5 100644
--- a/scripts/mqtt_listener.py
+++ b/scripts/mqtt_listener.py
@@ -12,6 +12,22 @@ mqtt_server = os.environ['el_mqtt_server']
mqtt_port = int(os.environ['el_mqtt_port'])
keepalive = int(os.environ['el_mqtt_keepalive'])
mqtt_topic = os.environ['el_mqtt_topic']
+mqtt_user = os.environ['el_mqtt_user']
+mqtt_pass = os.environ['el_mqtt_pass']
+
+tempsensors = [ 'Bad Temp',
+ 'Barnerom Temp',
+ 'Isobod Temp',
+ 'Kjøkken Temp Matskap',
+ 'Kontor Temp',
+ 'Loft Temp',
+ 'Soverom Temp',
+ 'Stue Temp Display',
+ 'Stue Temp Stuebord',
+ 'Stue Temp Teleskap',
+ 'Toalett motion',
+ 'Utebod Temp',
+ 'Vaskerom Temp']
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
@@ -26,30 +42,7 @@ def on_message(client, userdata, msg):
name = msg.topic.split('/')[1]
data = json.loads(msg.payload)
- if name.startswith('tmp') and 'temperature' in data and 'humidity' in data:
- sql = """INSERT INTO mqtt_temps
- (name,
- temperature,
- humidity,
- battery,
- linkquality,
- voltage,
- time,
- room)
- SELECT %s, %s, %s, %s, %s, %s, %s, sensors.room
- FROM sensors
- WHERE sensors.name = %s"""
- values = (name,
- data['temperature'],
- data['humidity'],
- data['battery'],
- data['linkquality'],
- data['voltage'],
- datetime.utcnow(),
- name)
-
-
- elif name == 'HAN' and 'current' in data:
+ if name == 'HAN' and 'current' in data:
sql = """INSERT INTO mqtt_han
(name,
current,
@@ -65,20 +58,25 @@ def on_message(client, userdata, msg):
data['linkquality'],
datetime.utcnow())
- elif name == 'toiletmotion' and 'temperature' in data:
+ elif name in tempsensors and 'temperature' in data:
+ data['humidity'] = data['humidity'] if 'humidity' in data else None
+ data['voltage'] = data['voltage'] if 'voltage' in data else None
sql = """INSERT INTO mqtt_temps
- (name,
+ (sensor_id,
temperature,
+ humidity,
battery,
linkquality,
- time,
- room)
- SELECT %s, %s, %s, %s, %s, sensors.room
+ voltage,
+ time)
+ SELECT sensors.id, %s, %s, %s, %s, %s, %s
FROM sensors
WHERE sensors.name = %s"""
- values = (name, data['temperature'],
+ values = (data['temperature'],
+ data['humidity'],
data['battery'],
data['linkquality'],
+ data['voltage'],
datetime.utcnow(),
name)
@@ -93,6 +91,8 @@ client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
+client.username_pw_set(mqtt_user, password=mqtt_pass)
+
client.connect(mqtt_server, mqtt_port, keepalive)
# Blocking call that processes network traffic, dispatches callbacks and