def __updateDisplay(self, DHT22Connection, PIRConnection, SerialConnection): try: #get number of pages to show cycles = 0; if DHT22Connection: cycles = cycles + 1 if PIRConnection: cycles = cycles + 1 if SerialConnection: cycles = cycles + 1 while True: try: if DHT22Connection: self.__showDHT22() time.sleep(self.pageTime/ cycles) if PIRConnection: self.__showPIR() time.sleep(self.pageTime / cycles) if SerialConnection: self.__showSerial() time.sleep(self.pageTime / cycles) except OSError: print("display disconnected", file=sys.stderr) try: self.oled = OledText(self.i2c, self.width, self.height) time.sleep(1) except Exception: print("no display found", file=sys.stderr) except KeyboardInterrupt: sys.exit(0)
def __init__(self): self.oled = OledText(self.i2c, 128, 64) self.oled.layout = { 1: BigLine(0, 0, size=15, font="FreeSans.ttf"), 2: BigLine(0, 15, size=15, font="FreeSans.ttf"), 3: BigLine(0, 30, size=15, font="FreeSans.ttf"), 4: SmallLine(0, 50, size=10, font="FreeSans.ttf"), } self.oled.auto_show = False self.blank()
def __init__(self, height = 64, width = 128, pageTime = 1): self.i2c = busio.I2C(board.SCL, board.SDA) self.height = height self.width = width self.oled = OledText(self.i2c, self.width, self.height) self.pageTime = pageTime self.oled.auto_show = False #disable auto write on oled_text module self.lastDetection = Value(ctypes.c_bool, False) self.lastTime = Value(ctypes.c_double, 0xdeadbeef) self.lastTemperature = Value(ctypes.c_double, 0xdeadbeef) self.lastHumidity = Value(ctypes.c_double, 0xdeadbeef) self.lastTempUpdate = Value(ctypes.c_double, 0xdeadbeef) self.lastHumUpdate = Value(ctypes.c_double, 0xdeadbeef) self.lastPotentiometer = Value(ctypes.c_int, 0xdeadbeef) self.lastPotentiometerUpdate = Value(ctypes.c_double, 0xdeadbeef)
class Screen: i2c = busio.I2C(SCL, SDA) def __init__(self): self.oled = OledText(self.i2c, 128, 64) self.oled.layout = { 1: BigLine(0, 0, size=15, font="FreeSans.ttf"), 2: BigLine(0, 15, size=15, font="FreeSans.ttf"), 3: BigLine(0, 30, size=15, font="FreeSans.ttf"), 4: SmallLine(0, 50, size=10, font="FreeSans.ttf"), } self.oled.auto_show = False self.blank() def write(self, line_1=None, line_2=None, line_3=None, line_4=None): if line_1 is not None: self.line_1 = line_1 if line_2 is not None: self.line_2 = line_2 if line_3 is not None: self.line_3 = line_3 self.oled.text(self.line_1, 1) self.oled.text(self.line_2, 2) self.oled.text(self.line_3, 3) self.oled.text(self.get_ip_address(), 4) self.oled.show() def blank(self): self.line_1 = "" self.line_2 = "" self.line_3 = "" self.line_4 = "" self.write("", "", "", "") def get_ip_address(self): cmd = "hostname -I | cut -d' ' -f1" ip_address = "" for i in range(100): ip_address = (subprocess.check_output( cmd, shell=True).decode("utf-8").strip()) if ip_address: return ip_address time.sleep(0.25) return "No IP address found"
import time import busio from board import SCL, SDA from oled_text import OledText, Layout64, BigLine, SmallLine i2c = busio.I2C(SCL, SDA) oled = OledText(i2c, 128, 64) oled.layout = Layout64.layout_icon_1big_2small() oled.auto_show = False oled.text('\uf2ca', 1) oled.text("VAR C", 2) oled.text("Room", 3) oled.text("temp", 4) oled.show() oled.auto_show = True
class SSD1306_I2C: def __init__(self, height = 64, width = 128, pageTime = 1): self.i2c = busio.I2C(board.SCL, board.SDA) self.height = height self.width = width self.oled = OledText(self.i2c, self.width, self.height) self.pageTime = pageTime self.oled.auto_show = False #disable auto write on oled_text module self.lastDetection = Value(ctypes.c_bool, False) self.lastTime = Value(ctypes.c_double, 0xdeadbeef) self.lastTemperature = Value(ctypes.c_double, 0xdeadbeef) self.lastHumidity = Value(ctypes.c_double, 0xdeadbeef) self.lastTempUpdate = Value(ctypes.c_double, 0xdeadbeef) self.lastHumUpdate = Value(ctypes.c_double, 0xdeadbeef) self.lastPotentiometer = Value(ctypes.c_int, 0xdeadbeef) self.lastPotentiometerUpdate = Value(ctypes.c_double, 0xdeadbeef) def __write(self, text, line): self.oled.text(text, line) def __clearAll(self): self.oled.clear() def __clear(self, line): self.oled.text("", line) def __setLayout(self, layout): self.oled.layout = layout def __show(self): self.oled.show() def updatePIR(self, data): self.lastDetection.acquire() self.lastTime.acquire() self.lastDetection.value = data['detection'] self.lastTime.value = data['timestamp'] self.lastTime.release() self.lastDetection.release() def updateDHT22(self, data): self.lastTemperature.acquire() self.lastHumidity.acquire() self.lastTempUpdate.acquire() self.lastHumUpdate.acquire() self.lastTemperature.value = data['temperature'] self.lastHumidity.value = data['humidity'] self.lastTempUpdate.value = data['temperatureTimestamp'] self.lastHumUpdate.value = data['humidityTimestamp'] self.lastHumUpdate.release() self.lastTemperature.release() self.lastHumidity.release() self.lastTempUpdate.release() def updateSerial(self, data): self.lastPotentiometer.acquire() self.lastPotentiometerUpdate.acquire() self.lastPotentiometer.value = data['potentiometer'] self.lastPotentiometerUpdate.value = data['timestamp'] self.lastPotentiometerUpdate.release() self.lastPotentiometer.release() def start(self, DHT22Connection, PIRConnection, SerialConnection): self.displayProcess = Process(target=self.__updateDisplay, args=(DHT22Connection, PIRConnection, SerialConnection,)) self.displayProcess.start() def stop(self): if self.displayProcess.is_alive(): self.displayProcess.join() pass def __showDHT22(self): #set 3 lines layout for DHT22 self.__setLayout( { 1: SmallLine(1, 1, font="FreeSans.ttf", size=14), 2: SmallLine(1, 17, font="FreeSans.ttf", size=14), 3: SmallLine(1, 33, font="FreeSans.ttf", size=14), 4: SmallLine(1, 49, font="FreeSans.ttf", size=14) }) self.lastTemperature.acquire() self.lastHumidity.acquire() self.lastTempUpdate.acquire() self.lastHumUpdate.acquire() if int(self.lastTempUpdate.value) == 0xdeadbeef or int(self.lastHumUpdate.value) == 0xdeadbeef: #check if data has been received self.lastHumUpdate.release() self.lastTempUpdate.release() self.lastHumidity.release() self.lastTemperature.release() return tempTime = time.localtime(self.lastTempUpdate.value) tempTime = time.strftime('%H:%M', tempTime) humTime = time.localtime(self.lastHumUpdate.value) humTime = time.strftime('%H:%M', humTime) self.__write(f'Temperature: {self.lastTemperature.value:.2f}', 1) self.__write(f'At time: {tempTime}',2) self.__write(f'Humidity: {self.lastHumidity.value:.2f}', 3) self.__write(f'At time: {humTime}', 4) self.__show() self.lastTemperature.release() self.lastHumidity.release() self.lastTempUpdate.release() self.lastHumUpdate.release() def __showPIR(self): #Set layout for PIR self.__setLayout( { 1: SmallLine(1, 2, font="FreeSans.ttf", size=14), 2: SmallLine(1, 18, font="FreeSans.ttf", size=14) }) self.lastDetection.acquire() self.lastTime.acquire() if int(self.lastTime.value) == 0xdeadbeef: #check if data has been received self.lastTime.release() self.lastDetection.release() return readTime = time.localtime(self.lastTime.value) readTime = time.strftime('%H:%M', readTime) self.__write(f'Position: {self.lastDetection.value}', 1) self.__write(f'At time: {readTime}', 2) self.__show() self.lastTime.release() self.lastDetection.release() def __showSerial(self): #Set layout for serial self.__setLayout( { 1: SmallLine(1, 2, font="FreeSans.ttf", size=14), 2: SmallLine(1, 18, font="FreeSans.ttf", size=14) }) self.lastPotentiometer.acquire() self.lastPotentiometerUpdate.acquire() if int(self.lastPotentiometerUpdate.value) == 0xdeadbeef: #check if data has been received self.lastPotentiometerUpdate.release() self.lastPotentiometer.release() return readTime = time.localtime(self.lastPotentiometerUpdate.value) readTime = time.strftime('%H:%M', readTime) self.__write(f'Potentiometer: {self.lastPotentiometer.value}', 1) self.__write(f'At time: {readTime}', 2) self.__show() self.lastPotentiometerUpdate.release() self.lastPotentiometer.release() def __updateDisplay(self, DHT22Connection, PIRConnection, SerialConnection): try: #get number of pages to show cycles = 0; if DHT22Connection: cycles = cycles + 1 if PIRConnection: cycles = cycles + 1 if SerialConnection: cycles = cycles + 1 while True: try: if DHT22Connection: self.__showDHT22() time.sleep(self.pageTime/ cycles) if PIRConnection: self.__showPIR() time.sleep(self.pageTime / cycles) if SerialConnection: self.__showSerial() time.sleep(self.pageTime / cycles) except OSError: print("display disconnected", file=sys.stderr) try: self.oled = OledText(self.i2c, self.width, self.height) time.sleep(1) except Exception: print("no display found", file=sys.stderr) except KeyboardInterrupt: sys.exit(0)
print("Counter " + str(Counter)) print("Shutdown") oled.text("Shutdown", 1) oled.text("", 2) os.system('sudo shutdown -h now') time.sleep(0.05) GPIO.cleanup() GPIO.add_event_detect(21, GPIO.RISING, callback=Interrupt, bouncetime=200) i2c = busio.I2C(SCL, SDA) """ Examples for a 128x32 px SSD1306 oled display. For more details see the 64px examples """ # Instantiate the display, passing its dimensions (128x64 or 128x32) oled = OledText(i2c, 128, 32) sensor = Adafruit_DHT.DHT22 # DHT22 sensor connected to GPIO12. pin = 12 print("[press ctrl+c to end the script]") try: # Main program loop while True: #input_state = GPIO.input(buttonPin) #if (not input_state): # print("Shutdown") # os.system('sudo shutdown -h now') # time.sleep(0.05) # GPIO.cleanup() humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
from board import SCL, SDA import busio from oled_text import OledText, Layout64 from time import sleep i2c = busio.I2C(SCL, SDA) # Create the display, pass its pixel dimensions oled = OledText(i2c, 128, 64) # Write to the oled while True: oled.layout = Layout64.layout_icon_only() oled.text('\uf244', 1) sleep(1) oled.text('\uf243', 1) sleep(1) oled.text('\uf242', 1) sleep(1) oled.text('\uf241', 1) sleep(1) oled.text('\uf240', 1) sleep(1)
import time import busio from board import SCL, SDA from oled_text import OledText, Layout64, BigLine, SmallLine i2c = busio.I2C(SCL, SDA) oled = OledText(i2c, 128, 64) oled.layout = Layout64.layout_icon_1big_2small() oled.auto_show = False oled.text('\uf043', 1) oled.text("VAR%", 2) oled.text("Room", 3) oled.text("Humidity", 4) oled.show() oled.auto_show = True
import time import busio from board import SCL, SDA from oled_text import OledText, Layout64, BigLine, SmallLine i2c = busio.I2C(SCL, SDA) oled = OledText(i2c, 128, 64) oled.layout = Layout64.layout_icon_1big_2small() oled.auto_show = False oled.text('\uf2db', 1) oled.text("VAR C", 2) oled.text("Pi CPU", 3) oled.text("temp", 4) oled.show() oled.auto_show = True
from board import SCL, SDA import busio from oled_text import OledText i2c = busio.I2C(SCL, SDA) # Create the display, pass its pixel dimensions oled = OledText(i2c, 128, 64) # Write to the oled oled.text("Hello ...", 1) # Line 1 oled.text("... world!", 2) # Line 2
def main(): # creating i2c instance i2c = io.I2C(board.SCL, board.SDA) # creating sensor object (i2c object, height, width) oled = OledText(i2c, 128, 64) # layout oled.layout = { 1: SmallLine(2, 4, font="FreeSans.ttf", size=12), # title 2: BigLine(5, 20, font="FreeSans.ttf", size=24), # value 3: BigLine(80, 24, font="FreeSans.ttf", size=18), # unit 4: BigLine(74, 20, font="FontAwesomeSolid.ttf", size=10), # degree 5: SmallLine(120, 20, font="FreeSans.ttf", size=8), # exponent 6: SmallLine(2, 50, font="FreeSans.ttf", size=12), # name } oled.text("WCWH BEVO Beacon", 1) while True: # Getting Newest Measurements # --------------------------- # standard m = get_measurements( variables=["CO2", "PM_C_2p5", "Lux", "TVOC", "NO2", "CO", "T_NO2"], units=["ppm", "ug/m", "lux", "ppb", "ppb", "ppm", "C"], names=[ "Carbon Dioxide", "Particulate Matter", "Light Level", "Nitrogen Dioxide", "TVOCs", "Carbon Monoxide", "Temperature" ]) # demo purposes #m = get_measurements(variables=["CO2","PM_C_2p5","PM_C_10","T_NO2","RH_NO2"],units=["ppm","ug/m","ug/m","F","%"],names=["Carbon Dioxide","PM2.5","PM10","Temperature","Relative Humidity"]) # Displaying Measurements # ----------------------- try: for value, unit, name in m: print(f"{name}: {value} {unit}") if name == "Carbon Monoxide": # converting raw CO measurements to ppm value /= 1000 value = round(value, 1) if unit == "F": value = round(1.8 * value + 32, 2) oled.text(f"{value}", 2) # output of measured value oled.text(f"{unit}", 3) # output of the variable if unit in ["C", "F"]: # adding degree symbol for temperature oled.text(f"\uf22d", 4) oled.text(f"", 5) elif unit == "ug/m": # adding exponent for pm oled.text(f"", 4) oled.text(f"3", 5) else: # no output on these "lines" oled.text(f"", 4) oled.text(f"", 5) oled.text(f"{name}", 6) # output of the display name oled.show() time.sleep(3) # holding display for 3 seconds except OSError: oled.clear() oled.text(f"ERROR", 3) time.sleep(3)