This tutorial describes the steps to connect to IBM Watson IoT platform using MQTT secure protocol via 4G mobile connection and publish some data. It uses a 4G modem (SIM7600) from Simcom to perform the same. The Modem is connected to an ESP32 Microcontroller Module. The Module talks to the Modem via Serial UART connection and sends the required ATtention (AT) commands to achieve the flow as described below -
Before we can program our SIM7600 modem to send data using MQTT, we need the following prerequisites -
- An IBM Cloud® account 📷.
- A Watson IoT Platform instance.We can create a Watson IoT Platform instance directly from the Watson IoT Platform page in the IBM Cloud Services Catalog 📷.
To send data using MQTT the SIM7600 modem, the device messages must conform to the Watson IoT Platform message payload requirements.For more information, see Developing devices on Watson IoT Platform 📷.
The Modem (SIM7600) must be equipped with an activated 4G simcard with sufficient credits. The demo uses only few Kilo Bytes of data transfer.
Step 1: Register the deviceRegistering a device involves classifying the device as a device type, giving the device a name, and providing device information. Then you provide a connection token or accept a token that is generated by Watson IoT Platform.
To add a device from the Watson IoT Platform dashboard:
In the IBM Cloud console, click Launch on the Watson IoT Platform service details page.
The Watson IoT Platform web console opens in a new browser tab at the following URL:
https://org_id.internetofthings.ibmcloud.com/dashboard/#/overview
Where org_id is the ID of your Watson IoT Platform organization.
- In the Overview dashboard, from the menu pane, select Devices and then click Add Device.
Create a device type for the device that you are adding.
Each device that is connected to the Watson IoT Platform must be associated with a device type. Device types are groups of devices that share common characteristics.
- Click Create device type.
Enter a device name, such as dropper
.
Note: The device type name must be no more than 36 characters and can contain only alphanumeric characters (a-z, A-Z, 0-9) and any of the following characters: _
, .
, and -
.
Note: The device type name must be no more than 36 characters and can contain only alphanumeric characters (a-z, A-Z, 0-9) and any of the following characters:_
,.
, and-
.
Enter a device name, such as drop
, and a description for the device type.
Note: The device type name must be no more than 36 characters and can contain only alphanumeric characters (a-z, A-Z, 0-9) and any of the following characters: _
, .
, and -
.
Optional: Enter device type attributes and metadata.
We can add and edit attributes and metadata later.
- Optional: Enter device type attributes and metadata.You can add and edit attributes and metadata later.
Click Next to go to the next step for authentication token.
We can choose to add our own authentication token or leave it blank allowing the system to generate automatically.
If we choose to create our own token, make sure that it is between 8 and 36 characters long and consists only of alphanumeric characters and the following characters: _
, .
, !
, &
, @
, ?
, \*
, +
, (
, )
, and -
.
The token must not contain repeated character sequences, dictionary words, user names, or other predefined sequences.
Click Next to go to the summary.Verify the summary information is correct and then click Finish to complete device registration.
A confirmation page with details of the newly defined device is shown -
In the device information page, copy and save the following details:
- Organization ID
- Device type
- Device ID
- Authentication method
We'll need the organization ID, device type, device ID, and authentication token to configure our device to connect to Watson IoT Platform.
Please remember, this is the only moment the Authentication Token will be displayed. If we lose the authentication token it is non-recoverable and we would have to start over with another device registration.
Step 2: Configure your device to connect to Watson IoT PlatformWe would be interacting with the modem using an ESP32 microcontroller module. Hence the device needs to be programmed with the required credential information to send the same to the modem for connection. A sketch is added below which describes how this is done.
with ESP32 we will be communicating via Hardware Serial with the SIM7600 modem. Pin connections are described in the schematics as well as defined in the sketch -
#define MODEM_TX 26
#define MODEM_RX 27
Additionally two pins are used to power on and reset the modem to start the initialisation process.
#define MODEM_PWRKEY 25
#define MODEM_RST 4
The generated device credentials for authentication including the standard URL and host endpoints need to be updated in the sketch before it is to be programmed into the device -
String ORG = "<ORG ID of the IBM IoT instance>";
String DEVICE_ID = "<Registered Device Id>";
String DEVICE_TYPE = "<Device Type>";
String TOKEN = "<Authentication Token>";
The following information is required for establishing MQTT connection via the modem:
URL: org_id.messaging.internetofthings.ibmcloud.com
Where org_id is the ID of your Watson IoT Platform organization.
Port: 8883 (encrypted)
Device ID: _orgid:_devicetype:_deviceid
User name: use-token-auth
Password: Authentication token
Event topic format: iot-2/evt/_eventid/fmt/_formatstring
Where _eventid specifies the event name that's shown in Watson IoT Platform, and _formatstring is the format of the event, such as JSON.
Message format: JSON
In this case the topic is e.g. iot-2/evt/status/fmt/json
String PROTOCOL_PREFIX = "tcp://"; //URL Prefix as required by SIM7600 module
String PORT = "8883"; //Secure port
String server_url = PROTOCOL_PREFIX + ORG + ".messaging.internetofthings.ibmcloud.com" + ":" + PORT;
String topic = "iot-2/evt/status/fmt/json";
String authMethod = "use-token-auth";
String token = TOKEN;
String clientId = "d:" + ORG + ":" + DEVICE_TYPE + ":" + DEVICE_ID;
Example payload in JSON format: { "d": {"a": 1} }
Connect the ESP32 module with a PC where the sketch is loaded in an Arduino IDE. Build and send the sketch to the device.
In the beginning of the sketch at the setup phase the MCU toggles the power and reset signals for the modem to kick off the modem initialisation. What I noticed specifically that there is a significant wait period after the modem is reset and before it is ready to accept command. Individual commands also take time to process and delay has been introduced accordingly in the sketch to address the same.
Step 2: Connect and publish dataAs described in the beginning, there are several steps in the MQTT connection setup and data publish. SIM7600 has a set of ATtention (AT) commands that needs to be executed. This tutorial describes connection through an encrypted channel (SSL) but does not deal with the advance mechanism of certificate based authentication.
This tutorial covers the MQTT publish event only. The subscribe event can also be executed in a similar fashion through a different set of AT commands
For the Publish event, the steps are as follows -
Initialise
Turn command echo off -
ATE0
Open 4G connection for MQTT -
AT+CMQTTSTART
Setup MQTT Client -
AT+CMQTTACCQ=0,"d:<ORG Id>:<Device Type>:<Device Id>",1
Connect MQTT Server -
AT+CMQTTCONNECT=0,"tcp://<ORG Id>.messaging.internetofthings.ibmcloud.com:8883",90,1,"use-token-auth","<TOKEN>"
Send Data
Setup MQTT publish topic (This is a 2 part command as it expects the topic string after this execution) The last numeric is the length of the topic string and has to match the length of the topic in consideration which is 25 characters long here. -
AT+CMQTTTOPIC=0,25
Send the topic string
iot-2/evt/status/fmt/json
Setup data to be send (This is also a two part command. Next step after this execution is to set the data to be published). The last numeric is the length of the payload and has to match the length of the data.
AT+CMQTTPAYLOAD=0,<Length of MQTT Client Id>
Set the data (13 characters long)
{"d":{"a":1}}
Publish the data
AT+CMQTTPUB=0,0,60
After this step, data can be seen pouring into Watson Iot Platform under the registered device -
Drill down to see details of the data received.
Disconnect and close connection
Disconnect MQTT Server -
AT+CMQTTDISC=0,60
Release the MQTT Client -
AT+CMQTTREL=0
Close the 4G connection
AT+CMQTTSTOP
SummaryThe tutorial described how to connect to IBM Watson IoT platform via MQTT protocol using a 4G modem. A specific model of the modem has been used here which has certain command sequence requirements. Also the process to register a device on the IoT platform has been described before connection can be made and data can be sent. The tutorial covered MQTT Publish aspect only. The Subscribe aspect can also be achieved in a similar fashion but through the use of different command sets on SIM7600 module.
Comments