Skip to content

Update BrewPi to search for Arduino tty path #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions BrewPiProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 20 additions & 9 deletions BrewPiUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@
import sys
import os
import serial
import glob

try:
import configobj
except ImportError:
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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion brewpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']) + "'")
Expand Down
18 changes: 6 additions & 12 deletions programArduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ****")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions settings/defaults.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down