I’ve broken something code wise in the code posted at the end of topic. The display in question is the 128x128 i2c OLED, which is setup with Pico Graphics.
Pico W weather station moch up.
display.text("USB-Powered", 10, 108, 1)
gets me
USB-Powered
but
display.text(“USB Powered”, 10, 108, 1)
gets me
USB
Powered
import time, math, os
import network
import pimoroni_bus
import veml6075
import breakout_icp10125
from machine import ADC, Pin, PWM, RTC
from pimoroni import Analog
from pimoroni_i2c import PimoroniI2C
from breakout_ltr559 import BreakoutLTR559
from breakout_bme280 import BreakoutBME280
from breakout_rtc import BreakoutRTC
i2cbus = PimoroniI2C(4, 5)
bme = BreakoutBME280(i2cbus)
ltr = BreakoutLTR559(i2cbus)
uv = veml6075.VEML6075(i2cbus)
connected = uv.initUV()
icp10125 = breakout_icp10125.BreakoutICP10125(i2cbus)
from picographics import PicoGraphics, DISPLAY_I2C_OLED_128X128
display = PicoGraphics(display=DISPLAY_I2C_OLED_128X128, bus=i2cbus)
display.clear()
display.set_font("bitmap8")
display.set_pen(15) # White
rtc = BreakoutRTC(i2cbus)
if rtc.is_12_hour:
rtc.set_24_hour()
start_time = time.time()
temp, press, hum = bme.read()
time.sleep(0.3)
temp, press, hum = bme.read()
time.sleep(0.3)
temp, press, hum = bme.read()
time.sleep(0.3)
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
RAIN_MM_PER_TICK = 0.2794
WIND_CM_RADIUS = 7.0
WIND_FACTOR = 0.0218
wind_direction_pin = Analog(26)
wind_speed_pin = Pin(9, Pin.IN, Pin.PULL_UP)
rain_pin = Pin(10, Pin.IN, Pin.PULL_DOWN)
last_rain_trigger = False
def wind_speed(sample_time_ms=1000):
# get initial sensor state
state = wind_speed_pin.value()
# create an array for each sensor to log the times when the sensor state changed
# then we can use those values to calculate an average tick time for each sensor
ticks = []
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) <= sample_time_ms:
now = wind_speed_pin.value()
if now != state: # sensor output changed
# record the time of the change and update the state
ticks.append(time.ticks_ms())
state = now
# if no sensor connected then we have no readings, skip
if len(ticks) < 2:
return 0
# calculate the average tick between transitions in ms
average_tick_ms = (time.ticks_diff(ticks[-1], ticks[0])) / (len(ticks) - 1)
if average_tick_ms == 0:
return 0
# work out rotation speed in hz (two ticks per rotation)
rotation_hz = (1000 / average_tick_ms) / 2
# calculate the wind speed in metres per second
circumference = WIND_CM_RADIUS * 2.0 * math.pi
wind_m_s = rotation_hz * circumference * WIND_FACTOR
return wind_m_s
def wind_direction():
# adc reading voltage to cardinal direction taken from our python
# library - each array index represents a 45 degree step around
# the compass (index 0 == 0, 1 == 45, 2 == 90, etc.)
# we find the closest matching value in the array and use the index
# to determine the heading
ADC_TO_DEGREES = (0.9, 2.0, 3.0, 2.8, 2.5, 1.5, 0.3, 0.6)
closest_index = -1
last_index = None
# ensure we have two readings that match in a row as otherwise if
# you read during transition between two values it can glitch
# fixes https://github.com/pimoroni/enviro/issues/20
def get_vsys():
conversion_factor = 3 * 3.3 / 65535
wlan = network.WLAN(network.STA_IF)
wlan_active = wlan.active()
try:
wlan.active(False)
Pin(25, mode=Pin.OUT, pull=Pin.PULL_DOWN).high()
Pin(29, Pin.IN)
vsys = ADC(29)
return vsys.read_u16() * conversion_factor
finally:
Pin(29, Pin.ALT, pull=Pin.PULL_DOWN, alt=7)
wlan.active(wlan_active)
power = Pin('WL_GPIO2', Pin.IN)
charging = Pin(22, mode=Pin.IN, pull=Pin.PULL_UP)
full_battery = 4.2
empty_battery = 2.8
while True:
display.set_pen(0)
display.clear()
display.set_pen(15)
time_elapsed = time.time() - start_time
hour = rtc.get_hours()
minute = rtc.get_minutes()
month = rtc.get_month()
date = rtc.get_date()
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
#print("Date: ", rtc_date, ", Time: ", rtc_time, sep="")
display.text(rtc_date, 15, 0, 1)
display.text(rtc_time, 30, 18, 1)
temp, press, hum = bme.read()
reading = bme.read()
pressmb = press / 100
#print('Temperature {:05.2f}*C'.format(temp))
#print('Humididty {:05.2f}%'.format(hum))
#print('Pressure {:05.2f}hPa'.format(pressmb))
t, p, status = icp10125.measure(icp10125.NORMAL)
if status == icp10125.STATUS_OK:
icptemp = t
icppress = p / 100
#print('ICP Pressure {:05.2f}mb'.format(icppress))
display.text('{:01.0f}C'.format(icptemp), 25, 36, 1)
display.text('{:02.0f}%'.format(hum), 70, 36, 1)
display.text('{:01.0f}mb'.format(icppress), 35, 54, 1)
reading = ltr.get_reading()
if reading is not None:
#print("Light:", reading[BreakoutLTR559.LUX],"LUX")
display.text('Lux={:01.0f}'.format(reading[BreakoutLTR559.LUX]), 30, 72, 1)
UVI, UVIA, UVIB = uv.readUV()
#print('UV {:05.1f} '.format(UVI))
#print('UVA {:05.1f}'.format(UVIA))
#print('UVB {:05.1f}'.format(UVIB))
display.text('UV={:01.1f}'.format(UVI), 30, 90, 1)
percentage = 100 * ((get_vsys() - empty_battery) / (full_battery - empty_battery))
if percentage > 100:
percentage = 100.00
if power.value() == True:
if charging.value() == 0:
#print ("Charging!")
display.text("Charging!", 24, 108, 1)
else:
#print ("USB Powered")
display.text("USB-Powered", 10, 108, 1)
else:
#print('{:.2f}V'.format(get_vsys()))
#print('{:.0f}%'.format(percentage))
display.text('{:.2f}V'.format(get_vsys()), 20, 108, 1)
display.text('{:.0f}%'.format(percentage), 70, 108, 1)
display.update()
time.sleep(0.1)
Weird thing is the following code gets me a proper
Hello World
all on the one line on the OLED.
from picographics import PicoGraphics, DISPLAY_I2C_OLED_128X128
from pimoroni_i2c import PimoroniI2C
i2cbus = PimoroniI2C(4, 5)
display = PicoGraphics(display=DISPLAY_I2C_OLED_128X128, bus=i2cbus)
display.clear()
display.set_font("bitmap8")
display.set_pen(15) # White
display.text("Hello World", 0, 0, scale=2)
display.update()