System Specs
- Processor – i5 13th Gen
- RAM – 16GB
- SSD – Nvme 500GB
- OS – Windows Home
- Raspberry Pi – Pi4 8GB
I have a Fast API application which is hosted on a Windows machine, also a react app on the same machine. The React App uses the fastAPI app for CRUD operations. This service is used to store the data for production lines in a manufacturing plant recording production count of each line in different shifts everyday.
The production count is sent to the server for each line via Raspberry Pi which is connected to the machine in the line which send the count via API from fastAPI app. Now the Raspberry Pi also has a screen which is used to display the details like production count target count balance etc.
I have written a script in raspberry pi which opens up the pywebview window and displays the react app page on the display, below is the script.
import webview
import requests
import threading
import time
# Define the URL to load on success
success_url = 'http://172.17.17.110:3000/line/12'
# Define the path to the error HTML file
error_html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connection Error</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f8f8f8;
font-family: Arial, sans-serif;
}
.error-message {
font-size: 48px;
color: #ff0000;
text-align: center;
}
</style>
</head>
<body>
<div class="error-message">Connection Error</div>
</body>
</html>
"""
# Define the health check API URL
health_check_url = 'http://172.17.17.110:8000/healthz'
# Time interval (in seconds) between health checks
check_interval = 5
request_timeout = 5
def load_error_html(window):
window.evaluate_js(f'document.documentElement.innerHTML = `{error_html_content}`')
def health_check(window):
first_success = False
while True:
print("FIRST SUCCESS", first_success)
try:
response = requests.get(health_check_url, timeout=request_timeout)
print("RESPONSE", response)
if response.status_code == 200:
if not first_success:
# Load the success URL in the webview window
window.load_url(success_url)
first_success = True
else:
if first_success:
# If the health check fails, load the error HTML
load_error_html(window)
first_success = False
except requests.RequestException:
print("REQUEST EXCEPTION")
if first_success:
load_error_html(window)
first_success = False
except requests.Timeout:
print("TIMEOUT EXCEPTION")
if first_success:
# If there is an exception, load the error HTML
load_error_html(window)
fisrt_success = False
# Wait for the specified interval before checking again
time.sleep(check_interval)
def create_window():
# Create an initial window with the error HTML
window = webview.create_window(title='IILMS',
html=error_html_content,
width=1920, height=1080, fullscreen=True,
resizable=False, text_select=False)
# Start the health check in a background thread
threading.Thread(target=health_check, args=(window,), daemon=True).start()
# Start the webview
webview.start()
if __name__ == '__main__':
create_window()
This script runs automatically on startup. There is another script running in background in Raspberry Pi which listens to the GPIO pins to send data to fastAPI app.
The fastAPI and React app are being run on startup using uvicorn main:app --host 0.0.0.0 --port 8000
and npm start --host 0.0.0.0
respectively.
When the windows machine starts up two cmd windows are created inside which above commands are ran. This whole system including the Raspberry Pis (count 16) and server machine are connected via local wired connection in which all the Raspberry Pis are connected with a switch and then the switch is connected with a Wi-Fi router and server is connected to the router via LAN cable.
The problem
The problem is that the script provided above gives a connection error with the HTML page if the Raspberry Pi has more than 7 connections active at a time.
Before the wired connection the Raspberry Pis were connected via Wi-Fi using Wi-Fi repeaters (2.4Ghz) connected to a main Wi-Fi router and the problem was the Raspberry Pi started disconnecting automatically from the repeaters even after setting the connection to be automatic and were not connecting after trying everything but other devices like mobiles and laptops were able to connect to the repeaters.
Then I decide to go for wired approach but now the above problem came up. I’ve got no idea what’s wrong with the overall setup and I thought Wi-Fi router might not be able to handle that many requests so I directly plugged the switch into the server machine, but still no luck.
2