Wearther data pulling from a bme280 and have it display on oled 128x64

Here is my code,

read bme280 data

def get_temp():
from bme280 import readBME280All

temperature,pressure,humidity = readBME280All()
return “%sF” % str(((temperature)*9/5)+32)

def get_pressure():
from bme280 import readBME280All

temperature,pressure,humidity = readBME280All()

return “%s hPa” % str(pressure)

def get_humidity():
from bme280 import readBME280All

temperature,pressure,humidity = readBME280All()
image = Image.open(r /home/pi/lib_oled96/FreeSans.ttf)
draw = ImageDraw.Draw(image)
return “%s percent” % str(humidity)
from PIL import ImageFont, ImageDraw
font = ImageFont.truetype(’/home/pi/lib_oled96/FreeSans.ttf’, 13)
draw.text((1, 1), ‘Ambient Conditions’, font=font, fill=1)

font = ImageFont.truetype(’/home/pi/Downloads/ssd1306-master/FreeSans.ttf’, 11)
draw.text((1, 20), get_temp(), font=font, fill=1)
draw.text((1, 35), get_pressure(), font=font, fill=1)
draw.text((1, 50), get_humidity(), font=font, fill=1)

oled.display()
sleep(5)
oled.cls()
I keep getting this error Traceback (most recent call last):
File “/media/pi/rootfs/home/pi/weathertest1.py”, line 6
return “%sF” % str(((temperature)*9/5)+32)
^
SyntaxError: ‘return’ outside function
I keep fixing but then I get a diffrent error. Can some tell me what is worng with my code.

1 Like

SyntaxError: ‘return’ outside function
I keep fixing but then I get a diffrent error. Can some tell me what is worng
with my code.

What seems to be wrong is the indentation of lines belonging to the body of a
function’s definiton with respect to the statement that introduces the
function’s declaration.

Example:

def function_one():
    statement_one()
    statement_two()
    return something

def function_two():
    statement_one()
    statement_two()
    return something_else

code_after()
code_after()

notice how the statements and return align for each function.

Try reformatting the lines between def and return and leave the rest of the
code as is after the last return.

1 Like

Thank you. Now I am wonering on how to get this to work
from PIL import ImageFont, ImageDraw
import os
image = ImageFont.truetype(r’/home/pi/lib_oled96/’“FreeSans.ttf”)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(r’/home/pi/lib_oled96/FreeSans.ttf’, 13)

draw.text((1, 1), ‘Ambient Conditions’, font=font, fill=1)

as I am getting this error I am a little confused on what it meas as i have the info listed unless i am missing something. As I am trying to learn.
im.load()
AttributeError: ‘FreeTypeFont’ object has no attribute ‘load’

1 Like

as I am getting this error I am a little confused on what it meas as i have
the info listed unless i am missing something. As I am trying to learn.
im.load()
AttributeError: ‘FreeTypeFont’ object has no attribute ‘load’

The code you posted might be incomplete because nowhere in it do you define
im for it to give that error message.

1 Like

here is the full code.

read bme280 data

def get_temp():
from bme280 import readBME280All
temperature,pressure,humidity = readBME280All()
return “%sF” % str(((temperature)*9/5)+32)

def get_pressure():
from bme280 import readBME280All
temperature,pressure,humidity = readBME280All()
return “%s hPa” % str(pressure)

def get_humidity():
from bme280 import readBME280All
temperature,pressure,humidity = readBME280All()
return “%s percent” % str(humidity)

from PIL import Image, ImageFont, ImageDraw

font = ImageFont.load(’/home/pi/FreeSans.ttf’)
image = ImageFont.truefont(’/home/pi/FreeSans.ttf’)
draw = ImageDraw.Draw(font)

draw.text((1, 1), ‘Ambient Conditions’, font=font, fill=1)

font = ImageFont.truetype(’/home/pi/FreeSans.ttf’, 11)

draw.text((1, 20), get_temp(), font=font, fill=1)
draw.text((1, 35), get_pressure(), font=font, fill=1)
draw.text((1, 50), get_humidity(), font=font, fill=1)

oled.display()
sleep(5)
oled.cls()
I am not getting this error i try finding info on but i seem do qute get what they are saying. here is the error i am getting
raise IOError(“cannot find glyph data file”)
OSError: cannot find glyph data file

1 Like

I recommend:

  • Checking you have the needed font files where they are specified in code,
    e.g. “/home/pi/FreeSans.ttf”

  • Reading the docs about the difference between bitmap and TrueType fonts and
    how they are loaded differently in PIL

1 Like