Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port codebase to Genuine MicroPython on ESP32 #30

Merged
merged 1 commit into from Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions terkin/datalogger.py
Expand Up @@ -30,7 +30,11 @@ def __init__(self):

# Maybe refactor to TerkinCore.
class TerkinDatalogger:
""" """
"""
Main class of project.
Handles loop & sleep, registers sensors, reads their data and stores them.
Shows up as 'datalogger' in the rest of the program.
"""

# Application metadata.
name = 'Terkin MicroPython Datalogger'
Expand Down Expand Up @@ -283,6 +287,9 @@ def get_sleep_time(self):
def register_sensors(self):
"""
Configure and register sensor objects.
There are three types of sensors: system, environment & busses. Only the former two are assigned to the latter (if applicable).
Definitions are in 'settings.py'.
The sensor are registered by calling their respective classes from terkin/drivers/
"""

# Add sensors.
Expand Down Expand Up @@ -419,7 +426,10 @@ def register_sensors(self):
#self.device.run_gc()

def read_sensors(self):
"""Read measurements from all sensor objects"""
"""
Read measurements from all sensor objects that have been registered in the sensor_manager.
Reading is done with the read() function of each respective sensor object.
"""

# Collect observations.
data = {}
Expand Down
6 changes: 2 additions & 4 deletions terkin/device.py
Expand Up @@ -243,10 +243,8 @@ def add(item=''):
add('=' * len(title))

# Machine runtime information.
if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom:
frequency = machine.freq() / 1000000
else:
frequency = machine.freq()[0] / 1000000
frequency = machine.freq() / 1000000

add('CPU freq {} MHz'.format(frequency))
add('Device id {}'.format(self.device_id))
add()
Expand Down
9 changes: 6 additions & 3 deletions terkin/drivers/ds18x20.py
Expand Up @@ -14,7 +14,10 @@


class DS18X20Sensor(AbstractSensor):
"""A generic DS18B20 sensor component."""
"""
A generic DS18B20 sensor component.
Used by terkin/datalogger to register and read() from this sensor.
"""

