1

I am currently trying to configure the Wifi of a RaspberryPI 3 A+.

I followed this guide: here to make the Raspberry a WiFi Accesspoint. now i have a small Flask webserver running. With that i want to deactivate the AP and instead connect the Raspberry to a wifi.

But that does not work.

So I acctivate the AP via:

    def setAP(ssid, pw):
        os.system('sudo systemctl stop dnsmasq')
        os.system('sudo systemctl stop hostapd')

        if not os.path.isfile('/etc/dhcpcd.conf.orig'):
            os.system('sudo mv /etc/dhcpcd.conf /etc/dhcpcd.conf.orig') 
            with open('/etc/dhcpcd.conf', "a+") as f:
                dat = f.read()
                if " static ip_address" not in dat:
                    f.write("interface wlan0\n static ip_address=192.168.4.1/24\n   nohook wpa_supplicant")

        os.system('sudo service dhcpcd restart')

        if not os.path.isfile('/etc/dnsmasq.conf.orig'):
            os.system('sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig')       
            with open('/etc/dnsmasq.conf', 'w') as f:
                f.write("interface=wlan0\n")
                f.write("dhcp-range = 192.168.4.2,192.168.4.20,255.255.255.0,24h\n")

        os.system('sudo service dnsmasq restart')

        with open('/etc/hostapd/hostapd.conf', "w") as f:
            f.write("interface=wlan0\n")
            f.write("driver=nl80211\n")
            f.write("ssid={:}\n".format(ssid))
            f.write("hw_mode=g\n")
            f.write("channel=7\n")
            f.write("wmm_enabled=0\n")
            f.write("macaddr_acl=0\n")
            f.write("auth_algs=1\n")
            f.write("ignore_broadcast_ssid=0\n")
            f.write("wpa=2\n")
            f.write("wpa_passphrase={:}\n".format(pw))
            f.write("wpa_key_mgmt=WPA-PSK\n")
            f.write("wpa_pairwise=TKIP\n")
            f.write("rsn_pairwise=CCMP\n")

        with open('/etc/default/hostapd', "w") as f:
            f.write('DAEMON_CONF="/etc/hostapd/hostapd.conf"')

        os.system('sudo systemctl unmask hostapd')
        os.system('sudo systemctl enable hostapd')
        os.system('sudo systemctl start hostapd')

Then Ill try to reverse that via:

    def setClient(ssid, pw, dhcp=True, ip=""):
        os.system('sudo systemctl stop dnsmasq')
        os.system('sudo systemctl stop hostapd')

        if os.path.isfile('/etc/dhcpcd.conf.orig'):
            os.system('sudo mv /etc/dhcpcd.conf.orig /etc/dhcpcd.conf') 

        os.system('sudo service dhcpcd restart')

        if os.path.isfile('/etc/dnsmasq.conf.orig'):
            print('configure dhcp')
            os.system('sudo mv /etc/dnsmasq.conf.orig /etc/dnsmasq.conf')   

        with open('/etc/default/hostapd', "w") as f:
            f.write(' ')


        if not os.path.isfile('/etc/wpa_supplicant/wpa_supplicant.conf.orig'):
            os.system('sudo mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.orig')   

And connect to a Wifi:

    os.system('sudo mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.orig')   

    with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w+') as f:
        f.write('ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n')
        f.write('update_config=1\n')
        f.write('country=DE\n\n')
        f.write('network={\n')
        f.write('\tssid="{:}"\n'.format(ssid))
        f.write('\tpsk="{:}"\n'.format(pw))
        f.write('}')
    os.system('sudo wpa_cli -i wlan0 reconfigure')

and here I get the Error:

Failed to connect to non-global ctrl_ifname: wlan0  error: No such file or directory

also now I cant connect to a Wifi with the grafical interface or raspi-config. It tells me "no Wireless interface found". with ifconfig i still see the wlan0 - but it hast some random? ip address.

And the AP is still active.

someone knows what i am doing wrong? or some tips how to do it better?

Omega
  • 27
  • 2

0 Answers0