RajkumarGara / remote-serial-pico

Access remote serial devices connected to Pico with Pi
0 stars 1 forks source link

Should not hard code this #10

Open horner opened 2 weeks ago

horner commented 2 weeks ago

https://github.com/RajkumarGara/remote-serial-pico/blob/fa289ffd1228fd6599bca0c39876204cb2871a09/src/pi/PicoScriptDeployer.py#L52

horner commented 2 weeks ago

Who's IP?

https://github.com/RajkumarGara/remote-serial-pico/blob/fa289ffd1228fd6599bca0c39876204cb2871a09/src/pi/PicoScriptDeployer.py#L81C18-L81C32

We dont need a DNS server to get anyones ip. If you want your own or the pico a DNS server is not needed.

RajkumarGara commented 1 week ago
import socket
import psutil

def get_ip_address(interface_names):
    for interface_name in interface_names:
        addresses = psutil.net_if_addrs().get(interface_name, [])
        for addr in addresses:
            # Check if it's an IPv4 address and not a loopback
            if addr.family == socket.AF_INET and not addr.address.startswith('127.'):
                return addr.address
    return None

def get_wireless_or_wired_ip():
    wireless_interfaces = [iface for iface in psutil.net_if_stats() if 'wlan' in iface]
    wired_interfaces = [iface for iface in psutil.net_if_stats() if 'eth' in iface]

    # Try to get the IP address from wireless interfaces first
    ip_address = get_ip_address(wireless_interfaces)

    if not ip_address:
        print('No wireless interfaces are up. Falling back to wired interfaces...')
        # Fall back to the wired IP address
        ip_address = get_ip_address(wired_interfaces)

    return ip_address or 'No network interface is up'

if __name__ == '__main__':
    ip_address = get_wireless_or_wired_ip()
    print('IP address:', ip_address)