Skip to content

Instantly share code, notes, and snippets.

@jake-b
Last active April 7, 2016 17:47
Show Gist options
  • Save jake-b/eba2ac042ff9a0ddbf747d7b93ba7bbd to your computer and use it in GitHub Desktop.
Save jake-b/eba2ac042ff9a0ddbf747d7b93ba7bbd to your computer and use it in GitHub Desktop.
A crude temperature logger for printers running Makerbot s3g based firmware
# Tool 0 temperature logger
# DO NOT RUN THIS PROGRAM WHILE PRINTING-- THIS IS DEISGNED FOR 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.usbmodem1411',
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" # Version rquest packet
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 takeToolReading(starttime):
ser.flushInput();
request = "\xD5\x03\x0A\x00\x02\xD6" # Get toolhead 0 temperature
ser.write(request)
response = ser.read(2+3+1)
readtime = time();
request = "\xD5\x03\x0A\x00\x20\x49" # Get toolhead 0 target temperature
ser.write(request)
response2 = ser.read(2+3+1)
# A place for results
temp = None
setpoint = None
# Parse the temperature response packet
if (len(response) == 6):
array = map(ord, response);
if (array[0] == 0xD5 and array[1] == 0x03 and array[2] == 0x81):
temp = array[3] + (array[4]<<8);
# Parse the setpoint response packet
if (len(response2) == 6):
array = map(ord, response2)
if (array[0] == 0xD5 and array[1] == 0x03 and array[2] == 0x81):
setpoint = array[3] + (array[4]<<8);
# Print the result
timestamp = readtime-starttime
print "%f %s %s" % (timestamp, temp, setpoint)
sys.stdout.flush() # I like to > this out to file and then 'tail -f' it.
# Setup the repeating timer to take BHP readings
# The parameter is the start time (used as offset for time stamps)
r = RepeatingTimer(1.0, takeToolReading, time())
r.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment