-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP32_TCP_SERVER_DEBUG.py
More file actions
160 lines (138 loc) · 4.13 KB
/
Copy pathESP32_TCP_SERVER_DEBUG.py
File metadata and controls
160 lines (138 loc) · 4.13 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import network
import socket
from machine import Pin
from time import sleep
# --- Motor Control Pins ---
IN1 = Pin(19, Pin.OUT)
IN2 = Pin(21, Pin.OUT)
IN3 = Pin(22, Pin.OUT)
IN4 = Pin(23, Pin.OUT)
# --- Optional control pins (like lights) ---
CONT1 = Pin(16, Pin.OUT)
CONT2 = Pin(14, Pin.OUT)
# --- Wi-Fi Access Point Setup ---
SSID = "RC_AMBULANCE"
PASSWORD = "ambulance123"
PORT = 5006
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=SSID, password=PASSWORD, authmode=3)
print("Access Point started. SSID:", SSID)
print("Waiting for connection...")
while not ap.active():
pass
print("AP active, IP:", ap.ifconfig()[0])
# --- Test all pins ---
print("\n=== GPIO PIN TEST ===")
test_pins = [
("IN1 (19)", IN1),
("IN2 (21)", IN2),
("IN3 (22)", IN3),
("IN4 (23)", IN4),
("CONT1 (16)", CONT1),
("CONT2 (14)", CONT2),
]
for name, pin in test_pins:
pin.value(1)
print(f"✓ {name} = ON (value={pin.value()})")
sleep(0.2)
pin.value(0)
print(f"✓ {name} = OFF (value={pin.value()})")
sleep(0.2)
print("\n=== All pins tested successfully! ===\n")
# --- Movement functions ---
def forward():
IN1.value(1)
IN2.value(0)
IN3.value(1)
IN4.value(0)
print("[MOTOR] Forward - IN1=1, IN2=0, IN3=1, IN4=0")
def backward():
IN1.value(0)
IN2.value(1)
IN3.value(0)
IN4.value(1)
print("[MOTOR] Backward - IN1=0, IN2=1, IN3=0, IN4=1")
def rotate_left():
IN1.value(0)
IN2.value(1)
IN3.value(1)
IN4.value(0)
print("[MOTOR] Rotate Left - IN1=0, IN2=1, IN3=1, IN4=0")
def rotate_right():
IN1.value(1)
IN2.value(0)
IN3.value(0)
IN4.value(1)
print("[MOTOR] Rotate Right - IN1=1, IN2=0, IN3=0, IN4=1")
def stop():
IN1.value(0)
IN2.value(0)
IN3.value(0)
IN4.value(0)
print("[MOTOR] Stop - All pins = 0")
def control_light1(state):
CONT1.value(1 if state else 0)
print(f"[LIGHT1] {'ON' if state else 'OFF'}")
def control_light2(state):
CONT2.value(1 if state else 0)
print(f"[LIGHT2] {'ON' if state else 'OFF'}")
# --- Command Map ---
COMMANDS = {
"F": forward,
"B": backward,
"L": rotate_left,
"R": rotate_right,
"S": stop,
"Q": lambda: control_light1(True),
"q": lambda: control_light1(False),
"W": lambda: control_light2(True),
"w": lambda: control_light2(False)
}
# --- TCP Socket Server ---
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", PORT))
s.listen(1)
print(f"Server listening on port {PORT}")
print("Waiting for connections...\n")
try:
while True:
conn, addr = s.accept()
print(f"\n[CONNECTION] Client connected from {addr[0]}:{addr[1]}")
try:
while True:
data = conn.recv(1024)
if not data:
print("[CONNECTION] Client sent empty data (disconnecting)")
break
cmd = data.decode().strip()
print(f"[RECEIVED] Raw command: '{cmd}'")
# Only use the first character for command
if len(cmd) > 0 and cmd[0] in COMMANDS:
print(f"[EXECUTE] Running command: {cmd[0]}")
COMMANDS[cmd[0]]()
# send acknowledgement back to the client
try:
conn.send(b"OK\n")
except Exception as _:
# If send fails, just continue (debug only)
pass
else:
print(f"[ERROR] Unknown command: {cmd}")
try:
conn.send(b"ERR\n")
except Exception:
pass
except Exception as e:
print(f"[ERROR] Connection error: {e}")
finally:
conn.close()
stop()
print("[CONNECTION] Client disconnected\n")
except KeyboardInterrupt:
print("\n[SHUTDOWN] Keyboard interrupt detected...")
stop()
s.close()
ap.active(False)
print("[SHUTDOWN] WiFi AP disabled. Program exited cleanly.")