Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Klasa porządkująca kod w przypadku użycia wielu włączników.
- Version: 1.0. Beta !!!!!!
- Uwagi:
- 1. Musisz użyć fizycznie włącznika, żeby był 'widziany' przez HA. !!!!!!
- 2. Przy każdym ponownym uruchomieniu Arduino, do serwera wysyłana jest informacja 'przedstawiająca' bramkę Mysensors.
- Na obeną chwile jest to zrobione specjalnie, ponieważ kod jest w fazie testów.
- 3. Poniżej przykład dodania bramki do HA:
- mysensors:
- gateways:
- - device: '/dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_55832343538351304042-if00'
- persistence_file: 'mysensors.json'
- 4. U mnie działa. :)
- */
- #include <Arduino.h>
- // Enable debug prints to serial monitor
- #define MY_DEBUG
- // Enable serial gateway
- #define MY_GATEWAY_SERIAL
- #include <SPI.h>
- #include <MySensors.h>
- #include <Bounce2.h>
- #define RELAY_ON 1 // GPIO value to write to turn on attached relay
- #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
- /*
- Klasa która porządkuje kod w przypadku użycia wielu wlączników
- */
- class Wlacznik
- {
- private:
- public:
- uint8_t ID; // id obiektu - każda wartosc musi być inna !!!!!!
- uint8_t RELAY_PIN; // pin przekaznika
- uint8_t BUTTON_PIN; // pin przycisku
- uint8_t ON; // stan na wyjsciu przekaznika gdy wlaczony
- uint8_t OFF; // stan na wyjsciu przekaznika gdy wylaczony
- MyMessage msg; // obiekt wysylajacy informacje o zdarzeniu do serwera HA
- Bounce debouncer;
- bool initialValueSent = false;
- Wlacznik(uint8_t id, uint8_t relay_pin, uint8_t button_pin, uint8_t on, uint8_t off);
- Wlacznik();
- void setup_();
- void present_();
- void loop_();
- void debouncerUpdated();
- void checkMessage(MyMessage message);
- };
- Wlacznik::Wlacznik(uint8_t id, uint8_t relay_pin, uint8_t button_pin, uint8_t on, uint8_t off)
- {
- ID = id;
- RELAY_PIN = relay_pin;
- BUTTON_PIN = button_pin;
- ON = on;
- OFF = off;
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, loadState(ID) ? ON : OFF); // przywraca poprzedni zapisany stan przekaznika w przypadku zaniku napiecia lub restartu
- debouncer = Bounce();
- msg = MyMessage(ID, V_STATUS);
- }
- Wlacznik::Wlacznik()
- {
- }
- void Wlacznik::setup_()
- {
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- debouncer.attach(BUTTON_PIN);
- debouncer.interval(10);
- }
- void Wlacznik::present_()
- {
- present(ID, S_BINARY);
- send(msg.set(loadState(ID))); // wysyłam do serwera aktualny stan przekaźnika
- }
- void Wlacznik::loop_()
- {
- if (debouncer.update())
- {
- debouncerUpdated();
- }
- }
- void Wlacznik::debouncerUpdated()
- {
- if (debouncer.read() == LOW)
- {
- saveState(ID, !loadState(ID));
- digitalWrite(RELAY_PIN, loadState(ID) ? ON : OFF);
- send(msg.set(loadState(ID)));
- }
- }
- void Wlacznik::checkMessage(MyMessage message)
- {
- if (message.sensor == ID)
- {
- digitalWrite(RELAY_PIN, message.getBool() ? ON : OFF);
- // Store state in eeprom
- saveState(message.sensor, message.getBool());
- send(msg.set(message.getBool() ? RELAY_ON : RELAY_OFF));
- }
- }
- //--------------------------------------------------------------------------------------------------------------------------------------------
- //Tutaj dodajemy urządzenia
- //--------------------------------------------------------------------------------------------------------------------------------------------
- // Włączniki
- Wlacznik w1; // Włącznik 1 - analogicznie kolejne
- void before()
- {
- // (id, pin przekaźnika, pin przycisku,stan przekażnika przy włączeniu, stan przekażnika przy wyłączeniu)
- w1 = Wlacznik(1, 31, A1, RELAY_ON, RELAY_OFF); // // Włącznik 1 - analogiknie kolejne
- }
- void setup()
- {
- delay(1000);
- w1.setup_(); // Włącznik 1 - analogicznie kolejne
- }
- void presentation()
- {
- // Send the sketch version information to the gateway and Controller
- sendSketchInfo("MySensors_Ard1_HA", "1.0"); // Dla każdej kolejnej bramki inna nazwa !!!
- w1.present_(); // Włącznik 1 - analogicznie kolejne
- }
- void loop()
- {
- w1.loop_(); // Włącznik 1 - analogicznie kolejne
- }
- void receive(const MyMessage &message)
- {
- w1.checkMessage(message); // Włącznik 1 - analogicznie kolejne
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement