Electronics 3 LabDesign Demo — Raspberry Pi 4 simulation

Official

An interactive Raspberry Pi 4 circuit simulation you can run free in your browser on Velxio, by lusyyy-am.

Components

Sketch code

import os
import csv
import time
import serial
import threading
import traceback
from datetime import datetime

import RPi.GPIO as GPIO
from RPLCD.i2c import CharLCD


# ==============================
# SETTINGS
# ==============================

CATS = {
    0: "Max",
    1: "Ruby",
    2: "Jasmine",
    3: "Belle",
    4: "Mango"
}

RFID_PORT = "/dev/ttyAMA0"

# BCM GPIO numbers
HX_DOUT = 9
HX_SCK = 11

SENSOR_OFFSET = 50000
COUNTS_PER_KG = 420000
MEASUREMENT_DELAY = 120  # seconds after RFID entry
MEASUREMENT_COUNT = 3

DATA_DIR = "sensikitty_data"
os.makedirs(DATA_DIR, exist_ok=True)

VISITS_FILE = os.path.join(DATA_DIR, "visits.csv")
CATS_FILE = os.path.join(DATA_DIR, "cats.csv")
SUMMARY_FILE = os.path.join(DATA_DIR, "daily_summary.csv")

FIELDS = [
    "Visit_ID", "Cat_ID", "RFID", "Name",
    "Entry_Time", "Exit_Time", "Duration_Seconds",
    "Weight_kg", "Status", "Media_Status",
    "Video_Reference", "Audio_Reference", "AI_Alert"
]

lock = threading.RLock()
running = threading.Event()
running.set()

current_cat = None
current_visit = None
active_view = "dashboard"

weight_total = 0.0
weight_count = 0
entry_monotonic = None
last_raw = None
last_weight_kg = None
scale_status = "Waiting"

lcd = None


# ==============================
# CSV STORAGE
# ==============================

def read_csv(path):
    if not os.path.exists(path):
        return []

    with open(path, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def write_csv(path, fields, rows):
    temp = path + ".tmp"

    with open(temp, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(
            f, fieldnames=fields, extrasaction="ignore"
        )
        writer.writeheader()
        writer.writerows(rows)

    os.replace(temp, path)


def save_summary():
    summary = {}

    for v in read_csv(VISITS_FILE):
        day = v.get("Entry_Time", "")[:10]
        name = v.get("Name", "")

        if not day or name not in CATS.values():
            continue

        summary.setdefault(
            day, {n: 0 for n in CATS.values()}
        )
        summary[day][name] += 1

    write_csv(
        SUMMARY_FILE,
        ["Date"] + list(CATS.values()),
        [
            {"Date": day, **counts}
            for day, counts in sorted(summary.items())
        ]
    )


def initialize_data():
    if not os.path.exists(CATS_FILE):
        write_csv(
            CATS_FILE,
            ["Cat_ID", "RFID", "Name"],
            [
                {
                    "Cat_ID": f"CAT{uid:03d}",
                    "RFID": uid,
                    "Name": name
                }
                for uid, name in CATS.items()
            ]
        )

    if not os.path.exists(VISITS_FILE):
        write_csv(VISITS_FILE, FIELDS, [])

    save_summary()


# ==============================
# LCD
# ==============================

def update_lcd():
    global lcd

    if lcd is None:
        return

    if current_cat is None:
        lines = ("Sensi

More projects by lusyyy-am