Tutorial: Building a ThingConnect IoT Application

From IoT
Jump to: navigation, search

Go Up to ThingConnect


This tutorial provides detailed steps for creating a new ThingConnect IoT (Internet of Things) application to connect to a Bluetooth LE device, and retrieve the data from its services.

To build a ThingConnect application, you need the following components:

  • The TBluetoothDeviceDiscoveryManager component is used to set up the connection with the device.
  • The corresponding ThingConnect device component is used to retrieve the services and the characteristics data from the device.
Note: The ThingConnect Device and the TBluetoothDeviceDiscoveryManager components are not available in the Tool Palette. You can download the ThingConnect components you want to use in your application through the GetIt Package Manager that is accessible via Tools > GetIt Package Manager. When you install a ThingConnect component through the GetIt Package Manager, the TBluetoothDeviceDiscoveryManager component is installed as well.

This tutorial uses the TPolarHeartRateMonitor component to connect to the Polar H7 Heart Rate Sensor and retrieves the body location and the current heart rate values. By using this tutorial, you can:

  • Connect to the Polar H7 heart rate sensor.
  • Start monitoring the Heart Rate service:
  • Stop monitoring the service characteristics (unsubscribing from the data updates).
  • Disconnect from the Polar H7 heart rate sensor.

Creating a ThingConnect Application

  1. Check that you have the Polar H7 Heart Rate Monitor component installed in the IDE. To do so:
    1. Go to Tools > GetIt Package Manager.
    2. Navigate to the Internet of Things category.
    3. Search for the Polar H7 Heart Rate Monitor component. If it is not installed in your IDE, click the Install button.
  2. Create a new Multi-Device Application.
    • For Delphi: Open File > New > Multi-Device Application - Delphi
    • For C++: Open File > New > Multi-Device Application - C++Builder
  3. In the Tool Palette, search for a TBluetoothDeviceDiscoveryManager component and drop it on the form.
  4. In the Tool Palette, drop a TPolarHeartRateMonitor component on the form.
  5. In the Object Inspector, configure the TPolarHeartRateMonitor parameters (see Working with ThingConnect Devices and Using Bluetooth LE IoT framework).
    • DiscoveryManager: BluetoothDeviceDiscoveryManager1
    • DeviceName: Polar H7 6E0C6C1D
      Note: The DeviceName corresponds to the name of your own device. For the Polar H7 Heart Rate Monitor, the DeviceName corresponds to the ID printed on the back of the device:
      PolarH7ID.png
ThingConnectApp1.png

Creating the Application UI

  1. Drop a TMemo component to the form.
  2. Drop a TLabel component to the form. In the Object Inspector:
    • Set the Align property to Top.
    • Set the Text property to Polar H7 Heart Rate Monitor.
  3. Drop a TPanel component to the form. In the Object Inspector:
    • Set the Align property to Top.
  4. Drop a TListBox component to the form. In the Object Inspector:
    • Set the Align property to Client.
  5. Add three list box items. Right-click in the TListBox1, and select Add TListBoxItem.
  6. Change the name of the TListBoxItems.
    • In the Object Inspector:
      • Set the Name property to BPM, Location and Status.
      • Clean the Text properties
      • .
  7. Drop three TLabel components to the form. In the Object Inspector, change the following properties of each label:
    Name Text
    First label lblBPM XX bpm
    Second label lblBodyLocation Body location
    Third label lblContactStatus Contact status
  8. In the Structure View:
    • Drag the lblBPM and drop it in the BPM list item.
    • Drag the lblBodyLocation and drop it in the Location list item.
    • Drag the lblContactStatus and drop it in the Status list item.
    • Set the Align property of the labels to Client.
  9. Add four TButtons to the Panel1. In the Object Inspector, set the following properties of each button:
    Name Text
    First button ButtonConnect Connect
    Second button ButtonDisconnect Disconnect
    Third button ButtonStartMonitor Start Monitoring
    Forth button ButtonStopMonitor Stop Monitoring
ThingConnectApp2.png

