Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
11 kB
4
Indexable
Never
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPUpdateServer.h>
#include <WiFiManager.h>
#include <ArduinoHA.h>
#include <Wire.h>
#include <PCF8575.h>

#define BROKER_ADDR IPAddress(192, 168, 200, 211)
#define I2C_SDA 21
#define I2C_SCL 22
#define PCF8575_ADDRESS 0x20
bool buttonDebounceDelay;

PCF8575 pcf8575;

WebServer server(80);
HTTPUpdateServer httpUpdateServer;
WiFiManager wm;

WiFiClient client;
HADevice device("ESP_ETAJ");
HAMqtt mqtt(client, device);

// devices types go here
HASwitch switch1("mySwitch1");
HASwitch switch2("mySwitch2");
HASwitch switch3("mySwitch3");
HASwitch switch4("mySwitch4");
HASwitch switch5("mySwitch5");
HASwitch switch6("mySwitch6");
HASwitch switch7("mySwitch7");
HASwitch switch8("mySwitch8");
HASwitch switch9("mySwitch9");
HASwitch switch10("mySwitch10");
HASwitch switch11("mySwitch11");
HASwitch switch12("mySwitch12");
HASwitch switch13("mySwitch13");
HASwitch switch14("mySwitch14");
HASwitch switch15("mySwitch15");
HASwitch switch16("mySwitch16");
HASwitch switch17("mySwitch17");
HASwitch switch18("mySwitch18");
HASwitch switch19("mySwitch19");
HASwitch switch20("mySwitch20");

#define NUME_PLACA "ESP_ETAJ"
#define RELEU1 32
#define RELEU2 33
#define RELEU3 25
#define RELEU4 26

#define INTRERUPATOR1 0
#define INTRERUPATOR2 34
#define INTRERUPATOR3 35
#define INTRERUPATOR4 27
#define INTRERUPATOR5 14
#define INTRERUPATOR6 12
#define INTRERUPATOR7 13
#define INTRERUPATOR8 15
#define INTRERUPATOR9 2
#define INTRERUPATOR10 4
#define INTRERUPATOR11 16
#define INTRERUPATOR12 17
#define INTRERUPATOR13 5
#define INTRERUPATOR14 18
#define INTRERUPATOR15 19
#define INTRERUPATOR16 23
#define INTRERUPATOR17 0
#define INTRERUPATOR18 0
#define INTRERUPATOR19 0
#define INTRERUPATOR20 0

