IotMqttSampleMathworksThingspeak

Communication with the MathWorks ThingSpeak Cloud is illustrated in this sample. Since this sample is essentially based on the sample IotMqttSampleUsingQueue, only the parts that are relevant for establishing a connection to the ThingSpeak Cloud are explained in this section.

Sample code can be downloaded as archive here: ThingSpeakSample.

MQTT device configuration

To transfer MQTT data to ThingSpeak, you must first register your MQTT device. Log in to ThingSpeak with your MathWorks account and select Devices > MQTT from the top menu. Select Add new Device. Name your device and authorize publish and subscribe on the appropriate ThingSpeak channels. Save your credentials.

Parameters for establishing a connection

The following code snippet illustrates the parameters that are necessary for establishing a connection with the MathWorks ThingSpeak Cloud. The parameters are essentially static parameters. These can also be specified in the declaration part during instantiation of the MQTT client. The data to be used in the following <Client ID>, <Username> and <Password> will be provided to you when you configure an MQTT device on ThingSpeak.

IF bSetParameter THEN
  bSetParameter := FALSE;
  fbMqttClient.sHostName:= 'mqtt3.thingspeak.com';
  fbMqttClient.nHostPort:= 1883;
  fbMqttClient.sClientId:= '<Client ID as provided by MQTT Device on ThingSpeak>';
  fbMqttClient.sUsername:= '<username as provided by MQTT Device on ThingSpeak>';
  fbMqttClient.sUserPassword:= '<password as provided by MQTT Device on ThingSpeak>';
END_IF

ThingSpeak also supports protection of the communication connection via TLS. The CA certificate must be downloaded from ThingSpeak and referenced in the function block via the TLS structure. For more information about the ThingSpeak CA certificate we recommend referring to the Mathworks ThingSpeak documentation.

IF bSetParameter THEN
  bSetParameter := FALSE;
  fbMqttClient.sHostName:= 'mqtt3.thingspeak.com';
  fbMqttClient.nHostPort:= 8883;
  fbMqttClient.sClientId:= '<Client ID as provided by MQTT Device on ThingSpeak>';
  fbMqttClient.sUsername:= '<username as provided by MQTT Device on ThingSpeak>';
  fbMqttClient.sUserPassword:= '<password as provided by MQTT Device on ThingSpeak>';
  
  stMqttTls.sVersion:= 'tlsv1.2';
  stMqttTls.sCA:= 'C:\TwinCAT\3.1\Config\Certificates\<thingSpeakCert.pem>;
  fbMqttClient.stTLS := stMqttTls;
END_IF

Publish

When publishing data to the MathWorks ThingSpeak Cloud, the topic must be specified in the following form:

channels / <channelID> / publish

<channelID> corresponds here to the ID of the channel that was designated and configured for data reception in the MathWorks ThingSpeak portal.

Your device must be authorized for publish on the specified channel. You can change the authorization via Devices > MQTT in the top menu if you are logged in to the ThingSpeak website.

Subscribe

When subscribing to data, the topic must be specified in the following form:

channels / <channelID> / subscribe / fields / <fieldKey>

<channelID> corresponds here to the ID of the channel that was designated and configured for data reception in the MathWorks ThingSpeak portal.

<fieldKey> corresponds to the name of the field from which a value is to be received.

Your device must be authorized for a subscribe on the specified channel. You can change the authorization via Devices > MQTT in the top menu when you are logged in to the ThingSpeak website

Data format

The MathWorks ThingSpeak Cloud uses its own string-based data format as payload. This data format is generated in the sample code referenced above in the F_Mqtt_ThingSpeak_CreatePayloadStr() function and can be used directly.

Requirements

Development environment

Target platform

PLC libraries to include

TwinCAT v3.1.4022.0

IPC or CX (x86, x64, ARM)

Tc3_IotBase,
Tc2_Utilities (>= v3.3.19.0)