#include // kompajlirat će se samo ako MASTER nije postavljen #ifndef MASTER #define LED 14 #define BUZZER_PIN 16 #define BUZZER_CHANNEL 0 //Mac Address slave-a kojem šaljemo podatke uint8_t peerMacAddress[6] = {0x24, 0x0A, 0xC4, 0xAE, 0x08, 0x68}; // MAC mastera int cnt = 0; int rcv = 0; int oldRcv = 0; long prevTime = 0; long intv = 1000; void setup() { Serial.begin(115200); modeStation(); InitESPNow(); addPeer(peerMacAddress); // dodavanje Slave-a esp_now_register_recv_cb(onDataRecv); // na event primanja podataka esp_now_register_send_cb(onDataSent); // vraća povratni info pinMode(PIN, OUTPUT); pinMode(LED, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); } void onDataRecv(const uint8_t *mac_addr, const uint8_t *value, int len) { Serial.println (*value); if (len > 0){digitalWrite(LED, HIGH); } // kod prvog paljenja ako master nije prisutan ne svijetli link digitalWrite(PIN, *value); if (*value == 1) { cnt++; if (cnt == 1000) { cnt = 0; Alarm_2(); } } rcv++; } //vraća povratnu informaicju samo u dvosmjernoj komunikaciji void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail"); } void Alarm_1() {Door_monitor tone(BUZZER_PIN, NOTE_D4, 250, BUZZER_CHANNEL); noTone(BUZZER_PIN, BUZZER_CHANNEL); delay(100); } void Alarm_2() { tone(BUZZER_PIN, NOTE_C8, 100, BUZZER_CHANNEL); noTone(BUZZER_PIN, BUZZER_CHANNEL); delay(100); tone(BUZZER_PIN, NOTE_C8, 100, BUZZER_CHANNEL); noTone(BUZZER_PIN, BUZZER_CHANNEL); delay(100); } void loop() { unsigned long currentTime = millis(); if(currentTime - prevTime > intv) { prevTime = currentTime; if (rcv > oldRcv) { oldRcv = rcv; } else if (rcv <= oldRcv) { digitalWrite(LED, LOW); Alarm_1(); } if (rcv > 100000){rcv = 0; oldRcv=0; } } } #endif