WiFi

Note:

VBOX Touch variants only

Callbacks

When connecting to a WiFi network you are going to need to know when you have succeeded in doing so. You will also need to know if your WiFi based connections (HTTP, MQTT) have succeeded. Hence there are callbacks for each of these.

Setting the callback

To set the WiFi callback you need to call the set_wifi_cb() function and pass it a valid function that you want it to callback to.

network.set_wifi_cb(cb_func)

When you receive a callback to this function you will be passed a number as a response. The number will be one of the constants in the network module that could start with EVENT_WIFI, EVENT_HTTP or EVENT_MQTT depending on the type of connection.

For example if you wish to check that the response you received is a successful connection to a WiFi network you could do the following:

if response == network.EVENT_WIFI_CONNECT_SUCCESS:
    print("WiFi connection succeeded")
elif response == network.EVENT_WIFI_CONNECT_FAIL:
    raise ConnectionError("Failed to connect to WiFi" + network.get_wifi_fail_reason)

This will print that it was a success or raise an error if there is a failure with the reason for this failure given by the wifi chip.

Note:

The reason for failure is not always accurate.

Scanning for WiFi devices

To scan for WiFi devices you need to use the .scan() function. This can perform either a WiFi or bluetooth scan so you need to specify that you want a WiFi connection. This automatically stores the data it receives back in a dictionary called wifi_networks_found in the structure SSID : BSSID.

You cannot specify a time for a WiFi scan.

network.scan('WiFi')

To check this dictionary simply get the variable from the class.

print(network.wifi_networks_found)

Setting up a profile

To connect to a WiFi network it is first necessary to set up the connection into a “profile”. This profile will be stored on the VBOX Touch and will be loaded in at the beginning of each power cycle if you are using the network module.

Hence you can load in the same profile and use it over and over. There are 10 different storage locations for WiFi profiles from 0-9 inclusive. When you create a profile there are a few parameters that need to be pass to the setup_wifi_profile() function. These are:

  • ssid[string] = The “name” of your network

  • password[string] = The password for your network if there is one. Defaults to a blank string.

  • profile[int] = The profile number you want to store it under. This is an int from 0-9. It defaults to 0.

  • auth[int] = The authentication used by the network. Use the network.AUTH to get the correct number. Defaults to AUTH_WPA_WPA2.

  • ipmode[int] = the ip mode of the network. Defaults to IP_DHCP. Can also be IP_STATIC.

  • always_active[bool] = When True, keeps the connection active. Defaults to False.

The only parameter that doesn’t have a default value is the SSID. To create this profile use the following function:

my_wifi_profile = network.setup_wifi_profile('MyWiFiNetwork', '1234', 1, network.AUTH_LEAP, network.IP_STATIC, False)

The function returns the number of the profile that you assigned.

Note:

This will also disconnect the WiFi if you enter the profile that is currently connected.

Erasing a profile

To erase a WiFi profile you need to pass the profile you wish to erase to the wifi_profile_erase() function.

network.wifi_profile_erase(my_wifi_profile)

Note:

This will also disconnect the WiFi if you enter the profile that is currently connected.

Connecting

When you then wish to connect to WiFi you have to pass a WiFi profile or the SSID of the network to the connect_to_wifi() function.

network.connect_to_wifi(my_wifi_profile)

This will then try and connect you to the network that you assigned in setup_wifi_profile. If this fails then you may be out of range of your network or could have entered the incorrect setup details.

Disconnecting

If you want to disconnect from a network you simply need to use the disconnect_from_wifi() function. You do not need to pass it a profile as it will use the only WiFi connection that is currently connected as there can only be one connected at a time.