[Python] Sensor script using DASH for User interface

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    Im working on a project at work that requires a UI for the machine operator to input required fields. The issue im having is trying to get the serial output to effect the blocks(Lowercase) input from the dcc.input its using a ultrasonic sensor and i need it to subract when a object(block) is place within a distance (>3500) then update the Dash app to relefct this in real time at current it makes no effect to the number displayed on the dashboard

    import dash
    from dash import dcc, html, Input, Output
    import serial
    import re

    app = dash.Dash(__name__)
    app.title = "Companyname COUNTER"


    app.layout = html.Div(style={'fontFamily': 'Arial, sans-serif', 'backgroundColor': '#f0f4f8', 'padding': '30px'},
    children=[
    html.H1("Companyname BLOCK COUNTER", style={'textAlign': 'center', 'color': '#333'}),

    html.Div([
    html.Div([
    html.Label("Type:", style={'color': '#fff'}),
    dcc.Input(
    id='input-1',
    type='text',
    value='EPS70',
    debounce=True,
    style={'width': '100%', 'padding': '10px', 'border': 'none', 'borderRadius': '5px'}
    )
    ], style={'backgroundColor': 'blue', 'padding': '20px', 'margin': '10px', 'borderRadius': '10px', 'width': '200px'}),
    html.Div([
    html.Label("Colour:", style={'color': '#fff'}),
    dcc.Input(
    id='input-2',
    type='text',
    value='Silver',
    debounce=True,
    style={'width': '100%', 'padding': '10px', 'border': 'none', 'borderRadius': '5px'}
    )
    ], style={'backgroundColor': 'blue', 'padding': '20px', 'margin': '10px', 'borderRadius': '10px', 'width': '200px'}),

    html.Div([
    html.Label("Size:", style={'color': '#fff'}),
    dcc.Input(
    id='input-3',
    type='text',
    value='0',
    debounce=True,
    style={'width': '100%', 'padding': '10px', 'border': 'none', 'borderRadius': '5px'}
    )
    ], style={'backgroundColor': 'blue', 'padding': '20px', 'margin': '10px', 'borderRadius': '10px', 'width': '200px'}),

    html.Div([
    html.Label("Amount:", style={'color': '#fff'}),
    dcc.Interval(
    id='input-5',
    interval=1 * 1000, # in milliseconds
    n_intervals=0),
    dcc.Input(
    id='input-4',
    type='number',
    value=0,
    debounce=True,
    style={'width': '100%', 'padding': '10px', 'border': 'none', 'borderRadius': '5px'}

    )

    ], style={'backgroundColor': 'blue', 'padding': '20px', 'margin': '10px', 'borderRadius': '10px', 'width': '200px'}),

    ], style={'display': 'flex', 'justifyContent': 'center', 'flexWrap': 'wrap'}),

    html.Hr(),

    html.Div(id='output-display', style={
    'textAlign': 'center',
    'fontSize': '100px',
    'marginTop': '20px',
    'backgroundColor': '#ffffff',
    'padding': '20px',
    'borderRadius': '10px',
    'boxShadow': '0 4px 8px rgba(0, 0, 0, 0.1)'
    }),
    dcc.Interval(
    id='interval-component',
    interval=1000, # 1000 ms = 1 second
    n_intervals=0
    )
    ])


    @app.callback(
    Output('output-display', 'children'),
    [Input(f'input-{i+1}', 'value') for i in range(4)]
    )

    def update_output(name, color, category, blocks, n_intervals):
    conditions_met = False
    Blocks = blocks
    ser = serial.Serial(port='COM7', baudrate=57600)
    for i in range(blocks):
    value = ser.readline(8)
    if value.startswith(b'R'):
    values_string = str(value)
    values_string = re.sub('[^0-9]', '', values_string)
    values_number = int(values_string)
    if values_number < 3500:
    if Blocks == Blocks and not conditions_met:
    Blocks -= 1
    conditions_met = True
    if values_number > 4000:
    conditions_met = False
    return (f"Type: {name} | Colour: {color} | Size: {category} | Amount: {blocks}")

    if __name__ == '__main__':
    app.run(debug=True)

    Continue reading...

Compartilhe esta Página