-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_speech_and_emergency_call.ino
More file actions
85 lines (74 loc) · 2.74 KB
/
Copy pathtext_to_speech_and_emergency_call.ino
File metadata and controls
85 lines (74 loc) · 2.74 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
import time
import pyttsx3
import serial
from twilio.rest import Client
from concurrent.futures import ThreadPoolExecutor, as_completed
recipients = [
{
'account_sid': 'AC1c35d0ac4f9e5427975422f365bXXXXX',
'auth_token': '03f2e4dc009f5a1b5e2725527ae47a93',
'twilio_number': '+1585620XXXXX',
'recipient_number': '+9189174XXXXX'
},
{
'account_sid': 'AC0447a88d6b27d70603c8a4b13a8XXXXX',
'auth_token': 'ad31ea66db6257c1c3ba65788df7a03b',
'twilio_number': '+122335XXXXX',
'recipient_number': '+9176848XXXXX'
},
# Add more accounts below as needed
]
PORT = 'COM7'
BAUD = 115200
ser = serial.Serial(PORT, BAUD, timeout=1)
time.sleep(2) # Let ESP32 reset
engine = pyttsx3.init()
engine.setProperty('rate', 150)
def notify_recipient(r):
"""Make the call and send SMS for one recipient dict r."""
client = Client(r['account_sid'], r['auth_token'])
# Place call
call = client.calls.create(
twiml='<Response><Say>This is an emergency. I am in urgent need of help. I am currently in a dangerous situation and unable to get to safety on my own. Please send immediate assistance to my location.</Say></Response>',
from_=r['twilio_number'],
to=r['recipient_number']
)
print(f"✅ Call sent to {r['recipient_number']} (SID: {call.sid})")
# Send SMS
msg = client.messages.create(
body="🚨 Alert: Your ESP32 device triggered an emergency!",
from_=r['twilio_number'],
to=r['recipient_number']
)
print(f"✅ SMS sent to {r['recipient_number']} (SID: {msg.sid})")
print("Listening for decoded Morse, call trigger, and text for TTS...")
while True:
try:
line = ser.readline().decode('utf-8', errors='ignore').strip()
if (
not line
or line == "[DEL]"
or line == "."
or line.startswith("Button state:")
):
continue
print("Received:", line)
if line == "CALL_TRIGGERED":
# Use a thread pool to notify all recipients in parallel
with ThreadPoolExecutor(max_workers=len(recipients)) as executor:
futures = {executor.submit(notify_recipient, r): r for r in recipients}
for fut in as_completed(futures):
r = futures[fut]
try:
fut.result()
except Exception as e:
print(f"❌ Failed for {r['recipient_number']}: {e}")
else:
# Speak out normal messages
engine.say(line)
engine.runAndWait()
except KeyboardInterrupt:
print("\nStopped by user.")
break
except Exception as e:
print(f"Error during processing: {e}")