This guide shows you how to get started writing Python programs for your Vernier Go Direct1 sensors. The guide contains the following topics:
If you are new to Python you should look over all of the steps. If you are familiar with Python you might just focus on installing the godirect module and downloading the examples.
1 Go Direct spectrometers are not supported.
We have developed a Python module called
godirect that allows you to communicate with Vernier Go Direct sensors via USB or Bluetooth Low Energy (BLE). This requires the following:
The godirect module is designed to work with Python 3, it is not compatible with Python 2. In some cases, Python is pre-installed and shipped with your operating system. Use the following steps to check for Python 3 on your machine, and to install Python 3 if needed:
With Python installed, you need to choose an application that you will use to write and run your Python programs. You can write your programs in a simple text or code editor, or you can use an Integrated Development Environment (IDE). An IDE is a software application that provides all of the tools to write, comment, edit, debug, develop and run software programs. Python has an IDE bundled with it called IDLE that is cross-platform, and suitable for beginners. If you are new to Python and programming, we recommend that you start with this tool.
Later, you may want to research the various IDEs, learn about the different features, and give one a try. For example, Visual Studio Code is a free IDE available for Windows, Linux, and Mac. There are many other choices, and you can find more information at python.org
Once you have Python 3 installed, you will use a tool called
pip (a Python 3 package manager) to install the godirect module and other Python modules that you may need. Python automatically includes
pip, so this tool is ready to use. Note that we will be using the
pip3 command, rather than just
pip, to ensure that the Vernier files will be associated with Python 3, and not Python 2.
pip3 commands are executed by running them in your operating systems’ tool for executing commands (Powershell, Command Prompt, or Terminal window). There are slight differences in the required steps to install the godirect module for Windows, macOs, and Linux (including Linux running on Raspberry Pi). Follow the steps outlined below for your platform.
Run the following command in Powershell or Command Prompt to install the godirect module for USB and BLE:
pip3 install godirect
If you received an error during the installation of godirect, part of which looks like the message below, it is probably because your Windows system does not have the appropriate compiler. The godirect module includes a package that requires a C++ compiler.
In order to install without this failure, you will need to follow the instructions to download and install the command line Build Tools for Visual Studio. The latest version of the tools download can be found by scrolling down to the All Downloads section and expanding Tools for Visual Studio (as shown in the figure below). After completing the installation of this tool repeat the
pip3 installation steps for godirect and Bleak.
Run the following command in Terminal to install the godirect module for USB and BLE:
pip3 install godirect
Run the following command in your terminal to install the godirect module for USB and BLE:
pip3 install godirect
Some Linux distributions will not include all of the tools required by the godirect module and its dependencies. If you see errors when you run the
pip3 install, you might need to first install the two packages as shown below, before trying again:
sudo apt install libusb1.0.0
sudo apt install libudev-dev
In order to communicate with Go Direct devices over USB on Linux systems, you will need to provide a special udev rule that allows the device to be identified when it is plugged in. The rule file is located on our github directory. Use the following steps to download the rule file and then move it to the proper location:
sudo cp vstlibusb.rules /etc/udev/rules.d/.
If you wish to connect to Go Direct device using a Bluegiga BLED112 Bluetooth® Low Energy Dongle, rather than the native BLuetooth radio, you will need to install the vernierpygatt module with this command:
pip3 install vernierpygatt
You will also need to pass
use_ble_bg=True into the godirect module. There is a comment regarding this in the
gdx.ble_open function in the gdx.py file.
At this point, you should have Python 3 installed and have the godirect module installed. Before moving to examples, confirm the installation of the godirect module by showing the version information. Run the following command in the terminal:
pip3 show godirect
pip3 show command will provide information about the installed package, including the version number.
Should you need to update to a newer version in the future, run the following command in the terminal:
pip3 install godirect --upgrade
With the godirect module installed, it is time to run an example.
The getting started examples demonstrate how to collect data from Go Direct sensors. Under the hood, these examples all use the godirect module to communicate with the Go Direct devices. However, to make things a bit simpler, we created a layer to abstract some of the details away and provide some simple functions. That layer is named gdx and can be found in the /gdx/ folder (note that this folder must be located in the same directory as the examples or programs that you write that use the gdx functions). All of the getting started examples make use of this layer for a cleaner, simpler entry point into coding with Go Direct devices.
The gdx functions used in a typical program to collect data include:
A simple program using the gdx module looks like this:
from gdx import gdx gdx = gdx.gdx() gdx.open_usb() gdx.select_sensors() gdx.start() for i in range(0,5): measurements = gdx.read() if measurements == None: break print(measurements) gdx.stop() gdx.close()
Here is some more information about the gdx functions, including how you might add arguments to a few of the functions:
from gdx import gdx gdx = gdx.gdx()
gdx.open_usb()will connect to all Go Direct devices connected via USB.
gdx.open_ble()is used in place of
gdx.open_usb()if the program will connect the Go Direct device(s) via Bluetooth instead of USB.
gdx.open_ble(“GDX-FOR 071000U9, GDX-HD 151000C1”)
gdx.open_ble()function it will search and connect to those specific Go Direct devices. In the above code snippet a Go Direct Force and Acceleration sensor with a serial number of 071000U9 and a Go Direct Hand Dynamometer with serial number 151000C1 will be opened via Bluetooth.
gdx.open_ble()function if you want your program to open the Go Direct device with the strongest rssi signal strength.
gdx.start(1000)to sample every 1000 milliseconds, or
gdx.start(100)to sample every 100 milliseconds.
measurements = gdx.read()
gdx.read()function will take single point readings from the selected sensors at the desired period and return the readings as a 1D list.
gdx.stop()function stops data collection but does not disconnect the sensor, so it is possible to start a new round of data collection.
gdx.close()you can not call any other gdx functions.
As stated earlier, these gdx functions are available in the gdx.py file, in the gdx folder. The gdx.py file is easy to locate and available for you to modify. Consider adding your own functions, or modifying the functions to meet your needs.
pip3 install bleak
error: option --single-version-externally-managed not recognized
This is a known incompatibility and the options to work around it are described here: https://github.com/hbldh/bleak/issues/147
All of the content in this repository is available under the terms of the BSD 3-Clause License.
Vernier products are designed for educational use. Our products are not designed nor are they recommended for any industrial, medical, or commercial process such as life support, patient diagnosis, control of a manufacturing process, or industrial testing of any kind.