diff --git a/README.md b/README.md index 10d008e..a477c5e 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use ``` 1. [Gadget Mode to setup a serial connection over USB](gadget.md) -1. [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) - +1. [Setup the OLED screen to print the IP](oled-display/README.md) + Based upon [Marcel's script on github to show the IP address on the OLED screen](https://github.com/mkyas/cpsy-display-ip) 1. Install Blinka by following the guide [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)] ## RU student-only guides at ROB-IOT diff --git a/Scripts/serial-ether-gadget.service b/Scripts/serial-ether-gadget.service index 05a912d..de4ff66 100644 --- a/Scripts/serial-ether-gadget.service +++ b/Scripts/serial-ether-gadget.service @@ -1,6 +1,7 @@ [Unit] Description=Serial and Ethernet over OTG USB -After=systemd-user-sessions.service +#After=systemd-user-sessions.service +Before=serial-getty@ttyGS0.service [Service] ExecStart=/root/rover-pi/Scripts/gadget.sh [Install] diff --git a/gadget.md b/gadget.md index 30afa82..2b46559 100644 --- a/gadget.md +++ b/gadget.md @@ -1,127 +1,129 @@ -# Gadget Mode to setup a serial connection over USB -Prepare Raspberry Pi OS connection over a microUSB serial cable - -Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget: -The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 - -## Important : -- Some of the serial cables we get from our supplier are flakey: if wiggling the cable when plugged into your computer causes it to sometimes see the serial port and sometimes not, get a new one. -- **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. -- You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. - - -## Procedure - -Make sure you have your USB cable plugged into the left port **USB** and not the right port marked **PWR**. -1. Open a connection to the device via ssh or Raspberry Pi Connect -1. Become root (the superuser) -``` -sudo su -``` -1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) -``` -echo "dtoverlay=dwc2" >> /boot/firmware/config.txt -``` -1. We add the required module `dwc2` for loading at startup -``` -echo "dwc2" >> /etc/modules -``` -1. Check that we can load the libcomposite module -``` -modprobe libcomposite -``` -1. Reboot -1. Open a new connection and become root (the superuser) -``` -sudo su -``` - -1. Put a copy of this repository in root's home directory using git -``` -apt install -y git -cd /root -git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git -``` -1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent - ``` -cd rover-pi/Scripts/ -./gadget.sh -``` -1. Install the systemd -``` -cp serial-ether-gadget.service /lib/systemd/system/. -``` -1. Enable the service -``` -systemctl enable serial-ether-gadget.service -``` -1. Enable the getty (which connects a login terminal to the serial port) -``` -systemctl enable serial-getty@ttyGS0.service -``` -1. Reboot and see if the serial port appears - -# Connecting to the serial terminal -You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port - -## Windows -- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html -- MobaXterm: https://mobaxterm.mobatek.net/ - -## Linux -If using a Linux VM, you will need to pass the Pi USB connection there. -This is similar to the procedure for accessing an Arduino. -1. Start the Linux VM if you haven't already -1. Menu: Devices > USB -1. Look for "Silicon Labs CP210x USB to UART Bridge" and click on it. -1. check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port. - ``` - ls -l /dev/ttyU* - ``` -1. The user must be in the dialout group to access it. Use the Linux command: `uid` -to check if you are in the group. If not in the dialout group, use: - ``` - sudo usermod -a -G dialout - ``` -1. You will use the GNU `screen` application in Linux to access the Pi. You can install it with: - ``` - sudo apt install screen - ``` -1. You should now gain access to the Pi with: - ``` - ` sudo screen /dev/ttyUSB0 115200 - ``` -1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry - -# Disabling the OTG mode to use a keyboard and mouse again - -**WARNING: Untested** -1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line - ``` - [all] - #dtoverlay=dwg2 - ``` -1. Comment out the dwg2 line in `/etc/modules` using a `#` in front of the line - ``` - #dwg2 - ``` -1. Disable the gadget service - ``` - systemctl disable serial-ether-gadget.service - ``` -1. Disable the getty - ``` - systemctl disable serial-getty@ttyGS0.service - ``` - -# Connecting over the USB ethernet to ssh into your Pi - -I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley - -https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ - - -# Untested - Here are items that bear investigating but haven't been tested yet - - - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) - +# Gadget Mode to setup a serial connection over USB +Prepare Raspberry Pi OS connection over a microUSB serial cable + +Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget: +The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383 + +## Important : +- **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again. +- The blue USB cables included in the kit in 2026 are flakey. We recommend replacing them ASAP. +- You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices. + + +## Procedure + +Make sure you have your USB cable plugged into the left port **USB** and not the right port marked **PWR**. +1. Open a connection to the device via ssh or Raspberry Pi Connect +1. Become root (the superuser) +``` +sudo su +``` +1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end) +``` +echo "dtoverlay=dwc2" >> /boot/firmware/config.txt +``` +1. We add the required module `dwc2` for loading at startup +``` +echo "dwc2" >> /etc/modules +``` +1. Check that we can load the libcomposite module +``` +modprobe libcomposite +``` +1. Reboot +``` +restart now +``` +1. Become root again +``` +sudo su +``` +1. Put a copy of this repository in root's home directory using git +``` +cd /root +apt install -y git +git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git +``` +1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent + ``` +cd rover-pi/Scripts/ +./gadget.sh +``` +1. Install the systemd +``` +cp serial-ether-gadget.service /lib/systemd/system/. +``` +1. Enable the service +``` +systemctl enable serial-ether-gadget.service +``` +1. Enable the getty (which connects a login terminal to the serial port) +``` +systemctl enable serial-getty@ttyGS0.service +``` +1. Reboot and see if the serial port appears + +# Connecting to the serial terminal +You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port + +## Windows +- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html +- MobaXterm: https://mobaxterm.mobatek.net/ + +## Linux +If using a Linux VM, you will need to pass the Pi USB connection there. +This is similar to the procedure for accessing an Arduino. +1. Start the Linux VM if you haven't already +1. Menu: Devices > USB +1. Look for "Silicon Labs CP210x USB to UART Bridge" and click on it. +1. check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port. + ``` + ls -l /dev/ttyU* + ``` +1. The user must be in the dialout group to access it. Use the Linux command: `uid` -to check if you are in the group. If not in the dialout group, use: + ``` + sudo usermod -a -G dialout + ``` +1. You will use the GNU `screen` application in Linux to access the Pi. You can install it with: + ``` + sudo apt install screen + ``` +1. You should now gain access to the Pi with: + ``` + ` sudo screen /dev/ttyUSB0 115200 + ``` +1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry + +# Disabling the OTG mode to use a keyboard and mouse again + +**WARNING: Untested** +1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line + ``` + [all] + #dtoverlay=dwg2 + ``` +1. Comment out the dwg2 line in `/etc/modules` using a `#` in front of the line + ``` + #dwg2 + ``` +1. Disable the gadget service + ``` + systemctl disable serial-ether-gadget.service + ``` +1. Disable the getty + ``` + systemctl disable serial-getty@ttyGS0.service + ``` + +# Connecting over the USB ethernet to ssh into your Pi + +I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley + +https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/ + + +# Untested + Here are items that bear investigating but haven't been tested yet + + - [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json) + diff --git a/oled-display/LICENSE b/oled-display/LICENSE new file mode 100644 index 0000000..929ff3b --- /dev/null +++ b/oled-display/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Marcel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/oled-display/README.md b/oled-display/README.md new file mode 100644 index 0000000..eda4ed2 --- /dev/null +++ b/oled-display/README.md @@ -0,0 +1,38 @@ +# cpsy-display-ip +Demonstrates the SSD1306 OLED by showing the IP address on the system boot. +Originally at https://github.com/mkyas/cpsy-display-ip and customized for Mechatronics 1 students. + +This project contains the file `display-ip.py` that displays an IP +address on the SSD1306 OLED screen and the file `display-ip.service` +that executes the script on boot after the Raspberry Pi is connected +to a network. + +To use the script, you first must connect the screen to the Pi's +I2C bus. + +## Setup + +Do the following steps on your Raspberry Pi. + +1. Enable I2C in `raspi-config` +1. Go to your home directory: `cd ~` +1. Clone this repository to your raspberry pi `git clone https://gitea.cs.ru.is/RU-V207/rover-pi` +1. Install [pillow](https://python-pillow.org/), numpy, and venv: + > sudo apt install python3-venv python3-pil python3-numpy +1. Create a virtual environment, here called cpsy: + > python3 -m venv cpsy +1. Edit the generated `pyvenv.cfg` to set `include-system-site-packages = true` +1. Activate the environment `source cpsy/bin/activate` +1. Install the display driver: `pip install adafruit-circuitpython-ssd1306` +1. Edit `display-ip.service`: + - Replace `foley` with your user name on the Pi. + - the `WorkingDirectory` points to the directory containing `display-ip.py` + - the path to python points to your virtual environment +1. Copy the edited `display-ip.service` to `/etc/systemd/system` + ```sudo cp display-ip.service /etc/systemd/system/.``` +1. Enable the service `sudo systemctl enable display-ip.service` +1. Test by starting the service `sudo systemctl start display-ip.service` which should turn on the screen. +1. If nothing happens, check on the status with `sudo systemctl status display-ip` and look for error messages. +1. If you make additional changes to the service file, you may have to reload the `systemd`: + ```sudo systemctl daemon-reload ``` + diff --git a/oled-display/display-ip.py b/oled-display/display-ip.py new file mode 100644 index 0000000..d4c6857 --- /dev/null +++ b/oled-display/display-ip.py @@ -0,0 +1,78 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +This demo will fill the screen with white, draw a black box on top +and then print the device's IP address in the center of the display + +This example is for use on (Linux) computers that are using CPython with +Adafruit Blinka to support CircuitPython libraries. CircuitPython does +not support PIL/pillow (python imaging library)! +""" + +import board +import digitalio +from PIL import Image, ImageDraw, ImageFont +import adafruit_ssd1306 +import socket + +# Define the Reset Pin +oled_reset = None +try: + oled_reset = digitalio.DigitalInOut(board.D4) +except: + pass + # No reset pin + +# Change these +# to the right size for your display! +WIDTH = 128 +HEIGHT = 64 +BORDER = 5 + +# Use for I2C. +i2c = board.I2C() # uses board.SCL and board.SDA +oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset) + +# Clear display. +oled.fill(0) +oled.show() + +# Create blank image for drawing. +# Make sure to create image with mode '1' for 1-bit color. +image = Image.new("1", (oled.width, oled.height)) + +# Get drawing object to draw on image. +draw = ImageDraw.Draw(image) + +# Draw a white background +draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255) + +# Draw a smaller inner rectangle +draw.rectangle( + (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1), + outline=0, + fill=0, +) + +# Load default font. +font = ImageFont.load_default() + +# Get the local IP address as a string. +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) + text = s.getsockname()[0] + +# Draw it into a box. +bbox = font.getbbox(text) +(font_width, font_height) = bbox[2] - bbox[0], bbox[3] - bbox[1] +draw.text( + (oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2), + text, + font=font, + fill=255, +) + +# Display image +oled.image(image) +oled.show() diff --git a/oled-display/display-ip.service b/oled-display/display-ip.service new file mode 100644 index 0000000..621f6cf --- /dev/null +++ b/oled-display/display-ip.service @@ -0,0 +1,13 @@ +[Unit] +Description=display-ip +After=network-online.target + +[Service] +Type=simple +User=foley +WorkingDirectory=/home/foley/rover-pi/oled-display +ExecStart=/home/foley/cpsy/bin/python3 display-ip.py +Restart=never + +[Install] +WantedBy=multi-user.target