Connecting to and Disconnecting from the Polar H7 Heart Rate Sensor

  1. Create the OnClick event of the ButtonConnect component (double-click on it) and add the following code:
    • For Delphi:
    procedure TForm1.ButtonConnectClick(Sender: TObject);
    begin
    {$IF Defined(WIN32) or Defined(WIN64)}
      BluetoothDeviceDiscoveryManager1.DiscoveryMethod := TDiscoveryMethod.Connect;
    {$ELSE}
      BluetoothDeviceDiscoveryManager1.DiscoveryMethod := TDiscoveryMethod.ScanResponse;
    {$ENDIF}
      BluetoothDeviceDiscoveryManager1.DiscoverDevices;
      Memo1.Lines.Add('Connecting...');
    end;
    • For C++:
    void __fastcall TForm1::ButtonConnectClick(TObject *Sender) {
    #ifdef _Windows
    	BluetoothDeviceDiscoveryManager1->DiscoveryMethod =  TDiscoveryMethod::Connect;
    #else
    	BluetoothDeviceDiscoveryManager1->DiscoveryMethod =  TDiscoveryMethod::ScanResponse;
    #endif
    	BluetoothDeviceDiscoveryManager1->DiscoverDevices();
    	Memo1->Lines->Add("Connecting...");
    }
  2. Add Iot.Family.BluetoothLE.GattTypes to the uses classes.
  3. Create the OnDeviceConnected event of the PolarHeartRateMonitor1 component (double-click on it) and add the following code:
    • For Delphi:
    procedure TForm1.PolarHeartRateMonitor1DeviceFound;
    begin
      Memo1.Lines.Add('New Polar H7 device found');
      ButtonConnect.Text := 'Connected';
      ButtonConnect.Enabled := False;
      ButtonDisconnect.Enabled := True;
      ButtonStartMonitor.Enabled := True;
      ButtonStopMonitor.Enabled := False;
      lblContactStatus.Text := 'Contact status: Connected';
    end;
    • For C++:
    void __fastcall TForm1::PolarHeartRateMonitor1DeviceFound()
    {
    	Memo1->Lines->Add("New Polar H7 device found");
    	ButtonConnect->Text = "Connected";
    	ButtonConnect->Enabled = False;
    	ButtonDisconnect->Enabled = True;
    	ButtonStartMonitor->Enabled = True;
    	ButtonStopMonitor->Enabled = False;
    	lblContactStatus->Text = "Conctact status: Connected";
    }
  4. Create the OnClick event of the ButtonDisconnect component (double-click on it) and add the following code:
    • For Delphi:
    procedure TForm1.ButtonDisconnectClick(Sender: TObject);
    begin
    	PolarHeartRateMonitor1.Disconnect;
    end;
    • For C++:
    void __fastcall TForm1::ButtonDisconnectClick(TObject *Sender)
    {
    	PolarHeartRateMonitor1->Disconnect();
    }
  5. Create the OnDeviceDisconnect event of the PolarHeartRateMonitor1 component (double-click on it in the Events tab) and add the following code:
    • For Delphi:
     procedure TForm1.PolarHeartRateMonitor1DeviceDisconnect (const Sender: TCustomGeneratedBluetoothLEComponent);
    begin
      Memo1.Lines.Add('Polar H7 device disconnected');
      ButtonConnect.Text := 'Connect';
      ButtonConnect.Enabled := True;
      ButtonDisconnect.Enabled := False;
      ButtonStartMonitor.Enabled := False;
      ButtonStopMonitor.Enabled := False;
      lblContactStatus.Text := 'Contact status: Disconnected';
    end;
    • For C++:
    void __fastcall TForm1::PolarHeartRateMonitor1DeviceDisconnect (TCustomGeneratedBluetoothLEComponent * const Sender)
    {
    	Memo1->Lines->Add("Polar H7 device disconnected");
    	ButtonConnect->Text = "Connect";
    	ButtonConnect->Enabled = True;
    	ButtonDisconnect->Enabled = False;
    	ButtonStartMonitor->Enabled = False;
    	ButtonStopMonitor->Enabled = False;
    	lblContactStatus->Text = "Contact status: Disconnected";
    }

