/* Hardware Arduino Mega - Ethernetshield W5100 - 8 Relaisboard - Digital 0-10 volt module - 2 Relaisboard Includes 5 Analoge Sensors, 4 Digital Sensors (Triggers), 4 Buttons, 9 Switches (Relais), 4 Dimmers (Fans) */ #include #include #include #define AS_1_PIN A8 #define AS_2_PIN A9 #define AS_3_PIN A10 #define AS_4_PIN A11 #define AS_5_PIN A12 #define DS_1_PIN 23 #define DS_2_PIN 25 #define DS_3_PIN 27 #define DS_4_PIN 29 #define Button_1_PIN 31 #define Button_2_PIN 33 #define Button_3_PIN 35 #define Button_4_PIN 37 #define Relais_1_PIN 36 #define Relais_2_PIN 34 #define Relais_3_PIN 32 #define Relais_4_PIN 30 #define Relais_5_PIN 28 #define Relais_6_PIN 26 #define Relais_7_PIN 24 #define Relais_8_PIN 22 #define Relais_9_PIN 38 #define Dimmer_State_1_PIN 40 #define Dimmer_State_2_PIN 42 #define Dimmer_State_3_PIN 44 #define Dimmer_State_4_PIN 46 #define Dimmer_Speed_1_PIN 4 //choose PWM Pin for this purpose #define Dimmer_Speed_2_PIN 8 #define Dimmer_Speed_3_PIN 12 #define Dimmer_Speed_4_PIN 13 #define BROKER_ADDR IPAddress(192,168,x,xx) // IP adres of MQTT-Homeassistant-server #define BROKER_USERNAME "xxxxxxx" // Login-account MQTT-Server #define BROKER_PASSWORD "xxxxxx" // PW MQTT-Server // Function for Temperature Calculation int TempCalc (int V){ int An0 = 415; // Analog input value at 0C int An19 = 431; // Analog input value at 19C return (V-An0) * 19/(An19-An0); } int TempValue1; int TempValue2; int TempValue3; int TempValue4; int TempValue5; unsigned long lastReadAt = millis(); unsigned long lastSentAt = millis(); bool lastInputState = false; byte mac[] = { 0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A }; // MAC-Arduino Board IPAddress staticIP(192, 168, 2, 45); // IP-Arduino Board IPAddress gateway(192, 168, 2, 1); // IP-Gateway IPAddress dns (192, 168, 2, 1); // DNS-server IPAddress subnet(255, 255, 255, 0); // Subnet-Mask EthernetClient client; HADevice device(mac, sizeof(mac)); HAMqtt mqtt(client, device); HATriggers triggers; ToggleButton Button_1(Button_1_PIN, false, 50, true, true); ToggleButton Button_2(Button_2_PIN, false, 50, true, true); ToggleButton Button_3(Button_3_PIN, false, 50, true, true); ToggleButton Button_4(Button_4_PIN, false, 50, true, true); HASensor Temp1("Temp1"); // "Temp" is unique ID of the sensor. You should define your own ID. HASensor Temp2("Temp2"); HASensor Temp3("Temp3"); HASensor Temp4("Temp4"); HASensor Temp5("Temp5"); HABinarySensor Sensor1("Input1", "moisture", true); // "input" is unique ID of the sensor. You should define you own ID. HABinarySensor Sensor2("Input2", "power", true); // "door" is device class (based on the class HA displays different icons in the panel) HABinarySensor Sensor3("Input3", "safety", true); // "true" is initial state of the sensor. In this example it's "true" as we use pullup resistor HABinarySensor Sensor4("Input4", "door", true); HASwitch Relais_1("Relais_1", false); // "Relais_1" is unique ID of the switch. False = initial state of the Relay in HA HASwitch Relais_2("Relais_2", false); HASwitch Relais_3("Relais_3", false); HASwitch Relais_4("Relais_4", false); HASwitch Relais_5("Relais_5", false); HASwitch Relais_6("Relais_6", false); HASwitch Relais_7("Relais_7", false); HASwitch Relais_8("Relais_8", false); HASwitch Relais_9("Relais_9", false); HAFan Dimmer_1("Dimmer_1", HAFan::SpeedsFeature);// HAFan::SpeedsFeature enables support for setting different speeds of fan. HAFan Dimmer_2("Dimmer_2", HAFan::SpeedsFeature); HAFan Dimmer_3("Dimmer_3", HAFan::SpeedsFeature); HAFan Dimmer_4("Dimmer_4", HAFan::SpeedsFeature); void onFanStateChanged(bool state, HAFan* f){ if (f == &Dimmer_1) { digitalWrite(Dimmer_State_1_PIN, (state ? LOW : HIGH)); // Setting Dimmer state (On/Off) } else if (f == &Dimmer_2) { digitalWrite(Dimmer_State_2_PIN, (state ? LOW : HIGH)); // Setting Dimmer state (On/Off) } else if (f == &Dimmer_3) { digitalWrite(Dimmer_State_3_PIN, (state ? LOW : HIGH)); // Setting Dimmer state (On/Off) } else if (f == &Dimmer_4) { digitalWrite(Dimmer_State_4_PIN, (state ? LOW : HIGH)); // Setting Dimmer state (On/Off) } } void onFanSpeedChanged(uint16_t speed, HAFan* p){ if (p== &Dimmer_1) { analogWrite(Dimmer_Speed_1_PIN, speed); // By changing values from 0 to 255 you can control motor speed } else if (p == &Dimmer_2) { analogWrite(Dimmer_Speed_2_PIN, speed); // By changing values from 0 to 255 you can control motor speed } else if (p == &Dimmer_3) { analogWrite(Dimmer_Speed_3_PIN, speed); // By changing values from 0 to 255 you can control motor speed } else if (p == &Dimmer_4) { analogWrite(Dimmer_Speed_4_PIN, speed); // By changing values from 0 to 255 you can control motor speed } } void onSwitchStateChanged(bool state, HASwitch* s){ if (s == &Relais_1) { digitalWrite(Relais_1_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_2) { digitalWrite(Relais_2_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_3) { digitalWrite(Relais_3_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_4) { digitalWrite(Relais_4_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_5) { digitalWrite(Relais_5_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_6) { digitalWrite(Relais_6_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_7) { digitalWrite(Relais_7_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_8) { digitalWrite(Relais_8_PIN, (state ? LOW : HIGH)); } else if (s == &Relais_9) { digitalWrite(Relais_9_PIN, (state ? LOW : HIGH)); } } void setup() { // initialize the button objects triggers.add("button_short_press", "Button_1"); triggers.add("button_short_press", "Button_2"); triggers.add("button_short_press", "Button_3"); triggers.add("button_short_press", "Button_4"); Button_1.begin(); Button_2.begin(); Button_3.begin(); Button_4.begin(); // configure analog sensors (optional) Temp1.setUnitOfMeasurement("°C"); Temp1.setDeviceClass("Temperature"); Temp1.setIcon("mdi:home"); Temp1.setName("Temperature 1"); Temp2.setUnitOfMeasurement("°C"); Temp2.setDeviceClass("Temperature"); Temp2.setIcon("mdi:home"); Temp2.setName("Temperature 2"); Temp3.setUnitOfMeasurement("°C"); Temp3.setDeviceClass("Temperature"); Temp3.setIcon("mdi:home"); Temp3.setName("Temperature 3"); Temp4.setUnitOfMeasurement("°C"); Temp4.setDeviceClass("Temperature"); Temp4.setIcon("mdi:home"); Temp4.setName("Temperature 4"); Temp5.setUnitOfMeasurement("°C"); Temp5.setDeviceClass("Temperature"); Temp5.setIcon("mdi:home"); Temp5.setName("Temperature 5"); pinMode(DS_1_PIN, INPUT_PULLUP); lastInputState = digitalRead(DS_1_PIN); pinMode(DS_2_PIN, INPUT_PULLUP); lastInputState = digitalRead(DS_2_PIN); pinMode(DS_3_PIN, INPUT_PULLUP); lastInputState = digitalRead(DS_3_PIN); pinMode(DS_4_PIN, INPUT_PULLUP); lastInputState = digitalRead(DS_4_PIN); digitalWrite(Relais_1_PIN, HIGH); pinMode(Relais_1_PIN, OUTPUT); digitalWrite(Relais_2_PIN, HIGH); pinMode(Relais_2_PIN, OUTPUT); digitalWrite(Relais_3_PIN, HIGH); pinMode(Relais_3_PIN, OUTPUT); digitalWrite(Relais_4_PIN, HIGH); pinMode(Relais_4_PIN, OUTPUT); digitalWrite(Relais_5_PIN, HIGH); pinMode(Relais_5_PIN, OUTPUT); digitalWrite(Relais_6_PIN, HIGH); pinMode(Relais_6_PIN, OUTPUT); digitalWrite(Relais_7_PIN, HIGH); pinMode(Relais_7_PIN, OUTPUT); digitalWrite(Relais_8_PIN, HIGH); pinMode(Relais_8_PIN, OUTPUT); digitalWrite(Relais_9_PIN, HIGH); pinMode(Relais_9_PIN, OUTPUT); pinMode(Dimmer_State_1_PIN, OUTPUT); // Set pin for output to control Dimmer State (On/Off) digitalWrite(Dimmer_State_1_PIN, HIGH); pinMode(Dimmer_Speed_1_PIN, OUTPUT); // Set pin for output to control Dimmer Speed (0-100%) pinMode(Dimmer_State_2_PIN, OUTPUT); digitalWrite(Dimmer_State_2_PIN, HIGH); pinMode(Dimmer_Speed_2_PIN, OUTPUT); pinMode(Dimmer_State_3_PIN, OUTPUT); digitalWrite(Dimmer_State_3_PIN, HIGH); pinMode(Dimmer_Speed_3_PIN, OUTPUT); pinMode(Dimmer_State_4_PIN, OUTPUT); digitalWrite(Dimmer_State_4_PIN, HIGH); pinMode(Dimmer_Speed_4_PIN, OUTPUT); // you don't need to verify return status Ethernet.begin(mac, staticIP, dns, gateway, subnet); Serial.begin(9600); // set device's details (optional) device.setManufacturer("Arduino"); device.setModel("ATmega2560"); device.setName("Zwembad"); device.setSoftwareVersion("27.01.2022"); // set icon (optional) Relais_1.setIcon("mdi:electric-switch"); Relais_1.setName("Relais_1"); Relais_2.setIcon("mdi:electric-switch"); Relais_2.setName("Relais_2"); Relais_3.setIcon("mdi:electric-switch"); Relais_3.setName("Relais_3"); Relais_4.setIcon("mdi:electric-switch"); Relais_4.setName("Relais_4"); Relais_5.setIcon("mdi:electric-switch"); Relais_5.setName("Relais_5"); Relais_6.setIcon("mdi:electric-switch"); Relais_6.setName("Relais_6"); Relais_7.setIcon("mdi:electric-switch"); Relais_7.setName("Relais_7"); Relais_8.setIcon("mdi:electric-switch"); Relais_8.setName("Relais_8"); Relais_9.setIcon("mdi:electric-switch"); Relais_9.setName("Relais_9"); // configure fan (or Dimmer in our case) (optional) Dimmer_1.setName("Dimmer_1"); Dimmer_1.setRetain(true); Dimmer_1.setSpeedRangeMin(1); // Min-Max = 0-100 Dimmer_1.setSpeedRangeMax(255); // Min-Max = 0-100 Dimmer_2.setName("Dimmer_2"); Dimmer_2.setRetain(true); Dimmer_2.setSpeedRangeMin(1); // Min-Max = 0-100 Dimmer_2.setSpeedRangeMax(255); // Min-Max = 0-100 Dimmer_3.setName("Dimmer_3"); Dimmer_3.setRetain(true); Dimmer_3.setSpeedRangeMin(1); // Min-Max = 0-100 Dimmer_3.setSpeedRangeMax(255); // Min-Max = 0-100 Dimmer_4.setName("Dimmer_4"); Dimmer_4.setRetain(true); Dimmer_4.setSpeedRangeMin(1); // Min-Max = 0-100 Dimmer_4.setSpeedRangeMax(255); // Min-Max = 0-100 // configure DigitalSensors (optional) Sensor1.setName("Alarm-1"); Sensor2.setName("Alarm-2"); Sensor3.setName("Alarm-3"); Sensor4.setName("Alarm-4"); // handle switch state Relais_1.onStateChanged(onSwitchStateChanged); Relais_2.onStateChanged(onSwitchStateChanged); Relais_3.onStateChanged(onSwitchStateChanged); Relais_4.onStateChanged(onSwitchStateChanged); Relais_5.onStateChanged(onSwitchStateChanged); Relais_6.onStateChanged(onSwitchStateChanged); Relais_7.onStateChanged(onSwitchStateChanged); Relais_8.onStateChanged(onSwitchStateChanged); Relais_9.onStateChanged(onSwitchStateChanged); // handle fan state and speed Dimmer_1.onStateChanged(onFanStateChanged); Dimmer_2.onStateChanged(onFanStateChanged); Dimmer_3.onStateChanged(onFanStateChanged); Dimmer_4.onStateChanged(onFanStateChanged); Dimmer_1.onSpeedChanged(onFanSpeedChanged); Dimmer_2.onSpeedChanged(onFanSpeedChanged); Dimmer_3.onSpeedChanged(onFanSpeedChanged); Dimmer_4.onSpeedChanged(onFanSpeedChanged); mqtt.begin(BROKER_ADDR, BROKER_USERNAME, BROKER_PASSWORD); } void loop() { mqtt.loop(); // read the buttons Button_1.read(); Button_2.read(); Button_3.read(); Button_4.read(); // if button state changed, update Triggers if (Button_1.changed()) triggers.trigger("button_short_press", "Button_1"); if (Button_2.changed()) triggers.trigger("button_short_press", "Button_2"); if (Button_3.changed()) triggers.trigger("button_short_press", "Button_3"); if (Button_4.changed()) triggers.trigger("button_short_press", "Button_4"); if ((millis() - lastSentAt) >= 5000) { lastSentAt = millis(); TempValue1=TempCalc(analogRead (AS_1_PIN)); Temp1.setValue(TempValue1); TempValue2=TempCalc(analogRead (AS_2_PIN)); Temp2.setValue(TempValue2); TempValue3=TempCalc(analogRead (AS_3_PIN));; Temp3.setValue(TempValue3); TempValue4=TempCalc(analogRead (AS_4_PIN)); Temp4.setValue(TempValue4); TempValue5=TempCalc(analogRead (AS_5_PIN)); Temp5.setValue(TempValue5); } if ((millis() - lastReadAt) > 30) { // read in 30ms interval // library produces MQTT message if a new state is different than the previous one Sensor1.setState(digitalRead(DS_1_PIN)); lastInputState = Sensor1.getState(); Sensor2.setState(digitalRead(DS_2_PIN)); lastInputState = Sensor2.getState(); Sensor3.setState(digitalRead(DS_3_PIN)); lastInputState = Sensor3.getState(); Sensor4.setState(digitalRead(DS_4_PIN)); lastInputState = Sensor4.getState(); lastReadAt = millis(); } }