Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[python], script stopt nadat het sluiten van de app.

Pagina: 1
Acties:

  • Simply_jeroen
  • Registratie: April 2004
  • Laatst online: 23-11 15:34
hey

ik heb een python script,
deze zorgt ervoor dat ik de uitgangen van een raspberry pi kan schakelen via een app. op me iphone
het script werkt perfect, alleen waneer ik de app. op me iphone sluit stopt het script ook, en is het niet mogelijk om nog iets te schakelen

kan iemand mijn vertellen wat er fout staat in het script,

ik wil ook graag dat het script start waneer ik de raspberry pi opstart nu moet ik elke keer het commando sudo python testsocket.py ingeven

mvg

Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 #! /usr/bin/python 

# Echo server program
import socket
import RPi.GPIO as GPIO

#####################################################################
#### SETUP IOs ######################################################
#####################################################################
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
# For LED1 we use pin 4 according BCM pin count 
# (see https://projects.drogon.net/raspberry-pi/wiringpi/pins/)
LED1 = 4
LED2 = 17
LED3 = 2
LED4 = 22
LED5 = 10
LED6 = 9
LED7 = 11
LED8 = 18


# For Switch input we use pin 17 according BCM pin count
SWITCH1 = 25
# set up GPIO output channel
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
GPIO.setup(LED4, GPIO.OUT)
GPIO.setup(LED5, GPIO.OUT)
GPIO.setup(LED6, GPIO.OUT)
GPIO.setup(LED7, GPIO.OUT)
GPIO.setup(LED8, GPIO.OUT)

# set up GPIO input with pull-up control
#   (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
GPIO.setup(SWITCH1, GPIO.IN, pull_up_down=GPIO.PUD_UP)


#####################################################################
#### SETUP SOCKET INTERFASE #########################################
#####################################################################
HOST = ''                 # Symbolic name meaning the local host
PORT = 54321              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
#####################################################################
#### Continous loop, that waits for new data and acts depending                          ####
#### on data content on it                                                                                        ####
#####################################################################
while 1:
    data = conn.recv(1024)
    print 'New Receive', data
    if not data: break
    print 'Received Data:', data
    if data == 'LED1 on\n':
        conn.send('on')
        GPIO.output(LED1, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED2 on\n':
        conn.send('on')
        GPIO.output(LED2, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED3 on\n':
        conn.send('on')
        GPIO.output(LED3, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED4 on\n':
        conn.send('on')
        GPIO.output(LED4, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED5 on\n':
        conn.send('on')
        GPIO.output(LED5, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED6 on\n':
        conn.send('on')
        GPIO.output(LED6, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED7 on\n':
        conn.send('on')
        GPIO.output(LED7, GPIO.HIGH)
        print 'On empfangen'
    if data == 'LED8 on\n':
        conn.send('on')
        GPIO.output(LED8, GPIO.HIGH)
        print 'On empfangen'
    elif data == 'LED1 off\n':
        conn.send('off')
        GPIO.output(LED1, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED2 off\n':
        conn.send('off')
        GPIO.output(LED2, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED3 off\n':
        conn.send('off')
        GPIO.output(LED3, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED4 off\n':
        conn.send('off')
        GPIO.output(LED4, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED5 off\n':
        conn.send('off')
        GPIO.output(LED5, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED6 off\n':
        conn.send('off')
        GPIO.output(LED6, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED7 off\n':
        conn.send('off')
        GPIO.output(LED7, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'LED8 off\n':
        conn.send('off')
        GPIO.output(LED8, GPIO.LOW)
        print 'Off empfangen'
    elif data == 'get status\n':
        print 'Input Status:', GPIO.input(SWITCH1)
        if GPIO.input(SWITCH1):
           conn.send('off')   # inverted logic here: pullup force to true with open switch
           print 'Read GIOP 0 result On'
        else:
           conn.send('on')    # inverted logic here: Switch switchs to ground -> false
           print 'Read GIOP 0 result Off'
        # ende if
    else:
        conn.send('unknown command')
        print 'Unknown command:', data
    # continue with while 1
conn.close()

  • windwarrior
  • Registratie: Januari 2009
  • Laatst online: 12-07-2024

windwarrior

ʍıupʍɐɹɹıoɹ

Ten eerste, zet je code eens in [code] tags, dat maakt het een stuk leesbaarder, plus dat in python indentation van belang is, dus dit ook nog ambigu kan zijn.

Anyhow, je accepteerd een connectie, waarna je een while 1 ingaat. Maar in je while 1 staat if not data: break, een gesloten socket voert natuurlijk geen data meer aan, dus de while 1 breakt.

Overigens, je script kan nooit meer dan 1 connectie accepteren, en zal na 1 keer een connectie gehad te hebben vastzitten in die while 1, beter is het om als je met je socket een nieuwe connectie ontvangt een thread te maken, en die thread de verdere connectie te laten afhandelen.

  • Simply_jeroen
  • Registratie: April 2004
  • Laatst online: 23-11 15:34
bedankt voor de reactie,
ik lees al wel dat ik heel weinig ervaring heb met python haha
ik dacht dat hij nu elke keer na het verbreken van de connectie ging wachten op een nieuw, maar niet dus

ik zal wel eens aan de slag gaan om een thread? te maken

  • windwarrior
  • Registratie: Januari 2009
  • Laatst online: 12-07-2024

windwarrior

ʍıupʍɐɹɹıoɹ

Een klein voorbeeldje van een server die meerdere connecties tegelijk ondersteunt, en niet stopt als een client disconnect:

Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import socket
import threading

class MainClass():
    
    def __init__(self, host, port):
        #do some init things :p
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((host, port))

    def wait_for_connections(self):
        #this is the wait for connections
        self.socket.listen(1)
        while 1:
            clientsock, addr = self.socket.accept()
            print "got a new client!"
            thread = ConnectionThread(clientsock)
            thread.start() #this is where a new thread is created and started, meaning that all code will be executed concurrently


class ConnectionThread(threading.Thread):
    def __init__(self, socket):
        super(ConnectionThread, self).__init__()
        self.socket = socket

    def run(self):
        running = True
        while running:
            data = self.socket.recv(4096)
            #do something with the received data
            #for example, close socket on disconnect :p
            if not data:
                running = False
            else:
                print data
        print "connection closed"
        self.socket.close() #little cleanup, and end thread


if __name__ == '__main__':
    mainclass = MainClass("localhost", 8080)
    mainclass.wait_for_connections()


De rest moet je zelf doen O-)


edit: he bah, is er geen syntax highlighter voor python :O

  • Simply_jeroen
  • Registratie: April 2004
  • Laatst online: 23-11 15:34
bedankt

ik ga er eens mee aan de slag

  • Simply_jeroen
  • Registratie: April 2004
  • Laatst online: 23-11 15:34
ik heb wat dingen geprobeerd, maar krijg het niet werkend...
ik lees wel dat er een loop? geplaatst kan worden

iemand enig idee hoe dit te doen
Pagina: 1