Guest User

Untitled

a guest
Feb 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import machine
  2. import onewire, ds18x20
  3. import ssd1306
  4. import time
  5.  
  6.  
  7.  
  8. class CumulativeMovingAverage():
  9. def __init__(self):
  10. #super(CumulativeMovingAverage, self).__init__(*args))
  11. self.average = 0
  12. self.count = 0
  13.  
  14. def update(self, measurement):
  15. self.count += 1
  16. self.average = (measurement + ((self.count - 1) * self.average) ) / self.count
  17.  
  18. oled_rst = machine.Pin(16, machine.Pin.OUT)
  19. # To communicate to OLED display over i2c-bus we need to gpio pin 16 to High
  20. oled_rst.value(1)
  21. cma = CumulativeMovingAverage()
  22. i2c = machine.I2C(scl=machine.Pin(15), sda = machine.Pin(4))
  23. # Create driver for OLED ssd1306
  24. oled = ssd1306.SSD1306_I2C(128, 64, i2c)
  25.  
  26. ds = ds18x20.DS18X20(onewire.OneWire(machine.Pin(17)))
  27. devices = ds.scan()
  28.  
  29. while True:
  30. for device in devices:
  31. ds.convert_temp()
  32. time.sleep_ms(750)
  33. temp = ds.read_temp(device)
  34. cma.update(temp)
  35. # Note to myself: display driver inherits framebuffer which offer text-method for writing. No scandics
  36. oled.fill(0)
  37. oled.text('Temperature:', 0, 0)
  38. oled.text('Now: {:.2f}'.format(temp), 0, 10)
  39. oled.text('Avrg: {:.2f}'.format(cma.average), 0, 20)
  40. oled.show()
Add Comment
Please, Sign In to add comment