Skip to content

Commit

Permalink
initated deepsleep, but is broken:
Browse files Browse the repository at this point in the history
* sleep hangs
* set sleeping_time to "0" produces "undefied variable"
  • Loading branch information
einsiedlerkrebs committed Apr 15, 2019
1 parent 55ce614 commit 191a46b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ download-requirements:
#Install Pytrack Board Libary
$(fetch) $(target_dir) https://raw.githubusercontent.com/pycom/pycom-libraries/0f123c7/pytrack/lib/pytrack.py

#Install Pycom Deepsleep Library
$(fetch) $(target_dir) https://raw.githubusercontent.com/pycom/pycom-libraries/1fbc177/deepsleep/deepsleep.py

#Install BME280 Libary
$(fetch) $(target_dir) https://raw.githubusercontent.com/catdog2/mpy_bme280_esp8266/d7e052b/bme280.py

Expand Down
2 changes: 2 additions & 0 deletions settings.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# TODO: Please note this is not the _real thing_ yet at it will just use
# this value to apply to ``time.sleep()`` after each duty cycle.
'interval': 1.0,
'sleeping_time': 1,
#'wake_up_pins': ['P99', 'P77']
}

# Networking configuration.
Expand Down
11 changes: 10 additions & 1 deletion terkin/datalogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, settings):
self.settings.dump()
self.device = None
self.sensor_manager = SensorManager()
self.loop_count = None

@property
def appname(self):
Expand Down Expand Up @@ -55,6 +56,8 @@ def start(self):
self.register_busses()
self.register_sensors()

self.device.enable_sleep()

self.start_mainloop()

def register_busses(self):
Expand Down Expand Up @@ -104,6 +107,7 @@ def start_mainloop(self):

# Feed the watchdog timer to keep the system alive.
self.device.feed_wdt()
self.loop_count = 0

# Indicate activity.
# TODO: Optionally disable this output.
Expand Down Expand Up @@ -154,6 +158,9 @@ def transmit_readings(self, data):

def loop(self):
#log.info('Terkin loop')
if self.device.sleeping_time >> 0 and self.loop_count >> 0:

This comment has been minimized.

Copy link
@amotl

amotl May 16, 2019

Member

This is not quite right, @einsiedlerkrebs. ">>" probably does bitshifting or anything else but will not perform the comparison as intended here, right?

self.device.get_wake_reason()
log.info("Sleep got interrupted by {}".format(self.device.wake_reason))

# Read sensors.
data = self.read_sensors()
Expand All @@ -169,4 +176,6 @@ def loop(self):

# Sleep until the next measurement cycle.
# TODO: Account for deep sleep here.
utime.sleep(self.settings.get('main.interval'))
# utime.sleep(self.settings.get('main.interval'))
self.device.start_sleep()
self.loop_count = self.loop_count + 1
71 changes: 71 additions & 0 deletions terkin/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def __init__(self, name=None, version=None, settings=None):
self.wdt = None
self.rtc = None

self.sleep = None
self.sleeping_time = None
self.wake_reason = None
self.interrupt_pins = None

@property
def appname(self):
return '{} {}'.format(self.name, self.version)
Expand Down Expand Up @@ -102,6 +107,72 @@ def start_rtc(self):
time.sleep_ms(50)
log.info('RTC: %s', self.rtc.now())

def enable_sleep(self):

from deepsleep import DeepSleep

self.sleep = DeepSleep()

interrupt_pins = self.settings.get('main.wake_up_pins')

if isinstance(interrupt_pins, list):
self.interrupt_pins = interrupt_pins
self.sleep.enable_pullups(self.interrupt_pins)
log.info("Sleep: interrupt pins: {}".format(self.interrupt_pins))
else:
log.info("Sleep: interrupt pins are not defined")

self.sleeping_time = self.settings.get('main.sleeping_time')

if self.sleeping_time >> 0:
log.info("Sleep: sleep would last {} seconds".format(self.sleeping_time))
else:
log.info("Sleep: sleep is disabled; sleeping_time: {}".format(self.sleeping_time))




def start_sleep(self, seconds=None):

if not self.sleep:
log.warning('Sleep was not initiated')
pass

if not seconds and self.sleeping_time:
s = self.sleeping_time
elif seconds:
s = seconds

if s >> 0:
log.info('Tired, sleeping for {} seconds, now'.format(s))
self.sleep.go_to_sleep(s)
else:
log.info('Tired now, but sleeping_time is set to zero')


def get_wake_reason(self):

self.wake_reason = None

import deepsleep
wake_reason = self.sleep.get_wake_status()

if wake_reason['wake'] == deepsleep.PIN_WAKE:
self.wake_reason = "pin"
log.info("Sleep: pin woke up")
elif wake_reason['wake'] == deepsleep.TIMER_WAKE:
self.wake_reason = "timer"
log.info("Sleep: timer woke up")
elif wake_reason['wake'] == deepsleep.POWER_ON_WAKE:
self.wake_reason = "reset"
log.info("Sleep: interrupt by device reset")
else:
self.wake_reason = "undefined"
log.info("Sleep: undefined reason for interruption")

return self.wake_reason


def run_gc(self):
"""
Run a garbage collection.
Expand Down

0 comments on commit 191a46b

Please sign in to comment.