From d112201710e1735cb2f7e12bd5978814cd496693 Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Sun, 20 Aug 2023 16:48:57 +0200 Subject: [PATCH] erster Commit --- LCD1602.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ degdisp.py | 40 +++++++++++++++++++ wdisp.py | 66 +++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 LCD1602.py create mode 100755 degdisp.py create mode 100755 wdisp.py diff --git a/LCD1602.py b/LCD1602.py new file mode 100644 index 0000000..a9cf7d6 --- /dev/null +++ b/LCD1602.py @@ -0,0 +1,114 @@ +# -*- coding: utf-8 -*- +import time +from smbus import SMBus +b = SMBus(1) + +#Device I2C Arress +LCD_ADDRESS = (0x7c>>1) + +LCD_CLEARDISPLAY = 0x01 +LCD_RETURNHOME = 0x02 +LCD_ENTRYMODESET = 0x04 +LCD_DISPLAYCONTROL = 0x08 +LCD_CURSORSHIFT = 0x10 +LCD_FUNCTIONSET = 0x20 +LCD_SETCGRAMADDR = 0x40 +LCD_SETDDRAMADDR = 0x80 + +#flags for display entry mode +LCD_ENTRYRIGHT = 0x00 +LCD_ENTRYLEFT = 0x02 +LCD_ENTRYSHIFTINCREMENT = 0x01 +LCD_ENTRYSHIFTDECREMENT = 0x00 + +#flags for display on/off control +LCD_DISPLAYON = 0x04 +LCD_DISPLAYOFF = 0x00 +LCD_CURSORON = 0x02 +LCD_CURSOROFF = 0x00 +LCD_BLINKON = 0x01 +LCD_BLINKOFF = 0x00 + +#flags for display/cursor shift +LCD_DISPLAYMOVE = 0x08 +LCD_CURSORMOVE = 0x00 +LCD_MOVERIGHT = 0x04 +LCD_MOVELEFT = 0x00 + +#flags for function set +LCD_8BITMODE = 0x10 +LCD_4BITMODE = 0x00 +LCD_2LINE = 0x08 +LCD_1LINE = 0x00 +LCD_5x8DOTS = 0x00 + + +class LCD1602: + def __init__(self, col, row): + self._row = row + self._col = col + self._showfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; + self.begin(self._row,self._col) + + + def command(self,cmd): + b.write_byte_data(LCD_ADDRESS,0x80,cmd) + + def write(self,data): + b.write_byte_data(LCD_ADDRESS,0x40,data) + + def setCursor(self,col,row): + if(row == 0): + col|=0x80 + else: + col|=0xc0; + self.command(col) + + def clear(self): + self.command(LCD_CLEARDISPLAY) + time.sleep(0.002) + def printout(self,arg): + if(isinstance(arg,int)): + arg=str(arg) + + for x in bytearray(arg,'utf-8'): + self.write(x) + + + def display(self): + self._showcontrol |= LCD_DISPLAYON + self.command(LCD_DISPLAYCONTROL | self._showcontrol) + + + def begin(self,cols,lines): + if (lines > 1): + self._showfunction |= LCD_2LINE + + self._numlines = lines + self._currline = 0 + + time.sleep(0.05) + + + # Send function set command sequence + self.command(LCD_FUNCTIONSET | self._showfunction) + #delayMicroseconds(4500); # wait more than 4.1ms + time.sleep(0.005) + # second try + self.command(LCD_FUNCTIONSET | self._showfunction); + #delayMicroseconds(150); + time.sleep(0.005) + # third go + self.command(LCD_FUNCTIONSET | self._showfunction) + # finally, set # lines, font size, etc. + self.command(LCD_FUNCTIONSET | self._showfunction) + # turn the display on with no cursor or blinking default + self._showcontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF + self.display() + # clear it off + self.clear() + # Initialize to default text direction (for romance languages) + self._showmode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT + # set the entry mode + self.command(LCD_ENTRYMODESET | self._showmode); + diff --git a/degdisp.py b/degdisp.py new file mode 100755 index 0000000..32f8da4 --- /dev/null +++ b/degdisp.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# encoding: utf-8 + +import time +import configparser +import logging +import LCD1602 +try: + import smbus2 +except ImportError: # Wenn smbus2 nicht zu importieren ist, nehmen wir smbus + from smbus import SMBus +import mysql.connector + +port = 1 +adresse_lcd1602 = 0x25 +deg = 0o1212 + +logging.basicConfig( format='%(asctime)s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) + + +def info(msg): + logging.info(msg) + +def debug(msg): + logging.debug(msg) + +def print_weather(): + lcd.setCursor(0, 0) + lcd.printout('0o{0:o}'.format(deg)) + + +#LCD1602 initialisieren +lcd=LCD1602.LCD1602(16,2) + +try: + while True: + print_weather() +except(KeyboardInterrupt): + lcd.clear() + diff --git a/wdisp.py b/wdisp.py new file mode 100755 index 0000000..83901f3 --- /dev/null +++ b/wdisp.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# encoding: utf-8 + +import time +import configparser +import logging +import bme280 +import LCD1602 +try: + import smbus2 +except ImportError: # Wenn smbus2 nicht zu importieren ist, nehmen wir smbus + from smbus import SMBus +import mysql.connector + +port = 1 +adresse_bme280 = 0x76 +adresse_lcd1602 = 0x25 + +logging.basicConfig( format='%(asctime)s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) + + +def info(msg): + logging.info(msg) + +def debug(msg): + logging.debug(msg) + +def print_weather(temperature, humidity, pressure): + lcd.setCursor(0, 0) + lcd.printout('{:05.2f}\223C'.format(temperature) + lcd.printout(' {:05.2f}hPa'.format(pressure)) + + lcd.setCursor(0, 1) + lcd.printout('{:05.2f}%'.format(humidity)) +# Datenbankverbindung herstellen. +config = configparser.ConfigParser() +config.read('db.ini') +mydb = mysql.connector.connect(host=config['weatherdb']['host'], user=config['weatherdb']['user'],password=config['weatherdb']['password'],database =config['weatherdb']['database']) +mycursor = mydb.cursor() +debug('Datenverbindung hergestellt.') + +#LCD1602 initialisieren +lcd=LCD1602.LCD1602(16,2) + +# BME280 initialisieren +bus = smbus2.SMBus(port) +calibration_params = bme280.load_calibration_params(bus, adresse_bme280) +debug('Sensor kalibriert.') +try: + while True: + data = bme280.sample(bus, adresse_bme280, calibration_params) + temperature = data.temperature + pressure = data.pressure + humidity = data.humidity + info('{:05.2f}°C {:05.2f}hPa {:05.2f}%'.format(temperature, pressure, humidity)) + print_weather(temperature,humidity, pressure) + sql = 'INSERT into stats (datum,temperature, humidity, pressure) VALUES(now(), %s, %s, %s)' + val = (temperature, humidity, pressure) + mycursor.execute(sql, val) + mydb.commit() + time.sleep(10) +except(KeyboardInterrupt): + mydb.close() + lcd.clear() + del lcd +