Folder and File Structure
Pi
This directory contains the scripts that will be run on the Raspberry Pi.
This includes individual Python scripts for each part of functionality e.g. run_inference.py
, along with the main.py
file that contains the main Python workflow that is run as part of the deployment from Node-Red.
This directory also contains the start.sh
script that runs the Node-Red deployment with the correct parameters.
Physical Folder
The Physical folder is the base for all sensing and actuating functions. It holds the two folders - Arduino and Node-Red, as well as the file lcd_display.py, which is used by the Node-Red for LCD outputs.
The Arduino folder holds the two Arduino sketches, one for each Arduino (Arduino 0 and Arduino 1). These Arduino numbers are to be assigned based on the ACM port each Arduino is connected to via USB (ACM0 and ACM1). The Node-Red folder holds the Node-Red flow .json which needs to be manually imported to Node-Red.
Other Folders
Other folders in the Pi directory act as logical separation for scripts within the main Python workflow.
Site
This folder holds the logic for the website. This consists of TypeScript files for pages and API endpoints of the site, alongside the backend functionality for handling user requests and sessions. This is run through the npm package manager, through command npm run dev
. A sub-folder in this folder holds the logic for interacting with the database, making SQL queries to an externally hosted Postgres database. See the README.md in the Site sub-folder for more granular information on site folder directories.
Pi Setup Instructions
Prerequisites
Python and Venv
- Install Python version 3.10.9
- Ensure that you have the correct version of python selected with
python --version
- If the response is not
Python Version 3.10.9
or similar, change your active version to the newly installed version
- If the response is not
- Navigate to the
pi
directory withcd cm2305/pi
- Create the virtual environment with
python -m venv ecosort
- Activate the environment with
./ecosort/bin/activate
(Linux) or./ecosort/Scripts/activate
(Windows) - install all required libraries with
pip install -r ./requirements.txt
Node-Red
- Install Node-Red with
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
. - On completion, run it with
node-red
. Open the given link for the browser interface. - Navigate to Settings>Manage Palette.
- Ensure the following packages appear:
node-red
node-red-node-serialport
- If not, search for and install them. Arduino IDE
- Install the IDE with
sudo apt-get install arduino
. - With the IDE, go to Tools>Manage Libraries....
- Install the
Servo
library.
Installation and Configuration
-
Set Up Physical Components:
- 1x Raspberry Pi
- 1x Raspberry Pi GrovePi+ Board
- 2x Arduino
- 2x Arduino Grove Hat
- 2x Servo Motor
- 2x Button
- 1x Motion Sensor
- 1x Pi Camera Module
- 2x USB-A to MicroUSB Cable
- 1x V5.0 RGB LCD Screen
-
Raspberry Pi:
- Install GrovePi+ Board
- Pin I2C (any): V5.0 RGB LCD Screen
- Ribbon Cable Port: Pi Camera Module
-
Arduino 0:
- Install Grove Hat
- Pin D5: Servo Motor
- Pin D3: Button
- Pin D6: Button
- Connect to Raspberry Pi using MicroUSB cable (ACM Port 0)
-
Arduino 1:
- Install Grove Hat
- Pin D5: Servo Motor
- Pin D2: Motion Sensor
- Connect to Raspberry Pi using MicroUSB cable (ACM Port 1)
- Upload Arduino Sketches with Arduino IDE
- Arduino 0:
arduino0_motor_button
- Arduino 1:
arduino1_motor_motionsensor
- Import Node-Red Flow from
cm2305/pi/physical/node-red
by Clicking Import in Node-Red Browser Interface
Website Setup Instructions
Navigate to the \site
directory in your shell of choice, then run npm install
to
install all required packages for the site. This may take a while.
Add a .env file to the directory, consisting of the following.
SESSION_SECRET=secretword
DATABASE_URL=postgres://neondb_owner:npg_4y5eOQkbnVsg@ep-dry-tree-abeicwnr-pooler.eu-west-2.aws.neon.tech/neondb?sslmode=require
The site is now ready to run locally, and will connect to the Postgres database without issue.
Run npm run dev
then navigate to http://localhost:3000 to view the site.
Running the Project
- On the Pi, run
~/cm2305/pi/start.sh
. On running, this should start Node-Red with required libraries.
Third-Party Software and Frameworks
-
Python 3.10.9
- Purpose: Core programming language used for backend scripts and control logic on the Raspberry Pi.
- Documentation: https://www.python.org/downloads/release/python-3109/
-
OpenCV
- Purpose: Python library for image processing and computer vision, intregal to classification pipeline.
- Documentation: https://pypi.org/project/opencv-python/
-
Inference SDK
- Purpose: Python library to integrate RoboFlow workflows, used for running CV model.
- Documentation: https://pypi.org/project/inference-sdk/
-
Node-RED (latest via official installer)
- Purpose: Visual programming environment to wire together hardware, APIs, and data flows.
- Documentation: https://nodered.org/docs/
-
node-red-node-serialport (latest)
- Purpose: Node-RED node to enable serial communication with Arduino boards over USB.
- Documentation: https://flows.nodered.org/node/node-red-node-serialport
-
Arduino IDE 1.8.xx or newer
- Purpose: Used to write and upload sketches to the two Arduinos.
- Documentation: https://www.arduino.cc/en/software
-
Servo Arduino Library 1.1.8
- Purpose: Controls servo motors connected to Arduino pins.
- Documentation: https://www.arduino.cc/reference/en/libraries/servo/
-
Node.js 22.14.0 and npm
- Purpose: JavaScript runtime and package manager required to install and run Node-RED on the Raspberry Pi.
- Documentation: https://nodejs.org/en
-
smbus2 0.5.0
- Purpose: Python library used for I2C communication with devices like the Grove LCD screen.
- Documentation: https://pypi.org/project/smbus2/
-
RPi.GPIO 0.7.1
- Purpose: Python library for controlling GPIO pins on the Raspberry Pi.
- Documentation: https://pypi.org/project/RPi.GPIO/
-
HeroUI 2.7
- Purpose: Used for UI components to make the site look pretty.
- Documentation: HeroUI v2.7
-
BCrypt for Node JS 5.1.1
- Purpose: Used to hash passwords for secure storage.
- Documentation: - BCrypt for Node JS - 5.1.1
-
D3 7.9
- Purpose: Displaying the dashboard bar chart.
- Documentation: d3 - 7.9
-
Next JS 15
- Purpose: For the website's server model, building on React
- Documentation: Next JS 15
-
React 18
- Purpose: For the site's frontend matter
- Documentation: React 18
-
Zod 3.24
- Purpose: Used to validate the Signup form fields
- Documentation: Zod 3.24
-
Postgres 3.4 for Node
- Purpose: Used to query the Postgres database
- Documentation: Postgres 3.4 for Node
Used for Hosting, not in repo:
-
Vercel
- Purpose: For the site, allowing for constant uptime
- Documentation: Vercel
-
Neon
- Purpose: For hosting the Postgres database with constant uptime.
- Documentation: Neon
Code Documentation
View separate README for more info on site.
Code is primarily documented via inline annotations.
The Node-Red flow features two 'comment' nodes that act as pointers for developers, such as an explaination of the Lock and Unlock nodes.
Troubleshooting
Pi
Swapped Arduino Functions
It's important that the two Arduinos are correctly configured with the right components, sketch, and serial port. If, on monitoring in Node-Red, the Arduinos have swapped functions, it's most likely the serial ports are incorrect. It should be Arduino 0 = ACM0 and Arduino 1 = ACM1. The easiest way to correct this is by unplugging both Arduinos and replugging them, with Arduino 0 being plugged in first.
Stranded in Middle of Node-Red Flow
If a component is misconfigured and interferes at a point in time after passing the Lock node but not the Unlock node, the system will never reset and won't take accept new inputs. If this happens, stop Node-Red with ctrl+c and re-enter ~/cm2305/pi/start.sh
.