Example: Connect a sensor to the Teamviewer IoT Host for Windows - TeamViewer Support
<main>
<article class="userContent">
<p><br></p><p>This example provides a quick overview of how to use the <strong>TeamViewer IoT Agent MQTT API</strong> to securely (encrypted and trusted) connect, to register sensors and metrics, to view device inventory, and to push data to the <strong>TeamViewer IoT Cloud</strong>.</p><p>For more information see <a href="https://community.teamviewer.com/English/kb/articles/109884-teamviewer-iot-agent-mqtt-api" rel="nofollow noreferrer ugc">TeamViewer IoT Agent MQTT API</a>.</p><p><br></p><h3></h3><h2 data-id="what-you-will-need">What you will need</h2><ul><li>The edge device is set up with a TeamViewer IoT Host or Windows (TeamViewer Service and Mosquitto broker process running) and has internet access.</li><li>Your login credentials for the edge device (user name, and password).</li><li>You are logged in to the <strong>IoT Management Console</strong>. To log in to the <strong>IoT Management Console</strong>, open <a href="https://teamviewer-iot.com" rel="nofollow noreferrer ugc">https://teamviewer-iot.com</a> and use your TeamViewer account credentials.</li><li><a href="https://mosquitto.org/download/" rel="nofollow noreferrer ugc">Mosquitto</a> is installed. Add Mosquitto installation path to <strong>Environment Variables</strong>.</li><li>Install <a href="https://git-scm.com/downloads" rel="nofollow noreferrer ugc">Git Bash</a> for Windows, this is needed to create CSR and Request Client Certificate</li><li>Advanced knowledge of <strong>Windows systems</strong> and <strong>command line operations</strong>.</li></ul><p><br></p><h3 data-id="-1"></h3><h2 data-id="setup">Setup</h2><h3 data-id="create-mqtt-client"><strong>Create MQTT Client</strong></h3><p>1. Generate A CSR using Git Bash</p><p>Open a Git Bash window and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">openssl req -nodes -new -newkey rsa:2048 -sha256 -out csr.pem
</pre><p>2. Get the hash of CSR using Git Bash. It is the SHA-256, which will be used in subscribe topic</p><p>Open a Git Bash window and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">openssl dgst -sha256 csr.pem
</pre><p>In this example the hash is <em>83a32b8cbae161ab995b1d3bce24715579c4c672f69ca48a24350baaaed40d17</em></p><p>3. Request a client certificate using Command Prompt.</p><p>Open a Command Prompt window (run as an administrator) and subscribe for the response:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_sub -h localhost -p 18883 --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /certBack/83a32b8cbae161ab995b1d3bce24715579c4c672f69ca48a24350baaaed40d17
</pre><p>📌<strong>Note:</strong> Do not close this window. The signed certificate will be sent back here in the subscribe topic (this cmd window). Copy the received signed certificate and save as a file (e.g., <em>clientCert.crt</em>) for future use.</p><p>Open a command prompt window (as an administrator) and publish the <em>csr.pem</em></p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_pub -h localhost -p 18883 --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/createClient -f csr.pem
</pre><p>4. Get Client ID for future use within MQTT topics using Git Bash.</p><p>Open a Git Bash window and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">openssl x509 -noout -subject -in clientCert.crt -nameopt multiline | grep commonName | sed -r 's/commonName/ /g' | sed -r 's/=/ /g' | xargs | tr -d " \t\n\r"
</pre><p>In this example the ClientID is <em>6d69c58223fb44a7b76ae61a18faf37c</em></p><p><br></p><h3 data-id="create-mqtt-client-1"><strong>Create MQTT Client</strong></h3><p>1. Monitoring</p><p>This publish request updates metric values. Prior creation of sensor and metrics is not required. The system checks for the existence of the given sensor and metric IDs. If those do not exist, the system creates missing sensor or metric.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_pub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6c4a946918dc4facbe354279752a9ddd/sensor/weather/metric/pushValues -m "{ \"metrics\": [{\"value\" : 5, \"metricId\" : \"temperature\" }] , \"timestamp\": 1590394246545}"
</pre><p>Alternatively, you can follow the steps below to create Sensors with Metrics.</p><p><br></p><h3 data-id="create-sensor"><strong>Create sensor</strong></h3><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_sub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/inbox
</pre><p>📌<strong>Note:</strong> Do not close this window. A message containing a JSON object with the name of the sensor and a unique ID will be received in this window.</p><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_pub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/create -m "{ \"name\" : \"SensorName\" }"
</pre><p>In this example the name of the sensor and the unique ID are <em>{ “name”: “SensorName”, “sensorId”: “d4170d999b9240a5863df622aad9fc4a” }</em></p><p><br></p><h3 data-id="create-metrics"><strong>Create metrics</strong></h3><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_sub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/d4170d999b9240a5863df622aad9fc4a/metric/inbox
</pre><p>📌<strong>Note:</strong> Do not close this window. A message containing a JSON object with the matchingIDs and a unique IDs will be received in this window.</p><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_pub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/d4170d999b9240a5863df622aad9fc4a/metric/create -m "{\"metrics\" : [{\"matchingId\" : \"1\", \"valueUnit\": \"NoSI.Dimensionless.PERCENT\", \"name\" : \"CPUUsage\" },{\"matchingId\" : \"2\",\"valueUnit\": \"NoSI.Dimensionless.PERCENT\", \"name\" :\"CPUIdle\" }, {\"matchingId\" : \"3\", \"valueType\" : \"integer\" , \"valueAnnotation\": \"goods per minute\", \"name\" : \"ShopFloor1\"}]}"
</pre><p>In this example the metrics and the uniques IDs are:</p><p><em>[{ “matchingId” : “1”, “metricId” : “94c2c5e193bb437195368c08f0bd86ab”},</em></p><p><em>{ “matchingId” : “2”, “metricId” : “94c2c5e193bb437195368c08f0bd86ac”},</em></p><p><em>{ “matchingId” : “3”, “metricId” : “94c2c5e193bb437195368c08f0bd86ad”}]</em></p><p><br></p><h3 data-id="push-data-to-a-sensor"><strong>Push data to a Sensor</strong></h3><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_sub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/d4170d999b9240a5863df622aad9fc4a/info/inbox
</pre><p>📌<strong>Note:</strong> Do not close this window. A message will appear that the data is pushed in this window.</p><p>Open a Command Prompt window (run as an administrator) and type in the following command:</p><pre class="code codeBlock" spellcheck="false" tabindex="0">mosquitto_pub -h localhost -p 18884 --cert clientCert.crt --key privkey.pem --cafile "C:\Program Files (x86)\Teamviewer\certs\TeamViewerAuthority.crt" -t /v1.0/6d69c58223fb44a7b76ae61a18faf37c/sensor/6d69c58223fb44a7b76ae61a18faf37c/metric/pushValues -m "{ \"metrics\": [ {\"value\" : 21.5, \"metricId\" : \"94c2c5e193bb437195368c08f0bd86ab\" }, {\"value\" : 21.5, \"metricId\" : \"94c2c5e193bb437195368c08f0bd86ac\" }, {\"value\" : 55, \"metricId\" : \"temperature\" }] , \"timestamp\": \"1590394246545\"}"
</pre><p>From the <strong>IoT Management Console</strong> navigate to <strong>Metrics</strong> and verify the created metrics.</p><p><br></p><h3 data-id="-2"></h3>
</article>
</main>