« MBS @ FMTraining.TV -… | Home | MBS Plugin Advent cal… »

MBS Plugin Advent calendar: 10 - Phidgets

Door 10 - Phidgets

Fact of the day
Did you know that Phidgets are used in schools to get students interested in computer science?

Do you already know the small input/output devices from Phidgets Inc. Phidgets are small additional devices that you can connect to your computer and with which you can then input or output data. For example you can connect a small motor, a temperature sensor, a humidity sensor, a gyroscope or a small LED display and exchange data with these devices.

In this example, I would like to introduce you to three different Phidgets and how we can read data from these Phidgets in FileMaker. Our three phidgets are a light sensor, a temperature sensor and a slider. In our example, the phidgets are all connected together to a hub. This hub has various outputs to which the individual Phidgets can be connected. This hub is then connected to the computer with a USB cable. All Phidgets also draw their power via this USB cable. It is also possible to purchase a wireless HUB. In this case, a separate power supply is required.

I would now like to show you how to retrieve the Phidget data via FileMaker. First of all, we need to load the Phidget libary. To be able to load the libary, we first have to download it. You can find the appropriate download here.

If you do not save the installation in a separate location, the standard paths to the library look like this:

Windows: C:\Program Files\Phidgets\Phidget22\phidget22.dll
Mac: /Library/Frameworks/Phidget22.framework
Linux: libphidget22.so

We then pass this path to the Phidget.Load function, which loads the library for us. Now we can create an instance of the light sensor using the Phidget.Create function. Here we specify the appropriate type of phidget from the list of types. In our case LightSensor.

Set Variable [ $$phidget ; Value: MBS( "Phidget.Create"; "LightSensor" ) ] 

Now we can set script triggers that refer to a script when a certain event comes from the Phidget. To do this, we call the function Phidget.SetScriptTrigger. First we specify the reference that we received from Phidget.Create. Then we specify the event for which the following script is to be called. In our case, we now want to handle the Attach and Detach events. There are events that exist for all Phidget like Attach and Detach, but the sensors can also have their own events, just as the light sensor has an IlluminanceChange event, but we will come to this in a moment. But first, let's define the scripts that should be called when a Phidget is connected. In this case, the Attach script should be called which is in the same file as the currently running script. If the Phidget is removed again, the Detached script is called. We will look at these scripts in a moment.

 Set Variable [ $r ; Value: MBS( "Phidget.SetScriptTrigger"; $$phidget; "Attach"; Get(FileName); "Attached_Light" ) ] 
 Set Variable [ $r ; Value: MBS( "Phidget.SetScriptTrigger"; $$phidget; "Detach_Light"; Get(FileName); "Detached" ) ] 
 

After we have set the script triggers we can now open the Phidget channel to receive data. When it is ready it will send us an attach event.

Set Variable [ $r ; Value: MBS( "Phidget.Open"; $$phidget ) ] 
 

Let's take a look at the attached script that is then called. When the Attach event fires, we get a JSON with information back. We first get this via Get(Scriptparameter) so that we can read it out. This is what such a JSON looks like:

{
	"ID":	"93005",
	"Tag":	"",
	"ScriptName":	"IlluminanceChange",
	"FileName":	"Phidget Light Sensor",
	"Trigger":	"IlluminanceChange",
	"illuminance":	595.57
}

We can now read the Phidget ID from this JSON. We need this to set and read the settings of the phidget.

Set Variable [ $parameter ; Value: Get(ScriptParameter) ] 
Set Variable [ $phidget ; Value: JSONGetElement ( $parameter ; "ID" ) ] 

We first set the setting with which the speed of data transmission is set. We set the data interval to 2000. Then we actually come to the most important point of the solution, because we have to specify the script that is called when the value we get from the sensor has changed. To do this, we use the function "Phidget.SetScriptTrigger" again, specify the Phidget ID to identify the Phidget, the IlluminanceChange event and specify the script "IlluminanceChange", which is in the same file, as the script to be called.

Set Variable [ $r ; Value: MBS( "Phidget.SetProperty"; $phidget; "DataInterval"; 2000 ) ] 
Set Variable [ $r ; Value: MBS( "Phidget.SetScriptTrigger"; $phidget; "IlluminanceChange"; Get(FileName); "Illuminance_Change" ) ] 

We'll see what the script does in a moment. The information we want now is the minimum and maximum value that our sensor can measure. To do this, we use the function "Phidget.GetProperty" to enter the ID in the parameters again and specify that we want the value MaxIlluminance to obtain the maximum value. For the minimum value for the light sensor, we specify MinIlluminance. To get the current value, we enter Illuminance here. Further values can also be determined using this function. The values to be determined depend on the sensor type. Please take a look at our documentation or the Phidgets documentation.

Set Field [ DoorTen::LightIntensityMax ; MBS("Phidget.GetProperty"; $phidget; "MaxIlluminance") ] 
Set Field [ DoorTen::LightIntensityMin ; MBS("Phidget.GetProperty"; $phidget; "MinIlluminance") ] 
Set Field [ DoorTen::Lightintensity ; MBS("Phidget.GetProperty"; $phidget; "Illuminance") ] 

We have already talked about the most important script: IlluminanceChange, which we call when the incoming value changes. This script also receives a JSON with data from our event that we can read out. With the illuminance key, we get the new brightness value that we can write back into the field.

Set Variable [ $json ; Value: Get(ScriptParameter) ] 
Set Field [ DoorTen::Lightintensity ; JSONGetElement ( $json ; "illuminance" ) ] 

Finally, if the Phidget is no longer to provide us with data, we must close the Phidget. You can see how this works in Script Close. The Phidget is closed with the Phidget.Close function and the memory space is released again with Phidget.Release.

This allows you to use your light sensor. It now works in a similar way with the other sensors, except that you have to specify different types, different events and different JSON keys.


The slider has another special characteristic. The slider is actually an adjustable resistor and we look at how much voltage passes through the resistor at a certain time interval. To do this, we need to know the channel on which the phidget is connected. We set this when we open the phidget in the open script.

...
Set Variable [ $$phidget ; Value: MBS( "Phidget.Create"; "VoltageInput" ) ] 
# 
# set where to find the voltage input, in our case on first port of a hub
Set Variable [ $r ; Value: MBS( "Phidget.SetProperty"; $$phidget; "Channel"; 0 ) ] 
Set Variable [ $r ; Value: MBS( "Phidget.SetProperty"; $$phidget; "HubPort"; 0 ) ] 
Set Variable [ $r ; Value: MBS( "Phidget.SetProperty"; $$phidget; "IsHubPortDevice"; 1 ) ] 
# 
Set Variable [ $r ; Value: MBS( "Phidget.SetScriptTrigger"; $$phidget; "Attach"; Get(FileName); "Attach_Slider" ) ] 
Set Variable [ $r ; Value: MBS( "Phidget.SetScriptTrigger"; $$phidget; "Detach"; Get(FileName); "Detached" ) ] 
...

In addition, we define a property in the attach script that specifies how often we fetch the value

Set Variable [ $r ; Value: MBS( "Phidget.SetProperty"; $phidget; "VoltageChangeTrigger"; ,1 ) ] 

Now you can realize great projects with your Phidgets. If you have realized a project with the Phidgets, please let us know what you have created. We are always happy to get feedback. Who knows, maybe Santa Claus will be driving his sleigh with Phidgets next year.


Monkeybread Software Logo with Monkey with Santa hat
9 👈 10 of 24 👉 11
Claris FileMaker Plugin
10 12 23 - 08:55