socket problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: socket problem (/thread-1184.html) |
socket problem - technoplusnl - Dec-11-2016 Hi, I'm fairly new to Python programming, and I wrote some code for a RaspberryPi that controls my astronomical observatory building. It controls the opening and closing of the roof by receiving packets sent by a website hosted on the same RaspberryPi. You can click on links on that site to open or close the roof. This works fine so far, but now I also need to be able to open the roof by reading two inputs. That also works as long as I comment out the code used for the packet reception, but not both at the same time. I removed the code from the (working) functions in the code below, the problem must be in the main code I guess. Thanks in advance for any input. Albert #!/usr/bin/python # Last modification Dec 11 2016, Albert van Duin import RPi.GPIO as GPIO import time import socket import subprocess import os loop = 1 # enable software loop telescope = 0 # telescope is off alarm = 0 # no alarm data = '' text = '' GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) # ROOF GPIO.setup(16, GPIO.OUT) # TELESCOPE GPIO.setup(18, GPIO.OUT) # ALARM GPIO.setup(22, GPIO.IN) # ROOF DIRECTION 1 = opening 0 = closing GPIO.setup(13, GPIO.IN) # ROOF POWER, direction determined by GPIO22 GPIO.setup(24, GPIO.IN) # OPEN_POSITION GPIO.setup(26, GPIO.IN) # CLOSE_POSITION GPIO.setup(15, GPIO.IN) # PARK_POSITION TCP_IP = '192.168.0.198' TCP_PORT = 5005 BUFFER_SIZE = 20 # Normally 1024, but we want a faster response s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.listen(1) def open_roof(): # function definition # code removed return def close_roof(): # function definition # code removed return def emergency_close(): #function definition # code removed return def telescope_on(): #function definition # code removed return def telescope_off(): #function definition # code removed return def alarm_on(): # function definition # code removed return def alarm_off(): # function definition # code removed return def stop(): #function definition # code removed return #--------------------------------------------------------------------------------------------- while loop == 1: # Main Program Loop runs until stopped by the STOP packet conn, addr = s.accept() print 'Connection address:', addr while 1: if (GPIO.input(22) == 1 and GPIO.input(13) == 1): #check inputs from LesveDome open_roof() if (GPIO.input(22) == 0 and GPIO.input(13) == 1): close_roof() data = conn.recv(BUFFER_SIZE) # look if a packet has arrived if not data: break # if no packet received print "received data:", data # if packet received: print packet if data == 'ROOF_OPEN': open_roof() if data == 'ROOF_CLOSE': close_roof() if data == 'EMERGENCY': emergency_close() if data == 'TELESCOPE_ON': telescope_on() if data == 'TELESCOPE_OFF': telescope_off() if data == 'ALARM_ON': alarm_on() if data == 'ALARM_OFF': alarm_off() if data == 'STOP': stop() conn.send(text) # echo message text to sender print "Script was halted at: ", time.strftime('%X %x %Z') # if loop is no longer 1, close down script conn.close() GPIO.cleanup() RE: socket problem - Larz60+ - Dec-11-2016 I think if you create a thread for the packets, you'll be ok. Here's one example of that: and here RE: socket problem - technoplusnl - Dec-11-2016 Thanks for the hint, I'll look into that! When it works, I'll report it here. |