| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 | 
							- #!/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)
 
-                     
 
-                     lacrosse_json = "{\"temperature\":" + str(temp) + ", \"humidity\":" + str(hum) + ", \"battery\":\"" + str(batterystate) + "\"}"
 
-                     mqttc.publish(mqtt_topic_prefix+"/"+str(currentsensor_name)+"/json", lacrosse_json, 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')
 
 
  |