123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #!/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')
|