GPIO work
This commit is contained in:
parent
d96559c2cc
commit
7183a941aa
Binary file not shown.
|
@ -13,7 +13,13 @@
|
|||
{% if sprinkler.enabled %}
|
||||
<article class="articlecontent">
|
||||
<header><h2><a href="sprinklers/{{sprinkler.id}}">{{ sprinkler.name }}</a></h2></header>
|
||||
<p>Currently the sprinkler status is: {{sprinkler.status}}</p>
|
||||
<p>
|
||||
Currently the sprinkler status is: {{sprinkler.status}}<br/>
|
||||
{% if not sprinkler.status %}| <a href="/sprinklers/{{sprinkler.id}}/on">Turn on </a>| Turn on for 10 mins |
|
||||
{% else %}| <a href="/sprinklers/{{sprinkler.id}}/on">Turn Off</a> |
|
||||
{% endif %}
|
||||
</p>
|
||||
|
||||
</article>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
<!--
|
||||
function toggle_visibility(id) {
|
||||
var e = document.getElementById(id);
|
||||
if(e.style.display == 'block')
|
||||
e.style.display = 'none';
|
||||
else
|
||||
if(e.style.display == 'none')
|
||||
e.style.display = 'block';
|
||||
else
|
||||
e.style.display = 'none';
|
||||
}
|
||||
//-->
|
||||
</script>
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
from django.core.cache import cache
|
||||
import RPi.GPIO as GPIO
|
||||
from sprinklers.model import Sprinkler
|
||||
from sprinklers.model import LogEntry
|
||||
|
||||
# NUMBER OF STATIONS
|
||||
num_stations = 16
|
||||
|
||||
# GPIO PIN DEFINES
|
||||
|
||||
pin_sr_clk = 4
|
||||
pin_sr_noe = 17
|
||||
pin_sr_dat = 21 # NOTE: if you have a RPi rev.2, need to change this to 27
|
||||
pin_sr_lat = 22
|
||||
|
||||
def get_sprinkler(sprinkler_id):
|
||||
sprinkler_id = int(sprinkler_id)
|
||||
try:
|
||||
sprinkler = Sprinkler.objects.get(id=sprinkler_id)
|
||||
except Sprinkler.DoesNotExist:
|
||||
return None
|
||||
return sprinkler
|
||||
|
||||
def enableShiftRegisterOutput():
|
||||
GPIO.output(pin_sr_noe, False)
|
||||
|
||||
def disableShiftRegisterOutput():
|
||||
GPIO.output(pin_sr_noe, True)
|
||||
|
||||
def setShiftRegister(values):
|
||||
GPIO.output(pin_sr_clk, False)
|
||||
GPIO.output(pin_sr_lat, False)
|
||||
for s in range(0,num_stations):
|
||||
GPIO.output(pin_sr_clk, False)
|
||||
GPIO.output(pin_sr_dat, values[num_stations-1-s])
|
||||
GPIO.output(pin_sr_clk, True)
|
||||
GPIO.output(pin_sr_lat, True)
|
||||
|
||||
def setup():
|
||||
values = [0]*num_stations
|
||||
|
||||
GPIO.cleanup()
|
||||
# setup GPIO pins to interface with shift register
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(pin_sr_clk, GPIO.OUT)
|
||||
GPIO.setup(pin_sr_noe, GPIO.OUT)
|
||||
disableShiftRegisterOutput()
|
||||
GPIO.setup(pin_sr_dat, GPIO.OUT)
|
||||
GPIO.setup(pin_sr_lat, GPIO.OUT)
|
||||
|
||||
setShiftRegister(values)
|
||||
enableShiftRegisterOutput()
|
||||
|
||||
def turn_on(sprinkler_id):
|
||||
values = [0]*num_stations
|
||||
sprinkler = get_sprinkler(sprinkler_id)
|
||||
|
||||
#Check if sprinkler is already on
|
||||
if sprinkler.status == True: return "Sprinkler already on"
|
||||
sprinkler.status = True
|
||||
|
||||
#loop through all sprinklers and set them appropriately
|
||||
for tmpSprinkler in Sprinkler.objects.all():
|
||||
#Check for masters
|
||||
if tmpSprinkler.IsMaster:
|
||||
values[tmpSprinkler.id] = 1
|
||||
else:
|
||||
values[tmpSprinkler.id] tmpSprinkler.status
|
||||
#Perform update on the HW
|
||||
setShiftRegister(values):
|
||||
|
||||
#Insert the log entry
|
||||
new_log = LogEntry(sprinkler_id=sprinkler.id)
|
||||
new_log.save()
|
||||
sprinkler.currentLog = new_log
|
||||
sprinkler.save()
|
Binary file not shown.
|
@ -5,19 +5,13 @@ from django.template import Context, loader
|
|||
from sprinklers.models import Sprinkler
|
||||
from sprinklers.models import LogEntry
|
||||
|
||||
def get_sprinkler(sprinkler_id):
|
||||
sprinkler_id = int(sprinkler_id)
|
||||
try:
|
||||
sprinkler = Sprinkler.objects.get(id=sprinkler_id)
|
||||
except Sprinkler.DoesNotExist:
|
||||
return None
|
||||
return sprinkler
|
||||
#from sprinklers import SprinklerUtils
|
||||
|
||||
def index(request):
|
||||
return HttpResponse("Hello, world. You're at the sprinkler index.")
|
||||
|
||||
def detail(request, sprinkler_id):
|
||||
tmp_sprinkler = get_sprinkler(sprinkler_id)
|
||||
tmp_sprinkler = SprinklerUtils.get_sprinkler(sprinkler_id)
|
||||
|
||||
log_entries = []
|
||||
#Retrieve all the log entries for this sprinkler
|
||||
|
@ -34,16 +28,7 @@ def detail(request, sprinkler_id):
|
|||
return HttpResponse(template.render(context))
|
||||
|
||||
def on(request, sprinkler_id):
|
||||
sprinkler = get_sprinkler(sprinkler_id)
|
||||
#Check if sprinkler is already on
|
||||
if sprinkler.status == True: return HttpResponse("Sprinkler already on")
|
||||
sprinkler.status = True
|
||||
|
||||
#Insert the log entry
|
||||
new_log = LogEntry(sprinkler_id=sprinkler.id)
|
||||
new_log.save()
|
||||
sprinkler.currentLog = new_log
|
||||
sprinkler.save()
|
||||
SprinklerUtils.turn_on(sprinkler_id)
|
||||
|
||||
return HttpResponse("Turning sprinkler on")
|
||||
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue