Browse Source

bisherige Version aus 09/2017, Empfängt diverse LaCrosse Sensoren und sendet die Daten an Domoticz (jeweilige Idx) sowie unterschiedliche MQTT-Topics

FloKra 5 years ago
commit
1b02d5b6a6

+ 209 - 0
jeelink2mqtt.py

@@ -0,0 +1,209 @@
+#!/usr/bin/python -u
+# -*- coding: utf-8 -*-
+#
+import serial
+import time
+import os
+import paho.mqtt.client as mqtt
+#import json
+#import math
+#import numpy as np
+#import httplib
+
+verbosemode = False
+
+def touch(fname, times=None):
+    with open(fname, 'a'):
+        os.utime(fname, times)
+
+def on_connect(client, userdata, flags, rc):
+    print("MQTT connected with result code " + str(rc))
+    #client.subscribe("wetter/atemp")
+
+def on_disconnect(client, userdata, rc):
+    if rc != 0:
+        print "Unexpected MQTT disconnection. Will auto-reconnect"
+        
+#def on_message(client, userdata, msg):
+#    #print(msg.topic + " " + str(msg.payload))
+#    global atemp
+#    atemp = msg.payload
+    
+
+minUpdateInterval = 60
+mqtt_topic_prefix = "LaCrosse"
+override_updateinterval_on_change = False
+atemp_sensor_idx = 94
+
+sensors = {}
+sensors_idx = {}
+with open("/home/pi/jeelink_sensors.csv", "r") as sensorscsv:
+    for line in sensorscsv:
+        if line.find('ID,DomoticzIdx,Name') == -1:
+            # nur Zeilen die nicht der header sind sind interessant
+            line = line.strip('\r')
+            line = line.strip('\n')
+            
+            parts = line.split(',')
+            sensorId = parts[0]
+            domoticzIdx = parts[1]
+            sensorName = parts[2]
+            
+            sensors[str(sensorId)] = str(sensorName)
+            sensors_idx[str(sensorId)] = str(domoticzIdx)
+
+            
+mqttc = mqtt.Client()
+mqttc.on_connect = on_connect
+mqttc.on_disconnect = on_disconnect
+##mqttc.on_message = on_message
+
+mqttc.connect("10.1.1.11", 1883, 60)
+
+mqttc.loop_start()
+#mqttc.loop_forever()
+
+ser = serial.Serial(port='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AL01MYTF-if00-port0',
+    baudrate = 57600,
+    parity=serial.PARITY_NONE,
+    stopbits=serial.STOPBITS_ONE,
+    bytesize=serial.EIGHTBITS,
+    timeout=1)
+
+#sensors = {'4':'Arbeitszimmer','16':'AussenGarten','60':'AussenParkplatz','50':'Bad','39':'Balkon','55':'Kueche','40':'Schlafzimmer'}
+#sensors_idx = {'4':'1','16':'94','60':'113','50':'4','39':'88','55':'6','40':'3'}
+
+if verbosemode:
+    print sensors
+    print sensors_idx
+
+sensors_lastTemp = {}
+sensors_lastHum = {}
+sensors_lastUpdate = {}
+    
+try:
+    while True:
+        msg_was_sent = 0
+        
+        #clear serial buffer to remove junk and noise
+        ser.flushInput()
+        
+        #read buffer until cr/lf
+        serLine = ser.readline()
+
+        if(serLine):
+            #print (repr(serLine))    #Echo the serial buffer bytes up to the CRLF back to screen
+            serLine = serLine.strip('\r')
+            serLine = serLine.strip('\n')
+            
+            if verbosemode:
+                print serLine
+            
+            if serLine.find('OK 9') != -1:
+                # uns interessieren nur reinkommende Zeilen die mit "OK 9 " beginnen
+                
+                # 0  1 2  3   4   5   6
+                # OK 9 ID XXX XXX XXX XXX
+                # |  | |  |   |   |   |
+                # |  | |  |   |   |   --- Humidity incl. WeakBatteryFlag
+                # |  | |  |   |   |------ Temp * 10 + 1000 LSB
+                # |  | |  |   |---------- Temp * 10 + 1000 MSB
+                # |  | |  |-------------- Sensor type (1 or 2) +128 if NewBatteryFlag
+                # |  | |----------------- Sensor ID
+                # |  |------------------- fix "9"
+                # |---------------------- fix "OK"
+                
+                bytes = serLine.split(' ')
+                
+                #addr = bytes[2]
+                #addr = "{0:x}".format(int(bytes[2]))
+                #addr = hex((int(bytes[2])))
+                addr = int(bytes[2])
+                addrhex = "{0:x}".format(int(bytes[2]))
+                
+                currentsensor_name = sensors.get(str(addr), None)
+                #if currentsensor_name == 0: 
+                if currentsensor_name is None: 
+                    fname = '/home/pi/logs/jeelink_unknown_sensor_' + str(addr)
+                    try:
+                        touch(fname)
+                    except:
+                        # guat dann hoit ned...
+                        pass
+                    if verbosemode:
+                        print "unknown sensor ID " + str(addr)
+                        temp = (int(bytes[4])*256 + int(bytes[5]) - 1000)/10.0
+                        print "Temp: " + str(temp)
+                
+                lastUpdate = sensors_lastUpdate.get(str(addr), None)
+                lastTemp = sensors_lastTemp.get(str(addr), None)
+                lastHum = sensors_lastHum.get(str(addr), None)
+                currentsensor_idx = sensors_idx.get(str(addr),None)
+                
+                senddata = False
+                if currentsensor_idx is not None:
+                    if override_updateinterval_on_change:
+                        if lastTemp != str(temp) or lastHum != str(hum):
+                            senddata = True
+                            #print "hier! " + str(temp) + " != " + str(lastTemp) + "    " + str(hum) + " != " + str(lastHum)
+                            
+                    if lastUpdate is not None:
+                        timediff = int(time.time()) - lastUpdate
+                        if timediff >= minUpdateInterval:
+                            senddata = True
+                            #print "do!"
+                    else:
+                        senddata = True
+                
+                if senddata:
+                    if int(bytes[3]) >= 128: 
+                        batt_new = 1
+                        type = int(bytes[3]) - 128
+                    else:
+                        batt_new = 0
+                        type = int(bytes[3])
+                    
+                    temp = (int(bytes[4])*256 + int(bytes[5]) - 1000)/10.0
+                    
+                    if int(bytes[6]) >= 128: 
+                        batt_low = 1
+                        hum = int(bytes[6]) - 128
+                    else:
+                        batt_low = 0
+                        hum = int(bytes[6])
+                        if hum > 100: 
+                            hum = 100
+                    
+                    if batt_low == 0:
+                        batterystate = "ok"
+                    else:
+                        batterystate = "low"
+                    
+                    sensors_lastUpdate[str(addr)] = int(time.time())
+                    sensors_lastTemp[str(addr)] = str(temp)
+                    sensors_lastHum[str(addr)] = str(hum)
+                    
+                    if int(currentsensor_idx) == atemp_sensor_idx:
+                        mqttc.publish("wetter/atemp", str(temp), qos=2, retain=True)
+                        mqttc.publish("wetter/ahum", str(hum), qos=2, retain=True)
+                    
+                    domoticz_json = "{\"idx\":" + str(currentsensor_idx) + ",\"nvalue\":0,\"svalue\":\"" + str(temp) + ";" + str(hum) + ";1\"}"
+                    if verbosemode:
+                        print domoticz_json
+                    mqttc.publish("domoticz/in", domoticz_json, qos=2, retain=False)
+                    
+                    mqttc.publish(mqtt_topic_prefix+"/"+str(currentsensor_name)+"/temperature", str(temp), qos=2, retain=True)
+                    mqttc.publish(mqtt_topic_prefix+"/"+str(currentsensor_name)+"/humidity", str(hum), qos=2, retain=True)
+                    mqttc.publish(mqtt_topic_prefix+"/"+str(currentsensor_name)+"/battery", str(batterystate), qos=2, retain=True)
+                    
+                    if verbosemode:
+                        print "addr: " + str(addr) + " = 0x" + str(addrhex) + "   batt_new: " + str(batt_new) + "   type: " + str(type) + "   batt_low: " + str(batt_low) + "   temp: " + str(temp) + "   hum: " + str(hum) + "   Name: " + str(currentsensor_name)
+                
+                    try:
+                        touch("/tmp/jeelink2mqtt_running")
+                    except:
+                        # guat dann ned...
+                        pass
+
+except KeyboardInterrupt, e:
+    print('\n')

