Skip to content

Instantly share code, notes, and snippets.

@electronut
Created April 24, 2014 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electronut/11259140 to your computer and use it in GitHub Desktop.
Save electronut/11259140 to your computer and use it in GitHub Desktop.
Alert Arduino via serial port when an event happens on the host computer.
"""
ardu_alert.py
Alert Arduino via serial port when an event happens on the host computer.
Author: Mahesh Venkitachalam
Website: electronut.in
"""
import sys, serial
import time
import argparse
# main() function
def main():
print 'starting ardu_alert...'
# create parser
parser = argparse.ArgumentParser(description="ardu_alert...")
# add expected arguments
parser.add_argument('--port', dest='strPort', required=True)
# parse args
args = parser.parse_args()
# open serial port
ser = serial.Serial(args.strPort, 9600)
# loop
while True:
try:
strData = ""
# read
try:
# open file
f = open('status.txt')
# read line, strip whitespace, convert to upper case
val = f.readline().strip().upper()
# prepare serial data
if val == 'OK':
strData = "1"
else:
strData = "0"
# close file
f.close()
except:
print 'Could not open status.txt'
# send data to Arduino
ser.write(strData)
ser.flush()
# wait for 2 seconds
time.sleep(2)
except KeyboardInterrupt:
print 'Exiting!'
# turn off leds
ser.write("2")
# close serial
ser.flush()
ser.close()
break
# call main
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment