jeetest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/python -u
  2. # -*- coding: utf-8 -*-
  3. #
  4. import serial
  5. import time
  6. import os
  7. import paho.mqtt.client as mqtt
  8. import json
  9. import math
  10. #import numpy as np
  11. def touch(fname, times=None):
  12. with open(fname, 'a'):
  13. os.utime(fname, times)
  14. minUpdateInterval = 60
  15. override_updateinterval_on_change = False
  16. sensors = {}
  17. sensors_idx = {}
  18. with open("jeelink_sensors.csv", "r") as sensorscsv:
  19. for line in sensorscsv:
  20. if line.find('ID,DomoticzIdx,Name') == -1:
  21. # nur Zeilen die nicht der header sind sind interessant
  22. line = line.strip('\r')
  23. line = line.strip('\n')
  24. parts = line.split(',')
  25. sensorId = parts[0]
  26. domoticzIdx = parts[1]
  27. sensorName = parts[2]
  28. sensors[str(sensorId)] = str(sensorName)
  29. sensors_idx[str(sensorId)] = str(domoticzIdx)
  30. #sensors = {'4':'Arbeitszimmer','16':'AussenGarten','60':'AussenParkplatz','50':'Bad','39':'Balkon','55':'Kueche','40':'Schlafzimmer'}
  31. #sensors_idx = {'4':'1','16':'94','60':'113','50':'4','39':'88','55':'6','40':'3'}
  32. print sensors
  33. print sensors_idx
  34. sensors_lastTemp = {}
  35. sensors_lastHum = {}
  36. sensors_lastUpdate = {}
  37. try:
  38. #while True:
  39. with open("jeelinkserlines.txt", "r") as ins:
  40. for serLine in ins:
  41. #serLine = "OK 9 60 1 4 118 75"
  42. ##if not null then...
  43. if(serLine):
  44. #print (repr(serLine)) #Echo the serial buffer bytes up to the CRLF back to screen
  45. serLine = serLine.strip('\r')
  46. serLine = serLine.strip('\n')
  47. print " "
  48. print serLine
  49. if serLine.find('OK 9') != -1:
  50. # uns interessieren nur reinkommende Zeilen die mit "OK 9 " beginnen
  51. # 0 1 2 3 4 5 6
  52. # OK 9 ID XXX XXX XXX XXX
  53. # | | | | | | |
  54. # | | | | | | --- Humidity incl. WeakBatteryFlag
  55. # | | | | | |------ Temp * 10 + 1000 LSB
  56. # | | | | |---------- Temp * 10 + 1000 MSB
  57. # | | | |-------------- Sensor type (1 or 2) +128 if NewBatteryFlag
  58. # | | |----------------- Sensor ID
  59. # | |------------------- fix "9"
  60. # |---------------------- fix "OK"
  61. bytes = serLine.split(' ')
  62. #addr = bytes[2]
  63. #addr = "{0:x}".format(int(bytes[2]))
  64. #addr = hex((int(bytes[2])))
  65. addr = int(bytes[2])
  66. addrhex = "{0:x}".format(int(bytes[2]))
  67. if int(bytes[3]) >= 128:
  68. batt_new = 1
  69. type = int(bytes[3]) - 128
  70. else:
  71. batt_new = 0
  72. type = int(bytes[3])
  73. temp = (int(bytes[4])*256 + int(bytes[5]) - 1000)/10.0
  74. if int(bytes[6]) >= 128:
  75. batt_low = 1
  76. hum = int(bytes[6]) - 128
  77. else:
  78. batt_low = 0
  79. hum = int(bytes[6])
  80. if hum > 100:
  81. hum = 100
  82. currentsensor = sensors.get(str(addr), None)
  83. #if currentsensor == 0:
  84. if currentsensor is None:
  85. #fname = 'logs/jeelink_unknown_sensor_' + str(addr)
  86. #touch(fname)
  87. print "unknown sensor ID " + str(addr)
  88. lastUpdate = sensors_lastUpdate.get(str(addr), None)
  89. lastTemp = sensors_lastTemp.get(str(addr), None)
  90. lastHum = sensors_lastHum.get(str(addr), None)
  91. idx = sensors_idx.get(str(addr),None)
  92. senddata = False
  93. if idx is not None:
  94. if override_updateinterval_on_change:
  95. if lastTemp != str(temp) or lastHum != str(hum):
  96. senddata = True
  97. #print "hier! " + str(temp) + " != " + str(lastTemp) + " " + str(hum) + " != " + str(lastHum)
  98. if lastUpdate is not None:
  99. timediff = int(time.time()) - lastUpdate
  100. if timediff >= minUpdateInterval:
  101. senddata = True
  102. #print "do!"
  103. else:
  104. senddata = True
  105. if senddata:
  106. sensors_lastUpdate[str(addr)] = int(time.time())
  107. sensors_lastTemp[str(addr)] = str(temp)
  108. sensors_lastHum[str(addr)] = str(hum)
  109. domoticz_json = "{\"idx\":" + str(idx) + ",\"nvalue\":0,\"svalue\":\"" + str(temp) + ";" + str(hum) + ";1\"}"
  110. print domoticz_json
  111. 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)
  112. time.sleep(1)
  113. except KeyboardInterrupt, e:
  114. print('\n')