IMD SolutionComputing LibraryShop main store

How-to

Send Flexy tag values to MindSphere over MQTT every 10 seconds

Edge computing and IIoT Published 5 min read By IMD Solution engineering team

Short answer

Open one MQTT session from the Flexy init section to the MindSphere MCIoT Extension broker on port 1883 with keepalive 30 and CleanSession 1, publish the three registration payloads on s/us, then register Ontimer 1 once and schedule it with Tset 1,10 so a single type-200 payload with both tag records goes out every 10 seconds. The timer registration belongs in the init section, not the cyclic one.

On this page
  1. How does a Flexy publish tag values to MindSphere over MQTT?
  2. What do you prepare before writing the script?
  3. Which MQTT session settings does MindSphere expect?
  4. What does the complete script look like?
  5. Where does Ontimer 1 go and why not the cyclic section?
  6. What goes inside each 10-second payload?
  7. How do you confirm MindSphere is receiving the values?
  8. When can you drop the script for the built-in connector?
  9. Questions asked

Connecting an Ewon Flexy to MindSphere takes one MQTT session and a short BASIC script. You open the session to the MindSphere MCIoT Extension broker on port 1883, publish three registration payloads on the s/us topic, then let a timer push a measurement payload every 10 seconds. The one detail most scripts get wrong is the timer registration: Ontimer 1 is called once, in the init section, and Tset 1,10 does the scheduling. The cyclic section stays empty.

How does a Flexy publish tag values to MindSphere over MQTT? #

You open one MQTT session from the Flexy BASIC init section to the MindSphere MCIoT Extension broker on TCP port 1883, send three registration payloads on the s/us topic, and then publish measurements on that same topic from a timer. The MCIoT Extension accepts Cumulocity-style MQTT devices, which means the payload format is line-based: a type number, then comma-separated fields, with each record on its own line. Registration happens once at session start; measurements repeat for as long as the session stays up.

What do you prepare before writing the script? #

You create the two tag variables Speed@ and Current@ under Tags > Values in the Flexy, and you collect the broker hostname, tenant name, MQTT username and password that your MCIoT Extension subscription assigns.

  1. In the Flexy tag editor under Tags > Values, create Speed@ and Current@ and bind each one to a live data source so both resolve to numbers.
  2. From your MCIoT Extension subscription, note the broker hostname it assigns for your region.
  3. Note the tenant name and the MQTT username; the login joins them with a slash, tenant first.
  4. Note the password the extension issued for that MQTT user, and fix one device identifier for the unit, for example ewon_000001.
  5. Replace each brace-delimited value in the code below with those four pieces of your own subscription before running anything.

Which MQTT session settings does MindSphere expect? #

The session runs on port 1883 with a keepalive of 30 seconds, CleanSession 1, and a username built as tenant name, a slash, then the MQTT username, exactly as the SetParam lines below set them. CleanSession 1 means the broker keeps no subscription state between connections, which is fine here because the Flexy only publishes. Nothing in this session is encrypted: the port carries plain MQTT, so the login and payloads travel in clear text and belong on a network path you trust.

What does the complete script look like? #

The whole job is about fifteen BASIC lines in the init section plus a short publish subroutine, while the cyclic section of the Flexy script stays completely empty. This is the working arrangement:

<eWON_init_section:>
Mqtt "Open","ewon_000001","mciotextension.{yourMCIoTExtensionRegion}.mindsphere.io"
Mqtt "SetParam","Port","1883"
Mqtt "SetParam","username","{tenantName}/{mciotExtensionUsername}"
Mqtt "SetParam","password","{password}"
Mqtt "SetParam","keepalive","30"
Mqtt "SetParam","CleanSession","1"
Mqtt "Connect"
Mqtt "Publish","s/us","100,ewon,MCIoT_MQTTdevice",0,0
Mqtt "Publish","s/us","110,0001,ewon,1.0",0,0
Mqtt "Publish","s/us","117,1",0,0
Ontimer 1,"goto MqttPublish"
Tset 1,10
End
MqttPublish:
$auxStr$ = "200,Speed,Machine," + STR$ Speed@ + ",rpm" + CHR$ 13
$auxStr$ = $auxStr$ + "200,Current,Machine," + STR$ Current@ + ",A"
Mqtt "Publish","s/us",$auxStr$,0,0
End

The three registration messages are type 100, which announces the device, type 110, which sets its identity and firmware version string, and type 117, which marks it as a managed agent. MindSphere will not file measurements for a device it has not seen these lines for on the current session, so they belong right after Connect and before the timer starts.

Where does Ontimer 1 go and why not the cyclic section? #