+ 12 - 0
jeelink2mqtt.service

@@ -0,0 +1,12 @@
+[Unit]
+Description=JeeLink2MQTT
+After=multi-user.target
+
+[Service]
+Type=simple
+User=pi
+ExecStart=/home/pi/jeelink2mqtt.py
+Restart=always
+
+[Install]
+WantedBy=multi-user.target

+ 5 - 0
jeelink2mqtt_disable_services.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+sudo systemctl stop jeelink2mqtt.service
+sudo systemctl stop jeelink2mqtt_wdog.service
+sudo systemctl disable jeelink2mqtt.service
+sudo systemctl disable jeelink2mqtt_wdog.service

+ 5 - 0
jeelink2mqtt_enable_services.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+sudo systemctl enable jeelink2mqtt_wdog.service
+sudo systemctl enable jeelink2mqtt.service
+sudo systemctl start jeelink2mqtt.service
+sudo systemctl start jeelink2mqtt_wdog.service

+ 8 - 0
jeelink2mqtt_install_services.sh

@@ -0,0 +1,8 @@
+#!/bin/sh
+sudo cp jeelink2mqtt.service /lib/systemd/system
+sudo cp jeelink2mqtt_wdog.service /lib/systemd/system
+sudo systemctl daemon-reload
+sudo systemctl enable jeelink2mqtt.service
+sudo systemctl enable jeelink2mqtt_wdog.service
+sudo systemctl start jeelink2mqtt.service
+sudo systemctl start jeelink2mqtt_wdog.service

