// Heat Pump Simulator with Serial communication
//////This setup is a miniature model of a real district heating system.
///It helps to test and demonstrate:
//Communication protocols (serial, MQTT, etc.).
///Real-time data streaming.
//Control commands from an AI optimizer.
///Visualization of heat plant operation./////
float currentOutput = 0; // kWh heat output
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0)); // Initialize random generator
}
void loop() {
// Simulate small natural fluctuations if no command received
currentOutput += random(-10, 11) / 100.0; // +/-0.1 kWh variation
if (currentOutput < 0) currentOutput = 0;
if (currentOutput > 1000) currentOutput = 1000; // max limit
// Send current output every 2 seconds
Serial.print("heat_pump_output:");
Serial.println(currentOutput, 2); // 2 decimals
// Check for command from edge device
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim();
// Command format: SET_OUTPUT:500
if (command.startsWith("SET_OUTPUT:")) {
String valueStr = command.substring(11);
float newOutput = valueStr.toFloat();
if (newOutput >= 0 && newOutput <= 1000) {
currentOutput = newOutput;
Serial.print("OUTPUT_SET_TO:");
Serial.println(currentOutput, 2);
} else {
Serial.println("ERROR:Invalid output value");
}
} else {
Serial.println("ERROR:Unknown command");
}
}
delay(2000); // wait 2 seconds
}