Ontimer 1 registers the publish handler once, so it belongs in the init section; Tset 1,10 then fires it every 10 seconds, and repeating the registration in the cyclic section adds nothing. The cyclic section runs continuously, so an Ontimer line there re-registers the same handler over and over without changing the interval. Placing the registration and the period in the init section is also the cleaner layout when the session misbehaves: if your unit publishes once and then goes quiet, the timer and status handling are traced in why a Flexy BASIC script publishes MQTT once then stops.

What goes inside each 10-second payload? #

Each publish sends one string holding two type-200 records separated by a carriage-return character (ASCII 13), Speed in rpm and Current in A under the Machine fragment, with QoS 0 and retain cleared. STR$ converts each tag value to text at the moment the handler runs, so the payload reflects live values, not stale copies. The final two zeros in the Mqtt Publish call are QoS 0 and no retain: the Flexy does not wait for a broker acknowledgment, which is normal behavior for how MQTT clients publish and subscribe through the broker, and a lost record is simply superseded by the next interval.

How do you confirm MindSphere is receiving the values? #

You confirm three things in order: both tags resolve to live numbers, the init section connects without a credential or broker error, and the s/us topic carries both records refreshing at the set interval.

  1. Open the Flexy tag list and read Speed@ and Current@; a tag that shows a fault code instead of a number sends that fault into the payload, so fix the source first.
  2. Run the init section and check the MQTT session state before trusting any publish; the commands that read it are covered in checking MQTT connection status in an Ewon Flexy BASIC script.
  3. In the MindSphere device view, confirm the device appears once, then watch both measurements update; two records per refresh means the payload string and separator are correct.

When can you drop the script for the built-in connector? #

Firmware v14.0 was announced with a menu-driven MindSphere connector that needs no BASIC script, so confirm that your installed firmware actually offers it before you retire the script above. The announcement described the connector as arriving with that firmware generation, so a unit on an older build still needs this script, and a firmware upgrade is its own change to validate. If a unit you move to that generation then publishes once and disconnects, the cause and fix are covered in why a Flexy MQTT client drops after one publish on firmware 14.0s01. Until the connector is verified on the installed build, the script above is the supported path.

Questions asked

Why does MindSphere answer NotAuthorized even with a seemingly correct username and password?

The MQTT user must be created inside the IOT Extension itself, not as an ordinary MindSphere account. The extension issues the device credentials it will accept, and the username must still carry the tenant prefix with the slash. A user created anywhere else in the tenant is refused at CONNECT with NotAuthorized.

Can one payload carry more than the two example tags?

Yes. Append another type-200 record to the same string before the Mqtt Publish line, separating each record with a carriage-return character (ASCII 13). Each record names its own measurement and unit, and MindSphere stores all of them under the same device, so one publish per interval can carry a dozen tags without extra traffic.

What happens to the connection after the Flexy reboots?

The init section runs again at startup, so the script reopens the MQTT session, resends the three registration payloads on s/us, and restarts timer 1. No manual action is needed as long as the finished script is saved in the Flexy and the broker credentials are still valid.

Why must the device identifier stay the same every time the script runs?

The identifier passed to Mqtt Open and repeated in the type-100 payload is what MindSphere files the measurements under. If you change it, the platform sees a new device and the measurement history splits across two, so fix one identifier for the unit and keep it in the saved script.

Keep reading

  • How-to

    Checking MQTT connection status in an Ewon Flexy BASIC script

    On an Ewon Flexy with firmware 14.x or later, a BASIC script reads the MQTT link state with the MQTT Status command: a returned 5 means the connection is operational and any other value means it is not. The firmware owns disconnection and reconnection, so the script only monitors and logs, and you investigate the internet path when the broker never comes back on its own.

    Edge computing and IIoT

  • How-to

    Publish one MQTT event when a PLC bit rises

    Publish on the transition, not the level. Retain the previous valid sample of the PLC Boolean and publish one MQTT event only when the pair reads false then true. An acquisition cycle cannot see a pulse shorter than itself, so latch short pulses in the PLC and reset the latch after the event publishes, and make the consumer tolerant of duplicate deliveries.

    Edge computing and IIoT

  • How-to

    Publish select tags over MQTT to cut Azure message costs

    Azure counts every message the transmitter publishes, and a Sparkplug topic subscription cannot select individual tags, so filtering on the receiving side bills every message anyway. Build a dedicated publish tag provider whose UDT instances bind a source-path parameter back to the SCADA provider, scope the transmitter to that provider alone, and deadband the members IT reports on.

    Edge computing and IIoT

Show a historian tag average over three days on an HMI