Making a server application using Fast API, React, Raspberry Pi inside a Windows machine

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Making a server application using Fast API, React, Raspberry Pi inside a Windows machine

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật