- 03 Mar 2023
- 7 Minutes to read
- Print
- PDF
GCP Example
- Updated on 03 Mar 2023
- 7 Minutes to read
- Print
- PDF
Google IoT Core
First, log into the Google IoT Core and open the Google Cloud Console. Once, you have logged in, you'll need to create all the resources required for establishing a connection and exchanging messages.
Google IoT Core Configuration
Creating a registry in the Google Cloud Platform
- Step 1: Log into Google IoT Core Platform and create a new registry, as shown in the below image:
Figure 48. IoT Core Panel
- Step 2: Next, fill in the required fields with the relevant characteristics from your Registry:
- Registry ID: In this case, MQTT Client.
- Region: In this example, the location is europe-west1.
Figure 49. Registry Creation Panel
- Protocol: Select the MQTT option.
- Stackdriver Logging: Sets the default logging parameter for the registry.
Figure 50. Registry Configuration Panel
- Client Pub/Sub Topics: Establishes the topic to public/subscribe for telemetry and state events. This section can be updated and changed at any time after creation of the registry.
Figure 51. Edit Registry Panel
The following screenshot shows the defined characteristics:
Figure 52. Registry Detail Panel
At this point, the registry has already been created, so the next step is to establish one or more devices to connect to the N3uron module and exchange messages.
Creating a device in the Google Cloud Platform
Each device requires a private-public key pair to authenticate with IoT Core, which can be defined using the following OpenSSL commands:
openssl genpkey -algorithm RSA -out rsa_private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem
The following screenshot demonstrates how to create a new device,
Figure 53. Create a Device Panel
- Step 1: Click on Create New Device. This will take you to a configuration tab.
- Step 2: During device creation, the public key must be applied in the configuration. The parameters applied in this configuration can be observed below:
- Device ID: In this example, the name of the device is, Test_Device.
Figure 54. Edit Device Panel
- Authentication: Select your preferred way for authentication and you should introduce the key previously generated. In this case, the key is uploaded.
Figure 55. Device Configuration within the create a device panel
- Step 3: Once configuration is finished, click on Create, and the device will be created.
Configuring the N3uron IIoT Platform
Create an MQTT module instance
First, users must create a module for establishing the connection with Google Cloud. In this example, for the MQTT Client module, the following steps should be taken:
- Step 1: Follow this route, System->Config->Modules->Modules->Model->New Module
Figure 56. How to create an instance using N3uron's MQTT Module Panel
- Step 2: Configure the basic parameters of the module. Name= MqttClient, Module type= MqttClient.
Figure 57. Module configuration within the N3uron MQTT module panel
Before moving onto the next step, it is mandatory to save the default Logger and API settings. At this point, all other parameters on the main screen will be set to default.
- Step 3: Click on the MQTT Module and create a new connection (client) by following Module name ->Model ->New connection.
Figure 58. New connection creation via N3uron's MQTT Module panel
- Step 4: Establish a name for the connection and continue configuring the basic parameters or the connection. In this case, the name of the connection is Google.
Figure 59. How to configure connections within N3uron's MQTT Module panel
The MQTT Client Module defined in N3uron can connect with Google Cloud using these configuration settings:
- Destination broker: Google IoT Core.
- Authentication Mode: JSON Web Tokens.
- Token duration: The maximum value is a full day, so any value equal or less than this is valid.
- Algorithm: RS256 to sign the JWT(there are a wide variety of options to choose from).
- Private key: The generated key in .pem format stored in the device.
- Audience: Sets the project_id generated in GCP. In this case, isentropic-card-332311.
- Protocol: MQTTS (MQTT can also be used)
- Host: Specifies the URL for the MQTT broker, in this case, mqtt.googleapis.com.
- Port: The port is 8883.
- Client ID: projects/PROJECT ID/locations/CLOUD REGION/registries/REGISTRY ID/devices/DEVICE ID
- QoS: 0 or 1 (QoS 2 is not supported and will cause the connection to be dropped)
The below screenshot demonstrates this example:
Figure 60. Example of the MQTT Client panel
Figure 61. Example of the MQTT Client panel
The MQTT module has now been created and is ready to use, Therefore, it is now time to generate a publication and a subscription.
Publishing & Subscribing Data via N3uron and Google Cloud Platforms
Publishing Data Using N3uron's MQTT Module Within the WebUI Explorer Panel
- Step 1: Generating a publication is very easy. Simply click on new publisher in the agents section of the MQTT module, as shown in the screenshot.
Figure 62. New Publisher option within the N3uron MQTT module panel
- Step 2: Provide a name. In this case, a very simple name has been used: Publisher.
Figure 63. Publisher configuration settings within the N3uron MQTT model panel
The device is created with an MQTT client and is connected to the MQTT bridge. You can now publish telemetry events to a topic with the following format: /devices/Device_ID/events
Messages sent to this topic are redirected to the topic that has been set for telemetry by default in the registry. The default telemetry topic is the Cloud Pub/Sub topic defined in the eventNotificationConfigs[i].pubsubTopicName field of the registry. If no default topic is defined, the data will be lost. To publish messages to other Cloud Pub/Sub topics, see Publishing Telemetry events to additional Cloud Pub/Sub topics.
- Step 3: To finish configuration, users should define the name of the topic within which they want to publish and create a tag filter without constraints. This allows all tags to publish their data in the topic. In this case, the topic used is /devices/Test_Device/events/messages.
Figure 64. Publisher configuration icon within the model area of the MQTT model panel
From now on, all data will be continuously published to the cloud. In order to display this data, users must enter the GCP console, go to the subscriptions section, and create a subscription for the previously generated default topic. Failure to complete this step will result in published data being lost. To do so, users should follow the below steps:
- Step 1: Click on the Subscription section and select Create Subscription.
Figure 65. Subscription panel in the Google Cloud platform
- Step 2: Give a name to the subscription and select the topic you want to subscribe to. In this case, the name of the subscription is subs (although this is not relevant) and the name of the topic subscribed is projects/isentropic-card-332311/topics/n3uron.
Figure 66. Create Subscription Panel in the Google Cloud Platform
- Step 3: Click on Create to finish configuration
You should now enter the created subscription and select pull in the messages section to obtain the published messages. The result should be similar to the screenshot below:
Figure 67. Published messages once subscribed to the topic in the Google Cloud Platform
Subscribing Data within the Google Cloud Platform
The process for generating subscriptions is similar to the publication process. The objective of the subscription is to receive tag events from remote devices via MQTT.
- Step 1: First, create a new subscriber, as previously done for the publisher.
Figure 68. New Subscriber option within N3uron's MQTT module panel
- Step 2: Name it as Subscriber in the subscriber name section.
Figure 69. Subscriber configuration settings in the N3uron Tags Panel
In order to receive a command, the device must:
- Be connected to Google IoT Core using the MQTT protocol
- Subscribe to the topic/devices/Device_ID/commands/# (the # wildcard is necessary). By subscribing to this topic, the device will be able to receive messages sent to /devices/Device_ID/commands/#, as well as those sent to subfolders, /devices/Device_ID/commands/#/{subfolder}. Subscriptions to a specific subfolder are not possible.
- Step 3: In this case, the unique compulsory configuration requires users to introduce the name of the topic from which the data will be obtained. In this case, the topic used is, /devices/Test_Device/commands/#.
A tag must now be created in N3uron, which will be updated according to the events received from the Google Core IoT. Follow the below steps to create this tag:
- Step 1: First click on Config > Tags > Model > New tag and label it with a specific name. In this example, Subscribed_Value is used.
- Step 2: Define the Source section and complete it using the previously defined MQTT Client module.
Figure 70. Tag configuration within the N3uron tags panel
- In Module name, the name used must be exactly the same as the module name.
- In the subscriber section, users must introduce the established connection (in this case Google)/the defined subscriber (in this case Subscriber). In this case, it would be Google/Subscriber.
Once this has been configured, the defined tag will be able to receive events from the Google Cloud.
- Step 1: Go to the GCP console, click on devices, select the device you created and select the send command.
Figure 71. Publication configuration in the Google Cloud Platform
- Step 2: Within the Message, Payload will introduce the following:
{
"/Subscribed_value": [{
"v": 3.14159,
"q": 192,
"ts": 1630668488618
}]
}
- Step 3: Click on the Send Command button.
Figure 72. Send Command panel in the Google Cloud Platform
- Step04: Go back to the N3uron WebUI interface and select Data/Real-Time in the left-hand side panel. You should now see the Subscribed_Value tag you previously created with a value of 3.14159.
Figure 73. Real-Time values in the Real-Time section of the N3uron Navigation column panel