From ba47a4cabdddfe9ef841188a74866c87a5603e4c Mon Sep 17 00:00:00 2001 From: st33v Date: Sun, 17 Jan 2016 18:37:05 +1100 Subject: basic timestamp writes to file --- ldr-counter.py | 24 +++++++++++++++++++++++- ldr-counter.service | 8 ++++++++ ldr-counter.timer | 11 +++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 ldr-counter.service create mode 100644 ldr-counter.timer diff --git a/ldr-counter.py b/ldr-counter.py index 7cc84a4..85c980c 100755 --- a/ldr-counter.py +++ b/ldr-counter.py @@ -23,6 +23,8 @@ # max current = 3.3/580 = 5.7 mA (well within spec) import RPi.GPIO as GPIO, time +from datetime import datetime, timezone + # set GPIO17 as read pin sensorPin = 17 @@ -33,7 +35,9 @@ obsCount = 6 # number of observations # Tell the GPIO library to use Broadcom GPIO references GPIO.setmode(GPIO.BCM) -# Define function to measure charge time +######################## Function definitions ################################ + +############# Measure charge time ############################################ def RCtime (): # Discharge capacitor GPIO.setup(sensorPin, GPIO.OUT) @@ -49,10 +53,28 @@ def RCtime (): if timer > maxWait : return -1 return timer +############ Append measurement to file ###################################### +def appendReading (ldrCount): + # open the file + + # create a timestamp in ISO 8601 format + timestamp = datetime.now(timezone.utc).astimezone().isoformat() + + # write the data + f = open("ldr-readings.txt", "a") + f.write(timestamp) # , ldrCount) + + f.close() + # Main program loop obs=list() # an empty list for num in range(0, obsCount): obs.append(RCtime()) print(obs) + print("sum=", sum(obs)) print("Ave time: ", sum(obs)/obsCount) # Measure timing using GPIO4 + +appendReading(obs) + + diff --git a/ldr-counter.service b/ldr-counter.service new file mode 100644 index 0000000..64c113c --- /dev/null +++ b/ldr-counter.service @@ -0,0 +1,8 @@ +[Unit] +Description=Read light level from LDR and store result in file + +[Service] +Type=oneshot +User=st33v +# WorkingDirectory=/home/st33v/light/ +ExecStart=/usr/local/bin/ldr-counter.py diff --git a/ldr-counter.timer b/ldr-counter.timer new file mode 100644 index 0000000..b18f9b9 --- /dev/null +++ b/ldr-counter.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Read light level and store result + +[Timer] +Persistent=true +OnBootSec=3min +OnUnitActiveSec=5m +Unit=ldr-counter.service + +[Install] +WantedBy=timers.target -- cgit v1.3