11 from sqlalchemy import create_engine
13 import MySQLdb.cursors
15 #May 27 21:32 /sys/bus/w1/devices/28-0416a1bab9ff
16 #May 27 21:33 /sys/bus/w1/devices/28-0416a1ac66ff
17 #May 27 21:35 /sys/bus/w1/devices/28-0516a207a4ff
18 #May 27 21:38 /sys/bus/w1/devices/28-0316a2193bff
19 #May 27 21:38 /sys/bus/w1/devices/28-0316a21383ff
22 # einen Sensor auslesen
25 class ReadsensorError(RuntimeError):
29 def readsensor(sensor_id):
30 sensorfile = "/sys/bus/w1/devices/28-{}/w1_slave".format(sensor_id)
31 file = open(sensorfile)
34 # 64 01 4b 46 7f ff 0c 10 01 : crc=01 YES
35 # 64 01 4b 46 7f ff 0c 10 01 t=22250
38 linecrc = file.readline()
39 match = re.search(": crc=[0-9a-f]{2} (YES|NO)",linecrc)
41 yesno = match.group(1)
43 raise ReadsensorError('Could not evaluate sensorfile "{}"'.format(sensorfile))
46 linetemp = file.readline()
47 match = re.search(" t=([-0-9]+)",linetemp)
49 temp_raw = match.group(1)
50 temp = float(temp_raw)/1000
53 jetzt = datetime.datetime.now()
55 return jetzt, temp_raw, temp
58 def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
59 # Schreiben des csv-files
60 with open(os.path.expanduser(config.get("csv", "filename")), "a", newline = "") as file:
61 writer = csv.writer(file, dialect = "excel")
62 writer.writerow([timestamp.strftime("%Y-%m-%d %H:%M"), sensor_id, sensor_name, "{:.1f}".format(value)])
65 def readcsvfile(csvfile):
66 with open(csvfile, "r", newline="") as file:
67 yield from csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value"))
70 def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
71 # Schreiben des db-files
72 user = config.get('database', 'user')
73 pwd = config.get('database','password')
74 host = config.get('database','hostname')
75 db = config.get('database','database')
77 engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
78 conn = engine.connect()
79 with warnings.catch_warnings():
80 # ignore _mysql_exceptions.Warning: Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'
81 warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning)
82 conn.execute("insert ignore into sensors (sensor_id, sensor_name, value_type, value_raw, value, timestamp) values (%s,%s,%s,%s,%s,%s)", sensor_id, sensor_name, value_type, value_raw, value, timestamp)
86 def process_csv_file(config, fromcsvfile, value_type, sensors):
87 # "timestamp", "sensor_id", "sensor_name", "value"
88 sensors_ids = [sensor_id for sensor_id, sensor_name in sensors]
89 for record in readcsvfile(fromcsvfile):
90 timestamp, sensor_id, sensor_name, value = (record["timestamp"], record["sensor_id"], record["sensor_name"], float(record["value"]))
91 if sensor_id in sensors_ids:
93 writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
96 def main(configfile, fromcsvfile):
97 config = configparser.ConfigParser()
98 config.read(configfile)
99 sensors = config.items('temperature')
100 value_type = "Wassertemperatur"
103 process_csv_file(config, fromcsvfile, value_type, sensors)
106 for sensor_id, sensor_name in sensors:
107 timestamp, value_raw, value = readsensor(sensor_id)
108 writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
109 writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
110 logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))
113 if __name__ == '__main__':
114 default_config_file = os.path.expanduser('~/seewasser.ini')
115 parser = argparse.ArgumentParser(description='Read sensor data')
116 parser.add_argument('--config', default=default_config_file, help='configuration file')
117 parser.add_argument('--fromcsvfile', help='write values from csv file to database')
118 args = parser.parse_args()
119 main(args.config, args.fromcsvfile)