0

I'm running this script on a raspberry for a game I created, where I have three different inputs in my while true:.

One of them is getting a socket message from a different raspberry. But the problem is that the rest of my while true doesnt execute no more. Only the first IF statement after I received the socket message.

How to get them all running?

Thanks in advance

#!/usr/bin/python
import RPi.GPIO as GPIO 
import time
import socket
import pygame
import serial
GPIO.setmode(GPIO.BCM)
UDP_IP = "192.168.0.21"
UDP_PORT = 20
sock = socket.socket(socket.AF_INET, # Internet
 socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, 20))
# Klopspelout, pianospel, sleutelspel, totaalspel
pinList = [27, 22, 4, 17]
klopinput = 21
sleutelinput = 11
# loop through pins
for i in pinList: 
 GPIO.setup(i, GPIO.OUT) 
GPIO.setup(klopinput, GPIO.IN)
GPIO.setup(sleutelinput, GPIO.IN, GPIO.PUD_UP)
# time to sleep between operations in the main loop
SleepTimeL = 2
#variables
totaal = 0
klop = 0 
sleutel = 0 
piano = 0
wacht = 0
GPIO.output(pinList[0], GPIO.LOW)
GPIO.output(pinList[1], GPIO.LOW)
GPIO.output(pinList[2], GPIO.LOW)
GPIO.output(pinList[3], GPIO.LOW)
data = 0
# main loop
#GPIO.cleanup()
while True:
 #if GPIO.input(klopinput) == True:
 # GPIO.output(pinList[0], GPIO.HIGH)
 # totaal += 1
 # print ('klopspel is goed')
 # time.sleep(SleepTimeL)
 # GPIO.output(pinList[0], GPIO.LOW)
 data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 print "received message:", data
 if data == "slotpiano2":
 print "slot open"
 totaal += 1
 time.sleep(1)
 if GPIO.input(klopinput) == True and wacht == 0 and klop == 0:
 GPIO.output(pinList[1], GPIO.HIGH)
 totaal += 1
 klop = 1
 print ('klop is goed')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[1], GPIO.LOW)
 time.sleep(SleepTimeL)
 if GPIO.input(sleutelinput) == False and sleutel == 0:
 GPIO.output(pinList[0], GPIO.HIGH)
 totaal += 1
 sleutel = 1
 wacht = 1
 print ('Sleutel is goed')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[0], GPIO.LOW) 
 time.sleep(SleepTimeL)
 wacht = 0 
 if totaal == 3:
 GPIO.output(pinList[3], GPIO.HIGH) 
 totaal = 0
 sleutel = 0
 klop = 0
 print ('reset')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[3], GPIO.LOW)
asked Dec 13, 2017 at 16:56
2
  • On each loop, sock.recvfrom() will block until data arrives on the socket - is this what you intended? Or did you want to ignore the socket if there is no data and move on to checking the GPIO inputs? Commented Dec 14, 2017 at 10:59
  • Or did you want to ignore the socket if there is no data and move on to checking the GPIO inputs? Yes, that last is correct. Commented Dec 15, 2017 at 8:15

1 Answer 1

2

OK as per your comment, if you want avoid blocking on the socket then you need to set it to nonblocking and use the select() function. I've posted a copy of your code below that shows the necessary edit, based on this answer, and there is lots of information about select() in the Python documentation.

#!/usr/bin/python
import RPi.GPIO as GPIO 
import time
import socket
import pygame
import serial
import select # select() function <---------
GPIO.setmode(GPIO.BCM)
UDP_IP = "192.168.0.21"
UDP_PORT = 20
sock = socket.socket(socket.AF_INET, # Internet
 socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, 20))
sock.setblocking(0) # set to non-blocking <-----------
# Klopspelout, pianospel, sleutelspel, totaalspel
pinList = [27, 22, 4, 17]
klopinput = 21
sleutelinput = 11
# loop through pins
for i in pinList: 
 GPIO.setup(i, GPIO.OUT) 
GPIO.setup(klopinput, GPIO.IN)
GPIO.setup(sleutelinput, GPIO.IN, GPIO.PUD_UP)
# time to sleep between operations in the main loop
SleepTimeL = 2
#variables
totaal = 0
klop = 0 
sleutel = 0 
piano = 0
wacht = 0
GPIO.output(pinList[0], GPIO.LOW)
GPIO.output(pinList[1], GPIO.LOW)
GPIO.output(pinList[2], GPIO.LOW)
GPIO.output(pinList[3], GPIO.LOW)
data = 0
# main loop
#GPIO.cleanup()
while True:
 #if GPIO.input(klopinput) == True:
 # GPIO.output(pinList[0], GPIO.HIGH)
 # totaal += 1
 # print ('klopspel is goed')
 # time.sleep(SleepTimeL)
 # GPIO.output(pinList[0], GPIO.LOW)
 fds = select.select([sock], [], [], 1.0) <-----------
 if (fds[0]): # sock has some data
 data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 print "received message:", data
 if data == "slotpiano2":
 print "slot open"
 totaal += 1
 time.sleep(1)
 if GPIO.input(klopinput) == True and wacht == 0 and klop == 0:
 GPIO.output(pinList[1], GPIO.HIGH)
 totaal += 1
 klop = 1
 print ('klop is goed')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[1], GPIO.LOW)
 time.sleep(SleepTimeL)
 if GPIO.input(sleutelinput) == False and sleutel == 0:
 GPIO.output(pinList[0], GPIO.HIGH)
 totaal += 1
 sleutel = 1
 wacht = 1
 print ('Sleutel is goed')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[0], GPIO.LOW) 
 time.sleep(SleepTimeL)
 wacht = 0 
 if totaal == 3:
 GPIO.output(pinList[3], GPIO.HIGH) 
 totaal = 0
 sleutel = 0
 klop = 0
 print ('reset')
 time.sleep(SleepTimeL)
 GPIO.output(pinList[3], GPIO.LOW)

(NB set the timeout to something relevant to your application if 1.0 seconds is not acceptable.)

answered Dec 15, 2017 at 12:15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.