Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,155 +1,35 @@
|
|
| 1 |
-
from flask import Flask, request
|
| 2 |
import requests
|
| 3 |
-
import threading
|
| 4 |
import time
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Set up logging
|
| 8 |
-
logging.basicConfig(level=logging.INFO)
|
| 9 |
-
logger = logging.getLogger(__name__)
|
| 10 |
-
|
| 11 |
app = Flask(__name__)
|
| 12 |
-
|
| 13 |
-
servo_state = {
|
| 14 |
-
'current_angles': [0, 0, 0],
|
| 15 |
-
'rotation_active': False,
|
| 16 |
-
'esp_ip': "manipulatable-ungenitive-lorenzo.ngrok-free.dev" # Your ngrok URL
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
state_lock = threading.Lock()
|
| 20 |
-
|
| 21 |
def send_servo_command(servo_num, angle):
|
| 22 |
-
"""Send command to specific servo with detailed logging"""
|
| 23 |
-
try:
|
| 24 |
-
with state_lock:
|
| 25 |
-
esp_ip = servo_state['esp_ip']
|
| 26 |
-
|
| 27 |
-
if not esp_ip:
|
| 28 |
-
error_msg = "No ESP32 IP configured"
|
| 29 |
-
logger.error(error_msg)
|
| 30 |
-
return error_msg
|
| 31 |
-
|
| 32 |
-
# Construct the URL - IMPORTANT: ngrok uses http (not https for free tier)
|
| 33 |
-
url = f"http://{esp_ip}/{servo_num}?angle={angle}"
|
| 34 |
-
logger.info(f"π§ Sending to ESP32: {url}")
|
| 35 |
-
|
| 36 |
-
# Add a timeout and better error handling
|
| 37 |
-
response = requests.get(url, timeout=10)
|
| 38 |
-
|
| 39 |
-
logger.info(f"π‘ ESP32 Response: HTTP {response.status_code} - {response.text}")
|
| 40 |
-
|
| 41 |
-
if response.status_code == 200:
|
| 42 |
-
success_msg = f"Servo {servo_num} β {angle}Β°"
|
| 43 |
-
logger.info(f"β
{success_msg}")
|
| 44 |
-
return success_msg
|
| 45 |
-
else:
|
| 46 |
-
error_msg = f"ESP32 returned HTTP {response.status_code}"
|
| 47 |
-
logger.error(f"β {error_msg}")
|
| 48 |
-
return error_msg
|
| 49 |
-
|
| 50 |
-
except requests.exceptions.ConnectTimeout:
|
| 51 |
-
error_msg = "Connection timeout - ngrok/ESP32 not responding"
|
| 52 |
-
logger.error(f"β {error_msg}")
|
| 53 |
-
return error_msg
|
| 54 |
-
except requests.exceptions.ConnectionError as e:
|
| 55 |
-
error_msg = f"Cannot connect to ESP32 via ngrok: {str(e)}"
|
| 56 |
-
logger.error(f"β {error_msg}")
|
| 57 |
-
return error_msg
|
| 58 |
-
except Exception as e:
|
| 59 |
-
error_msg = f"Unexpected error: {str(e)}"
|
| 60 |
-
logger.error(f"β {error_msg}")
|
| 61 |
-
return error_msg
|
| 62 |
-
|
| 63 |
-
def continuous_rotation():
|
| 64 |
-
"""Background thread for continuous rotation"""
|
| 65 |
-
ROTATION_SPEED = 5
|
| 66 |
-
ROTATION_DELAY = 0.2 # seconds between updates
|
| 67 |
-
|
| 68 |
-
while True:
|
| 69 |
-
with state_lock:
|
| 70 |
-
rotation_active = servo_state['rotation_active']
|
| 71 |
-
|
| 72 |
-
if rotation_active:
|
| 73 |
-
try:
|
| 74 |
-
logger.info("π Rotation active - moving servos")
|
| 75 |
-
messages = []
|
| 76 |
-
|
| 77 |
-
# Rotate all servos
|
| 78 |
-
for i, servo_num in enumerate([1, 2, 3]):
|
| 79 |
-
with state_lock:
|
| 80 |
-
servo_state['current_angles'][i] += ROTATION_SPEED
|
| 81 |
-
if servo_state['current_angles'][i] > 180:
|
| 82 |
-
servo_state['current_angles'][i] = 0
|
| 83 |
-
|
| 84 |
-
angle = servo_state['current_angles'][i]
|
| 85 |
-
|
| 86 |
-
# Send command to servo
|
| 87 |
-
msg = send_servo_command(servo_num, angle)
|
| 88 |
-
messages.append(msg)
|
| 89 |
-
|
| 90 |
-
logger.info(f"π― Rotation completed: {messages}")
|
| 91 |
-
time.sleep(ROTATION_DELAY)
|
| 92 |
-
|
| 93 |
-
except Exception as e:
|
| 94 |
-
logger.error(f"π₯ Rotation error: {e}")
|
| 95 |
-
time.sleep(1)
|
| 96 |
-
else:
|
| 97 |
-
time.sleep(0.1)
|
| 98 |
-
|
| 99 |
-
# ... keep your existing routes but add a debug route ...
|
| 100 |
-
|
| 101 |
-
@app.route('/debug_esp')
|
| 102 |
-
def debug_esp():
|
| 103 |
-
"""Test connection to ESP32 directly"""
|
| 104 |
try:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
response = requests.get(test_url, timeout=10)
|
| 115 |
-
|
| 116 |
-
return jsonify({
|
| 117 |
-
'status': 'success',
|
| 118 |
-
'message': f'ESP32 is reachable',
|
| 119 |
-
'url_called': test_url,
|
| 120 |
-
'http_status': response.status_code,
|
| 121 |
-
'response_preview': response.text[:200] if response.text else 'Empty response'
|
| 122 |
-
})
|
| 123 |
-
|
| 124 |
except Exception as e:
|
| 125 |
-
return
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
'result': result
|
| 142 |
-
})
|
| 143 |
-
|
| 144 |
-
# ... keep your existing start_rotation, stop_rotation, etc routes ...
|
| 145 |
|
| 146 |
if __name__ == '__main__':
|
| 147 |
-
|
| 148 |
-
logger.info("π Starting Servo Controller")
|
| 149 |
-
logger.info(f"π― ESP32 Target: {servo_state['esp_ip']}")
|
| 150 |
-
|
| 151 |
-
# Start the continuous rotation thread
|
| 152 |
-
rotation_thread = threading.Thread(target=continuous_rotation, daemon=True)
|
| 153 |
-
rotation_thread.start()
|
| 154 |
-
|
| 155 |
-
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 1 |
+
from flask import Flask, request
|
| 2 |
import requests
|
|
|
|
| 3 |
import time
|
| 4 |
+
ESP_IP = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def send_servo_command(servo_num, angle):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
+
print(ESP_IP)
|
| 9 |
+
url = f"http://{ESP_IP}/{servo_num}?angle={angle}"
|
| 10 |
+
response = requests.get(url, timeout=1)
|
| 11 |
+
time.sleep(60/angle)
|
| 12 |
+
print(servo_num)
|
| 13 |
+
url = f"http://{ESP_IP}/stop"
|
| 14 |
+
response = requests.get(url, timeout=1)
|
| 15 |
+
return f"Servo {servo_num} β {angle}Β°"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
+
return f"Error: {e}"
|
| 18 |
+
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def the():
|
| 21 |
+
send_servo_command(1, 180)
|
| 22 |
+
send_servo_command(2, 180)
|
| 23 |
+
send_servo_command(3, 180)
|
| 24 |
+
return 'hello worlddd'
|
| 25 |
+
|
| 26 |
+
@app.route('/input')
|
| 27 |
+
def get_input():
|
| 28 |
+
global ESP_IP
|
| 29 |
+
data = request.args.get('data')
|
| 30 |
+
ESP_IP = data.replace('_','.')
|
| 31 |
+
print(ESP_IP)
|
| 32 |
+
return ESP_IP
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if __name__ == '__main__':
|
| 35 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|