Commit cdfc71a6 authored by chrissi^'s avatar chrissi^
Browse files

Current config: DS18x20 pool temperature


Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent 2c481d6a
# DS18x20 temperature sensor driver for MicroPython.
# MIT license; Copyright (c) 2016 Damien P. George
from micropython import const
_CONVERT = const(0x44)
_RD_SCRATCH = const(0xbe)
_WR_SCRATCH = const(0x4e)
class DS18X20:
def __init__(self, onewire):
self.ow = onewire
self.buf = bytearray(9)
def scan(self):
return [rom for rom in self.ow.scan() if rom[0] == 0x10 or rom[0] == 0x28]
def convert_temp(self):
self.ow.reset(True)
self.ow.writebyte(self.ow.SKIP_ROM)
self.ow.writebyte(_CONVERT)
def read_scratch(self, rom):
self.ow.reset(True)
self.ow.select_rom(rom)
self.ow.writebyte(_RD_SCRATCH)
self.ow.readinto(self.buf)
if self.ow.crc8(self.buf):
raise Exception('CRC error')
return self.buf
def write_scratch(self, rom, buf):
self.ow.reset(True)
self.ow.select_rom(rom)
self.ow.writebyte(_WR_SCRATCH)
self.ow.write(buf)
def read_temp(self, rom):
buf = self.read_scratch(rom)
if rom[0] == 0x10:
if buf[1]:
t = buf[0] >> 1 | 0x80
t = -((~t + 1) & 0xff)
else:
t = buf[0] >> 1
return t - 0.25 + (buf[7] - buf[6]) / buf[7]
else:
t = buf[1] << 8 | buf[0]
if t & 0x8000: # sign bit set
t = -((t ^ 0xffff) + 1)
return t / 16
......@@ -8,6 +8,8 @@ import micropython
import config
import json
from rgb import RgbChannel
import onewire
import ds18x20
class Uptime:
......@@ -22,6 +24,22 @@ class Uptime:
def pub(self):
self._mqtt_client.publish(self._mqtt_topic, str(self._uptime))
class OW:
def __init__(self, mqtt_client, mqtt_topic, pin):
self._ds = ds18x20.DS18X20(onewire.OneWire(pin))
self._mqtt_client = mqtt_client
self._mqtt_topic = mqtt_topic
def pub(self):
roms = self._ds.scan()
self._ds.convert_temp()
for rom in roms:
sns_id = "0x"+"".join(["{:>02x}".format(x) for x in rom])
print(sns_id)
t = self._ds.read_temp(rom)
self._mqtt_client.publish(self._mqtt_topic+sns_id, str(t))
class Adc:
def __init__(self, mqtt_client, mqtt_topic):
......@@ -205,16 +223,22 @@ uptime = Uptime(m, "ledctl/{}/uptime".format(m.machine_id))
s.register(uptime.increment, timeout=1)
s.register(uptime.pub, timeout=10)
#
led = OnBoardLed(m, "ledctl/{}/OnBoardLed".format(m.machine_id))
s.register(led.pub, timeout=10)
#led = OnBoardLed(m, "ledctl/{}/OnBoardLed".format(m.machine_id))
#s.register(led.pub, timeout=10)
#str1 = RgbChannel(m, "ledctl/{}/String1".format(m.machine_id), esp8266pinmap.D1, esp8266pinmap.D2, esp8266pinmap.D3)
#s.register(str1.pub, timeout=10)
#
str1 = RgbChannel(m, "ledctl/{}/String1".format(m.machine_id), esp8266pinmap.D1, esp8266pinmap.D2, esp8266pinmap.D3)
s.register(str1.pub, timeout=10)
#str2 = RgbChannel(m, "ledctl/{}/String2".format(m.machine_id), esp8266pinmap.D4, esp8266pinmap.D5, esp8266pinmap.D6)
#s.register(str2.pub, timeout=10)
a = Adc(m, "ledctl/{}/input_voltage".format(m.machine_id))
s.register(a.sample, 1)
s.register(a.pub, 30)
ow = OW(m,"singularity/pool/temp/", machine.Pin(esp8266pinmap.D1))
s.register(ow.pub, 10)
# start scheduler
# runs forever
s.run()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment