Skip to content

Instantly share code, notes, and snippets.

@jake-b
Created April 7, 2016 17:51
Show Gist options
  • Save jake-b/2d6745a928da627090cef0f07681dcca to your computer and use it in GitHub Desktop.
Save jake-b/2d6745a928da627090cef0f07681dcca to your computer and use it in GitHub Desktop.
A crude platform temperature logger for printers running Makerbot s3g based firmware
# Platform temp logger
# DO NOT RUN THIS PROGRAM WHILE PRINTING-- THIS IS DEISGNED FOR HBP TEMP TESTING
# USING THE PREHEAT FUNCTION AND NOT DESIGNED TO BE RUN WHILE PRINTING!
#################################################################################
# Copyright (c) 2013, jake (at) allaboutjake (dot) com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * The name of the author and/or copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, AUTHOR, OR ANY CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from threading import Timer
from time import sleep
from time import time
import serial
import sys
# From: https:#gist.github.com/alexbw/1187132
# Simple repeating timer implementation
class RepeatingTimer(object):
"""
USAGE:
from time import sleep
def myFunction(inputArgument):
print(inputArgument)
r = RepeatingTimer(0.5, myFunction, "hello")
r.start(); sleep(2); r.interval = 0.05; sleep(2); r.stop()
"""
def __init__(self,interval, function, *args, **kwargs):
super(RepeatingTimer, self).__init__()
self.args = args
self.kwargs = kwargs
self.function = function
self.interval = interval
def start(self):
self.callback()
def stop(self):
self.interval = False
def callback(self):
if self.interval:
self.function(*self.args, **self.kwargs)
Timer(self.interval, self.callback, ).start()
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/tty.usbmodemfd121',
baudrate=115200, timeout=None)
# Send a "version" packet and then flush the input as a way to synchonize the stream
ser.flushInput()
version_packet = "\xD5\x03\x00\x00\x64\x04"
ser.write(version_packet)
ser.read(1) # since timeout=None, this will wait for a response
ser.flushInput() # flush the rest.
# This function will be called periodically to take a temp reading and output it to file
def takeHBPReading(starttime):
ser.flushInput();
request = "\xD5\x03\x0A\x00\x1E\xE8"
ser.write(request)
response = ser.read(2+3+1)
readtime = time();
if (len(response) == 6):
array = map(ord, response);
#print array
if (array[0] == 0xD5 and array[1] == 0x03 and array[2] == 0x81):
temp = array[3] + (array[4]<<8);
timestamp = readtime-starttime
print "%f %d" % (timestamp, temp)
sys.stdout.flush() # I like to > this out to file and then 'tail -f' it.
else:
print("Unexpected Response 1", array)
ser.flushInput()
else:
print "Unexpected Response 2"
ser.flushInput()
# Setup the repeating timer to take BHP readings
# The parameter is the start time (used as offset for time stamps)
r = RepeatingTimer(10.0, takeHBPReading, time())
r.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment