3B+側
import smbus
import time
import json
import sqlite3
import streamlit as st
bus = smbus.SMBus(1)
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS data
(ID text, Value real, Timestamp text)''')
def get_data():
data_json = bus.read_i2c_block_data(0x42, 0, 16)
data_str = ''.join(chr(i) for i in data_json)
data = json.loads(data_str)
return data
def save_data(data):
c.execute("INSERT INTO data VALUES (?, ?, ?)",
(data['ID'], data['Value'], data['Timestamp']))
conn.commit()
def display_data(data):
st.write(f"ID: {data['ID']}")
st.write(f"Value: {data['Value']}")
st.write(f"Timestamp: {data['Timestamp']}")
while True:
data = get_data()
save_data(data)
display_data(data)
time.sleep(2)
conn.close()