Receiving Data Updates from the Polar H7 HRM Device

  1. Create the OnClick event of the ButtonStartMonitor component (double-click on it) and add the following code:
    • For Delphi:
    procedure TForm1.ButtonStartMonitorClick(Sender: TObject);
    begin
      // Subscribing to the Service characteristics:
      PolarHeartRateMonitor1.SubscribeHeartRateMeasurement;
      Memo1.Lines.Add('Subscribing to HRM');
      PolarHeartRateMonitor1.RefreshBodySensorLocation;
      Memo1.Lines.Add('Refreshing Body Sensor Location');
      ButtonStartMonitor.Enabled := False;
      ButtonConnect.Enabled := False;
      ButtonDisconnect.Enabled := True;
      ButtonStartMonitor.Enabled := False;
      ButtonStopMonitor.Enabled := True;
    end;
    • For C++:
    void __fastcall TForm1::ButtonStartMonitorClick(TObject *Sender) {
    	// Subscribing to the Service characteristics:
    	PolarHeartRateMonitor1->SubscribeHeartRateMeasurement();
    	Memo1->Lines->Add("Subscribing to HRM");
    	PolarHeartRateMonitor1->RefreshBodySensorLocation();
    	Memo1->Lines->Add("Refreshing Body Sensor Location");
    	ButtonStartMonitor->Enabled = False;
    	ButtonConnect->Enabled = False;
    	ButtonDisconnect->Enabled = True;
    	ButtonStartMonitor->Enabled = False;
    	ButtonStopMonitor->Enabled = True;
    }
  2. Implement the event handler of the OnBodySensorLocationUpdate event:
    • For Delphi:
    procedure TForm1.PolarHeartRateMonitor1BodySensorLocationUpdate(Sender: TObject; const Value: TGattBodySensorLocation);
    begin
      case Value of
        TGattBodySensorLocation.Unread:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Unread';
        TGattBodySensorLocation.Other:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Other';
        TGattBodySensorLocation.Chest:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Chest';
        TGattBodySensorLocation.Wrist:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Wrist';
        TGattBodySensorLocation.Finger:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Finger';
        TGattBodySensorLocation.Hand:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Hand';
        TGattBodySensorLocation.EarLobe:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': EarLobe';
        TGattBodySensorLocation.Foot:
          lblBodyLocation.Text := lblBodyLocation.Text+ ': Foot';
      end;
      Memo1.Lines.Add('Body sensor location update...');
    end;
    • For C++:
    void __fastcall TForm1::PolarHeartRateMonitor1BodySensorLocationUpdate (TObject *Sender, const TGattBodySensorLocation Value) {
    	switch (Value) {
    	case TGattBodySensorLocation::Unread:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Unread";
    		break;
    	case TGattBodySensorLocation::Other:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Other";
    		break;
    	case TGattBodySensorLocation::Chest:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Chest";
    		break;
    	case TGattBodySensorLocation::Wrist:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Wrist";
    		break;
    	case TGattBodySensorLocation::Finger:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Finger";
    		break;
    	case TGattBodySensorLocation::Hand:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Hand";
    		break;
    	case TGattBodySensorLocation::EarLobe:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": EarLobe";
    		break;
    	case TGattBodySensorLocation::Foot:
    		lblBodyLocation->Text = lblBodyLocation->Text + ": Foot";
    		break;
    	default:
    		lblBodyLocation->Text = lblBodyLocation->Text;
    	}
    	Memo1->Lines->Add("Body sensor location update...");
    }
  3. Implement the event handler of the OnHeartRateMeasurementUpdate event:
    • For Delphi:
    procedure TForm1.PolarHeartRateMonitor1HeartRateMeasurementUpdate (Sender: TObject; const Value: TGattHeartRateMeasurement);
    var
      val: TGattHeartRateMeasurement;
    begin
      val := Value;
      lblBPM.Text := IntToStr(val.HeartRateMeasurement) + 'bpm';
      Memo1.Lines.Add('HRM update...');
    end;
    • For C++:
    void __fastcall TForm1::PolarHeartRateMonitor1HeartRateMeasurementUpdate (TObject *Sender, const TGattHeartRateMeasurement &Value) {
    	TGattHeartRateMeasurement val = Value;
    	lblBPM->Text = IntToStr(val.HeartRateMeasurement) + "bpm";
    	Memo1->Lines->Add("HRM update...");
    }
  4. Create the OnClick event of the ButtonStopMonitor component (double-click on it) and add the following code:
    • For Delphi:
    procedure TForm1.ButtonStopMonitorClick(Sender: TObject);
    begin
      PolarHeartRateMonitor1.UnSubscribeHeartRateMeasurement;
      Memo1.Lines.Add('UnSubscribing from HRM');
      ButtonConnect.Enabled := False;
      ButtonDisconnect.Enabled := True;
      ButtonStartMonitor.Enabled := True;
      ButtonStopMonitor.Enabled := False;
    end;
    • For C++:
    void __fastcall TForm1::ButtonStopMonitorClick(TObject *Sender) {
    	PolarHeartRateMonitor1->UnSubscribeHeartRateMeasurement();
    	Memo1->Lines->Add("UnSubscribing from HRM");
    	ButtonConnect->Enabled = False;
    	ButtonDisconnect->Enabled = True;
    	ButtonStartMonitor->Enabled = True;
    	ButtonStopMonitor->Enabled = False;
    }

Running the Application

To run the application in your device, just click Run or click F9.

  • Click the Connect button to connect to the Polar H7 heart rate monitor.
  • Click the Start Monitoring button to start receiving data updates from the measurements from the device.
  • Click the Stop Monitoring button to stop receiving data updates from the measurements from the device (unsubscribing from the characteristic).
  • Click the Disconnect button to disconnect from the Polar H7 heart rate device.
ThingConnectApp3.png

See Also

Personal tools
In other languages