summaryrefslogtreecommitdiffstats
path: root/skripti/bme280_udp_logger.py
diff options
context:
space:
mode:
Diffstat (limited to 'skripti/bme280_udp_logger.py')
-rwxr-xr-xskripti/bme280_udp_logger.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/skripti/bme280_udp_logger.py b/skripti/bme280_udp_logger.py
new file mode 100755
index 0000000..1debe6c
--- /dev/null
+++ b/skripti/bme280_udp_logger.py
@@ -0,0 +1,28 @@
+#!/usr/bin/python3
+import socket
+import struct
+import sqlite3
+sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM);
+sock.bind(("::", 3232))
+conn = sqlite3.connect('db')
+cur = conn.cursor()
+cur.execute('''
+CREATE TABLE IF NOT EXISTS meritve (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ temp REAL NOT NULL,
+ humid REAL NOT NULL,
+ pres REAL NOT NULL,
+ datetime DATETIME DEFAULT CURRENT_TIMESTAMP
+)
+''')
+conn.commit()
+while True:
+ data, addr = sock.recvfrom(1024)
+ if len(data) != 16:
+ print(f"Received malformed from {addr}: {data}")
+ continue
+ info = struct.unpack("ffff", data)
+ print(f"Received from {addr}: {info}")
+ cur = conn.cursor()
+ cur.execute('INSERT INTO meritve (temp, humid, pres) VALUES (?, ?, ?)', (info[1], info[2], info[3]))
+ conn.commit()