# Using Soracom Beam to Send Data to ClearBlade IoT Core

Forward device data to ClearBlade IoT Core.

> [!WARNING]
>
> **Before getting started, you will need the following**:
>
> - A Soracom account and a registered IoT SIM
> - A device with a network connection, such as a Raspberry Pi and a Soracom Onyx LTE USB dongle
> - A ClearBlade account and a ClearBlade project

## Step 1: Prepare ClearBlade IoT Core

### Create a Registry

1. Create a registry.

   ![Image showing the steps](https://docs.soracom.io/_astro/add_registry.BBtncvmy_Z1uKcq3.webp)

   > [!WARNING]
   >
   > You can also use an existing registry.
   >
   > For more details on how to operate ClearBlade IoT Core, refer to [Creating registries and devices](https://clearblade.atlassian.net/wiki/x/tABDgw).

2. Access the [ClearBlade IoT Core console](https://iot.clearblade.com/) and click **＋ADD REGISTRY** → **Create Registry**.

   ![Image showing the steps](https://docs.soracom.io/_astro/create_.a_registrypng.BkiszCKm_1sVNfR.webp)

   The "Create a registry" screen will appear.

   | Actions | Description |
   | - | - |
   | **Registry properties** → **Registry ID** | Enter any Registry ID. From here onwards, it will be represented as `${registry_id}` (e.g., `sample`). (\*1) |
   | **Registry properties** → **Region** | Select a region. From here onwards, it will be represented as `${region}` (e.g., `asia-east1`). (\*1) |
   | **Cloud Pub/Sub topics** → **Select a Cloud Pub/Sub topic** | Select a Google Cloud Pub/Sub topic. To create a topic, use the [Google Cloud Pub/Sub Topic screen](https://console.cloud.google.com/cloudpubsub). |

   (\*1) Cannot be changed after creation.

3. Click **SHOW ADVANCED OPTIONS**.

4. Set the following items.

   | Actions | Description |
   | - | - |
   | Protocols → MQTT | **Check** this option. |
   | Protocols → HTTP | **Uncheck** this option. |

   ![Image showing MQTT and HTTP settings](https://docs.soracom.io/_astro/MQTT_and_HTTP_settings.HjLCyJ9h_Z1uuJQy.webp)

5. Click **CREATE**.

### Create a Secret/Public Key Pair

To connect a device on ClearBlade IoT Core, you will need to create a secret/public key pair. For Beam, it supports RS256 and ES256 as encryption algorithms for the keys. Below is an example of creating a secret/public key pair using the `openssl` command for RS256. In the following steps, the public key (e.g., `rsa_public.pem`) will be uploaded to ClearBlade IoT Core, and the private key (e.g., `rsa_private.pem`) will be uploaded to Soracom.

1. Create the secret key.

   ```bash
   openssl genpkey -algorithm RSA -out rsa_private.pem -pkeyopt rsa_keygen_bits:2048
   ```

   A secret key (e.g., `rsa_private.pem`) will be created.

2. Create the public key.

   ```bash
   openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem
   ```

   A public key corresponding to the secret key (e.g., `rsa_public.pem`) will be created.

### Upload the Public Key to ClearBlade IoT Core and Register a Device

1. Access the [ClearBlade IoT Core console](https://iot.clearblade.com/), then click **+ ADD REGISTRY** to create a device.

2. Click **Devices** → **+** in order.

   ![Image showing Devices tab and add button](https://docs.soracom.io/_astro/device-tab-add-button.D8M_V0eK_ZScizQ.webp)

3. Enter any **Device ID**.

   The device will be represented as `${device_id}` (e.g., `beam-device-01`) from here onwards.

4. Click **COMMUNICATION, LOGGING, AUTHENTICATION**.

5. Set the following items in **Authentication (optional)**.

   | Item | Actions |
   | - | - |
   | Input method | Select "Enter manually". |
   | Public key format | Select the public key algorithm created from the pair of secret/public keys (RS256). |
   | Public key value | Paste the content of the public key (e.g., `rsa_public.pem`) created from the secret/public key pair. |

   ![Image showing the Registry selection](https://docs.soracom.io/_astro/authentication.D093NRgF_fYrlq.webp)

6. Click **SUBMIT**.

## Step 2: Set up Soracom Beam

Set the MQTT entry point for Beam.

### Register Authentication Information to Google Cloud IoT Core

To connect Beam with ClearBlade IoT Core, you need to register the authentication information you created earlier (secret key) in the User Console. Click on your username, Security, then CREDENTIALS. For more details on how to use authentication information storage, refer to this guide.

Enter the following information:

| Item | Actions |
| - | - |
| CONFIGURATION NAME | Enter any authentication ID. For example, `Google-Cloud-IoT-Core-credential`. |
| PROJECT ID | Enter project ID (e.g., `sample-project`). |
| REGION | Enter the region ID (e.g., `asia-east1`). |
| REGISTRY ID | Enter the registry ID (e.g., `sample`). |
| DEVICE ID | Enter the device ID (e.g., `beam-device-01`). |
| ALGORITHM | Select the encryption algorithm corresponding to the secret/public key pair you created (e.g., RS256). |
| PRIVATE KEY | Paste the content of the private key file (e.g., `rsa_private.pem`). |

### Set up the MQTT Entry Point for Beam

1. In the SIM group settings, select **Soracom Beam Settings**.

   For more details on how to configure a group in Beam, refer to [Group Settings](https://docs.soracom.io/en/services/groups/settings).

2. Click **MQTT entry point**.

3. Set the following.

   | Item | Directions |
   | - | - |
   | CONFIGURATION NAME | Insert a name of your choice |
   | PORT NUMBER | 1883 |
   | HOST NAME | Enter the FQDN (Fully Qualified Domain Name) of the host, e.g., `asia-east1-mqtt.clearblade.com`. For details on the MQTT host in ClearBlade IoT Core, refer to [Retargeting devices](https://clearblade.atlassian.net/wiki/spaces/IC/pages/2210299905/Targeting+devices) |
   | PORT NUMBER | Enter port number `8883` |
   | CREDENTIALS SET | Select the authentication information you registered earlier |
   | Append IMSI to topic | Disable the IMSI option |
   | PLATFORM VERSION | Select the platform version `201912` |

   ![Image showing MQTT settings](https://docs.soracom.io/_astro/mqtt-beam-entry-point.DdV85H8B_22bISu.webp)

4. Click **Save**.

You can now configure Beam settings for your IoT SIMs.

## Step 3: Send Data to ClearBlade IoT Core Using Beam

Here, we will use `mosquitto_pub`, an MQTT message broker provided as open-source software, to publish data.

1. **Install mosquitto-clients**

   Download and install `mosquitto-clients` [here](http://mosquitto.org/download/).

   For Raspberry Pi, you can install it with the following command:

   ```bash
   sudo apt install mosquitto-clients
   ```

   Run the following command on a device that belongs to the IoT SIM in the group settings to send data.

   For example, if the Device ID is beam-device-01, the command is:

   ```bash
   mosquitto_pub -h beam.soracom.io -t /devices/beam-device-01/events -m "Hello, World"
   ```

   **Example output**:

   ```text
   Client (null) sending CONNECT
   Client (null) received CONNACK (0)
   Client (null) sending PUBLISH (d0, q0, r0, m1, '/devices/beam-device-01/events', ... (12 bytes)
   Client (null) sending DISCONNECT
   ```

   The data will be sent via Beam, and it will be forwarded to ClearBlade IoT Core.

   Next, confirm that the data was successfully transmitted.

2. **Access ClearBlade Cloud Console**

   Go to the console and click **Registry** → **Telemetry Pub/Sub Topics** to check the data transmission.

   ![ClearBlade Cloud Console showing Registry](https://docs.soracom.io/_astro/registry_projects.CC8PgXK0_dhBfu.webp)

3. Google Cloud Pub/Sub will display the topic.

   Click on **Subscription**.

4. Click on **Message → PULL** to retrieve the sent data.

   The data you sent will appear, for example:

   - Device ID: `device01`
   - Message: "Hello, World"

## Step 4: Receive Data from ClearBlade IoT Core on Your Device

1. Run the following command on the device that belongs to the IoT SIM in the group settings.

   For example, if the **Device ID** is `beam-device-01`, the command is:

   ```bash
   mosquitto_sub -d -h beam.soracom.io -t /devices/beam-device-01/commands/#
   ```

   **Example output**:

   ```text
   Client (null) sending CONNECT
   Client (null) received CONNACK (0)
   Client (null) sending SUBSCRIBE (Mid: 1, Topic: /devices/beam-device-01/commands/#, QoS: 0)
   Client (null) received SUBACK
   Subscribed (mid: 1): 0
   ```

2. **Access ClearBlade Cloud console**

   Go to the console, and click **Registry** to view the device.

3. Click **Devices → beam-device-01** and view the device details.

   ![Send command button in ClearBlade Console](https://docs.soracom.io/_astro/device_details.Bbc1SHnY_Z11Ok32.webp)

4. Click the **⋮ → Send command** button.

   ![Send command button in ClearBlade Console](https://docs.soracom.io/_astro/send_command.D7heiccQ_Z1z5myh.webp)

5. In the **Send command** dialog:

   - Set **Format** to **Text**.
   - Enter the command in **Command data**, for example: `Hello!`.
   - Click **SEND COMMAND**.

   ![Send command dialog](https://docs.soracom.io/_astro/send_text.CSZPKtmF_jDBdO.webp)

   The device receives the message:

   ```text
   Client (null) received PUBLISH (d0, q0, r0, m0, '/devices/beam-device-01/commands', ... (6 bytes))
   Hello!
   ```

   If the device is not receiving messages, the following feedback will be displayed repeatedly:

   ```text
   Client (null) sending PINGREQ
   Client (null) received PINGRESP
   Client (null) sending PINGREQ
   Client (null) received PINGRESP
   ...
   ```
