Published on September 25, 2022.
There were a number of great sessions at last week's Xojo Developer Retreat. In fact, I enjoyed and learned a lot from all of the sessions that I attended. But it was in Christian Schmitz's session, where he reviewed the latest updates to his popular MBS Plugins, that I had what you might call a "lightbulb moment."
In that session, Christian mentioned that MBS has support for Phidgets, which he described as USB sensors that can be used to measure things like temperature, humidity, light, PH, and so on. I've been looking for an easy way to measure these types of environmental variables, and so I made a note to look into Phidgets when I returned from the conference.
And that's exactly what I did. Last Thursday, I ordered a few of the Phidgets, including a hub, sensors for temperature, humidity, and light, as well as the cables needed to hook everything up. The total cost of the order came to $115 USD, which included priority shipping.
The Phidgets arrived on Friday, and with the help of the example Xojo projects that come with the MBS Phidgets Plugin, I had a working desktop app in around 30 minutes. The app reads measurements from the three sensor devices, and displays the current temperature, humidity, and illuminance.
Here's an animation showing the application running on a Mac.
Click the image to view a larger version.
I've also tested the app on a Windows 11 machine, and it works perfectly. I haven't had a chance to test it on a Raspberry Pi yet, but I feel confident that it'll run on it as well.
In this blog post, I'll walk through how the app works, share some of the code, and also provide a link to the Xojo project so that you can try it yourself.
As I mentioned above, I initially ordered a hub and a few sensors from the Phidgets Web site. Here's what I ordered.
Click the image to view a larger version.
In order to use the Phidgets, I had to install a library, which was easily obtained from this page: https://www.phidgets.com/docs/Operating_System_Support
As far as the MBS Plugins go, I had previously installed the MBS Xojo Main Plugin. So I only needed to install the MBS Xojo Phidgets Plugin.
Loading the Phidgets library is as simple as adding the following to the application's Open event handler.
Click the image to view a larger version.
The app consists of only one window, on which three label objects have been placed - one for each of the values that will be pulled from the sensors and displayed to the user. The labels are named LabelTemperature, LabelHumidity, and LabelIlluminance.
Here's a screenshot of the window.
Click the image to view a larger version.
In order to use the classes that MBS provides via the Phidgets plugin, you have to create subclasses based on them.
For the temperature sensor, I created a subclass of PhidgetTemperatureSensorMBS, which I named PhidgetTemperatureSensor. I then added a TemperatureChanged event handler, which takes the temperature measured by the device, converts it from degrees Celsius to degrees Fahrenheit, and then updates the value of the corresponding label (LabelTemperature). Here's what the event handler looks like.
Click the image to view a larger version.
I then created similar subclasses for the humidity and light sensors.
Next, I added instances of the subclasses as properties of the app's MainWindow. And finally, in the window's Open event handler, I added code to initialize and open the subclass instances. Here's what that looks like.
Click the image to view a larger version.
And that's all there is to it. If you've been following along, you're ready to run the app!
One of the plugins that MBS provides in its MBS Xojo Complete Plugin Set is a CURL plugin, which provides really nice, really convenient support for integrating with Amazon S3 buckets. The plugin's CURLSMBS class, which includes a SetupAWS method, is particularly helpful, as it eliminates the need to implement signing requests using AWS4-HMAC-SHA256.
So that got me thinking: How difficult would it be to add support for uploading the sensor readings to a file in an S3 bucket?
It turns out that it's not very difficult at all. Here's how I did it.
First, I added a method to the app's window and called it "S3Upload." Let's walk through that method.
I started by creating a dictionary in which the latest sensor values are stored. I've also added a value for the current timestamp. I then convert the dictionary into a JSON-encoded string.
Dim Payload As New Dictionary Payload.Value( "timestamp" ) = DateTime.Now.SQLDateTime Payload.Value( "temperature" ) = ( SensorTemperature.Temperature * 1.8 ) + 32 Payload.Value( "humidity" ) = SensorHumidity.Humidity Payload.Value( "illuminance" ) = SensorLight.Illuminance Dim PayloadJSON As String = GenerateJSON( Payload )
Next, I create an array of HTTP headers that will be used in the API call. One header specifies the file's content-type, while the other is used to set the access control list for the file in such a way that it can be read by anyone.
Dim HTTPHeaders() As String HTTPHeaders.Add( "Content-Type: application/json" ) HTTPHeaders.Add( "x-amz-acl: public-read" )
I then prepare the values that will be needed by the SetupAWS method.
Dim AWSAccessKeyId as String = "*** YOUR KEY ID ***" Dim AWSSecretAccessKey as String = "*** YOUR ACCESS KEY ***" Dim Region as String = "us-east-1" Dim BucketName As String = "*** YOUR BUCKET NAME ***" Dim FileName As String = "environment.json.txt" Dim Service as String = "s3" Dim Path As String = "/" + BucketName + "/" + EncodeURLComponent(Filename) Dim Domain as String Dim Verb as String = "PUT" Dim HashedPayload as String = EncodeHex( SHA256MBS.Hash( PayloadJSON ) )
That's pretty straightforward, but one thing to notice is that the HashedPayload is being set using the SHA256MBS class provided by the MBS Xojo Encryption Plugin. (Which reminds me: You'll need to install that plugin, the MBS Util Plugin, and the MBS Xojo CURL Plugin as well.) The hashed value is then hex-encoded.
Next, I create an instance of the CURLSMBS class, set it's payload, and then use the SetupAWS method to sign the request.
Dim cURL As New CURLSMBS Call cURL.SetInputData( PayloadJSON ) Call cURL.SetupAWS( AWSAccessKeyId, AWSSecretAccessKey, Region, Service, Path, Domain, Verb, HashedPayload, HTTPHeaders )
And finally, here's the code to send the request, and evaluate the response.
Dim cURLError As Integer = cURL.Perform Dim DebugMessage As String = cURL.DebugData Dim OutputData As string = cURL.OutputData Dim HTTPResult As Integer = cURL.GetInfoResponseCode If ( ( cURLError <> 0 ) Or ( HTTPResult <> 200 ) ) Then MsgBox "An error occurred while saving to S3. ( Error: " + cURLError.ToString + ", Status Code: " + HTTPResult.ToString + ")" End If
As far as timing goes, I want the settings to be uploaded when the app starts up, and then every minute afterwards.
To upload the settings when the app starts up, I added some code to the Open event handler of the MainWindow, like this.
Try S3Upload Catch RuntimeException SleepMBS( 1.0 ) S3Upload End Try
Notice that I've wrapped the call to the S3Upload method in a Try / Catch block. I'm doing this because it is likely that the sensors will not have initialized yet. So if an error occurs, the app pauses for a second (using the MBS SleepMBS method), and then tries again.
To upload the settings every minute, I simply added a Timer to the MainWindow. The Timer's Run Mode is set to "Multiple," and its Period is 60000 (which is equivalent to 60 seconds). The Timer's Action simply calls the S3Upload method of the window.
And that's all there is to it. When the app runs, it will upload a JSON-encoded payload to the S3 bucket, which looks like this:
{"timestamp":"2022-09-25 20:12:40","temperature":74.156000000000005912,"humidity":56.929999999999999716,"illuminance":8.5289000000000001478}
And an updated version of the file will be uploaded every minute.
I'm continually amazed at the types of applications that you can build with Xojo, and when you combine it with power and flexibility of the MBS Plugins, things get very interesting. Combine all of that with Phidgets, and you can create some very compelling, and very useful, applications. The app that I've discussed in this post is just one example of that.
I've ordered additional Phidgets that work with RFID tags, detect motion, and more - and I'm looking forward to experimenting with them as well. But more importantly, I'm excited to put all of this to practical use, especially for clients who can benefit from applications that can utilize sensors and controls.
If you'd like to download the Xojo project that I referred to in this post, click here.
Note that you'll need to add your own MBS license information (in the App's Open event hander) and your S3 credentials as well (in the MainWindow's S3Upload method). And remember that you'll need to purchase the Phidgets that I referred to as well.
All of the MBS plugins that I used in this project are available as part of the MBS Xojo Complete Plugin Set.
And finally, I want to thank Christian Schmitz for all of the work that he puts into the MBS Plugins, which makes building applications like this possible. Thanks Christian!
Hello, I'm Tim Dietrich. I develop custom software for businesses that are running on NetSuite, including mobile apps, Web portals, Web APIs, and more.
I'm the developer of several popular NetSuite open source solutions, including the SuiteQL Query Tool, SuiteAPI, and more.
I founded SuiteStep, a NetSuite development studio, to provide custom software and AI solutions - and continue pushing the boundaries of what's possible on the NetSuite platform.
Copyright © 2025 Tim Dietrich.