def acquire_bus(self, bus):
"""
Expand All @@ -35,8 +38,8 @@ def start(self):

# Vanilla MicroPython 1.11
if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
from ds18x20 import DS18X20
self.driver = DS18X20NativeDriverAdapter(DS18X20(onewire_bus))
import ds18x20
self.driver = DS18X20NativeDriverAdapter(ds18x20.DS18X20(onewire_bus))

# Pycom MicroPython 1.9.4
elif platform_info.vendor == platform_info.MICROPYTHON.Pycom:
Expand Down
70 changes: 70 additions & 0 deletions terkin/lib/DFRobot_MAX17043.py
@@ -0,0 +1,70 @@
import time

from machine import I2C, Pin

# Get I2C bus
i2c = I2C(scl = Pin(22), sda = Pin(21), freq=400000)

MAX17043_ADDR = 0x36
MAX17043_VCELL = 0x02
MAX17043_SOC = 0x04
MAX17043_MODE = 0x06
MAX17043_VERSION = 0x08
MAX17043_CONFIG = 0x0c
MAX17043_COMMAND = 0xfe

class DFRobot_MAX17043():

def __init__(self):
pass

def begin(self):
self.write16(MAX17043_COMMAND, 0x5400)
time.sleep(0.01)
if self.read16(MAX17043_CONFIG) == 0x971c:
self.write16(MAX17043_MODE, 0x4000)
time.sleep(0.01)
self.write16(MAX17043_CONFIG, 0x9700)
return 0
else:
return -1

def readVoltage(self):
return (1.25 * (self.read16(MAX17043_VCELL) >> 4))

def readPercentage(self):
tmp = self.read16(MAX17043_SOC)
return ((tmp >> 8) + 0.003906 * (tmp & 0x00ff))

def setInterrupt(self, per):
if per > 32:
per = 32
elif per < 1:
per = 1
per = 32 - int(per)
self.writeRegBits(MAX17043_CONFIG, per, 0x01f, 0)

def clearInterrupt(self):
self.writeRegBits(MAX17043_CONFIG, 0, 0x01, 5)

def setSleep(self):
self.writeRegBits(MAX17043_CONFIG, 1, 0x01, 7)

def setWakeUp(self):
self.writeRegBits(MAX17043_CONFIG, 0, 0x01, 7)

def write16(self, reg, dat):
buf = bytearray(2)
buf[0] = dat >> 8
buf[1] = dat & 0x00ff
i2c.writeto_mem(MAX17043_ADDR, reg, buf)

def read16(self, reg):
buf = i2c.readfrom_mem(MAX17043_ADDR, reg, 2)
return ((buf[0] << 8) | buf[1])

def writeRegBits(self, reg, dat, bits, offset):
tmp = self.read16(reg)
tmp = (tmp & (~(bits << offset))) | (dat << offset)
self.write16(reg, tmp)

16 changes: 13 additions & 3 deletions terkin/lib/hx711.py
Expand Up @@ -5,6 +5,8 @@
import utime
from terkin import logging
from machine import Pin, enable_irq, disable_irq, idle
from terkin.util import get_platform_info
platform_info = get_platform_info()

log = logging.getLogger(__name__)

Expand All @@ -20,8 +22,15 @@ class HX711:
def __init__(self, dout, pd_sck, gain=128):

# Define two pins for clock and data.
self.pSCK = Pin(pd_sck, mode=Pin.OUT)
self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP)
if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
self.pSCK = Pin(int(pd_sck[1:]), mode=Pin.OUT)
self.pOUT = Pin(int(dout[1:]), mode=Pin.IN, pull=Pin.PULL_UP)
elif platform_info.vendor == platform_info.MICROPYTHON.Pycom:
self.pSCK = Pin(pd_sck, mode=Pin.OUT)
self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP)
else:
raise NotImplementedError('HX711 is '
'not implemented on this platform')

self.initialized = False

Expand Down Expand Up @@ -202,7 +211,8 @@ def power_up(self):

# Unfreeze pin hold when coming from deep sleep.
# https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72
self.pSCK.hold(False)
if platform_info.vendor == platform_info.MICROPYTHON.Pycom:
self.pSCK.hold(False)

log.info('HX711 power up')
self.pSCK.value(False)
Expand Down
2 changes: 1 addition & 1 deletion terkin/network/wifi.py
Expand Up @@ -583,7 +583,7 @@ def read(self):

if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
stats = {}
stats['system.wifi.channel'] = self.station.config('channel')
#stats['system.wifi.channel'] = self.station.config('channel')

try:
stats['system.wifi.rssi'] = self.station.status('rssi')
Expand Down
14 changes: 10 additions & 4 deletions terkin/sensor/core.py
Expand Up @@ -278,18 +278,18 @@ def start(self):
try:
# Vanilla MicroPython 1.11
if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
from onewire import OneWire
import onewire
pin = Pin(int(self.pins['data'][1:]))

self.adapter = onewire.OneWire(pin)
# Pycom MicroPython 1.9.4
elif platform_info.vendor == platform_info.MICROPYTHON.Pycom:
from onewire_python import OneWire
pin = Pin(self.pins['data'])
self.adapter = OneWire(pin)

else:
raise NotImplementedError('1-Wire driver not implemented on this platform')

self.adapter = OneWire(pin)
self.scan_devices()

except Exception as ex:
Expand Down Expand Up @@ -342,7 +342,13 @@ def start(self):
""" """
# Todo: Improve error handling.
try:
self.adapter = I2C(self.number, mode=I2C.MASTER, pins=(self.pins['sda'], self.pins['scl']), baudrate=100000)
if platform_info.vendor == platform_info.MICROPYTHON.Vanilla:
self.adapter = I2C(self.number, sda = Pin(int(self.pins['sda'][1:])), scl = Pin(int(self.pins['scl'][1:])), freq=100000)
elif platform_info.vendor == platform_info.MICROPYTHON.Pycom:
self.adapter = I2C(self.number, mode=I2C.MASTER, pins=(self.pins['sda'], self.pins['scl']), baudrate=100000)
else:
raise NotImplementedError('I2C Bus is '
'not implemented on this platform')
self.scan_devices()
except Exception as ex:
log.exc(ex, 'I2C hardware driver failed')
Expand Down