Index
1. Introduction
2. Properties and Property Functions
3. Functions
4. Callbacks
5. Discovery message
1. Introduction
This document describes all the variables and functions for the Webifi Python Library. The Python library can be installed using (for Linux users, add sudo in front of command):
pip install webifi
To upgrade to the latest version:
pip install webifi --upgrade
The Python library can also be downloaded from:
https://github.com/webifi-me/WebifiPythonLibrary.git
We also have a tutorial and a detailed example using TkInter available on GitHub at: https://github.com/webifi-me/WebifiPythonLibraryExample.
If you downloaded or cloned the library then you also need to install websocket client. Please use the following command to install websocket client:
pip install websocket-client
2. Properties and Property Functions
Below is a list of properties that can be set directly:
Property | Description |
---|---|
name | This is the name that will be seen by other devices when they send a discovery request. |
connected | This property will be set to true when the user credentials are correct and the service has been started and running normally. |
Below is a list of property functions:
Function | Description |
---|---|
set_connect_name | Set the connect name before starting the connection. Returns 0 if successful or 10 if the connect name could not be set. The connect name cannot be changed after the service is started. |
get_connect_name | Returns the connect name. |
set_connect_password | Set the connect password before starting the connection. The connect password is not your account password, it is the password used when you configured your connect name. Returns 0 if successful or 10 if connect password could not be set. The connect password cannot be changed after the service is started. |
get_connect_password | Returns the connect password. |
set_network_names | Takes a list of network names as an input parameter. These will be the default networks that this program will belong to. For more details about how this works please check our How It Works Page. The network names can be changed after the service has started. |
get_network_names | Returns a list of all the network names that this program belongs to. |
set_discoverable | The input parameter must be either true or false. If true then this instance will respond if it receives a discovery request from one of your other devices. The default value for this property is true. |
get_discoverable | Returns true if this instance is discoverable. |
set_download_timeout | Set the maximum amount of time a download request should wait if there is no data available. This is only applicable to Long Polling, not used with WebSocket. A smaller number uses more bandwidth and a large number will take a long time to recover when something goes wrong with the connection. The default value is 2 minutes and the maximum value is 10 minutes. Normally the user does not need to change this value. |
get_download_timeout | Returns the current download request timeout duration. |
set_use_encryption | When its input variable is true all the data will be exchanged using SSL/TLS. The default value of this property is false. |
get_use_encryption | Returns the current encryption state. |
get_session_id | Returns the address of this instance on the Webifi network. This property is important if you design more complex systems where you would like another device to only send data to you. |
get_counters |
Returns the values of the upload and download counters. The upload counter is incremented each time data is sent. The download counter (only used with Long Polling) is incremented each time data is received or if the download request timed out and a new download request is started. This function can be used as follows:
# it is assumed that this code is used inside a class and that the service has been started # the instance is assigned to self.webifi counters = self.webifi.get_counters() upload_counter = counters.upload download_counter = counters.download |
set_use_websocket | Set to true if you want the connection to use HTML5 WebSocket. If set to false Long Polling will be used for the connection. This setting is true by default because WebSocket is a lot faster. |
get_use_websocket | Return true if WebSocket is enabled, false if Long Polling will be used. |
3. Functions
Function | Description |
---|---|
start | This function will start the service and create a connection to the Webifi server. |
send_data | This function will send data to your other connected devices. The input parameter, the send_data object, is created as the example below shows:
# creates an instance of a class for sending data with all the # correct variables created send_data = Webifi_p27.CreateSendData() send_data.data = 'Data that will be sent' # optional fields # send_data.data_type = 'data type' # send_data.to_networks = to_networks_list # send_data.to_session_ids = to_session_ids_list self.webifi.send_data(send_data) |
close_connection | Call this function when you want to disconnect from our service. |
convert_error_code_to_string | This is a handy function for converting an error code to a description of the error. |
get_buffered_amount | This function returns the number of send packets (not the number of bytes) that is currently in the queue for sending. |
send_discovery | Call this function to discover the names and session IDs of your other devices currently connected. If this function is called without any input parameters then the discovery message will be sent to the default networks this device is part of. If a list of network names are passed into this function then the discovery message will be sent to those networks. Discovery messages are explained in more detail later in this document. |
4. Callbacks
The following functions are used to set the callbacks for the Python Library:
Set Callback | Description |
---|---|
set_connection_status_callback | This callback is called each time the connection status changes, in other words when your device connects or disconnects from our service. |
set_data_received_callback |
When data is received from another device this callback is called. There are three variables passed to this callback:
|
set_error_callback | When something goes wrong then the error callback is called. The error code is passed to the callback. |
Below is an example of how the callbacks are used. This small program will accept keyboard input and when you press enter the data will be sent to your other devices. Any data received will be displayed in the console. Type “quit” to stop the program. Create an empty file (remember to copy the Webifi Python Library into the same folder if you did not install it) called webifiConsoleTest.py and copy and paste the code below into the file. On lines 15 and 16 enter your connect name and password. The easiest way to communicate with this console app is to use our Test Page.
If using Python 2.7:
import Webifi_p27 def connection_status_callback(connected): if connected: print ('Connection successful') else: print ('Connection failed') def data_received_callback(data, data_type, from_who): print ('Data received: ' + data) webifi = Webifi_p27.Webifi() webifi.set_connect_name('your connect name') webifi.set_connect_password('your password') network_names = ['network 1'] webifi.set_network_names(network_names) webifi.set_connection_status_callback(connection_status_callback) webifi.set_data_received_callback(data_received_callback) webifi.name = 'My Python Test' webifi.start() keyboard_input = '' while keyboard_input != "quit": keyboard_input = raw_input("Enter message to send: ") if keyboard_input != 'quit': send_data = Webifi_p27.CreateSendData() send_data.data = keyboard_input webifi.send_data(send_data) else: webifi.close_connection()
If using Python 3:
import Webifi_p3 def connection_status_callback(connected): if connected: print ('Connection successful') else: print ('Connection failed') def data_received_callback(data, data_type, from_who): print ('Data received: ' + data) webifi = Webifi_p3.Webifi() webifi.set_connect_name('your connect name') webifi.set_connect_password('your password') network_names = ['network 1'] webifi.set_network_names(network_names) webifi.set_connection_status_callback(connection_status_callback) webifi.set_data_received_callback(data_received_callback) webifi.name = 'My Python Test' webifi.start() keyboard_input = '' while keyboard_input != "quit": keyboard_input = input("Enter message to send: ") if keyboard_input != 'quit': send_data = Webifi_p3.CreateSendData() send_data.data = keyboard_input webifi.send_data(send_data) else: webifi.close_connection()
5. Discovery message
The session IDs are dynamically assigned to devices connecting to the Webifi server. If you need to send data to a specific device or devices then you need to know their session IDs. The discovery message is an easy way to find the session IDs of all your connected devices. When you call the Send Discovery function the following message will be sent:
- data_type: Discovery
- data: ? (A question mark character needs to be sent)
- to_networks: When empty the discovery message will be sent to the default networks the sending device is listening to. If a list of networks are specified then all the discoverable devices on these networks will respond.
There isn’t a special event handler for the discovery response, the responses will be received by the data received event. This gives the most flexibility for the user to process the discovery responses any way the user needs. The discovery response looks as follows:
- data_type: Discovery Response
- data: User defined name on the responding device
- from_who: The session ID that was discovered. This needs to be stored by the device doing the discovery.
Below is an example of how the discovery can be implemented by the user:
def data_received_callback(self, data, data_type, from_who): if data_type == 'Discovery Response': # device discovered, name will be in data and the session ID will be in the from_who variable pass else: # data was received, process... pass