|
|
@ -1,13 +1,25 @@ |
|
|
|
#!/usr/bin/python3 |
|
|
|
#!/usr/bin/python3 |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
CLI Meter |
|
|
|
C.L.I. Meter |
|
|
|
==================================================================== |
|
|
|
====================================================================== |
|
|
|
Takes a reading of the light using the meter attached via to the |
|
|
|
I wrote this script with the intention of using it as one part within |
|
|
|
intended Raspberry PI. The reading is the sent to a server where the |
|
|
|
a single cron-job. The cron-job begins with "startup.sh" and that, |
|
|
|
reading is stored. The intended use for this script is to be run via |
|
|
|
in-turn, will call this script when it is ready to do so. |
|
|
|
the command-line without the need for a G.U.I. This should be the |
|
|
|
|
|
|
|
version used when the "Return to Ritherdon" project is live. |
|
|
|
This scrip takes a reading of the current light-level in the room, |
|
|
|
|
|
|
|
using the Raspberry Pi (and the attached light-meter attached) it is |
|
|
|
|
|
|
|
running on. From there, it sends a J.S.O.N. object to the server |
|
|
|
|
|
|
|
(specified at "api_url"). The process is then repeated until it is |
|
|
|
|
|
|
|
killed manually or via "shutdown.sh" (as a cron-job most likely). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
You can use this script on Raspberry Pis without a G.U.I. because it |
|
|
|
|
|
|
|
is all C.L.I. based. If you prefer something more graphical, you can |
|
|
|
|
|
|
|
use "light_meter.py" -- which is part of this repository at time of |
|
|
|
|
|
|
|
writing. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
To end this script manually, use the usual "Ctrl-c" keyboard |
|
|
|
|
|
|
|
interrupt -- or something like Htop... doesn't matter. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
import json |
|
|
|
import json |
|
|
@ -15,7 +27,7 @@ import RPi.GPIO as GPIO |
|
|
|
import time, math |
|
|
|
import time, math |
|
|
|
import requests |
|
|
|
import requests |
|
|
|
from datetime import datetime |
|
|
|
from datetime import datetime |
|
|
|
# import pdb |
|
|
|
# import pdb # For testing. |
|
|
|
|
|
|
|
|
|
|
|
# Using BCM (Broadcom) names when referencing the GPIO pins. |
|
|
|
# Using BCM (Broadcom) names when referencing the GPIO pins. |
|
|
|
GPIO.setmode(GPIO.BCM) |
|
|
|
GPIO.setmode(GPIO.BCM) |
|
|
@ -24,13 +36,14 @@ GPIO.setwarnings(True) |
|
|
|
a_pin = 18 # Charges the capacitor. |
|
|
|
a_pin = 18 # Charges the capacitor. |
|
|
|
b_pin = 23 # Discharges the capacitor. |
|
|
|
b_pin = 23 # Discharges the capacitor. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Make sure this is valid. |
|
|
|
api_url = "http://3.9.19.84/api/readings/add/1" |
|
|
|
api_url = "http://3.9.19.84/api/readings/add/1" |
|
|
|
|
|
|
|
|
|
|
|
def discharge(): |
|
|
|
def discharge(): |
|
|
|
GPIO.setup(a_pin, GPIO.IN) |
|
|
|
GPIO.setup(a_pin, GPIO.IN) |
|
|
|
GPIO.setup(b_pin, GPIO.OUT) |
|
|
|
GPIO.setup(b_pin, GPIO.OUT) |
|
|
|
GPIO.output(b_pin, False) |
|
|
|
GPIO.output(b_pin, False) |
|
|
|
time.sleep(0.01) # 0.01 -- initial value |
|
|
|
time.sleep(0.01) # 0.01 -- initial/default value. |
|
|
|
|
|
|
|
|
|
|
|
def charge_time(): |
|
|
|
def charge_time(): |
|
|
|
GPIO.setup(b_pin, GPIO.IN) |
|
|
|
GPIO.setup(b_pin, GPIO.IN) |
|
|
@ -77,7 +90,7 @@ def update_reading(): |
|
|
|
def main(): |
|
|
|
def main(): |
|
|
|
try: |
|
|
|
try: |
|
|
|
while True: |
|
|
|
while True: |
|
|
|
#pdb.set_trace() |
|
|
|
#pdb.set_trace() # For testing. |
|
|
|
update_reading() |
|
|
|
update_reading() |
|
|
|
except KeyboardInterrupt: |
|
|
|
except KeyboardInterrupt: |
|
|
|
print("Keyboard Interrupt: quitting program.") |
|
|
|
print("Keyboard Interrupt: quitting program.") |
|
|
@ -88,5 +101,5 @@ def main(): |
|
|
|
GPIO.cleanup() |
|
|
|
GPIO.cleanup() |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
if __name__ == "__main__": |
|
|
|
# time.sleep(60) |
|
|
|
# time.sleep(60) # For testing/debugging. |
|
|
|
main() |
|
|
|
main() |
|
|
|