+ 3 - 0
jeelink2mqtt_start_services.sh

@@ -0,0 +1,3 @@
+#!/bin/sh
+sudo systemctl start jeelink2mqtt.service
+sudo systemctl start jeelink2mqtt_wdog.service

+ 3 - 0
jeelink2mqtt_stop_services.sh

@@ -0,0 +1,3 @@
+#!/bin/sh
+sudo systemctl stop jeelink2mqtt.service
+sudo systemctl stop jeelink2mqtt_wdog.service

+ 285 - 0
jeelink2mqtt_wdog.log

@@ -0,0 +1,285 @@
+2017-09-04 01:04:58 starting wdog...
+2017-09-04 01:06:58 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 01:17:56 starting wdog...
+2017-09-04 01:22:56 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 01:35:31 starting wdog...
+2017-09-04 01:35:31 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-09-04 01:39:32 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 02:13:47 starting wdog...
+2017-09-04 02:13:47 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 11:43:52 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 11:54:41 starting wdog...
+2017-09-04 13:43:42 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-04 23:13:47 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-09-08 12:07:49 starting wdog...
+2017-10-13 01:02:19 starting wdog...
+2017-10-13 01:02:19 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-13 01:03:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-13 01:04:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-13 01:07:20 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-13 01:12:20 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-17 20:59:15 starting wdog...
+2017-10-17 20:59:15 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-17 21:00:16 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-17 21:03:16 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-17 21:07:16 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-18 23:19:52 starting wdog...
+2017-10-18 23:19:52 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-18 23:23:53 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-19 12:03:21 starting wdog...
+2017-10-19 12:03:22 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-19 12:06:22 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-27 21:24:41 starting wdog...
+2017-10-27 21:24:41 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-10-27 21:34:42 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-27 21:37:42 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-10-27 21:40:42 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-12-03 21:11:35 starting wdog...
+2017-12-13 16:27:01 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2017-12-13 17:16:47 starting wdog...
+2017-12-13 17:16:48 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2017-12-13 17:20:48 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-16 21:38:50 starting wdog...
+2018-01-16 21:38:50 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-01-26 14:02:27 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 14:25:34 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 14:42:36 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 14:46:36 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 14:51:43 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 14:58:14 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:26:12 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:31:17 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:33:11 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:44:14 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:56:09 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 15:57:10 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 16:26:30 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 16:31:31 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 16:35:35 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 16:57:39 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 17:09:28 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-01-26 17:48:54 starting wdog...
+2018-01-26 17:48:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 15:00:42 starting wdog...
+2018-02-01 15:00:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:12:01 starting wdog...
+2018-02-01 17:12:01 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:13:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:14:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:15:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:16:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:17:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:18:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:19:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:20:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:21:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:22:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:23:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:24:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:25:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:26:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:27:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:28:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:29:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:30:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:31:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:32:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:33:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:34:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:35:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:36:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:37:02 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:38:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:39:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:40:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:41:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:42:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:43:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:44:03 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:45:39 starting wdog...
+2018-02-01 17:45:39 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-01 17:59:11 starting wdog...
+2018-02-01 17:59:11 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+1970-01-01 01:00:32 starting wdog...
+1970-01-01 01:00:33 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:02:12 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:03:12 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:04:12 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:05:12 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:06:12 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:07:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:08:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:09:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:10:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:11:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+1970-01-01 01:00:23 starting wdog...
+1970-01-01 01:00:23 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:41:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 18:53:32 starting wdog...
+2018-02-02 18:53:32 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-02 21:05:37 starting wdog...
+2018-02-02 21:05:37 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-04 21:16:38 starting wdog...
+2018-02-04 21:16:38 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-07 20:06:25 starting wdog...
+2018-02-07 20:06:25 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-07 20:17:48 starting wdog...
+2018-02-07 20:17:48 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-02-07 20:32:48 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-03-19 10:14:26 starting wdog...
+2018-03-19 10:14:26 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-03-19 10:15:27 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-03-21 11:16:26 starting wdog...
+2018-03-21 11:16:26 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-03-21 11:17:27 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-03-21 16:56:24 starting wdog...
+2018-03-21 16:56:24 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-03-21 16:57:25 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-18 11:22:37 starting wdog...
+2018-05-18 11:22:37 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-19 06:47:04 starting wdog...
+2018-05-19 06:47:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-19 06:48:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-19 06:49:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-19 06:50:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-19 06:51:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-24 06:34:44 starting wdog...
+2018-05-24 06:34:44 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-24 06:35:45 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-24 06:36:45 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-24 06:37:45 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-05-24 06:38:44 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-06 12:20:20 starting wdog...
+2018-06-06 12:20:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-09 22:31:42 starting wdog...
+2018-06-09 22:31:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-09 22:32:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-09 22:33:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-09 22:34:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-09 22:35:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-12 14:27:18 starting wdog...
+2018-06-12 14:27:18 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-12 14:28:19 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-15 09:17:40 starting wdog...
+2018-06-15 09:17:40 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-15 09:18:40 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-15 09:19:40 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-15 09:20:40 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-15 09:21:40 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-17 21:27:51 starting wdog...
+2018-06-17 21:27:51 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-17 21:28:51 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-17 21:29:51 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-17 21:30:51 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 01:32:00 starting wdog...
+2018-06-20 01:32:00 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 01:33:00 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 01:34:00 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 01:35:00 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 02:13:21 starting wdog...
+2018-06-20 02:13:21 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 02:14:21 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 02:15:21 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-20 02:16:21 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-25 20:01:58 starting wdog...
+2018-06-25 20:01:58 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-25 20:02:58 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-25 20:03:58 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-25 20:04:58 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-25 20:05:59 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:49:06 starting wdog...
+2018-06-27 12:49:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:50:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:51:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:52:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:53:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:55:06 starting wdog...
+2018-06-27 12:55:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:56:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:57:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:58:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 12:59:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:00:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:01:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:02:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:03:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:04:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:05:06 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:06:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:07:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:08:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:09:07 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:17:01 starting wdog...
+2018-06-27 13:17:01 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:19:54 starting wdog...
+2018-06-27 13:19:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:20:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:21:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:22:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:23:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:24:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:25:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:26:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:27:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:28:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:29:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:30:54 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 13:32:19 starting wdog...
+2018-06-27 13:32:19 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 15:59:13 starting wdog...
+2018-06-27 15:59:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-06-27 18:13:15 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-06-27 18:14:15 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-06-27 18:15:15 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-06-27 18:16:15 statusfile too old - jeelink2mqtt..py seems to hang - restarting
+2018-07-03 17:06:36 starting wdog...
+2018-07-03 17:06:36 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-07-18 13:18:19 starting wdog...
+2018-07-18 13:18:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-07-18 13:19:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-07-18 13:25:10 starting wdog...
+2018-07-18 13:25:10 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-07-18 13:26:10 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-03 23:19:48 starting wdog...
+2018-09-03 23:19:48 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-03 23:20:48 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-05 17:25:28 starting wdog...
+2018-09-05 17:25:28 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-05 17:26:29 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-06 21:45:42 starting wdog...
+2018-09-06 21:45:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-06 21:46:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-06 21:50:04 starting wdog...
+2018-09-06 21:50:04 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-06 21:51:05 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 09:59:45 starting wdog...
+2018-09-07 09:59:45 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 10:00:47 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 10:16:09 starting wdog...
+2018-09-07 10:16:09 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 10:17:09 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 10:38:33 starting wdog...
+2018-09-07 10:38:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 10:39:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-07 11:48:42 starting wdog...
+2018-09-07 11:48:42 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:29:13 starting wdog...
+2018-09-14 11:29:13 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:30:14 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:31:14 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:32:14 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:33:14 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:34:15 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:35:15 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:36:33 starting wdog...
+2018-09-14 11:36:33 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:37:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:38:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:39:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:40:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:41:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:46:20 starting wdog...
+2018-09-14 11:46:20 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:53:34 starting wdog...
+2018-09-14 11:53:34 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting
+2018-09-14 11:54:35 statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting

