130 lines
2.9 KiB
C++
130 lines
2.9 KiB
C++
// FireBeetle 2 ESP32-E
|
|
// 5v power for the LEDs taken from the
|
|
// bottom of the board under the USB connector.
|
|
// 470 Ohm resistor between the data line and the LED strip.
|
|
// See https://esp32io.com/tutorials/esp32-ws2812b-led-strip
|
|
#include <Adafruit_NeoPixel.h>
|
|
#include <WiFi.h>
|
|
#include <esp_now.h>
|
|
|
|
#define PIN 16
|
|
#define NUMPIXELS 4
|
|
|
|
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ400);
|
|
|
|
// #define DELAYVAL 250
|
|
|
|
#define ESPNOW_WIFI_CHANNEL 6
|
|
|
|
// Dinner alert state
|
|
bool dinnerAlert = false;
|
|
unsigned long dinnerStartTime = 0;
|
|
unsigned long lastBlinkTime = 0;
|
|
bool ledState = false;
|
|
|
|
uint8_t breath = 0;
|
|
uint32_t last_breath_time = 0;
|
|
|
|
void onDataReceived(const esp_now_recv_info *mac_addr,
|
|
const unsigned char *data, int data_len) {
|
|
char message[data_len + 1];
|
|
memcpy(message, data, data_len);
|
|
message[data_len] = '\0';
|
|
|
|
Serial.print("Received message: ");
|
|
Serial.println(message);
|
|
|
|
if (strcmp(message, "dinner") == 0) {
|
|
Serial.println("Dinner alert received!");
|
|
dinnerAlert = true;
|
|
dinnerStartTime = millis();
|
|
lastBlinkTime = millis();
|
|
ledState = false;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
// while (!Serial){
|
|
// delay(10);
|
|
// }
|
|
pixels.begin();
|
|
|
|
// Initialize WiFi in station mode
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
|
|
while (!WiFi.STA.started()) {
|
|
delay(100);
|
|
}
|
|
|
|
// Initialize ESP-NOW
|
|
if (esp_now_init() != ESP_OK) {
|
|
Serial.println("Error initializing ESP-NOW");
|
|
return;
|
|
}
|
|
|
|
// Register callback function
|
|
esp_now_register_recv_cb(onDataReceived);
|
|
Serial.println("ESP-NOW initialized and ready to receive messages");
|
|
|
|
Serial.println("Dinner RECV");
|
|
Serial.println("Wi-Fi parameters:");
|
|
Serial.println(" Mode: STA");
|
|
Serial.println(" MAC Address: " + WiFi.macAddress());
|
|
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
|
|
|
|
// Serial.println("setup done");
|
|
}
|
|
|
|
void blinkAllLEDs() {
|
|
if (millis() - lastBlinkTime >= 2000) {
|
|
lastBlinkTime = millis();
|
|
ledState = !ledState;
|
|
|
|
if (ledState) {
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
|
|
}
|
|
} else {
|
|
pixels.clear();
|
|
}
|
|
pixels.show();
|
|
}
|
|
}
|
|
|
|
void breathe() {
|
|
uint8_t val = breath > 128 ? 256 - breath : breath;
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
pixels.setPixelColor(i, pixels.Color(0, 0, val));
|
|
pixels.show();
|
|
}
|
|
}
|
|
|
|
void dinnerAnimation() {
|
|
if (millis() - dinnerStartTime > 15 * 1000) {
|
|
dinnerAlert = false;
|
|
Serial.println("Dinner alert timeout - returning to normal mode");
|
|
pixels.clear();
|
|
pixels.show();
|
|
} else {
|
|
blinkAllLEDs();
|
|
}
|
|
}
|
|
|
|
void breathAnimation() {
|
|
unsigned long time = millis();
|
|
if (time % 25 == 0 && time != last_breath_time) {
|
|
breath++;
|
|
last_breath_time = time;
|
|
breathe();
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
if (dinnerAlert) {
|
|
dinnerAnimation();
|
|
} else {
|
|
breathAnimation();
|
|
}
|
|
}
|