How to get started in VB.NET with Yoctopuce modules

How to get started in VB.NET with Yoctopuce modules

This week, we add to our "for the beginners" series and we tackle Visual Basic .Net. We are going to write a small application displaying the values of a Yoctopuce sensor.







This post assumes that you already have some knowledge of Visual Basic as its aim is not to be a Visual Basic tutorial, but to explain how to use our modules in an application written in VB.NET. In the same way, if you are not familiar with Yoctopuce modules, we recommend that you start by reading the previous posts of this series, in particular the one on the logical structure of Yoctopuce modules.

Installing the API


In order to use the Yoctopuce library in your project, you must first download it and install it somewhere on your disk. You can download the library from our site on this page. It's a zip file containing the library, some usage examples, and the documentation. The important part is the "Sources" directory, containing the source files in Visual Basic, but also a "dll" subdirectory with the 32 and 64 bit DLLs needed by the VB code. Copy this directory somewhere on your disk and the installation is done. Note that you can also download the library from GitHub if you'd rather use Git.

Creating the Visual Studio project


Note: to illustrate this post, we used Visual Studio 2017, but the principle is the same for all the Visual Studio versions since version 2010.

When you have started Visual Studio, create a new Visual Basic project of type Windows Forms App.

Create a new Visual Basic project of type Windows Forms App
Create a new Visual Basic project of type Windows Forms App



Then you must tell Visual Studio that it must compile and use the Yoctopuce library. For this project, we are going to use the yocto_api.vb and yocto_temperature.vb files, as well as the yapi.dll DLL, located in the "dll" subdirectory. Right-click on your project in the "Solution Explorer" panel and select : Add > Existing Item....

Select the yocto_api.vb and yocto_temperature.vb files in the location where you copied the library sources and use the "Add as link" button. Using the "Add as link" button is significant: it allows you to use the files from their original location rather than copying them into your project. Thus, if you update the Yoctopuce library files, the application automatically uses the new file version when next compiling. You can also simply add the files with the "Add" button, but if you update the Yoctopuce library, don't forget to replace these files with the new ones.

Add the files from the Yoctopuce library
Add the files from the Yoctopuce library



Do the operation again with the yapi.dll file located in the "dll" subdirectory. Note that you need to change the file filter and select "All files (*.*)" instead of "VB Code files...".

When you have added the DLL, select it in the Solution Explorer panel and modify its "Copy to Output Directory" property to "Copy always". The effect is to systematically copy the api.dll DLL in the executable directory when compiling.

Automatically copy the DLL when compiling
Automatically copy the DLL when compiling



We can now write the application itself.

Initializing the API


The first thing that you need to do is to initialize the API with YAPI.RegisterHub. A good time to do so is when loading the Form. Thus, if the API initialization fails, we can display an error message and stop the application.

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim errmsg as String = ""
    If YAPI.RegisterHub("usb", errmsg) <> YAPI_SUCCESS Then
      MessageBox.Show("Init error:" + errmsg)
      Application.Exit()
    End If
  End Sub
End Class



Creating the user interface

We insert two labels: the first one to indicate the name of the detected sensor. The second one to indicate the temperature.

Add two labels
Add two labels



We initialize the label text when loading the Form.

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim errmsg as String = ""
    If YAPI.RegisterHub("usb", errmsg) <> YAPI_SUCCESS Then
      MessageBox.Show("Init error:" + errmsg)
      Application.Exit()
    End If
    label1.Text = "No sensor detected"
    Label2.Text = "N/A"
  End Sub
End Class



Updating the interface with the temperature


In the opposite to a console application, we cannot create an endless loop to read the sensor permanently. Therefore we are going to use a Timer which calls a function with a 10Hz frequency. It's this function which takes care of:

  • Calling YAPI.HandleEvents on a regular basis. In fact, it's not a 100% necessary in our case, but it's good practice to give control to the Yoctopuce API from time to time.
  • Calling YAPI.UpdateDeviceList once every two seconds, which forces the API to re-detect the connection of new modules. It's a rather heavy process, so we avoid doing it too often, hence the two second delay between two calls
  • Detecting the first YTemperature function available with YTemperature.FirstTemperature()
  • If this function is available, reading the temperature and updating the interface.


This gives us the following code:

