Hi, I want to use django on a raspberry pi to show a website, which keeps track of numbers, that are entered on a keypad, which is attached to the raspberry pi via GPIO. I have been able to set up django to serve a website and I have a python script that reads the GPIO from the raspberry and prints out the number that was typed on the console.
But I don’t know where to put the python code inside of django. I tried putting the keypad code in a view, but that failed, probably because there is a while true loop to read the GPIO input all the time. I found this: python - Raspberry Pi and Django - Background check GPIO Button - Stack Overflow but they seem more advanced and I don’t understand the answers.
Is “Celery” what I need? Or can I use “GPIO.add_event_detect” and where would I put that in my django files? Maybe someone can point me in the right direction. Sorry if this is stupid, I am new to this…
This is the keypad code I want to integrate:
import RPi.GPIO as GPIO
import time
L1 = 5
L2 = 6
L3 = 13
L4 = 19
C1 = 26
C2 = 16
C3 = 25
C4 = 21
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)
GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def readLine(line, characters):
GPIO.output(line, GPIO.HIGH)
if(GPIO.input(C1) == 1):
print(characters[0])
if(GPIO.input(C2) == 1):
print(characters[1])
if(GPIO.input(C3) == 1):
print(characters[2])
if(GPIO.input(C4) == 1):
print(characters[3])
GPIO.output(line, GPIO.LOW)
try:
while True:
readLine(L1, ["1","2","3","A"])
readLine(L2, ["4","5","6","B"])
readLine(L3, ["7","8","9","C"])
readLine(L4, ["*","0","#","D"])
time.sleep(0.3)
except KeyboardInterrupt:
print("\nApplication stopped!")