Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import machine
- import onewire, ds18x20
- import ssd1306
- import time
- class CumulativeMovingAverage():
- def __init__(self):
- #super(CumulativeMovingAverage, self).__init__(*args))
- self.average = 0
- self.count = 0
- def update(self, measurement):
- self.count += 1
- self.average = (measurement + ((self.count - 1) * self.average) ) / self.count
- oled_rst = machine.Pin(16, machine.Pin.OUT)
- # To communicate to OLED display over i2c-bus we need to gpio pin 16 to High
- oled_rst.value(1)
- cma = CumulativeMovingAverage()
- i2c = machine.I2C(scl=machine.Pin(15), sda = machine.Pin(4))
- # Create driver for OLED ssd1306
- oled = ssd1306.SSD1306_I2C(128, 64, i2c)
- ds = ds18x20.DS18X20(onewire.OneWire(machine.Pin(17)))
- devices = ds.scan()
- while True:
- for device in devices:
- ds.convert_temp()
- time.sleep_ms(750)
- temp = ds.read_temp(device)
- cma.update(temp)
- # Note to myself: display driver inherits framebuffer which offer text-method for writing. No scandics
- oled.fill(0)
- oled.text('Temperature:', 0, 0)
- oled.text('Now: {:.2f}'.format(temp), 0, 10)
- oled.text('Avrg: {:.2f}'.format(cma.average), 0, 20)
- oled.show()
Add Comment
Please, Sign In to add comment