diff --git a/Bme280.py b/Bme280.py new file mode 100644 index 0000000..d7e1021 --- /dev/null +++ b/Bme280.py @@ -0,0 +1,235 @@ +# +# +# Authors: Paul Cunnane 2016, Peter Dahlebrg 2016, Nils Schulte 2020 +# +# This module borrows from the Adafruit BME280 Python library. Original +# Copyright notices are reproduced below. +# +# Those libraries were written for the Raspberry Pi. This modification is +# intended for the MicroPython and esp8266 boards. +# +# Copyright (c) 2014 Adafruit Industries +# Author: Tony DiCola +# +# Based on the BMP280 driver with BME280 changes provided by +# David J Taylor, Edinburgh (www.satsignal.eu) +# +# Based on Adafruit_I2C.py created by Kevin Townsend. +# +# 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. + +import time +from ustruct import unpack, unpack_from +from array import array + +# BME280 default address. +BME280_I2CADDR = 0x76 + +# Operating Modes +BME280_OSAMPLE_1 = 1 +BME280_OSAMPLE_2 = 2 +BME280_OSAMPLE_4 = 3 +BME280_OSAMPLE_8 = 4 +BME280_OSAMPLE_16 = 5 + +BME280_REGISTER_CONTROL_HUM = 0xF2 +BME280_REGISTER_CONTROL = 0xF4 + + +class BME280: + + def __init__(self, + mode=BME280_OSAMPLE_1, + address=BME280_I2CADDR, + i2c=None, + **kwargs): + # Check that mode is valid. + if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, + BME280_OSAMPLE_8, BME280_OSAMPLE_16]: + raise ValueError( + 'Unexpected mode value {0}. Set mode to one of ' + 'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or ' + 'BME280_ULTRAHIGHRES'.format(mode)) + self._mode = mode + self.address = address + if i2c is None: + raise ValueError('An I2C object is required.') + self.i2c = i2c + + # load calibration data + dig_88_a1 = self.i2c.readfrom_mem(self.address, 0x88, 26) + dig_e1_e7 = self.i2c.readfrom_mem(self.address, 0xE1, 7) + self.dig_T1, self.dig_T2, self.dig_T3, self.dig_P1, \ + self.dig_P2, self.dig_P3, self.dig_P4, self.dig_P5, \ + self.dig_P6, self.dig_P7, self.dig_P8, self.dig_P9, \ + _, self.dig_H1 = unpack("> 4) + + self.dig_H6 = unpack_from("> 4 + raw_press = ((readout[0] << 16) | (readout[1] << 8) | readout[2]) >> 4 + # temperature(0xFA): ((msb << 16) | (lsb << 8) | xlsb) >> 4 + raw_temp = ((readout[3] << 16) | (readout[4] << 8) | readout[5]) >> 4 + # humidity(0xFD): (msb << 8) | lsb + raw_hum = (readout[6] << 8) | readout[7] + + result[0] = raw_temp + result[1] = raw_press + result[2] = raw_hum + + def read_compensated_data(self, result=None): + """ Reads the data from the sensor and returns the compensated data. + + Args: + result: array of length 3 or alike where the result will be + stored, in temperature, pressure, humidity order. You may use + this to read out the sensor without allocating heap memory + + Returns: + array with temperature, pressure, humidity. Will be the one from + the result parameter if not None + """ + self.read_raw_data(self._l3_resultarray) + raw_temp, raw_press, raw_hum = self._l3_resultarray + # temperature + var1 = ((raw_temp >> 3) - (self.dig_T1 << 1)) * (self.dig_T2 >> 11) + var2 = (((((raw_temp >> 4) - self.dig_T1) * + ((raw_temp >> 4) - self.dig_T1)) >> 12) * self.dig_T3) >> 14 + self.t_fine = var1 + var2 + temp = (self.t_fine * 5 + 128) >> 8 + + # pressure + var1 = self.t_fine - 128000 + var2 = var1 * var1 * self.dig_P6 + var2 = var2 + ((var1 * self.dig_P5) << 17) + var2 = var2 + (self.dig_P4 << 35) + var1 = (((var1 * var1 * self.dig_P3) >> 8) + + ((var1 * self.dig_P2) << 12)) + var1 = (((1 << 47) + var1) * self.dig_P1) >> 33 + if var1 == 0: + pressure = 0 + else: + p = 1048576 - raw_press + p = (((p << 31) - var2) * 3125) // var1 + var1 = (self.dig_P9 * (p >> 13) * (p >> 13)) >> 25 + var2 = (self.dig_P8 * p) >> 19 + pressure = ((p + var1 + var2) >> 8) + (self.dig_P7 << 4) + + # humidity + h = self.t_fine - 76800 + h = (((((raw_hum << 14) - (self.dig_H4 << 20) - + (self.dig_H5 * h)) + 16384) + >> 15) * (((((((h * self.dig_H6) >> 10) * + (((h * self.dig_H3) >> 11) + 32768)) >> 10) + + 2097152) * self.dig_H2 + 8192) >> 14)) + h = h - (((((h >> 15) * (h >> 15)) >> 7) * self.dig_H1) >> 4) + h = 0 if h < 0 else h + h = 419430400 if h > 419430400 else h + humidity = h >> 12 + + if result: + result[0] = temp + result[1] = pressure + result[2] = humidity + return result + + return array("i", (temp, pressure, humidity)) + + @property + def valuesStr(self): + """ human readable values """ + + t, p, h = self.read_compensated_data() + + p = p // 256 + pi = p // 100 + pd = p - pi * 100 + + hi = h // 1024 + hd = h * 100 // 1024 - hi * 100 + return ("{}C".format(t / 100), "{}.{:02d}hPa".format(pi, pd), + "{}.{:02d}%".format(hi, hd)) + + @property + def values(self): + """ returns temperature,pressure and humidity in °C, hPa and % """ + temperature,pressure,humidity = self.read_compensated_data() + temperature = temperature / 100 + pressure = pressure / 256 + humidity = humidity / 1024 + return (temperature,pressure,humidity) + + @property + def temperature(self): + """ measured temperature in °C """ + temperature,_,_ = self.values + return temperature + + @property + def pressure(self): + """ measured pressure in hPa """ + _,pressure,_ = self.values + return pressure + + @property + def humidity(self): + """ measured rel. humidity in % """ + _,_,humidity = self.values + return humidity \ No newline at end of file diff --git a/Hardware.py b/Hardware.py index 5293761..2b1fd16 100644 --- a/Hardware.py +++ b/Hardware.py @@ -8,6 +8,7 @@ import HousingLEDs import LED import Button import HousingCapButton +import Bme280 #Init i2c Bus on 17,18 i2cBus = machine.I2C(sda = machine.Pin(17), scl=machine.Pin(18)) @@ -52,4 +53,12 @@ rightButton = Button.Button(pin=36,inverted=True) housingButton = HousingCapButton.HousingCapButton(pin=15) buttons = (leftButton,middleButton,rightButton) -leds = (leftButtonLED,middleButtonLED,rightButtonLED) \ No newline at end of file +leds = (leftButtonLED,middleButtonLED,rightButtonLED) + +#Init Sensors +try: + sensorBme = Bme280.BME280(i2c=i2cBus) +except Exception as e: + print(e) + + \ No newline at end of file diff --git a/Screens.py b/Screens.py index 48a3371..1102af7 100644 --- a/Screens.py +++ b/Screens.py @@ -163,6 +163,18 @@ class RingtoneSettingsScreen(): await numberScreen HW.buzzer.stop() +class HumidityScreen(): + def __iter__(self): + HW.housingLEDs.fill((0,50,200)) + numberScreen = ShowNumberScreen(HW.sensorBme.humidity,numberGetter=lambda : HW.sensorBme.humidity) + await numberScreen + +class TemperatureScreen(): + def __iter__(self): + HW.housingLEDs.fill((0,50,200)) + numberScreen = ShowNumberScreen(HW.sensorBme.temperature,numberGetter=lambda : HW.sensorBme.temperature) + await numberScreen + class ShowIpScreen(): def __init__(self): self.IP = "0" @@ -216,6 +228,31 @@ class ShowLongNumberScreen(): HW.buttons[1].setCallbacks() __iter__ = __await__ # https://github.com/micropython/micropython/issues/2678 +class ShowNumberScreen(): + def __init__(self,number,numberGetter=None): + self.number = number + self.running = False + self.update = numberGetter + + def onButtonReleased(self,pushDownTime): + HW.leds[1].off() + if pushDownTime > 700: + self.running = False + + def __await__(self): + self.running = True + HW.buttons[1].setCallbacks(onPushUp=self.onButtonReleased,onPushDown=lambda :HW.leds[1].on()) + while self.running: + if self.update != None: + self.number = self.update() + while (not HW.stepperminu.isAtTarget() or not HW.stepperhour.isAtTarget()): + await asyncio.sleep_ms(200) + HW.stepperminu.rotateTo(-((self.number%10)/12)) + HW.stepperhour.rotateTo(-((self.number//10)/12)) + await asyncio.sleep_ms(500) + HW.buttons[1].setCallbacks() + __iter__ = __await__ # https://github.com/micropython/micropython/issues/2678 + class GetNumberScreen(): def __init__(self,acceptedInputNumbers,startNumber = None,onStart=None,onNumberChange = None,onPicked = None): self.acceptedInputNumbers = acceptedInputNumbers diff --git a/main.py b/main.py index 7324498..6ff5e0b 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,8 @@ async def run_screens(): (Screens.ClockScreen(),(140,70,255)),\ (Screens.SetAlarmTimeScreen(),(255,0,0)),\ (Screens.RingtoneSettingsScreen(),(255,0,127)),\ + (Screens.TemperatureScreen(),(0,50,200)),\ + (Screens.HumidityScreen(),(0,50,200)),\ (Screens.SetTimeScreen(),(100,200,50)),\ (Screens.InitHandsScreen(),(0,0,255)),\ (Screens.ShowIpScreen(),HW.HousingLEDs.YELLOW),\