Send Data to the Platform

  1. Under User Functions, click Functions.
  2. Click New Function.
  3. Enter a name and description, if desired.
  4. From the Template drop down menu, choose Python.
  5. Add the following code to send, for each row, the value stored in each column to the previously defined properties.
    import paho.mqtt.publish as publish
    import json
    import pandas as pd
    import time
    from datetime import datetime, timezone
    
    host = "mqtt.swx.altairone.com"
    space = "EnterValue"
    
    # Enter your thing_uid
    thing_uid = "EnterValue"
    
    # Enter your MQTT credentials
    username = 'EnterValue'
    password = 'EnterValue'
    
    def handle(req):
      path = "EnterValue"
      data_pd = pd.read_csv(path, delimiter=",")
      df = pd.DataFrame(data_pd)
      
      properties_history_topic = "spaces/{}/things/{}/properties-history".format(space, thing_uid)
      
      for i in range(len(df)):
          current_time = datetime.now(timezone.utc)
          formatted_time = current_time.isoformat()
          machine_failure_data = {
              "HDF": float(df["HDF"][i]),
              "OSF": float(df["OSF"][i]),
              "PWF": float(df["PWF"][i]),
              "RNF": float(df["RNF"][i]),
              "TWF": float(df["TWF"][i])
          }
        
          payload = {
              "at": formatted_time,
              "properties": {
                  "UID": float(df["UID"][i]),
                  "air_temperature": float(df["air_temperature"][i]),
                  "machine_failure": machine_failure_data,
                  "process_temperature":  float(df["process_temperature"][i]),
                  "product_ID": df["product_ID"][i],
                  "rotational_speed": float(df["rotational_speed"][i]),
                  "tool_wear":  float(df["tool_wear"][i]),
                  "torque":  float(df["torque"][i]),
                  "type": df["type"][i]
              }
          }
      
          print(payload)
          time.sleep(0.5)
          print("Data:", payload)
          publish.single(properties_history_topic, payload=json.dumps(payload), qos=2, retain=False, hostname=host, port=1883, client_id="",
                         keepalive=120, will=None, auth={'username': username, 'password': password}, tls=None)
                         
      return {
        "status_code": 200,
        "body": "upload finished!"
      }
  6. Edit the following variables with your values:
    space | thing_uid | username | password | path
    Note: Username and password are the MQTT credentials of the Thing defined before that will store the data. To get the path, click the Link button next to the file under Object Storage. Set the expiration time and click Save.
    Figure 1.


  7. Include the following libraries from the drop-down menu:
    paho_mqtt | pandas
  8. Click Save.
  9. Run the Function by clicking the Test button.
    The API Inspector opens with the request for invoking the Python script already filled.
  10. Click Send to invoke the Function.
    Figure 2.


    Note: You can easily customize the code to use with your own values by updating the names of the properties.