Page 1 of 1

help with code

Posted: Fri Oct 11, 2019 7:23 pm
by trackseven
Hi to all

I am fairy new to the rapspberry pi

I have a raspberry pi zero W

I have a 128x32 oled connected to it . that displays the stats

Code: Select all


# 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.


# 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 time
import subprocess

from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306


# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)

# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height.  Change these
# to the right size for your display!
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

# Clear display.
disp.fill(0)
disp.show()

# 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()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the
# same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)

while True:

    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    # 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 | cut -d\' \' -f1"
    IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
    CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB  %.2f%%\", $3,$2,$3*100/$2 }'"
    MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB  %s\", $3,$2,$5}'"
    Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")

    # Write four lines of text.

    draw.text((x, top+0), "IP: "+IP, font=font, fill=255)
    draw.text((x, top+8), CPU, font=font, fill=255)
    draw.text((x, top+16), MemUsage, font=font, fill=255)
    draw.text((x, top+25), Disk, font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.show()
    time.sleep(.1)
    
    
I also have a pressure / temp sensor bmp 388

that works

Code: Select all

#bmp388-4.py
import time
import board
import busio
import adafruit_bmp3xx

i2c = busio.I2C(board.SCL, board.SDA)
bmp388 = adafruit_bmp3xx.BMP3XX_I2C(i2c)

bmp388.pressure_oversampling = 8
bmp388.temperature_oversampling = 2
bmp388.altitude_oversampling = 2
bmp388.temperature2 = (bmp388.temperature  * 9/5)  + 28
bmp388.sea_level_pressure = 1014.5

while True:
    print("--------------------------")
    print("Temperature:  {0:0.2f} Deg F ".format(bmp388.temperature2))
    print("Pressure:     {:6.1f}  Mbar   ".format(bmp388.pressure))
    print("Altitude:     {0:0.2f} Feet ".format(bmp388.altitude))
    time.sleep(1)
I tried use both programs together but I am stuck


this is the code i came up with and would like to replace so it displays on the 128x32 oled IP , Temp , Press, Alt and remove CPU MemUsage , Disk

Code: Select all

import time
import board
import busio
import subprocess
from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import adafruit_bmp3xx

i2c = busio.I2C(board.SCL, board.SDA)
bmp388 = adafruit_bmp3xx.BMP3XX_I2C(i2c)

bmp388.pressure_oversampling = 8
bmp388.temperature_oversampling = 2
bmp388.altitude_oversampling = 2
bmp388.temperature2 = (bmp388.temperature  * 9/5)  + 28
bmp388.sea_level_pressure = 1014.5

Temp     = bmp388.temperature2
Pressure = bmp388.pressure
Altitude  = bmp388.altitude

i2c = busio.I2C(SCL, SDA)

disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

disp.fill(0)
disp.show()

width = disp.width
height = disp.height
image = Image.new('1', (width, height))

draw = ImageDraw.Draw(image)

draw.rectangle((0, 0, width, height), outline=0, fill=0)

padding = -2
top = padding
bottom = height-padding

x = 0

font = ImageFont.load_default()

while True:

    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    cmd      = "hostname -I | cut -d\' \' -f1"
    IP       = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd      = "top -bn1 | grep load | awk '{printf \"Mem: %.2f\", $(NF-2)}'"
    Memory   = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd      = "free -m | awk 'NR==2{printf \"Load: %s/%sMB %.1f%%\",$3,$2,$3*100/$2 }'"
    Load     = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd      = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
    Disk     = subprocess.check_output(cmd, shell=True).decode("utf-8")

    #%s/%sMB %.2f%%\",

    print("Temperature:  {0:0.2f} Deg F ".format(bmp388.temperature2))
    print("Pressure:     {:6.1f}  Mbar   ".format(bmp388.pressure))
    print("Altitude:     {0:0.2f} Feet ".format(bmp388.altitude))
    print("IP :"+IP)
    print("CPU :"+Load)
    print("Hardrive:"+Disk)

    draw.text((x, top+0), "IP: "+IP, font=font, fill=255)
    draw.text((x, top+8), Memory, font=font, fill=255)
    draw.text((x, top+16), Load, font=font, fill=255)
    draw.text((x, top+25), Disk, font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.show()
    time.sleep(.1)




Any help would be greatly appriciated . Thank you