Some of you have asked us, concerned about the possibility of using Python to generate a custom graphical interface to use with openDAQ, but not having enough experience to build one from scratch.
Based on the two demos which are distributed freely with openDAQ (EasyDAQ and DAQControl), it is easy to modify the code and create a custom application with all the essential elements of a graphical environment to record the signal from one or more sensors.
In this article we will explain step by step how to create a small example consisting on a graph of temperature versus time. The ideas of possible applications that can be created by combining Python and openDAQ are endless, but I think this simple tutorial can serve as a starting guide to create your own applications.
For this example we will use the code of the example program DAQControl. We will create an application that generates a graph of temperature versus time. The timing between points is not critical, so it will be performed by software.
Cleaning up the code
First step, of course, is getting the original code and the building tools necessary to run the entire process. On the Support page you will find links to download the Python IDE and some libraries used in our examples. You will find information of the respective installation process in the download pages. You will also need, of course, to download the source code for DAQControl from GitHub.
The process that we will follow is modify the code of DAQControl to make it more suitable to our desires.
Then, keep a copy of daqcontrol.py, renaming it as (e.g.) example1.py. In this file we will start by eliminating all those parts of the code that are not needed:
At the beginning of the code, the method Scan handles generating the list of available serial ports, it is related with InitDlg (the welcome dialog of the application). We could eliminate all of this and use a fixed serial port, if we knew what port number will be identifying openDAQ in our PC, but for our example we decided to keep it.
The method ComThread handles low-level threaded (as an independent process) communication with the device, so it is essential and may not be erased. But, indeed, we will want to modify this function. We will return to it later.
TimerThread manages data acquisition from capture, counter and encoder modes. Since we will not use any of them, we can eliminate the method completely.
PageThree and PageFour can also be erased, because they control the tabs for digital control and timer functions, respectively, which are not needed. For this example we will be using a tab to display the controls, when we could do it all on the main grid, but for now we will do this way to simplify.
In PageOne there are all the controls for analog configuration of openDAQ. We will not need many of them, and therefore we will keep the code but it needs to be modified.
In Mainframe we will delete all references to PageThree and PageFour, as they no longer exist. It is important to remove all references to timerThread throughout the program, as it also no longer exists. After doing all this, the program has been very simplified, we have left only the contents of the first tab, the analog controller:
Modifying the functions
Now, let's remove unnecessary controls and change labels to fit what we really have connected to the instrument.
In this experiment we will connect a LM35 temperature sensor to the input device A7. Remember that this sensor generates a voltage in millivolts that is ten times the environment temperature in °C. The sensor is powered directly from the +5V output of openDAQ.
- We have eliminated from PageOne the two sampleList which enumerate the input ports available to use with openDAQ (we use the A7 against mass and there is no need to select it).
- Then we have changed some text labels from the graph and controls to fit our needs.
- We also deleted the container for the DAC output configuration (StaticBox), and all references to it (outputSizer).
- In PlayEvent, we will change the code to always use static channels:
self.ch1 = 7
self.ch2 = 0
self.range = 1
self.rate = self.editrate.GetValue()*1000
The rest of changes are relatively simple and straightforward to perform. Be careful with references to control values that were just removed. Remember that we will use a value of 7 for Ch+, 0 for Ch-, and 1 for Range (+-4V) values. Also be aware of the sizer settings window of the graph.
As the code is already very simplified, from now it will be quite easy for you to continue making the desired changes even without much knowledge of Python.
In less than 15 minutes of playing with the code, this is the aspect that we have given to our new interface of temperature: