summaryrefslogblamecommitdiffstats
path: root/skripti/bme280_udp_logger.py
blob: 1debe6c947491f0c76649c53cc90d40244f66994 (plain) (tree)



























                                                                                                        
#!/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()