<aside> đź’ˇ THE STARTING POINT:

Photoelectric distance sensor.json

</aside>


<aside> 💡 The overall logic: each time the distance sensor reaches a limit value, it sends a message with “1” as payload. By this we can count each part coming through.

</aside>

Untitled

  1. You start with a MQTT-IN node that runs under the topic of your optica distance sensor (as defined in documentation)

    1. Define Server (vernemq…)
    2. Define topic (as in documentation)
    3. give it a name
  2. After that place a json for the convertation from string to object:

    Untitled

  3. Then use the distance function to make the distance to the only payload.

    msg.payload = msg.payload.Distance
    return msg;
    
  4. Implement the rising edge (it will compare to the msg.payload). The value for the threshold has to be defined out of the context in production. In this example it is 30.

    Untitled

  5. After this the payload gets redefined in the “piece count” function. The example uses a subflow for the count:

    Untitled

    //if you want to process further in a subflow to add timestamp 
    //and then store the message under the topic count
    //this stands in piece count:
    msg.payload = 1;
    return msg;
    
    //alternative (just do all at once): define the message.payload and the topic.
    msg.payload = {
    "timestamp_ms": Date.now(),
    "count": 1
    }
    msg.topic = "ia/"customerID"/"location"/"AssetID"/count"
    return msg;