Capstone Simulation (3D) — Raspberry Pi 3 simulation

Capstone 1

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

Components

Sketch code

import time
import csv
import json
import os
import random
import urllib.request

from datetime import datetime

try:
    import RPi.GPIO as GPIO
except ImportError:
    GPIO = None

try:
    from smbus2 import SMBus
except ImportError:
    SMBus = None

try:
    from RPLCD.i2c import CharLCD
except ImportError:
    CharLCD = None


# ==========================================
# HERMES I - RASPBERRY PI
# ==========================================

ESP32_IP = "REPLACE_WITH_ESP32_IP"
ESP32_URL = f"http://{ESP32_IP}/status"

UPDATE_INTERVAL = 1.0
NETWORK_TIMEOUT = 1.0

MPU6050 = 0x68
BMP280 = 0x76
ADS1115 = 0x48
LCD_ADDRESS = 0x27

TRIG = 27
ECHO = 22

STATUS_LED = 17
BUTTON1 = 23
BUTTON2 = 24
BUZZER = 18

BATTERY_CAPACITY_AH = 10.0
LOW_BATTERY = 20.0
CRITICAL_BATTERY = 10.0

DATA_DIR = "hermes_data"
TELEMETRY_FILE = os.path.join(
    DATA_DIR, "telemetry.csv"
)

gpio_ready = False
bus = None
lcd = None

low_battery_test = False
connection_loss_test = False

last_button1 = True
last_button2 = True

remaining_ah = BATTERY_CAPACITY_AH

mpu_ready = False
adc_ready = False


# ==========================================
# INITIALIZATION
# ==========================================

def initialize():

    global gpio_ready, bus, lcd
    global mpu_ready, adc_ready

    if GPIO is not None:

        try:
            GPIO.setwarnings(False)
            GPIO.setmode(GPIO.BCM)

            GPIO.setup(
                STATUS_LED,
                GPIO.OUT,
                initial=GPIO.LOW
            )

            GPIO.setup(
                BUZZER,
                GPIO.OUT,
                initial=GPIO.LOW
            )

            GPIO.setup(
                BUTTON1,
                GPIO.IN,
                pull_up_down=GPIO.PUD_UP
            )

            GPIO.setup(
                BUTTON2,
                GPIO.IN,
                pull_up_down=GPIO.PUD_UP
            )

            gpio_ready = True

        except Exception as error:
            print("GPIO error:", error)

    if SMBus is not None:

        try:
            bus = SMBus(1)
        except Exception as error:
            print("I2C error:", error)

    if bus is not None:

        try:
            bus.write_byte_data(
                MPU6050, 0x6B, 0
            )
            mpu_ready = True

        except Exception:
            pass

        try:
            bus.read_byte(ADS1115)
            adc_ready = True

        except Exception:
            pass

    if CharLCD is not None:

        try:
            lcd = CharLCD(
                "PCF8574",
                LCD_ADDRESS,
                port=1,
                cols=16,
                rows=2
            )

        except Exception as error:
            print("LCD error:", error)

    os.makedirs(DATA_DIR, exist_ok=True)


# ==========================================
# LCD DISPLAY
# ==========================================

def update_lcd(line1, line2):

    if lcd is None:
        return

    try:
        lcd.cursor_pos = (0, 0

More projects by lusyyy-am