+ 11 - 0
jeelink2mqtt_wdog.service

@@ -0,0 +1,11 @@
+[Unit]
+Description=JeeLink2MQTT_wdog
+After=multi-user.target
+
+[Service]
+Type=simple
+ExecStart=/home/pi/jeelink2mqtt_wdog.sh
+Restart=always
+
+[Install]
+WantedBy=multi-user.target

+ 28 - 0
jeelink2mqtt_wdog.sh

@@ -0,0 +1,28 @@
+#!/bin/sh
+# monitor "jeelink2mqtt.py"
+
+logfile="/home/pi/jeelink2mqtt_wdog.log"
+statfile=/tmp/jeelink2mqtt_running
+
+timestmp=`date "+%Y-%m-%d %H:%M:%S"`
+echo $timestmp   starting wdog...>> $logfile
+
+while :
+do	
+	if [ -f "$statfile" ]
+	then 
+		if test `find "$statfile" -mmin +2` # Statusfile darf maximal 2 Minuten alt sein, sonst hängt vermutlich etwas (WS liefert mindestens alle 60s was)
+		then
+			timestmp=`date "+%Y-%m-%d %H:%M:%S"`
+			echo $timestmp   statusfile too old - jeelink2mqtt..py seems to hang - restarting >> $logfile
+			#killall wettersensoren.py
+			systemctl restart jeelink2mqtt.service
+		fi
+	else
+		timestmp=`date "+%Y-%m-%d %H:%M:%S"`
+		echo $timestmp   statusfile does not exist - jeelink2mqtt.py seems not to be running - restarting >> $logfile
+		#killall wettersensoren.py
+		systemctl restart jeelink2mqtt.service
+	fi
+	sleep 60
+done

