Siemens CPU S7-1200 / -1500 to Azure IoT-Hub

Luk K 221 Reputation points
2021-01-18T09:12:54.823+00:00

Hey all,

I am looking at whether it is possible to have a Siemens S7-1200 / -1500 CPU send MQTT messages directly to the Azure-IoT-Hub, but it is currently not working.

I am wondering if the Azure-IoT-Hub accepts regular MQTT messages. The PLC is linked with a CA-certificate and the connection is created correctly. But from the moment I send the first MQTT message it goes wrong. Azure then closes the connection for a reason unknown to me.

Since all tutorials about Azure-IoT-Hub always work with an SDK that you have to download, I don't really get any further. It is not possible to work with SDKs on a Siemens PLC. Programming is in "SCL" (Structured Control Language) language and messages must be completely constructed from zero.

The first message to be sent is the "Connect" function according to the MQTT standard (if I'm not mistaken). For my code I used this example: r_example_mqtt.htm
I'm also following the standard MQTT v3.1.1 specification

Below is a screenshot of the data being sent to the CPU interface in which you see the connect MQTT message. Maybe there is an error, but then I don't see it yet. I have already tried several things (for example turning on the "clean session" bit) but no success yet . User & password is not required as the connection is established with certificates (this works). For the rest, I have other options turned off to keep the message as simple as possible.
57477-plc-mqtt-connect.jpg

But something tells me that Azure-IoT-Hub is not really an MQTT-broker and it is simply not going to work this way. So if someone can confirm this to me? Or even better: know how to get it done?

Thanks in advance

PS: Siemens has also released "LMQTT_Client" function blocks, but these also do not work with the Azure-IoT-Hub (same error). Sending messages to another MQTT-Broker as for example AWS-IOT-Hub (Amazon) works fine with these blocks. fb-lmqtt_client-for-simatic-s7-cpu

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,274 questions
{count} votes

Accepted answer
  1. Luk K 221 Reputation points
    2021-01-26T12:14:50.577+00:00

    I got everything working. Connecting, publish, subscribe and direct methodes. Only thing not yet tested is receiving messages from the "devicebound" topic.

    As for help to others: Created a post on the Siemens Forums with things to watch out for:
    https://support.industry.siemens.com/tf/ww/en/posts/s7-1200-1500-mqtt-to-azure-iot-hub/238118/?page=0&pageSize=10#post985431

    I don't want to type the same twice.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Luk K 221 Reputation points
    2021-01-19T12:56:47.94+00:00

    Ok, Got it working.... There was an error at my "TRCV" function block. Siemens isn't always clear in their manuals.

    So for others trying to do the same. The Siemens LMQTT library blocks at this time are NOT compatible with Azure. But you can write your own following the MQTT v3.1.1 specs with some differences. Maybe Siemens does an update. Date at this time is 19-jan-2021.

    For Azure: Even with a TLS-Connection (certificates) you will have to send the username: See IoT-Hub-MQTT-Support "Using the MQTT protocol directly (as a device). So send the username with the CONNECT message.

    To publish a message the topic is always: devices/{device_id}/messages/events/

    1 person found this answer helpful.

  2. Luk K 221 Reputation points
    2021-01-19T07:19:16.4+00:00

    Hey asergaz,

    First thing first, I got a little step further. I can now send the MQTT "connect" message without Azure disconnecting. Azure also needs the username inside the connect message. Normally this isn't needed with a TLS-connection but Azure still needs it.

    But, I'm still not receiving anything back. The CPU is waiting for the "connack" message but not receiving it. I'm still in the process of figuring out why?

    Yes, logs would be of great help. Unfortunately I don't have them on the CPU side. I am currently investigating if I can find this in the Azure IoT hub. In the CPU I can only see that 80 bytes have been sent and 4 bytes have arrived.

    As an answer to your question about IoT-Edge: No, that's not an option. A Siemens S7-1500 is not an Edge device. I don't know that either so wouldn't know how to get started.

    PS: Yes, the Baltimore certificate is installed on the CPU. Like said: The CPU connects fine to Azure. The problem is sending/receiving the actual MQTT-messages.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.