Move ESP-NOW lighting control to lib
This commit is contained in:
parent
5b69f482c3
commit
76b8fed961
5 changed files with 33 additions and 34 deletions
54
lib/EspNowLightControl/EspNowLightControlClient.cpp
Normal file
54
lib/EspNowLightControl/EspNowLightControlClient.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#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
|
Loading…
Add table
Add a link
Reference in a new issue