7 Replies Latest reply on Jun 28, 2016 11:05 AM by Intel Corporation

    Connect to WLAN under C++

    Christoph.P

      Hi!

      I'm using this code to connect to a target WLAN under C++:

       

      string ssid = "myssid";

      string password = "mypassword";

       

      system("systemctl start wpa_supplicant");

      system("wpa_cli -i wlan0 disconnect");

      system("wpa_cli -i wlan0 remove_network all");

      system("wpa_cli -i wlan0 add_network");

      system("wpa_cli -i wlan0 set_network 0 mode 0");

      system("wpa_cli -i wlan0 set_network 0 ssid \\\"" + ssid + "\\\"");

      system("wpa_cli -i wlan0 set_network 0 auth_alg OPEN");

      system("wpa_cli -i wlan0 set_network 0 key_mgmt WPA-PSK");

      system("wpa_cli -i wlan0 set_network 0 proto RSN");

      system("wpa_cli -i wlan0 set_network 0 psk \\\"" + password + "\\\"");

      system("wpa_cli -i wlan0 set_network 0 scan_ssid 1");

      system("wpa_cli -i wlan0 select_network 0");

      system("wpa_cli -i wlan0 enable_network 0");

      system("wpa_cli -i wlan0 reassociate");

       

      Sometimes it connects well, but sometime, it doest.

      "wpa_cli -i wlan0 status"

       

      then shows:

       

      .wpa_state=SCANNING

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

       

      .bssid=b8:55:10:d5:14:ac

      ssid=myssid

      id=0

      mode=station

      pairwise_cipher=CCMP

      group_cipher=CCMP

      key_mgmt=WPA2-PSK

      wpa_state=4WAY_HANDSHAKE

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

       

      .wpa_state=SCANNING

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

       

      .wpa_state=SCANNING

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

       

      .wpa_state=SCANNING

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

       

      .wpa_state=DISCONNECTED

      p2p_device_address=7a:4b:87:aa:6f:a0

      address=78:4b:87:aa:6f:a0

        • 1. Re: Connect to WLAN under C++
          sylyca

          Hi,

           

          I have the same behaviour, with a bash script :

           

          #!/bin/sh

          if [ $# -ne 2 ]

          then

                  echo "usage: $0 SSID passphrase"

                  exit

          fi

          config="/tmp/${1}.conf"

          cat <<EOF > "$config"

          ctrl_interface=/var/run/wpa_supplicant

          ctrl_interface_group=0

          config_methods=virtual_push_button virtual_display push_button keypad

          update_config=1

          fast_reauth=1

          device_name=Edison

          manufacturer=Intel

          model_name=Edison

          EOF

           

          wpa_cli -i wlan0 terminate 2> /dev/null

          systemctl stop wpa_supplicant

          wpa_supplicant -B -i wlan0 -c "$config"

          udhcpc -i wlan0 -n

          I need to reboot my wifi when it fails.

           

          Hope this help to understand!

          • 2. Re: Connect to WLAN under C++
            Christoph.P

            That's funny. Because when I run my bash script manually (which does the same as the code) it works..

            How can that be?

             

            if [ $# != 2 ] ; then

            echo "$0 <SSID> <passphrase>"

            exit

            fi

            wpa_cli -iwlan0 disconnect

            wpa_cli -iwlan0 remove_network all

            wpa_cli -iwlan0 add_network

            wpa_cli -iwlan0 set_network 0 mode 0

            wpa_cli -iwlan0 set_network 0 ssid \"$1\"

            wpa_cli -iwlan0 set_network 0 auth_alg OPEN

            wpa_cli -iwlan0 set_network 0 key_mgmt WPA-PSK

            wpa_cli -iwlan0 set_network 0 proto RSN

            wpa_cli -iwlan0 set_network 0 psk \"$2\"

            wpa_cli -iwlan0 set_network 0 scan_ssid 1

            wpa_cli -iwlan0 select_network 0

            wpa_cli -iwlan0 enable_network 0

            wpa_cli -iwlan0 reassociate

            wpa_cli -iwlan0 status

            • 3. Re: Connect to WLAN under C++
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi,

              What image are you using? What happens if you use wpa_cli -iwlan0 set_network 0 scan_ssid 0?

              Sergio
               

              • 4. Re: Connect to WLAN under C++
                Christoph.P

                Im Using 3.10.17-poky-edison+.

                 

                wpa_cli -iwlan0 set_network 0 scan_ssid 0

                 

                gives me "OK"

                • 5. Re: Connect to WLAN under C++
                  Christoph.P

                  I now saw that the log of the wpa_supplicant throw this:

                   

                   

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: Trying to associate with b8:55:10:d5:14:ac (SSID='mySSID' freq=2462 MHz)

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: Trying to associate with b8:55:10:d5:14:ac (SSID='mySSID' freq=2462 MHz)

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: CTRL-EVENT-ASSOC-REJECT status_code=1

                  Jun 10 14:54:15 edison wpa_supplicant[768]: Retrying assoc: 1

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: CTRL-EVENT-DISCONNECTED bssid=b8:55:10:d5:14:ac reason=3 locally_generated=1

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: CTRL-EVENT-ASSOC-REJECT status_code=1

                  Jun 10 14:54:15 edison wpa_supplicant[768]: current_ssid == NULL

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: Trying to associate with b8:55:10:d5:14:ac (SSID='mySSID' freq=2462 MHz)

                  Jun 10 14:54:15 edison wpa_supplicant[768]: wlan0: CTRL-EVENT-ASSOC-REJECT bssid=b8:55:10:d5:14:ac status_code=1

                  Jun 10 14:54:15 edison wpa_supplicant[768]: Retrying assoc: 2

                   

                  When you do a google search in "reason=3" in combination with wpa_supplicant, you get some threads who don't say what's the reason

                  for failing, but say that sometimes it it helpful to kill the service and try again.

                   

                  In te end not very satifying because there is no answer for the reason.

                   

                  Please tell me if you know more about reason=3.

                  • 6. Re: Connect to WLAN under C++
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation
                    • 7. Re: Connect to WLAN under C++
                      Intel Corporation
                      This message was posted on behalf of Intel Corporation

                      Do you still need assistance with this case? I noticed in previous posts that you’re not using the latest image. You can download the latest image here https://software.intel.com/edison-image/latest . After flashing this image, try again to run wpa_cli -iwlan0 set_network 0 scan_ssid 0 and let us know your results.

                      -Sergio