π
2011-12-17 01:01
in Linux, Osa
The MyZeo folks definitely deserve credit for releasing open firmware and instructions on how to interface with their bedside Zeo device via a custom made serial port: http://zeorawdata.sourceforge.net/starting.html
Since I had been working on arduino, I happened to have one of those 3.3V FTDI USB-serial converters, and was able to make a cable that connects my bedside Zeo to my server closer via a custom cable going through my existing Cat-5 wiring.
The example code they gave is however, while very fancy, not that simple to borrow from for a more simple application that simply needs to keep track of the sleep stage.
So, after finding the relevant info, I compiled the code below, which I'm posting for others to use (you can also download it here: zeo raw data code sample).
#!/usr/bin/python
# This is based off the Zeo Raw Data Library at the bottom of
# http://zeorawdata.sourceforge.net/starting.html
# This script is a much simpler version of how to log basic data than the cool
# and pretty GUI_Viewer from http://zeorawdata.sourceforge.net/examples.html
# This script is an easier example to steal from for simple logging/integration.
# By Marc MERLIN <marc_soft@merlins.org> / 2011/12/17
# License: GPLv3.
#gandalfthegrey [mc]$ ./logsleep.py
# 2011-12-17 12:19:10: HeadbandDocked
# 2011-12-17 12:19:15: HeadbandUnDocked
# 2011-12-17 12:19:22: Sleep state: Undefined
# 2011-12-17 12:19:52: Sleep state: Undefined
# 2011-12-17 12:20:22: Sleep state: Undefined
# 2011-12-17 12:20:52: Sleep state: Undefined
# 2011-12-17 12:21:22: Sleep state: Undefined
# 2011-12-17 12:21:52: Sleep state: Undefined
# 2011-12-17 12:22:22: Sleep state: Undefined
# 2011-12-17 12:22:52: Sleep state: Undefined
# 2011-12-17 12:23:22: Sleep state: Undefined
# 2011-12-17 12:23:52: NightStart
# 2011-12-17 12:23:52: Sleep state: Awake
# 2011-12-17 12:24:22: Sleep state: Awake
# 2011-12-17 12:24:52: Sleep state: Awake
# 2011-12-17 12:25:22: Sleep state: Awake
# 2011-12-17 12:25:52: Sleep state: Awake
# 2011-12-17 12:26:23: Sleep state: Awake
# 2011-12-17 12:26:32: HeadbandDocked
# System Libraries
import time
import sys
# Zeo Libraries
from ZeoRawData import BaseLink, Parser
from ZeoRawData.Utility import *
# User customizable variables
port = '/dev/ttyUSB0'
class Callbacks:
def logtime(self):
return time.strftime("%Y-%m-%d %H:%M:%S: ", time.localtime())
def SliceCallback(self, slice):
if slice['SleepStage']:
print self.logtime() + "Sleep state: " + slice['SleepStage']
def EventCallback(self, logtime, version, event):
print self.logtime() + event
if __name__ = "__main__":
link = BaseLink.BaseLink(port)
parser = Parser.Parser()
callbacks = Callbacks()
# Add Callbacks and Start the Link
link.addCallback(parser.update)
parser.addSliceCallback(callbacks.SliceCallback)
parser.addEventCallback(callbacks.EventCallback)
link.start()
# Infinitely loop to allow the script to run continuously
while(True):
time.sleep(5) |