Skip to content
Snippets Groups Projects
Name Last commit Last update
pi
site
.gitignore
README.md
README_WEBAPP.md

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
  • Navigate to the pi directory with cd 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

  1. 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)
  1. Upload Arduino Sketches with Arduino IDE
  • Arduino 0: arduino0_motor_button
  • Arduino 1: arduino1_motor_motionsensor
  1. 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

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.