Skip to content

Using Python With Coreflux

Coreflux is a versatile IoT platform that provides a comprehensive ecosystem for managing and monitoring your data hub. Its flexibility extends to various programming languages, including Python, allowing developers to integrate machine learning models, sensors, and other components seamlessly. In this documentation, we'll explore the integration of Coreflux with Python. Using the Siemens PLC S7-1200 as our example, we'll demonstrate how to achieve seamless orchestration through code.

Setting Up the Environment

Before diving into the integration, ensure you have the necessary Python libraries installed:

pip install paho-mqtt

Installing and Configuring a Flux Asset

In the example we are using , an S7-1200 PLC with Coreflux,then you need to install the coreflux_S7mqtt asset. This can be done by sending a command to the $SYS/Coreflux/Command MQTT topic:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.publish("$SYS/Coreflux/Command", "-I coreflux_S7mqtt")

client = mqtt.Client()
client.on_connect = on_connect

client.connect("YOUR_COREFLUX_SERVER_IP", 1883, 60)

After installing the asset, you can configure it using the provided asset configuration example. First, retrieve the configuration example using the assetHelp command:

client.publish("$SYS/Coreflux/Command", "assetHelp")

Once you have the configuration example, modify the JSON as required for your setup. For instance, you might need to set the IP address of your S7-1200 PLC, specify the MQTT broker details, and define the tags for the PLC variables you want to monitor or control.

After modifying the configuration JSON, save the configuration using the assetConfigSave command:

asset_guid = "YOUR_ASSET_GUID"  # Replace with the actual GUID of your asset container
config_json = "YOUR_MODIFIED_JSON"  # Replace with your modified configuration JSON

client.publish("$SYS/Coreflux/Command", f"-assetConfigSave {asset_guid} {config_json}")

Finally, start the asset:

client.publish("$SYS/Coreflux/Command", "-S S7PLC")

Integrating an ML Model for a Temperature Sensor

Let's assume you have a simple ML model that predicts whether a given temperature reading indicates an anomaly. This model can be integrated with the temperature sensor data from the S7-1200 PLC.

import paho.mqtt.client as mqtt
from sklearn.externals import joblib

# Load your ML model
model = joblib.load('path_to_your_model.pkl')

def on_message(client, userdata, msg):
    temperature = float(msg.payload)  # Assuming the payload is the temperature value from MD3000
    prediction = model.predict([[temperature]])
    if prediction[0] == 1:  # Assuming 1 indicates an anomaly
        print(f"Anomaly detected at temperature: {temperature}")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("YOUR_COREFLUX_SERVER_IP", 1883, 60)
client.subscribe("path_to_your_S7_topic/MD3000")  # Replace with the appropriate topic for MD3000


In this example, the Python script subscribes to the MQTT topic corresponding to the MD3000 memory location in the S7-1200 PLC. When a new temperature reading is received, it's passed to the ML model to check for anomalies.