Public Class Form1
  ...

  Dim sensor As YTemperature = Nothing
  dim hardwaredetect As Integer = 0

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim errmsg As String = ""

    If hardwaredetect = 0 Then
      YAPI.UpdateDeviceList(errmsg)
    End If
    hardwaredetect = (hardwaredetect + 1) Mod 20
    YAPI.HandleEvents(errmsg)
    If sensor Is Nothing Then
      sensor = YTemperature.FirstTemperature()
    End If
    label1.Text = sensor.get_friendlyName()
    label2.Text = Str(sensor.get_currentValue()) + sensor.get_unit()
    End If
  End Sub
End Class



The preceding code works, but if the user disconnects the module, the application crashes. To avoid this problem, you must check that the temperature sensor is still connected with the isOnline() method.

With this modification, the complete code of the Form looks like this:

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim errmsg as String = ""
    If YAPI.RegisterHub("usb", errmsg) <> YAPI_SUCCESS Then
      MessageBox.Show("Init error:" + errmsg)
      Application.Exit()
    End If
    label1.Text = "No sensor detected"
    Label2.Text = "N/A"
    Timer1.Enabled = True
  End Sub

  Dim sensor As YTemperature = Nothing
  dim hardwaredetect As Integer = 0

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim errmsg As String = ""

    If hardwaredetect = 0 Then
      YAPI.UpdateDeviceList(errmsg)
    End If
    hardwaredetect = (hardwaredetect + 1) Mod 20
    YAPI.HandleEvents(errmsg)
    If sensor Is Nothing Then
      sensor = YTemperature.FirstTemperature()
    End If
    If Not sensor Is Nothing Then
      If sensor.isOnline() Then
        label1.Text = sensor.get_friendlyName()
        label2.Text = Str(sensor.get_currentValue()) + sensor.get_unit()
      Else
        label1.Text = "Sensor is offline"
        label2.Text = "OFFLINE"
        sensor = Nothing
      End If
    End If
  End Sub
End Class



When we compile all this, we obtain the expected result, a Windows window displaying the temperature as soon as a Yoctopuce temperature sensor is connected.

It works!
It works!



Note: The complete code of the application is available on GitHub: https://github.com/yoctopuce-examples/vb_beginners

A small improvement


This example manages only temperature sensors, but by modifying two lines only, we can make it work with any Yoctopuce sensor: we only have to use the YSensor class instead of the YTemperature class:

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim errmsg as String = ""
    If YAPI.RegisterHub("usb", errmsg) <> YAPI_SUCCESS Then
      MessageBox.Show("Init error:" + errmsg)
      Application.Exit()
    End If
    label1.Text = "No sensor detected"
    Label2.Text = "N/A"
    Timer1.Enabled = True
  End Sub

  Dim sensor As YSensor = Nothing
  dim hardwaredetect As Integer = 0

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim errmsg As String = ""

    If hardwaredetect = 0 Then
      YAPI.UpdateDeviceList(errmsg)
    End If
    hardwaredetect = (hardwaredetect + 1) Mod 20
    YAPI.HandleEvents(errmsg)
    If sensor Is Nothing Then
      sensor = YSensor.FirstSensor()
    End If
    If Not sensor Is Nothing Then
      If sensor.isOnline() Then
        label1.Text = sensor.get_friendlyName()
        label2.Text = Str(sensor.get_currentValue()) + sensor.get_unit()
      Else
        label1.Text = "Sensor is offline"
        label2.Text = "OFFLINE"
        sensor = Nothing
      End If
    End If
  End Sub
End Class



Now, the application works also with a light sensor, a voltmeter, or any Yoctopuce sensor.

It works with the other sensors as well
It works with the other sensors as well




This "magic trick" works because all the Yoctopuce classes corresponding to sensor-type functions inherit from the YSensor class. You can thus use the YSensor class to access the basic functions of a sensor, such as obtaining the current value or the measuring unit.

Conclusion

We have purposefully kept this code very simple. For example, it manages only one sensor connected by USB. Obviously, the library allows for the management of several sensors, whether connected by USB or connected to a remote YoctoHub.

Now that we have seen the basics, you can write your own applications, with the help of the reference documentation. You can also browse our blog, which is full of examples on how to use our modules. And if with all of that you still can't manage on your own, you can always send an email to support@yoctopuce.com.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.