54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#ifndef EspNowLightControlClient_cpp
|
|
#define EspNowLightControlClient_cpp
|
|
|
|
#include <esp_now.h>
|
|
#include <WiFi.h>
|
|
#include "LightControlPayload.cpp"
|
|
|
|
class EspNowLightControlClient {
|
|
uint8_t broadcastAddress[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
public:
|
|
bool init() {
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
if (esp_now_init() != ESP_OK)
|
|
return false;
|
|
|
|
esp_now_peer_info_t peerInfo = {};
|
|
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
|
|
peerInfo.channel = 0;
|
|
peerInfo.encrypt = false;
|
|
|
|
esp_err_t peerResult = esp_now_add_peer(&peerInfo);
|
|
if (peerResult != ESP_OK) {
|
|
Serial.println("Failed to add peer");
|
|
Serial.println(esp_err_to_name(peerResult));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void send() {
|
|
for (int i = 1; i <= 5; i++) {
|
|
LightControlPayload data = LightControlPayload();
|
|
data.id = i;
|
|
data.brightness = 1;
|
|
data.on = false;
|
|
data.timer = 0;
|
|
|
|
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&data, sizeof(data));
|
|
if (result == ESP_OK)
|
|
Serial.println("Sent with success");
|
|
else {
|
|
Serial.println("Error sending the data");
|
|
Serial.println(esp_err_to_name(result));
|
|
}
|
|
|
|
delay(100);
|
|
}
|
|
}
|
|
};
|
|
|
|
#endif
|