OLED Display Add-on – Dojo Derby

OLED displays allow your vehicle to communicate information such as it’s IP Address and Troubleshooting Information in a form factor about the size of a postage stamp.

OLED Display SSD1306 – https://www.amazon.com/dp/B0833PF7ML

Note – These are pretty generic displays with many vendors selling the same basic product. The main differences you’ll find are the size of the mounting screw holes, the pin layout (same pins in different positions), and some screens have the first line in one color with the other lines in another. (Like a Title Bar)

Wiring

Wiring is pretty self explanatory. VCC goes to a 5V pin, GND to ground, and SCL and SDA go to those pins on the Raspberry Pi (On the 4 SDA goes to pin 3 and SCL goes to pin 5)

OS Setup

First Install the Adafruit_BBIO module into Python3

sudo pip3 install Adafruit_BBIO

Line 1 – Next install Python tools that will be needed.

Line 2 – Then copy the Adafruit files from GIT. I would recommend doing this in /var/www/html so that the files are with the rest of the fils for your project, but it doesn’t really matter

Line 3 – Go to the new folder that was created

Line 4 – Run the setup script

sudo python -m pip install --upgrade pip setuptools wheel git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git cd Adafruit_Python_SSD1306 sudo python3 setup.py install
Code language: PHP (php)

Connect the OLED display to the Pi board. Then run i2cdetect to verify that the display is connected. You should see an i2c address of 3c.

sudo i2cdetect -y 1

Code Explanation

This is the script you sue to display information from your vehicle. Add it to rc.local to have it run at startup.

Save oled-display.py in /var/www/html

Edit rc.local with sudo nano /etc/rc.local and add the line sudo python3 /var/www/html/oled-display.py &

oled-display.py

This code is a cut/paste/hack of the stats.py script that is downloaded when you copy the folder from GIT.

Line 10 – Import the subprocess module. This allows you to query the operating system and be able to turn the return value into a variable value.

Line 20 – This is used for the 128×32 screen. If you use a different size screen you can find different configurations by looking in the stats.py script.

Line 53 – This starts the while loop to continuously refresh the OLED screen as new values are changed on the vehicle.

Lines 58 – 65 – This uses the subprocess module to query iwgetid which returns information about the wifi connection. The next line strips out unwanted text to give us the value for the SSID. If iwgetid fails to get a value (It’s not connected) the ssid variable is given the value of “no wifi connection”.

Lines 68 – 73 -These lines use subprocess to get the IP Address of the vehicle and then format the value into just the IP Address and assign the value to the variable IP

Lines 75-78 – This gets the current speed value from the speed.txt file, and sets the speedCommand variable to that value.

Lines 80 – 83 – This gets the current command value from the command.txt file, and sets the command variable to that value.

Lines 86-89 – These lines print out the values for ssid, IP, speed, and command variable values.

import time import Adafruit_GPIO.SPI as SPI import Adafruit_SSD1306 from PIL import Image from PIL import ImageDraw from PIL import ImageFont import subprocess # Raspberry Pi pin configuration: RST = None # on the PiOLED this pin isnt used # Note the following are only used with SPI: DC = 23 SPI_PORT = 0 SPI_DEVICE = 0 # 128x32 display with hardware I2C: disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) # Initialize library. disp.begin() # Clear display. disp.clear() disp.display() # Create blank image for drawing. # Make sure to create image with mode '1' for 1-bit color. width = disp.width height = disp.height image = Image.new('1', (width, height)) # Get drawing object to draw on image. draw = ImageDraw.Draw(image) # Draw a black filled box to clear the image. draw.rectangle((0,0,width,height), outline=0, fill=0) # Draw some shapes. # First define some constants to allow easy resizing of shapes. padding = -2 top = padding bottom = height-padding # Move left to right keeping track of the current x position for drawing shapes. x = 0 # Load default font. font = ImageFont.load_default() while True: # Draw a black filled box to clear the image. draw.rectangle((0,0,width,height), outline=0, fill=0) try: ssid = subprocess.check_output(['iwgetid'], errors=None) ssid = str(ssid) ssid = ssid.replace('wlan0 ESSID:"','') ssid = ssid.replace('b\'','') ssid = ssid.replace('"\\n\'','') except subprocess.CalledProcessError: ssid = "no wifi connection" # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load cmd = "hostname -I" IP = subprocess.check_output(cmd, shell = True ) IP = str(IP) IP = IP.replace('b\'','') IP = IP.replace(' \\n\'','') print("IP: " + (IP)) file = open("/var/www/html/speed.txt","r") speedCommand = file.read() print("Speed: " +(speedCommand)) file.close() file = open("/var/www/html/command.txt","r") command = file.read() print("Command: " +(command)) file.close() # Write two lines of text. draw.text((x, top), (ssid), font=font, fill=255) draw.text((x, top+8), "IP: " + (IP), font=font, fill=255) draw.text((x, top+16), "Speed: " + (speedCommand), font=font, fill=255) draw.text((x, top+25), "Command: "+ (command), font=font, fill=255) # Display image. disp.image(image) disp.display() time.sleep(.1) # Copyright (c) 2017 Adafruit Industries # Author: Tony DiCola & James DeVito # # 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.
Code language: PHP (php)