Penerima Waktu Tunda WiFi

#ifdef ESP32
#include <esp_now.h>
#include <WiFi.h>
#else
#include <espnow.h>
#include <ESP8266WiFi.h>
#define ESP_OK 0
#endif

#define lampPin 0

bool lampON = false;
bool lock = false;


// struktur untuk pengiriman data harus sama dengan struktur pada penerima
typedef struct struct_message {
  int id;     //id pengirim, lock counter
  int x;      //on interval counter
  int y;      //motion (>0)
} struct_message;

struct_message myData;

// Membuat struktur untuk tiap board
struct_message board1;

struct_message boardsStruct[3] = {board1};

volatile int counter = 0;
int max_counter = 0;
int lock_counter = 1;

// callback saat data diterima
#ifdef ESP32
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len)
#else
void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len)
#endif
{
  char macStr[18];
  Serial.print("Paket diterima dari: ");
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.println(macStr);
  memcpy(&amp;myData, incomingData, sizeof(myData));
  Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
  // Update the structures with the new incoming data
  boardsStruct[myData.id - 1].x = myData.x;
  boardsStruct[myData.id - 1].y = myData.y;
  Serial.printf("x value: %d \n", boardsStruct[myData.id - 1].x);
  Serial.printf("y value: %d \n", boardsStruct[myData.id - 1].y);
  Serial.println();

  if (!lock) counter = 0;

}


void setup() {
  Serial.begin(74880);
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  //daftrakan callback
  esp_now_register_recv_cb(OnDataRecv);

  pinMode(lampPin, OUTPUT);
  Serial.println("Turn Off lamp");
  digitalWrite(lampPin, LOW);
  lampON = false;
}


void loop() {
  // Mengakses data dari setiap board
  //Serial.println(boardsStruct[0].x,DEC);
  //delay(1000);

  //update counter
  if (boardsStruct[0].x > 0)
    max_counter = boardsStruct[0].x;

  if (boardsStruct[0].id > 0)
    lock_counter = boardsStruct[0].id;

  //get motion information and restart counter?
  if (boardsStruct[0].y > 0 &amp;&amp; counter == 0) {
    if (!lampON) {
      Serial.println("Turn On lamp");
        digitalWrite(lampPin, HIGH);

      lampON = true;
    }
  }


  //stop counter turn off lamp
  if (counter >= max_counter &amp;&amp; lampON) {
    Serial.println("Turn Off lamp");
  digitalWrite(lampPin, LOW);
    lampON = false;
    lock = true;
    Serial.println("Lock");
    delay(lock_counter * 1000);
    Serial.println("Release");
    lock = false;
  }

  if (lampON) {
    counter++;
    Serial.print(max_counter - counter);
    delay(1000);
  }

}