Created
April 24, 2014 15:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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