Configuring Webserver Pi

Creating SSH Key

The webserver needs its own SSH key for authenticating with the Sensor Pis. This key is required to be created before you set up the backend docker image.

  1. Run ssh-keygen -t rsa

  2. Press enter to store the key in the default location

  3. Press enter twice to create a key with no password

Copying the SSH Key

The created SSH key must now be copied to the sensor Pis.

  1. Ensure all four Pis are connected to the configured vending machine router via ethernet

  2. Run ssh-copy-id pi@192.168.0.11

  3. Enter yes and press enter

  4. Enter the password raspberry

  5. Test that the key was copied successfully with ssh pi@192.168.0.11. It should not prompt you for a password

  6. Enter exit to end the SSH connection

  7. Repeat the above steps for the other two sensor Pis with IPs 192.168.0.12 and 192.168.0.13

Setting the IP

The IP address of the webserver needs to be static. This should already be done, but these instructions still might be needed. This is easiest to do with a monitor, keyboard, and mouse.

  1. Right click on the internet icon in the top right of the raspberry pi toolbar.

  2. Select “Wireless & Wired Network Settings”

  3. Set “Configure” to “interface” and “eth0”.

  4. Ensure “Automatically configure empty options” is checked

  5. Ensure “Disable IPv6” is checked

  6. Set “IPv4 Address” to 192.168.0.100. (If you need to remove the static IP, ensure this field is blank.)

  7. Click “Apply” and restart the pi.

Installing Docker and Docker-Compose

Docker is used to manage multiple services for the backend and ensure the backend runs across reboots.

  1. Ensure the Pi is connected to the internet

  2. Install the latest docker with curl -sSL https://get.docker.com | sh

  3. Add the pi user to the docker group with sudo usermod -aG docker pi

  4. Reboot the Pi

  5. Install additional dependencies with sudo apt-get install -y libffi-dev libssl-dev

  6. Install Docker-Compose with sudo pip3 install docker-compose

Installing and Starting the Backend

If you just need to rebuild the image, skip to step 5.

  1. Ensure the Pi is connected to the internet

  2. Download the backend project with git clone https://github.com/Kalafut-organization/elephant_vending_machine_backend.git

  3. Use cd elephant_vending_machine_backend to enter the project directory

  4. Use cp ~/.ssh/id_rsa . to copy the SSH key you generated previously into the project directory

  5. Run docker-compose build --no-cached to build the images (Ensure you have a good internet connection for this step. An ethernet connection is recommended.)

  6. Connect the Pi to the configured project router via ethernet

  7. Start the backend server with docker-compose up

Installing and Starting the Frontend

If you just need to rebuild the image, skip to step 5.

  1. Ensure the Pi is connected to the internet

  2. Download the frontend project with git clone https://github.com/Kalafut-organization/elephant_vending_machine_frontend.git

  3. Update npm with curl https://www.npmjs.com/install.sh | sudo sh

  4. Navigate to the cloned directory

  5. Ensure that the .env file has the address that the backend is using. For the backend running in docker, this should be http://192.168.0.100.

  6. Remove the static IP of the pi. (see “Setting the static IP” above)

  7. Run docker-compose build --no-cached to build the images (Ensure you have a good internet connection for this step. An ethernet connection is recommended.)

  8. Restore the static IP of the pi. (see “Setting the static IP” above)

  9. Connect the Pi to the configured project router via ethernet

  10. Start the backend server with docker-compose up

Working in Development vs Production (Docker)

The purpose of getting this application to work with Docker is so that it can automatically run when the pi is turned on, and so that it can be easily run on different hardware in the future. During development, though, it is best to run the front and back end without docker so that realtime logging can be seen in terminals. Also, everytime a change is made, docker images need to be rebuilt (which takes a while). See the readme files in each github repository to see how to run everything in development.

If you are running in development, it is best to stop the docker containers first. To see if they are running, use the command docker ps. You can stop them by navigating into each repository and running docker-compose down. If you made changes and want to test running with docker, you just need to rebuild the images then start the containers (see “Installing and Starting the Frontend/Backend” above). The containers should then automatically start everytime the webserver pi is turned on.

When you are switching between these two methods of running the code, you have to make sure the front end can still access the back end. The address used for this is defined in .env in the front end. When running in development, it should have port 5000. When running in production with docker, it should not have a port.

Finally, keep in mind that the front end is configured to run on port 3000 in development and port 4000 in production with docker.