void handleNotFound() {  // default webserver response for unknow requests
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void onSwitchCommand(bool state, HASwitch* sender) {
  if (sender == &switch1) {
    pcf8575.write(0, !state);
  } else if (sender == &switch2) {
    pcf8575.write(1, !state);
  } else if (sender == &switch3) {
    pcf8575.write(2, !state);
  } else if (sender == &switch4) {
    pcf8575.write(3, !state);
  } else if (sender == &switch5) {
    pcf8575.write(4, !state);
  } else if (sender == &switch6) {
    pcf8575.write(5, !state);
  } else if (sender == &switch7) {
    pcf8575.write(6, !state);
  } else if (sender == &switch8) {
    pcf8575.write(7, !state);
  } else if (sender == &switch9) {
    pcf8575.write(8, !state);
  } else if (sender == &switch10) {
    pcf8575.write(9, !state);
  } else if (sender == &switch11) {
    pcf8575.write(10, !state);
  } else if (sender == &switch12) {
    pcf8575.write(11, !state);
  } else if (sender == &switch13) {
    pcf8575.write(12, !state);
  } else if (sender == &switch14) {
    pcf8575.write(13, !state);
  } else if (sender == &switch15) {
    pcf8575.write(14, !state);
  } else if (sender == &switch16) {
    pcf8575.write(15, !state);
  } else if (sender == &switch17) {
    digitalWrite(RELEU1, !state);
  } else if (sender == &switch18) {
    digitalWrite(RELEU2, !state);
  } else if (sender == &switch19) {
    digitalWrite(RELEU3, !state);
  } else if (sender == &switch20) {
    digitalWrite(RELEU4, !state);
  }

  sender->setState(state);  // report state back to the Home Assistant
}

void setup() {
  Wire.begin(I2C_SDA, I2C_SCL);
  pcf8575.begin(PCF8575_ADDRESS);
  pcf8575.write16(0xFFFF); // Set all pins to HIGH initially (off)

  pinMode(RELEU1, OUTPUT);
  pinMode(RELEU2, OUTPUT);
  pinMode(RELEU3, OUTPUT);
  pinMode(RELEU4, OUTPUT);

  // aici incepi sa definesti intrerupatoarele ca input
  pinMode(INTRERUPATOR1, INPUT_PULLUP);
  pinMode(INTRERUPATOR2, INPUT_PULLUP);
  pinMode(INTRERUPATOR3, INPUT_PULLUP);
  pinMode(INTRERUPATOR4, INPUT_PULLUP);
  pinMode(INTRERUPATOR5, INPUT_PULLUP);
  pinMode(INTRERUPATOR6, INPUT_PULLUP);
  pinMode(INTRERUPATOR7, INPUT_PULLUP);
  pinMode(INTRERUPATOR8, INPUT_PULLUP);
  pinMode(INTRERUPATOR9, INPUT_PULLUP);
  pinMode(INTRERUPATOR10, INPUT_PULLUP);
  pinMode(INTRERUPATOR11, INPUT_PULLUP);
  pinMode(INTRERUPATOR12, INPUT_PULLUP);
  pinMode(INTRERUPATOR13, INPUT_PULLUP);
  pinMode(INTRERUPATOR14, INPUT_PULLUP);
  pinMode(INTRERUPATOR15, INPUT_PULLUP);
  pinMode(INTRERUPATOR16, INPUT_PULLUP);
  pinMode(INTRERUPATOR17, INPUT_PULLUP);
  pinMode(INTRERUPATOR18, INPUT_PULLUP);
  pinMode(INTRERUPATOR19, INPUT_PULLUP);
  pinMode(INTRERUPATOR20, INPUT_PULLUP);

  digitalWrite(RELEU1, HIGH);
  digitalWrite(RELEU2, HIGH);
  digitalWrite(RELEU3, HIGH);
  digitalWrite(RELEU4, HIGH);
  delay(100);

  WiFi.mode(WIFI_AP_STA);
  wm.setConfigPortalTimeout(120);
  bool res = wm.autoConnect("ESP_ETAJ");
  if (!res) {
    ESP.restart();
  }

  httpUpdateServer.setup(&server);

  server.on("/", []() {  // trigger manual reset
    server.send(200, "text/html", NUME_PLACA);
  });

  server.on("/reset", []() {  // trigger manual reset
    server.send(200, "text/html", "reset");
    delay(1000);
    ESP.restart();
  });

  server.onNotFound(handleNotFound);
  server.begin();

  switch1.onCommand(onSwitchCommand);
  switch1.setName("sw-etaj-1");
  switch1.setIcon("mdi:lightbulb");
  switch2.onCommand(onSwitchCommand);
  switch2.setName("sw-etaj-2");
  switch2.setIcon("mdi:lightbulb");
  switch3.onCommand(onSwitchCommand);
  switch3.setName("sw-etaj-3");
  switch3.setIcon("mdi:lightbulb");
  switch4.onCommand(onSwitchCommand);
  switch4.setName("sw-etaj-4");
  switch4.setIcon("mdi:lightbulb");
  switch5.onCommand(onSwitchCommand);
  switch5.setName("sw-etaj-5");
  switch5.setIcon("mdi:lightbulb");
  switch6.onCommand(onSwitchCommand);
  switch6.setName("sw-etaj-6");
  switch6.setIcon("mdi:lightbulb");
  switch7.onCommand(onSwitchCommand);
  switch7.setName("sw-etaj-7");
  switch7.setIcon("mdi:lightbulb");
  switch8.onCommand(onSwitchCommand);
  switch8.setName("sw-etaj-8");
  switch8.setIcon("mdi:lightbulb");
  switch9.onCommand(onSwitchCommand);
  switch9.setName("sw-etaj-9");
  switch9.setIcon("mdi:lightbulb");
  switch10.onCommand(onSwitchCommand);
  switch10.setName("sw10");
  switch10.setIcon("mdi:lightbulb");
  switch11.onCommand(onSwitchCommand);
  switch11.setName("sw11");
  switch11.setIcon("mdi:lightbulb");
  switch12.onCommand(onSwitchCommand);
  switch12.setName("sw12");
  switch12.setIcon("mdi:lightbulb");
  switch13.onCommand(onSwitchCommand);
  switch13.setName("sw13");
  switch13.setIcon("mdi:lightbulb");
  switch14.onCommand(onSwitchCommand);
  switch14.setName("sw14");
  switch14.setIcon("mdi:lightbulb");
  switch15.onCommand(onSwitchCommand);
  switch15.setName("sw15");
  switch15.setIcon("mdi:lightbulb");
  switch16.onCommand(onSwitchCommand);
  switch16.setName("sw16");
  switch16.setIcon("mdi:lightbulb");
  switch17.onCommand(onSwitchCommand);
  switch17.setName("sw17");
  switch17.setIcon("mdi:lightbulb");
  switch18.onCommand(onSwitchCommand);
  switch18.setName("sw18");
  switch18.setIcon("mdi:lightbulb");
  switch19.onCommand(onSwitchCommand);
  switch19.setName("sw19");
  switch19.setIcon("mdi:lightbulb");
  switch20.onCommand(onSwitchCommand);
  switch20.setName("sw20");
  switch20.setIcon("mdi:lightbulb");

  mqtt.begin(BROKER_ADDR);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(INTRERUPATOR1) == LOW) {
    pcf8575.write(0, switch1.getCurrentState());
    switch1.setState(!switch1.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR2) == LOW) {
    pcf8575.write(1, switch2.getCurrentState());
    switch2.setState(!switch2.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR3) == LOW) {
    pcf8575.write(2, switch3.getCurrentState());
    switch3.setState(!switch3.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR4) == LOW) {
    pcf8575.write(3, switch4.getCurrentState());
    switch4.setState(!switch4.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR5) == LOW) {
    pcf8575.write(4, switch5.getCurrentState());
    switch5.setState(!switch5.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR6) == LOW) {
    pcf8575.write(5, switch6.getCurrentState());
    switch6.setState(!switch6.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR7) == LOW) {
    pcf8575.write(6, switch7.getCurrentState());
    switch7.setState(!switch7.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR8) == LOW) {
    pcf8575.write(7, switch8.getCurrentState());
    switch8.setState(!switch8.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR9) == LOW) {
    pcf8575.write(8, switch9.getCurrentState());
    switch9.setState(!switch9.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR10) == LOW) {
    pcf8575.write(9, switch10.getCurrentState());
    switch10.setState(!switch10.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR11) == LOW) {
    pcf8575.write(10, switch11.getCurrentState());
    switch11.setState(!switch11.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR12) == LOW) {
    pcf8575.write(11, switch12.getCurrentState());
    switch12.setState(!switch12.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR13) == LOW) {
    pcf8575.write(12, switch13.getCurrentState());
    switch13.setState(!switch13.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR14) == LOW) {
    pcf8575.write(13, switch14.getCurrentState());
    switch14.setState(!switch14.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR15) == LOW) {
    pcf8575.write(14, switch15.getCurrentState());
    switch15.setState(!switch15.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR16) == LOW) {
    pcf8575.write(15, switch16.getCurrentState());
    switch16.setState(!switch16.getCurrentState());
    buttonDebounceDelay = true; 
  } else if (digitalRead(INTRERUPATOR17) == LOW) {
    digitalWrite(RELEU1, switch17.getCurrentState());
    switch17.setState(!switch17.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR18) == LOW) {
    digitalWrite(RELEU2, switch18.getCurrentState());
    switch18.setState(!switch18.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR19) == LOW) {
    digitalWrite(RELEU3, switch19.getCurrentState());
    switch19.setState(!switch19.getCurrentState());
    buttonDebounceDelay = true;
  } else if (digitalRead(INTRERUPATOR20) == LOW) {
    digitalWrite(RELEU4, switch20.getCurrentState());
    switch20.setState(!switch20.getCurrentState());
    buttonDebounceDelay = true;
  }

  server.handleClient();
  mqtt.loop();
  if (buttonDebounceDelay) {
    delay(300);
    buttonDebounceDelay = false;
  }
}
Leave a Comment