Certainly! Below is a Node-RED function node script that simulates the operations of motors and machines in an oat-making factory. This script generates simulated data for various machines, including their operational parameters like motor speed, temperature, current consumption, and vibration levels.
To use this script:
-
Add Nodes to Your Flow:
- Inject Node: This will trigger the simulation at regular intervals.
- Function Node: Where you'll paste the simulation script.
- Debug Node: To display the output of the simulation.
-
Configure the Inject Node:
- Set it to trigger at your desired interval (e.g., every second).
-
Paste the Script into the Function Node:
- Open the function node and replace its contents with the script provided below.
-
Connect the Nodes:
- Connect the inject node to the function node.
- Connect the function node to the debug node.
Here's the function node script:
// Node-RED function node script to simulate motors and machines in an oat-making factory
// List of machines in the oat-making factory
var machines = [
{id: 1, name: "Cleaning Machine", type: "Cleaner"},
{id: 2, name: "Dehuller", type: "Dehuller"},
{id: 3, name: "Rolling Mill", type: "Roller"},
{id: 4, name: "Dryer", type: "Dryer"},
{id: 5, name: "Conveyor Belt A", type: "Conveyor"},
{id: 6, name: "Conveyor Belt B", type: "Conveyor"},
{id: 7, name: "Packaging Machine", type: "Packager"}
];
// Function to generate random data for a machine
function generateMachineData(machine) {
var data = {
id: machine.id,
name: machine.name,
type: machine.type,
timestamp: new Date().toISOString(),
status: "Running",
parameters: {}
};
// Generate parameters based on machine type
switch(machine.type) {
case "Cleaner":
data.parameters.motorSpeed = parseFloat(getRandomInRange(1000, 1500)); // RPM
data.parameters.temperature = parseFloat(getRandomInRange(50, 70)); // Celsius
data.parameters.current = parseFloat(getRandomInRange(10, 15)); // Amps
data.parameters.vibration = parseFloat(getRandomInRange(0.5, 1.5)); // mm/s
break;
case "Dehuller":
data.parameters.motorSpeed = parseFloat(getRandomInRange(800, 1200)); // RPM
data.parameters.temperature = parseFloat(getRandomInRange(60, 80));
data.parameters.current = parseFloat(getRandomInRange(12, 18));
data.parameters.vibration = parseFloat(getRandomInRange(0.6, 1.8));
break;
case "Roller":
data.parameters.motorSpeed = parseFloat(getRandomInRange(500, 1000));
data.parameters.temperature = parseFloat(getRandomInRange(70, 90));
data.parameters.current = parseFloat(getRandomInRange(15, 20));
data.parameters.vibration = parseFloat(getRandomInRange(0.7, 2.0));
break;
case "Dryer":
data.parameters.temperature = parseFloat(getRandomInRange(80, 120)); // Drying temperature
data.parameters.humidity = parseFloat(getRandomInRange(5, 15)); // Percentage
data.parameters.current = parseFloat(getRandomInRange(8, 12));
break;
case "Conveyor":
data.parameters.speed = parseFloat(getRandomInRange(1, 5)); // m/s
data.parameters.temperature = parseFloat(getRandomInRange(40, 60));
data.parameters.current = parseFloat(getRandomInRange(5, 10));
data.parameters.vibration = parseFloat(getRandomInRange(0.4, 1.0));
break;
case "Packager":
data.parameters.packagingRate = parseFloat(getRandomInRange(50, 100)); // Units per minute
data.parameters.temperature = parseFloat(getRandomInRange(45, 65));
data.parameters.current = parseFloat(getRandomInRange(6, 9));
data.parameters.vibration = parseFloat(getRandomInRange(0.3, 0.9));
break;
default:
// For any other type
data.parameters.status = "Unknown Machine Type";
break;
}
return data;
}
// Helper function to get random number in a range
function getRandomInRange(min, max) {
return (Math.random() * (max - min) + min).toFixed(2);
}
// Generate data for each machine
var outputData = [];
for (var i = 0; i < machines.length; i++) {
var machineData = generateMachineData(machines[i]);
outputData.push(machineData);
}
// Set the output message
msg.payload = outputData;
return msg;
Explanation:
-
Machines Array: The script defines an array of machine objects, each representing a different machine in the oat-making factory.
-
generateMachineData Function: This function generates simulated data for a given machine based on its type. Different types of machines have different parameters.
-
Parameters Generated:
- motorSpeed: The rotational speed of the motor in RPM.
- temperature: The temperature of the machine in Celsius.
- current: The electrical current the motor is drawing in Amps.
- vibration: The vibration level of the machine in mm/s.
- humidity: For the dryer, the humidity level in percentage.
- speed: For conveyors, the speed in meters per second.
- packagingRate: For the packaging machine, the rate at which it packages units per minute.
-
getRandomInRange Function: A helper function that returns a random number within the specified range, simulating the variability in machine parameters during operation.
-
Output Data: The script collects the simulated data for all machines and assigns it to msg.payload, which can then be passed to other nodes in the flow.
Sample Output:
When you run the flow, the debug node will display an array of simulated machine data similar to this:
[
{
"id": 1,
"name": "Cleaning Machine",
"type": "Cleaner",
"timestamp": "2023-10-14T10:20:30.123Z",
"status": "Running",
"parameters": {
"motorSpeed": 1205.34,
"temperature": 62.15,
"current": 12.75,
"vibration": 1.02
}
},
{
"id": 2,
"name": "Dehuller",
"type": "Dehuller",
"timestamp": "2023-10-14T10:20:30.123Z",
"status": "Running",
"parameters": {
"motorSpeed": 950.28,
"temperature": 75.33,
"current": 14.62,
"vibration": 1.25
}
},
// ...additional machine data...
]
Usage Tips:
-
Adjusting Parameters: You can customize the ranges in the getRandomInRange function calls to better simulate your specific factory's operations.
-
Adding Machines: To simulate additional machines, simply add more entries to the machines array.
-
Schedule Simulation: Use the inject node's scheduling options to simulate the data at intervals that suit your needs (e.g., every second, every minute).
-
Processing Data: Connect the function node to other nodes to process, store, or visualize the simulated data. For example, you can use a chart node to display temperature trends or store data in a database.
Example Flow Setup:
- Inject Node: Configured to trigger at regular intervals.
- Function Node: Contains the simulation script provided.
- Debug Node: Set to display
msg.payload to view the output.
- Optional Nodes:
- Chart Node: To visualize specific parameters over time.
- Database Nodes: To store the data for historical analysis.
- Dashboard Nodes: To create a user interface for monitoring.
Visual Representation:
[image: Node-RED Flow Example] open
(Note: Please replace https://i.imgur.com/YourImageLink.png with an actual image URL if you have one. Since I cannot provide images, consider this a placeholder.)
Further Customization:
- Machine Status Simulation: You could enhance the script to simulate machines being stopped or under maintenance by randomly assigning different statuses.
- Error Conditions: Introduce occasional anomalies in the data to simulate errors or faults, such as sudden spikes in temperature or drops in motor speed.
- Interactive Controls: Use dashboard input nodes to control the simulation parameters in real-time.
Example of Introducing an Error Condition:
// After declaring 'data' in the generateMachineData function
// Introduce a fault with a 5% chance
if (Math.random() < 0.05) {
data.status = "Fault";
data.parameters.errorCode = "E101";
data.parameters.errorMessage = "Overheating detected";
}
This snippet adds a random chance for a machine to have a fault, including an error code and message.
I hope this script helps you simulate the motors and machines in your oat-making factory using Node-RED. If you need further customization or assistance with integrating this script into your flow, feel free to ask!