Forum: PC-Programmierung Raspberry Pi mit NET IO App


von Schelzer (Gast)


Lesenswert?

Hallo,

ich habe mir die NET IO App heruntergeladen 
(http://netio.davideickhoff.de) um mein Raspberry damit von meinem 
Smartphone zu steuern. Nun ist das GUI für das Smartphone fertig und 
schickt seine Daten auch erfolgreich zum Raspberry. Leider bin ich ein 
totaler Anfänger im Bereich Python Programmierung und ich finde mein 
Fehler einfach nicht...
1
#!/usr/bin/python 
2
3
# Echo server program
4
import socket
5
import RPi.GPIO as GPIO
6
7
#Setup IO's
8
# Raspberry Pi Pins
9
GPIO.setmode(GPIO.BCM)
10
#Variablen Deklarierung
11
UP = 10
12
DOWN = 8
13
LEFT = 5
14
RIGHT = 3
15
DRIVE = 7
16
17
#GPIOS Deklarierung
18
19
GPIO.setup(UP, GPIO.OUT)
20
GPIO.setup(DOWN, GPIO.OUT)
21
GPIO.setup(LEFT, GPIO.OUT)
22
GPIO.setup(RIGHT, GPIO.OUT)
23
GPIO.setup(DRIVE, GPIO.OUT)
24
25
#Setup Socket
26
HOST = ''                 # Symbolic name meaning the local host
27
PORT = 54321              # Arbitrary non-privileged port
28
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29
s.bind((HOST, PORT))
30
s.listen(1)
31
conn, addr = s.accept()
32
print 'Connected by', addr
33
34
#Loop
35
36
while 1:
37
data = conn.recv(1024)
38
print 'New Receive', data
39
#Haupt- if Schleife
40
if not data: break
41
print 'Received Data:', data
42
43
# Oben Remote
44
45
if data == 'UP on\n':
46
conn.send('on')
47
GPIO.output(UP, GPIO.HIGH)
48
print 'received on'
49
50
if data == 'UP off\n':
51
conn.send('off')
52
GPIO.output(UP, GPIO.LOW)
53
print 'received off'
54
        
55
#Unten Remote
56
57
if data == 'DOWN on\n':
58
conn.send('on')
59
GPIO.output(DOWN, GPIO.HIGH)
60
print 'received on'
61
62
if data == 'DOWN off\n':
63
conn.send('off')
64
GPIO.output(DOWN, GPIO.LOW)
65
print 'received off'
66
67
#Links Remote
68
69
if data == 'LEFT on\n':
70
conn.send('on')
71
GPIO.output(LEFT, GPIO.HIGH)
72
print 'received on'
73
74
if data == 'LEFT off\n':
75
conn.send('off')
76
GPIO.output(LEFT, GPIO.LOW)
77
print 'received off'
78
79
#Rechts Remote
80
81
if data == 'RIGHT on\n':
82
conn.send('on')
83
GPIO.output(RIGHT, GPIO.HIGH)
84
print 'received on'
85
86
if data == 'RIGHT off\n':
87
conn.send('off')
88
GPIO.output(RIGHT, GPIO.LOW)
89
print 'received off'
90
91
#Drive on/off Remote
92
93
if data == 'DRIVE on\n':
94
conn.send('on')
95
GPIO.output(DRIVE, GPIO.HIGH)
96
print 'received on'
97
98
if data == 'DRIVE off\n':
99
conn.send('off')
100
GPIO.output(DRIVE, GPIO.LOW)
101
print 'received off'
102
103
# end if
104
    else:
105
        conn.send('unknown command')
106
        print 'Unknown command:', data
107
    # continue with while 1
108
conn.close()

Da ich den Anfang und das Ende aus der DEMO-datei entnommen habe kann es 
daran vermutlich nicht liegen, das script meckert ja erst immer bei den 
IF-Bedingungen. Kann mir ja eventuell jemand weiterhelfen ?

von Alexander F. (alexf91)


Lesenswert?

Hast du die Einrückungen wirklich so gesetzt?
In Python wird nämlich statt über Klammern über Einrückungen 
strukturiert.

Also sollte das z.B. so aussehen:
1
while 1:
2
    data = conn.recv(1024)
3
    print 'New Receive', data
4
#Haupt- if Schleife
5
    if not data:
6
        break
7
    print 'Received Data:', data

von Schelzer (Gast)


Lesenswert?

Vielen Dank erstmal für die schnelle Antwort !

Ich teste das Programm heute Abend mal und geb dann bescheid obs daran 
gelegen hat !

von Schelzer (Gast)


Lesenswert?

Also habs gerade nochmal ausprobiert, es funktioniert immernoch nicht 
...
Hier der Code nochmal
1
#!/usr/bin/python 
2
3
# Echo server program
4
import socket
5
import RPi.GPIO as GPIO
6
7
#Setup IO's
8
# Raspberry Pi Pins
9
GPIO.setmode(GPIO.BCM)
10
#Variablen Deklarierung
11
UP = 10
12
DOWN = 8
13
LEFT = 5
14
RIGHT = 3
15
DRIVE = 7
16
17
#GPIOS Deklarierung
18
19
GPIO.setup(UP, GPIO.OUT)
20
GPIO.setup(DOWN, GPIO.OUT)
21
GPIO.setup(LEFT, GPIO.OUT)
22
GPIO.setup(RIGHT, GPIO.OUT)
23
GPIO.setup(DRIVE, GPIO.OUT)
24
25
#Setup Socket
26
HOST = ''                 # Symbolic name meaning the local host
27
PORT = 54321              # Arbitrary non-privileged port
28
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29
s.bind((HOST, PORT))
30
s.listen(1)
31
conn, addr = s.accept()
32
print 'Connected by', addr
33
34
#Loop
35
36
while 1:
37
  data = conn.recv(1024)
38
  print 'New Receive', data
39
#Haupt- if Schleife
40
  if not data: break
41
  print 'Received Data:', data
42
43
# Oben Remote
44
45
    if data == 'UP on\n':
46
    conn.send('on')
47
    GPIO.output(UP, GPIO.HIGH)
48
    print 'received on'
49
50
    if data == 'UP off\n':
51
    conn.send('off')
52
    GPIO.output(UP, GPIO.LOW)
53
    print 'received off'
54
        
55
#Unten Remote
56
57
    if data == 'DOWN on\n':
58
    conn.send('on')
59
    GPIO.output(DOWN, GPIO.HIGH)
60
    print 'received on'
61
62
    if data == 'DOWN off\n':
63
    conn.send('off')
64
    GPIO.output(DOWN, GPIO.LOW)
65
    print 'received off'
66
67
#Links Remote
68
69
    if data == 'LEFT on\n':
70
    conn.send('on')
71
    GPIO.output(LEFT, GPIO.HIGH)
72
    print 'received on'
73
74
    if data == 'LEFT off\n':
75
    conn.send('off')
76
    GPIO.output(LEFT, GPIO.LOW)
77
    print 'received off'
78
79
#Rechts Remote
80
81
    if data == 'RIGHT on\n':
82
    conn.send('on')
83
    GPIO.output(RIGHT, GPIO.HIGH)
84
    print 'received on'
85
86
    if data == 'RIGHT off\n':
87
    conn.send('off')
88
    GPIO.output(RIGHT, GPIO.LOW)
89
    print 'received off'
90
91
#Drive on/off Remote
92
93
    if data == 'DRIVE on\n':
94
    conn.send('on')
95
    GPIO.output(DRIVE, GPIO.HIGH)
96
    print 'received on'
97
98
    if data == 'DRIVE off\n':
99
    conn.send('off')
100
    GPIO.output(DRIVE, GPIO.LOW)
101
    print 'received off'
102
103
# end if
104
    else:
105
        conn.send('unknown command')
106
        print 'Unknown command:', data
107
    # continue with while 1
108
conn.close()


Es kommt der Fehler "Indentationerror: unexpected indent"

von W. M. (thematsche)


Angehängte Dateien:

Lesenswert?

Probier mal das angehaengte File aus.
Dann vergleiche mal dein File und das angehaengte und denke darueber 
nach.
Und bitte erst dann weiterfragen.
Weil wenn man ueber eine Treppe gehen will und einen Tip bekommt wie man 
eine Stufe ueberwindet, dann sollte man sich den Tip zu Herzen nehmen 
und nich gleich bei der zweiten Stufe wieder fragen.
Da kann es dann durchaus vorkommen, dass man keinen Tip mehr 
bekommt.....

Gruss.

Edit:
Ooops, irgendwie ist das File 2x reingerutscht.

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.