summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xldr-counter.py24
-rw-r--r--ldr-counter.service8
-rw-r--r--ldr-counter.timer11
3 files changed, 42 insertions, 1 deletions
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