samedi 2 juillet 2016

Socket receiving in while 1

I'm trying to create socket connections in python. I need to listen to server until it sends a message, thus I need to use while True. Client:

import RPi.GPIO as GPIO
import time
import socket

GPIO.setmode(GPIO.BOARD)

pinLDR = 7
pinLED = 11
touch = False
sock = socket.socket()
sock.connect(('192.168.1.67', 9092))

while True:
    print sock.recv(256)

def rc_time ():
    count = 0

    GPIO.setup(pinLDR, GPIO.OUT)
    GPIO.output(pinLDR, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(pinLDR, GPIO.IN)

    while (GPIO.input(pinLDR) == GPIO.LOW):
    count += 1

    return count

def led(lh):
    GPIO.setup(pinLED, GPIO.OUT)
    if lh == 1:
        GPIO.output(pinLED, GPIO.HIGH)
    else:
        GPIO.output(pinLED, GPIO.LOW)

try:
    while True:
        print(str(rc_time()))
        if rc_time() > 5000:
            if touch == False:
                print "triggered"
                sock.send("triggered")
                touch = True
        else:
            if touch == True:
                sock.send("nottriggered")
                print "nottriggered"
                touch = False
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()
sock.close()

But I have a problem with it. Nothing is printed even if a server sends a message. And the whole code after first while True doesn't work

Aucun commentaire:

Enregistrer un commentaire