Azure IoT Hub
TsgcIoTAzure_MQTT_Client — MQTT 3.1.1 client tuned for Azure IoT Hub with SAS-token and X.509 authentication.
TsgcIoTAzure_MQTT_Client — MQTT 3.1.1 client tuned for Azure IoT Hub with SAS-token and X.509 authentication.
IoT Hub is a managed service, hosted in the cloud, that acts as a central message hub for bi-directional communication between your IoT application and the devices it manages. You can use Azure IoT Hub to build IoT solutions with reliable and secure communications between millions of IoT devices and a cloud-hosted solution backend. You can connect virtually any device to IoT Hub.
TsgcIoTAzure_MQTT_Client| Standards & specs | Azure IoT — MQTT support · Azure IoT — device-to-cloud |
| Component class | TsgcIoTAzure_MQTT_Client (unit sgcIoT_Azure_MQTT_Client) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
Active | Published or public property used to configure or query the component. |
BoundPort | Published or public property used to configure or query the component. |
BoundPortMax | Published or public property used to configure or query the component. |
BoundPortMin | Published or public property used to configure or query the component. |
MQTTHeartBeat | Published or public property used to configure or query the component. |
WatchDog | Published or public property used to configure or query the component. |
OnMQTTSubscribe | Published or public property used to configure or query the component. |
OnMQTTUnSubscribe | Published or public property used to configure or query the component. |
Certificate | Published or public property used to configure or query the component. |
SAS | Published or public property used to configure or query the component. |
The principal public methods exposed by the component.
SendAndWait_DeviceToCloud() | Public function exposed by the component. |
PublishAndWait() | Public function exposed by the component. |
ReadConnectionString() | Public procedure exposed by the component. |
Subscribe_CloudToDevice() | Public function exposed by the component. |
UnSubscribe_CloudToDevice() | Public function exposed by the component. |
Send_DeviceToCloud() | Public function exposed by the component. |
Subscribe_DeviceTwins() | Public function exposed by the component. |
UnSubscribe_DeviceTwins() | Public function exposed by the component. |
Subscribe_DirectMethod() | Public function exposed by the component. |
UnSubscribe_DirectMethod() | Public function exposed by the component. |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical Connect to Azure IoT Hub configuration sourced from the online help.
oClient := TsgcIoTAzure_MQTT_Client.Create(nil); oClient.Azure.IoTHub := 'youriothub.azure-devices.net'; oClient.Azure.DeviceId := 'YourDeviceId'; oClient.SAS.Enabled := True; oClient.SAS.SecretKey := 'YourSecretKey'; oClient.OnMQTTConnect := OnMQTTConnectEvent; oClient.Active := True; procedure OnMQTTConnect(Connection: TsgcWSConnection; const Session: Boolean; const ReturnCode: TmqttConnReturnCode); begin ShowMessage('Connected to Azure IoT Hub'); end;
TsgcIoTAzure_MQTT_Client *oClient = new TsgcIoTAzure_MQTT_Client(); oClient->Azure->IoTHub = "youriothub.azure-devices.net"; oClient->Azure->DeviceId = "YourDeviceId"; oClient->SAS->Enabled = true; oClient->SAS->SecretKey = "YourSecretKey"; oClient->OnMQTTConnect = OnMQTTConnectEvent; oClient->Active = true; private void OnMQTTConnect(TsgcWSConnection *Connection, const bool Session, const TmqttConnReturnCode *ReturnCode) { ShowMessage("Connected to Azure IoT Hub"); }
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
Azure IoT allows you to register devices from code using DPS. Currently, the library supports registering a device passing the Scope Id and Registration Id as parameters.
oClient := TsgcIoTAzure_MQTT_Client.Create(nil); Try oClient.Certificate.CertFile := 'cert.pem'; oClient.Certificate.KeyFile := 'key.pem'; oClient.Certificate.Enabled := True; oResponse := TsgcIoT_Azure_OperationRegistrationState.Create; Try if oClient.ProvisioningDeviceClient_Register('scope_id', 'registration_id', oResponse) then ShowMessage('#Provisioning Register OK: ' + oResponse.Status) else ShowMessage('#Provisioning Register Error: ' + oResponse.Status); Finally FreeAndNil(oResponse); End; Finally FreeAndNil(oClient); End;
TsgcIoTAzure_MQTT_Client* oClient = new TsgcIoTAzure_MQTT_Client(NULL); try { oClient->Certificate->CertFile = L"cert.pem"; oClient->Certificate->KeyFile = L"key.pem"; oClient->Certificate->Enabled = true; TsgcIoT_Azure_OperationRegistrationState* oResponse = new TsgcIoT_Azure_OperationRegistrationState(); try { if (oClient->ProvisioningDeviceClient_Register(L"scope_id", L"registration_id", oResponse)) ShowMessage(L"#Provisioning Register OK: " + oResponse->Status); else ShowMessage(L"#Provisioning Register Error: " + oResponse->Status); } __finally { delete oResponse; } } __finally { delete oClient; }
TsgcIoTAzure_MQTT_Client oClient = new TsgcIoTAzure_MQTT_Client(); oClient.Certificate.CertFile = "cert.pem"; oClient.Certificate.KeyFile = "key.pem"; oClient.Certificate.Enabled = true; TsgcIoT_Azure_OperationRegistrationState oResponse = new TsgcIoT_Azure_OperationRegistrationState(); if (oClient.ProvisioningDeviceClient_Register("scope_id", "registration_id", oResponse)) MessageBox.Show("#Provisioning Register OK: " + oResponse.Status); else MessageBox.Show("#Provisioning Register Error: " + oResponse.Status);
IoT hub facilitates file uploads from connected devices by providing them with shared access signature (SAS) URIs or X509 certificates.
procedure UploadFileToAzure; begin oDialog := TOpenDialog.Create(nil); Try if oDialog.Execute then AzureIoT.UploadFile(oDialog.FileName); Finally oDialog.Free; End; end;
void UploadFileToAzure() { TOpenDialog* oDialog = new TOpenDialog(NULL); try { if (oDialog->Execute()) { AnsiString fileName = oDialog->FileName; AzureIoT::UploadFile(fileName.c_str()); } } __finally { oDialog->Free(); } }
void UploadFileToAzure() { TOpenDialog oDialog = new TOpenDialog(); if (oDialog.Execute()) { AzureIoT.UploadFile(oDialog.FileName); } }
IoT Hub provides three options for device apps to expose functionality to a back-end app:
oClient.Subscribe_DirectMethod;
oClient->Subscribe_DirectMethod();
When sending information from the device app to the solution back end, IoT Hub exposes the following options:
oClient.Send_DeviceToCloud('{"temp": 10}', azuIoTQoS1);
oClient->Send_DeviceToCloud("{\"temp\": 10}", azuIoTQoS1);
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\10.IoT_Clients\AzureWindowsService