From 13022f6db861cb3b9a4c1452300fe66bb0b8402f Mon Sep 17 00:00:00 2001 From: Geo Van O Date: Fri, 24 Oct 2014 22:45:35 -0400 Subject: [PATCH] Update BrewPi to search for Arduino tty path Searches /dev for arduino Removes 'port' from defaults.cfg, so that not setting it in the main config doesn't override the search Still allows 'port' to be set by the user in the main .cfg file, to override the search path Removes altport from config & default config as it is no longer needed --- BrewPiProcess.py | 6 ++++-- BrewPiUtil.py | 29 ++++++++++++++++++++--------- brewpi.py | 5 ++++- programArduino.py | 18 ++++++------------ settings/defaults.cfg | 2 -- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/BrewPiProcess.py b/BrewPiProcess.py index 2c1d3a6..13229b1 100644 --- a/BrewPiProcess.py +++ b/BrewPiProcess.py @@ -125,8 +125,10 @@ def parseProcess(self, process): if cfg: cfg = cfg[0] # add full path to config file bp.cfg = util.readCfgWithDefaults(cfg) - - bp.port = bp.cfg['port'] + try: + bp.port = bp.cfg['port'] + except KeyError: + bp.port = util.findArduino() bp.sock = BrewPiSocket.BrewPiSocket(bp.cfg) return bp diff --git a/BrewPiUtil.py b/BrewPiUtil.py index c1d760f..e89d507 100644 --- a/BrewPiUtil.py +++ b/BrewPiUtil.py @@ -18,6 +18,7 @@ import sys import os import serial +import glob try: import configobj @@ -25,7 +26,19 @@ print "BrewPi requires ConfigObj to run, please install it with 'sudo apt-get install python-configobj" sys.exit(1) - +def findArduino(): + """ + Discovers arduino tty path + TO DO: This will need to be updated when we deal with multiple arduinos + Params: None + Returns: string + """ + arduino = "/dev/ttyACM0" + for file in glob.glob("/dev/serial/by-id/*Arduino*"): + arduino = os.path.realpath(file) + return arduino + + def addSlash(path): """ Adds a slash to the path, but only when it does not already have a slash at the end @@ -100,19 +113,17 @@ def removeDontRunFile(path='/var/www/do_not_run_brewpi'): def setupSerial(config): ser = None conn = None - port = config['port'] + try: + port = config['port'] + except KeyError: + port = findArduino() dumpSerial = config.get('dumpSerial', False) # open serial port try: ser = serial.Serial(port, 57600, timeout=0.1) # use non blocking serial. except serial.SerialException as e: - logMessage("Error opening serial port: %s. Trying alternative serial port %s." % (str(e), config['altport'])) - try: - port = config['altport'] - ser = serial.Serial(port, 57600, timeout=0.1) # use non blocking serial. - except serial.SerialException as e: - logMessage("Error opening alternative serial port: %s. Script will exit." % str(e)) - exit(1) + logMessage("Error opening serial port: %s. Please add port=/path/to/arduino in config.cfg . Script will exit." % str(e)) + exit(1) # yes this is monkey patching, but I don't see how to replace the methods on a dynamically instantiated type any other way if dumpSerial: diff --git a/brewpi.py b/brewpi.py index f440739..00ca9d8 100644 --- a/brewpi.py +++ b/brewpi.py @@ -321,7 +321,10 @@ def resumeLogging(): else: return {'status': 1, 'statusMessage': "Logging was not paused."} -port = config['port'] +try: + port = config['port'] +except KeyError: + port = util.findArduino() ser, conn = util.setupSerial(config) logMessage("Notification: Script started for beer '" + urllib.unquote(config['beerName']) + "'") diff --git a/programArduino.py b/programArduino.py index 8bda85e..02d668d 100644 --- a/programArduino.py +++ b/programArduino.py @@ -45,20 +45,13 @@ def loadBoardsFile(arduinohome): return open(arduinohome + 'hardware/arduino/boards.txt', 'rb').readlines() -def openSerial(port, altport, baud, timeoutVal): +def openSerial(port, baud, timeoutVal): # open serial port try: ser = serial.Serial(port, baud, timeout=timeoutVal) return [ser, port] except serial.SerialException as e: - printStdErr("Error opening serial port: %s. Trying alternative serial port %s." % (str(e), altport)) - try: - ser = serial.Serial(altport, baud, timeout=timeoutVal) - return [ser, altport] - except serial.SerialException as e: - printStdErr("Error opening alternative serial port: %s. Script will exit." % str(e)) - return [None, None] - + printStdErr("Error opening serial port: %s. Please set port in config.cfg and try again." % (str(e))) def programArduino(config, boardType, hexFile, restoreWhat): printStdErr("**** Arduino Program script started ****") @@ -87,7 +80,8 @@ def programArduino(config, boardType, hexFile, restoreWhat): printStdErr("Devices will " + ("" if restoreDevices else "not ") + "be restored" + (" if possible" if restoreSettings else "")) - ser, port = openSerial(config['port'], config['altport'], 57600, 0.2) + port = findArduino() + ser, port = openSerial(port, 57600, 0.2) if ser is None: printStdErr("Could not open serial port. Programming aborted.") return 0 @@ -195,7 +189,7 @@ def programArduino(config, boardType, hexFile, restoreWhat): ser.close() del ser # Arduino won't reset when serial port is not completely removed if boardType == 'leonardo': - ser, port = openSerial(config['port'], config['altport'], 1200, 0.2) + ser, port = openSerial(port, 1200, 0.2) if ser is None: printStdErr("Could not open serial port at 1200 baud to reset Arduino Leonardo") return 0 @@ -218,7 +212,7 @@ def programArduino(config, boardType, hexFile, restoreWhat): countDown -= 1 printStdErr("Back up in " + str(countDown) + "...") - ser, port = openSerial(config['port'], config['altport'], 57600, 0.2) + ser, port = openSerial(port, 57600, 0.2) if ser is None: printStdErr("Error opening serial port after programming. Program script will exit. Settings are not restored.") return 0 diff --git a/settings/defaults.cfg b/settings/defaults.cfg index 350bcaa..8a779d4 100644 --- a/settings/defaults.cfg +++ b/settings/defaults.cfg @@ -3,8 +3,6 @@ scriptPath = /home/brewpi/ wwwPath = /var/www/ -port = /dev/ttyACM0 -altport = /dev/ttyACM1 boardType = leonardo beerName = My First BrewPi Run interval = 120.0