+ 8 - 0
jeelink_sensors.csv

@@ -0,0 +1,8 @@
+ID,DomoticzIdx,Name
+4,1,T5-Arbeitszimmer
+16,94,Garten
+60,113,Parkplatz
+50,4,T5-Bad
+39,88,T5-Balkon
+55,6,T5-Kueche
+40,3,T5-Schlafzimmer

+ 84 - 0
jeelinkserlines.txt

@@ -0,0 +1,84 @@
+OK 9 27 1 4 232 52
+OK 9 40 1 4 223 55
+OK 9 17 129 4 237 52
+OK 9 39 1 4 122 70
+OK 9 16 1 4 116 80
+OK 9 50 1 4 221 56
+OK 9 60 1 4 118 75
+OK 9 27 1 4 232 52
+OK 9 40 1 4 223 55
+OK 9 16 1 4 116 80
+OK 9 39 1 4 122 70
+OK 9 50 1 4 221 56
+OK 9 43 1 4 131 106
+OK 9 55 1 4 222 54
+OK 9 27 1 4 232 52
+OK 9 40 1 4 223 55
+OK 9 16 1 4 116 80
+OK 9 39 1 4 122 70
+OK 9 50 1 4 221 56
+OK 9 55 1 4 222 54
+OK 9 27 1 4 232 52
+OK 9 16 1 4 116 80
+OK 9 39 1 4 122 70
+OK 9 50 1 4 221 56
+OK 9 43 1 4 131 106
+OK 9 50 1 4 222 56
+OK 9 55 1 4 222 54
+OK 9 60 1 4 118 75
+OK 9 16 1 4 116 80
+OK 9 40 1 4 223 55
+OK 9 39 1 4 122 70
+OK 9 50 1 4 221 56
+OK 9 50 1 4 222 56
+OK 9 27 1 4 232 52
+OK 9 55 1 4 222 54
+OK 9 60 1 4 118 75
+OK 9 16 1 4 116 80
+OK 9 40 1 4 223 55
+OK 9 39 1 4 122 70
+OK 9 50 1 4 221 56
+OK 9 43 1 4 131 106
+OK 9 39 1 4 111 72
+OK 9 40 1 4 223 55
+OK 9 60 1 4 107 77
+OK 9 16 1 4 106 82
+OK 9 55 1 4 222 54
+OK 9 50 1 4 222 55
+OK 9 17 129 4 237 52
+OK 9 39 1 4 112 73
+OK 9 60 1 4 108 77
+OK 9 16 1 4 106 82
+OK 9 50 1 4 222 56
+OK 9 40 1 4 223 55
+OK 9 55 1 4 222 54
+OK 9 50 1 4 222 55
+OK 9 39 1 4 111 73
+OK 9 8 1 4 128 106
+OK 9 60 1 4 107 77
+OK 9 50 1 4 222 55
+OK 9 39 1 4 111 72
+OK 9 40 1 4 223 55
+OK 9 44 129 4 238 53
+OK 9 55 1 4 222 54
+OK 9 50 1 4 221 55
+OK 9 39 1 4 111 72
+OK 9 44 129 4 238 53
+OK 9 43 1 4 124 106
+OK 9 50 1 4 222 55
+OK 9 39 1 4 111 73
+OK 9 40 1 4 223 55
+OK 9 44 129 4 238 52
+OK 9 60 1 4 107 77
+OK 9 55 1 4 222 54
+OK 9 50 1 4 222 56
+OK 9 39 1 4 111 73
+OK 9 16 1 4 107 82
+OK 9 44 129 4 238 52
+OK 9 50 1 4 222 55
+OK 9 39 1 4 111 73
+OK 9 40 1 4 223 55
+OK 9 44 129 4 238 52
+OK 9 60 1 4 107 77
+OK 9 55 1 4 222 54
+OK 9 50 1 4 222 55

+ 144 - 0
jeetest.py

@@ -0,0 +1,144 @@
+#!/usr/bin/python -u
+# -*- coding: utf-8 -*-
+#
+import serial
+import time
+import os
+import paho.mqtt.client as mqtt
+import json
+import math
+#import numpy as np
+
+def touch(fname, times=None):
+    with open(fname, 'a'):
+        os.utime(fname, times)
+        
+
+minUpdateInterval = 60
+override_updateinterval_on_change = False
+
+sensors = {}
+sensors_idx = {}
+with open("jeelink_sensors.csv", "r") as sensorscsv:
+    for line in sensorscsv:
+        if line.find('ID,DomoticzIdx,Name') == -1:
+            # nur Zeilen die nicht der header sind sind interessant
+            line = line.strip('\r')
+            line = line.strip('\n')
+            
+            parts = line.split(',')
+            sensorId = parts[0]
+            domoticzIdx = parts[1]
+            sensorName = parts[2]
+            
+            sensors[str(sensorId)] = str(sensorName)
+            sensors_idx[str(sensorId)] = str(domoticzIdx)
+
+        
+#sensors = {'4':'Arbeitszimmer','16':'AussenGarten','60':'AussenParkplatz','50':'Bad','39':'Balkon','55':'Kueche','40':'Schlafzimmer'}
+#sensors_idx = {'4':'1','16':'94','60':'113','50':'4','39':'88','55':'6','40':'3'}
+
+print sensors
+print sensors_idx
+
+sensors_lastTemp = {}
+sensors_lastHum = {}
+sensors_lastUpdate = {}
+
+try:
+    #while True:
+    with open("jeelinkserlines.txt", "r") as ins:
+        for serLine in ins:
+        #serLine = "OK 9 60 1 4 118 75"
+
+            ##if not null then...
+            if(serLine):
+                #print (repr(serLine))    #Echo the serial buffer bytes up to the CRLF back to screen
+                serLine = serLine.strip('\r')
+                serLine = serLine.strip('\n')
+                
+                print " "
+                print serLine
+                
+                if serLine.find('OK 9') != -1:
+                    # uns interessieren nur reinkommende Zeilen die mit "OK 9 " beginnen
+                    
+                    # 0  1 2  3   4   5   6
+                    # OK 9 ID XXX XXX XXX XXX
+                    # |  | |  |   |   |   |
+                    # |  | |  |   |   |   --- Humidity incl. WeakBatteryFlag
+                    # |  | |  |   |   |------ Temp * 10 + 1000 LSB
+                    # |  | |  |   |---------- Temp * 10 + 1000 MSB
+                    # |  | |  |-------------- Sensor type (1 or 2) +128 if NewBatteryFlag
+                    # |  | |----------------- Sensor ID
+                    # |  |------------------- fix "9"
+                    # |---------------------- fix "OK"
+                    
+                    bytes = serLine.split(' ')
+                    
+                    #addr = bytes[2]
+                    #addr = "{0:x}".format(int(bytes[2]))
+                    #addr = hex((int(bytes[2])))
+                    addr = int(bytes[2])
+                    addrhex = "{0:x}".format(int(bytes[2]))
+                    
+                    if int(bytes[3]) >= 128: 
+                        batt_new = 1
+                        type = int(bytes[3]) - 128
+                    else:
+                        batt_new = 0
+                        type = int(bytes[3])
+                    
+                    temp = (int(bytes[4])*256 + int(bytes[5]) - 1000)/10.0
+                    
+                    if int(bytes[6]) >= 128: 
+                        batt_low = 1
+                        hum = int(bytes[6]) - 128
+                    else:
+                        batt_low = 0
+                        hum = int(bytes[6])
+                        if hum > 100: 
+                            hum = 100
+                    
+                    
+                    currentsensor = sensors.get(str(addr), None)
+                    #if currentsensor == 0: 
+                    if currentsensor is None: 
+                        #fname = 'logs/jeelink_unknown_sensor_' + str(addr)
+                        #touch(fname)
+                        print "unknown sensor ID " + str(addr)
+                    
+                    lastUpdate = sensors_lastUpdate.get(str(addr), None)
+                    lastTemp = sensors_lastTemp.get(str(addr), None)
+                    lastHum = sensors_lastHum.get(str(addr), None)
+                    idx = sensors_idx.get(str(addr),None)
+                    
+                    senddata = False
+                    if idx is not None:
+                        if override_updateinterval_on_change:
+                            if lastTemp != str(temp) or lastHum != str(hum):
+                                senddata = True
+                                #print "hier! " + str(temp) + " != " + str(lastTemp) + "    " + str(hum) + " != " + str(lastHum)
+                        if lastUpdate is not None:
+                            timediff = int(time.time()) - lastUpdate
+                            if timediff >= minUpdateInterval:
+                                senddata = True
+                                #print "do!"
+                        else:
+                            senddata = True
+                    
+                    if senddata:                           
+                        sensors_lastUpdate[str(addr)] = int(time.time())
+                        sensors_lastTemp[str(addr)] = str(temp)
+                        sensors_lastHum[str(addr)] = str(hum)
+                        
+                        domoticz_json = "{\"idx\":" + str(idx) + ",\"nvalue\":0,\"svalue\":\"" + str(temp) + ";" + str(hum) + ";1\"}"
+                        
+                        print domoticz_json
+                                        
+                    print "addr: " + str(addr) + " = 0x" + str(addrhex) + "   batt_new: " + str(batt_new) + "   type: " + str(type) + "   batt_low: " + str(batt_low) + "   temp: " + str(temp) + "   hum: " + str(hum) + "   Name: " + str(currentsensor)
+                    time.sleep(1)
+                    
+
+except KeyboardInterrupt, e:
+    print('\n')