diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d35cb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.pyc diff --git a/README.md b/README.md index 2260f51..6374b6e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Serial Client Preparation ----------- -Disable the login shell (raspi-config > Interfacing Options > Serial) +1. Disable the login shell (raspi-config > Interfacing Options > Serial) +2. Install Python3 modules (pip3 install serial python-crontab) Run --- diff --git a/cron.py b/cron.py new file mode 100755 index 0000000..2881499 --- /dev/null +++ b/cron.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +from crontab import CronTab +import os +import csv +import sys + +def whereami(): + return os.path.dirname(os.path.abspath(__file__)) + +class Cron(CronTab): + def __init__(self, user=True): + CronTab.__init__(self, user=user) + + def clearCron(self): + return self.remove_all() + + def getJob(self, code): + return self.new(command="%s/send.py %s" % (whereami(), code)) + + def buildJob(self, code, minute=False, hour=False): + job = self.getJob(code) + job.setall(time(hour, minute)) + job.enable() + + def readCSV(self, path): + self.clearCron() + with open(path) as csvfile: + r = csv.DictReader(csvfile, [minute, hour, code]) + for row in r: + self.buildJob(row["code"], row["minute"], row["hour"]) + self.write() + + +if __name__ == "__main__": + Cron().readCSV(sys.argv[1]) diff --git a/send.py b/send.py new file mode 100755 index 0000000..c389af4 --- /dev/null +++ b/send.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import time +import serial +import sys + +def sendMessage(code): + with serial.Serial( + port='/dev/ttyAMA0', + baudrate = 9600, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + timeout=1 + ) as s: + s.write(code + "\r") + +if __name__ == "__main__": + sendMessage(sys.argv[1])