# Simple Messaging

## Introduction <a href="#introduction" id="introduction"></a>

IBM Cloud is a cloud service that offers, among other things, an IoT platform that lets IoT devices connect and send data that can later be analyzed using IBM cloud services. This IoT platform offers a REST API as well as an MQTT broker interface. In this tutorial we will learn how to set up IBM Cloud and connect to it using our network simulator.

## 1. Sign up with IBM Cloud <a href="#id-1-sign-up-with-ibm-bluemix" id="id-1-sign-up-with-ibm-bluemix"></a>

Before starting, sign up for a free IBM Cloud account at <https://www.ibm.com/cloud>, and then log in to your account. You will be asked to create an organization and a space: follow the instructions and then you will be taken to your account’s dashboard. Click **Catalog** in the top menu, then the **Internet of Things** category in the left menu, and then **Internet of Things Platform**:

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiORZDvL6DAwRp3Efye%2F-LiOYweN5UBJxwH_oKg1%2FScreenshot%20from%202019-06-27%2017-28-48.png?alt=media\&token=fa1f65aa-27c7-47c5-a197-2c3b19d4ba7b)

Now you have to create a service: insert a name for the service (for example: IoTIFY) and then click **Create**; then, on the welcome page click **Launch**. You are now taken to the IBM Watson IoT Platform dashboard.

## 2. Create a Device in IBM Cloud <a href="#id-2-create-a-device-in-bluemix" id="id-2-create-a-device-in-bluemix"></a>

Once the service is launched, the next step is to create a device type: on the left menu, click **Devices**, activate the **Device Types** tab, and then click the **Add Device Type** button; on the next screen, enter a name for your type and click **Next**. You can optionally define template data or add metadata to the device type, but this is not mandatory, and you can simply skip these steps when creating the device type. Click **Finish** to complete the device type registration.

Now you can go to the **Browse** tab, click on **Add Device**; in the **Device Type** field, select the previously created device type from the drop-down menu. The only mandatory data to insert for a new device is the device ID: choose an ID for your device, then go through the following steps and finally click **Finish**.&#x20;

The next screen shows all the info for your device, including an authentication token: take note of this token now, since it will be needed when setting up the IoTIFY network simulator:

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiORZDvL6DAwRp3Efye%2F-LiOcxukG01Qvhx3D_Vj%2FScreenshot%20from%202019-06-27%2017-48-07.png?alt=media\&token=7b48cd22-9c6a-4f01-8ce7-f2100a4774d3)

The setup phase in IBM Cloud is completed: now it’s time to send data to it from our network simulator.&#x20;

## 3. Create a template in IoTIFY network simulator <a href="#id-3-create-a-template-in-iotify-network-simulator" id="id-3-create-a-template-in-iotify-network-simulator"></a>

You can start by importing our predefined template from the link below

```
https://raw.githubusercontent.com/iotify/nsim-examples/master/connectors/ibm-connector.json
```

Or, You can create one yourself.

* To create a new template, go to the `TEMPLATES` tab of IoTIFY Network Simulator and click on `New Template`
* Then, select `MQTT(S)` as connection protocol, give the template a name of your choice (for example, IBMcloud) and click on `CREATE`.
* The next screen allows you to customize the template. In the MQTT parameters section, do the following configuration:
  * select `MQTTS (TCP)` as protocol
  * insert a string with the format `<org-id>.messaging.internetofthings.ibmcloud.com` as endpoint URL, where `<org-id>` is the identifier of your organization in your IBM Cloud account. You can find this information on the top right of your IBM Watson IoT Platform dashboard, below your account's IBM ID.&#x20;
  * The topic of the MQTT messages sent by the devices must be of the form `iot-2/evt/<event_id>/fmt/json` , where  is a name of your choice that identifies the events published by a device.&#x20;
  * Then, as`ClientID` you have to insert a string with the format `d:<org-id>:<device_type>:<device_id>`, where&#x20;
    * `<org-id>` is the same as above
    * `<device_type>` is the name of the device type associated to your device in IBM IoT Platform
    * `<device_id>` is the identifier of your device.

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiSmI5kRUGnzaPOIK1z%2F-LiSmYkaR5dnzdDYb2hD%2FScreenshot%20from%202019-06-28%2010-08-03.png?alt=media\&token=c92a4cea-4572-475a-bbe4-01868cf45187)

In the credentials ections, you have to supply authentication credentials: insert **`use-token-auth`**&#x61;s username, and the authentication token assigned when you created your device as password:

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiSmI5kRUGnzaPOIK1z%2F-LiSn8UXHc7-PurUfM8t%2FScreenshot%20from%202019-06-28%2010-15-57.png?alt=media\&token=1fafc9b5-6b6a-4e36-8b1e-805d52eafc4b)

The MQTT message contents must be formatted in JSON and must contain a single top-level property called *d*. For example:

```
{
    var mystate = {
        d: {
            "true Power": chance.integer({min: 100, max: 3000 }),
            "time": moment.now()
        }
    };
    return JSON.stringify(mystate)
}
```

Click on **PREVIEW** to verify if your template has been configured correctly; then, if the preview is successful, save your template and you are all set up to send data to IBM Cloud.

## 4. Send data to IBM Cloud <a href="#id-4-send-data-to-bluemix" id="id-4-send-data-to-bluemix"></a>

In IoTIFY Network Simulator, launch a new simulation, and as your virtual devices start sending MQTT messages you will be able to see your generated data in the IBM IoT Platform dashboard: click on your device in the dashboard, and you will see events as they come in from our simulator:

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiSnPL1yhdrdhm3dV8r%2F-LiSo2DFFfnvcQZrdmPK%2FScreenshot%20from%202019-06-28%2012-00-57.png?alt=media\&token=7aa5aab5-70c4-4fa9-a9d7-5a2f4bd49dc2)

Clicking on an event will show you the corresponding message contents, generated by the simulator according to your template:

![](https://1549501356-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LhKFvCh4dnxgthNTIuu%2F-LiSnPL1yhdrdhm3dV8r%2F-LiSo7kUffFrrnbYXCuv%2FScreenshot%20from%202019-06-28%2012-04-44.png?alt=media\&token=e88b074a-a9e4-4104-a650-a36154d8a760)

That’s it! Enjoy using our network simulator with IBM Cloud!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iotify.io/temp/untitled-3/simple-messaging.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
