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
|
@ -1,60 +0,0 @@
|
|||
#ifndef EspNowControl_cpp
|
||||
#define EspNowControl_cpp
|
||||
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
class EspNowControl {
|
||||
typedef struct payload {
|
||||
int id;
|
||||
bool on;
|
||||
float brightness;
|
||||
unsigned long timer;
|
||||
} payload;
|
||||
|
||||
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++){
|
||||
payload data = payload();
|
||||
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