Skip to content

Commit

Permalink
populate SensorManager, add bus management, add OneWireBus
Browse files Browse the repository at this point in the history
  • Loading branch information
einsiedlerkrebs committed Mar 27, 2019
1 parent 8ea0655 commit 82755fa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
15 changes: 11 additions & 4 deletions hiveeyes/datalogger.py
Expand Up @@ -13,6 +13,7 @@
from terkin.datalogger import TerkinDatalogger
from hiveeyes.sensor_hx711 import HX711Sensor
from hiveeyes.sensor_ds18x20 import DS18X20Sensor
from terkin.sensor import OneWireBus


class HiveeyesDatalogger(TerkinDatalogger):
Expand All @@ -34,6 +35,13 @@ def register_sensors(self):
# Add some sensors for the Hiveeyes project.
self.device.tlog('Registering Hiveeyes sensors')

ds18x20_settings = self.settings.get('sensors.registry.ds18x20')
owb = OneWireBus()
owb.register_pin("data", ds18x20_settings['pin_data'])
owb.start()

self.register_bus("onewire:0", owb)

# Setup the HX711.
try:
self.add_hx711_sensor()
Expand Down Expand Up @@ -78,11 +86,10 @@ def add_ds18x20_sensor(self):
Setup and register the DS18X20 sensor component with your data logger.
"""

# Initialize HX711 sensor component.
ds18x20_settings = self.settings.get('sensors.registry.ds18x20')

bus = self.sensor_manager.get_bus_by_name('onewire:0')
ds18x20_sensor = DS18X20Sensor()
ds18x20_sensor.register_pin('data', ds18x20_settings['pin_data'])
ds18x20_sensor.acquire_bus(bus)


# Select driver module. Use "gerber" (vanilla) or "heisenberg" (extended).

Expand Down
26 changes: 9 additions & 17 deletions hiveeyes/sensor_ds18x20.py
Expand Up @@ -4,9 +4,7 @@
# License: GNU General Public License, Version 3

import time
from machine import Pin
from onewire.onewire import DS18X20
from onewire.onewire import OneWire
from binascii import hexlify

from terkin.sensor import AbstractSensor
Expand Down Expand Up @@ -36,40 +34,34 @@ def __init__(self):
self.devices = []
self.readings = None
self.sensors = None
self.bus = None


def start(self):

# Initialize the OneWire hardware driver.
try:
self.wire = OneWire(Pin(self.pins['data']))
except Exception as ex:
print('ERROR: OneWire hardware driver failed. {}'.format(ex))
raise
def acquire_bus(self, bus):
self.bus = bus

def start(self):
if self.bus is None:
raise KeyError("Bus missing")

# Initialize the DS18x20 hardware driver.
try:
self.sensors = DS18X20(self.wire)
self.sensors = DS18X20(self.bus.adapter)
except Exception as ex:
print('ERROR: DS18X20 hardware driver failed. {}'.format(ex))
raise

self.scan_devices()


def scan_devices(self):
self.devices = [rom for rom in self.wire.scan() if rom[0] == 0x10 or rom[0] == 0x28]
print(list(map(hexlify, self.devices)))


def read(self):

d = {}
print('INFO: Acquire reading from DS18X20')
self.sensors.start_conversion()
time.sleep_ms(750)
# for loop goes here
for device in self.devices:
time.sleep(1)
value = self.sensors.read_temp_async(device)
name = "temperature_" + hexlify(device).decode()
d[name] = value
Expand Down
48 changes: 47 additions & 1 deletion terkin/sensor.py
Expand Up @@ -3,20 +3,48 @@
# (c) 2019 Andreas Motl <andreas@hiveeyes.org>
# License: GNU General Public License, Version 3

from onewire.onewire import OneWire
from machine import Pin
from binascii import hexlify

class SensorManager:
def __init__(self):
self.sensors = []
self.busses = {}
pass



def register_sensor(self, sensor):
self.sensors.append(sensor)

def register_bus(self, name, bus):
self.busses[name] = bus

pass


def get_bus_by_name(selfs, name):
return self.busses.get(name)

def get_sensor_by_name(self, name):
pass


class AbstractBus:
def __init__(self):
"""
convention <type>:<index>
"""
self.name = None
self.adapter = None
self.devices = []
self.pins = {}

def register_pin(self, name, pin):
self.pins[name] = pin


class AbstractSensor:
"""
Abstract sensor container, containing meta data as readings
Expand All @@ -31,8 +59,8 @@ def __init__(self):
"""
self.address = None
self.bus = None
self.pins = {}
self.parameter = {}
self.pins = {}

def start(self):
raise NotImplementedError("Must be implemented in sensor driver")
Expand All @@ -57,6 +85,24 @@ def read(self):
pass


class OneWireBus(AbstractBus):
# Initialize the OneWire hardware driver.

def start(self):
try:
self.adapter = OneWire(Pin(self.pins['data']))
self.scan_devices()
except Exception as ex:
print('ERROR: OneWire hardware driver failed. {}'.format(ex))
raise


def scan_devices(self):
self.devices = [rom for rom in self.adapter.scan() if rom[0] == 0x10 or rom[0] == 0x28]
print(list(map(hexlify, self.devices)))




class MemoryFree:

Expand Down

0 comments on commit 82755fa

Please sign in to comment.