diff --git a/sat2aprs.py b/sat2aprs.py new file mode 100644 index 0000000..e57b39f --- /dev/null +++ b/sat2aprs.py @@ -0,0 +1,79 @@ +import re +import socket +from flask import Flask, request +from twilio.twiml.messaging_response import MessagingResponse + +app = Flask(__name__) + +def decimal_to_ddmm(value): + degrees = int(value) + minutes_decimal = (value - degrees) * 60 + abs_minutes = abs(minutes_decimal) + minutes = round(abs_minutes, 2) + return "{:02d}{:05.2f}".format(abs(degrees), abs(minutes)) + +# APRS-IS login info +serverHost = 'rotate.aprs2.net' +serverPort = 14580 +aprsUser = 'CALL' +aprsPass = 'PASS' +text = 'Sat2APRS' + +# Create and connect the APRS-IS socket +aprs_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +try: + aprs_sock.connect((serverHost, serverPort)) + login_str = 'user {} pass {} vers Sat2APRS 0.1b\r\n'.format(aprsUser, aprsPass) + aprs_sock.send(login_str.encode()) + response = aprs_sock.recv(1024).decode('utf-8') + print('APRS-IS login response:', response) +except Exception as e: + print('Error connecting to APRS-IS:', e) + +def send_aprs_packet(callsign, lat, lon, text): + lat_ddmm = decimal_to_ddmm(lat) + lon_ddmm = decimal_to_ddmm(lon) + aprs_packet = '{}>APRS:!{}{}\{}{}S{}\r\n'.format(callsign, lat_ddmm, "N" if lat >= 0 else "S", lon_ddmm, "E" if lon >= 0 else "W", text) + + try: + aprs_sock.sendall(aprs_packet.encode()) + print('Sent APRS packet:', aprs_packet.strip()) + except Exception as e: + print('Error sending APRS packet:', e) + + +@app.route('/sms', methods=['POST']) +def webhook(): + incoming_sms = request.form.get('Body', '') + latitude = None + longitude = None + + # Log incoming message + print('Received Message:', incoming_sms) + + # Search for latitude and longitude in the message using regular expressions + lat_long_match = re.search(r'Location:\s*(-?\d+\.\d+),(-?\d+\.\d+)', incoming_sms) + + if lat_long_match: + latitude = float(lat_long_match.group(1)) + longitude = float(lat_long_match.group(2)) + print('Detected Lat Long: ({}, {})'.format(latitude, longitude)) + + # Convert latitude and longitude to DDMM.MM format without dashes + lat_ddmm = decimal_to_ddmm(latitude) + long_ddmm = decimal_to_ddmm(longitude) + + print('Converted Lat Long to DDMM.MM: {}, {}'.format(lat_ddmm, long_ddmm)) + print('{}/{}'.format(lat_ddmm, long_ddmm)) + + # Send APRS packet + send_aprs_packet(aprsUser, latitude, longitude, text) + + else: + print('No Lat Long detected.') + + response = MessagingResponse() + return str(response) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5001, debug=True)