Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket problem
#1
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()
Reply
#2
I think if you create a thread for the packets, you'll be ok.

Here's one example of that:

and here
Reply
#3
Thanks for the hint, I'll look into that! When it works, I'll report it here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket server problem H84Gabor 0 1,208 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
  socket loop problem monamour 9 6,565 Nov-28-2019, 12:04 PM
Last Post: buran
  problem in socket Mamad 2 2,323 Nov-10-2019, 11:44 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020