summaryrefslogtreecommitdiffstats
path: root/skripti/bme280_udp_logger.py
blob: 1debe6c947491f0c76649c53cc90d40244f66994 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()