185 lines
4.4 KiB
C++
185 lines
4.4 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 LED_PIN 16
|
|
#define BUTTON_PIN 12
|
|
#define NUMPIXELS 8
|
|
|
|
#define ESPNOW_WIFI_CHANNEL 6
|
|
|
|
Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
|
|
|
|
// Button variables
|
|
bool lastButtonState = HIGH;
|
|
bool currentButtonState = HIGH;
|
|
unsigned long lastDebounceTime = 0;
|
|
unsigned long debounceDelay = 50;
|
|
|
|
// Broadcast MAC address for ESP-NOW
|
|
uint8_t recvMAC[] = {0x08, 0x3A, 0xF2, 0x39, 0x0A, 0xA8};
|
|
// 08:3A:F2:39:0A:A8
|
|
|
|
#define DELAYVAL 250
|
|
|
|
// Dinner alert state
|
|
// bool dinnerAlert = false;
|
|
// unsigned long dinnerStartTime = 0;
|
|
// unsigned long lastBlinkTime = 0;
|
|
// bool ledState = false;
|
|
|
|
void sendDinnerMessage() {
|
|
const char *message = "dinner";
|
|
esp_err_t result = esp_now_send(recvMAC, (uint8_t *)message, strlen(message));
|
|
|
|
if (result == ESP_OK) {
|
|
Serial.println("Dinner message sent successfully");
|
|
} else {
|
|
Serial.println("Error sending dinner message");
|
|
}
|
|
}
|
|
|
|
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_ack") == 0) {
|
|
Serial.println("Dinner ack received!");
|
|
// dinnerAlert = true;
|
|
// dinnerStartTime = millis();
|
|
// lastBlinkTime = millis();
|
|
// ledState = false;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
// while (!Serial){
|
|
// delay(10);
|
|
// }
|
|
// Serial.println("setup");
|
|
// pixels.begin();
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
// Initialize button pin
|
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
|
|
|
// 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);
|
|
|
|
// Add peer
|
|
esp_now_peer_info_t peerInfo;
|
|
memset(&peerInfo, 0, sizeof(peerInfo));
|
|
memcpy(peerInfo.peer_addr, recvMAC, 6);
|
|
peerInfo.channel = ESPNOW_WIFI_CHANNEL;
|
|
peerInfo.encrypt = false;
|
|
|
|
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
|
Serial.println("Failed to add broadcast peer");
|
|
return;
|
|
}
|
|
Serial.println("Dinner SEND");
|
|
Serial.println("Wi-Fi parameters:");
|
|
Serial.println(" Mode: STA");
|
|
Serial.println(" MAC Address: " + WiFi.macAddress());
|
|
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
|
|
}
|
|
|
|
// void blinkAllLEDs() {
|
|
// if (millis() - lastBlinkTime >= 500) { // Blink every 500ms
|
|
// lastBlinkTime = millis();
|
|
// ledState = !ledState;
|
|
|
|
// if (ledState) {
|
|
// // Turn on all LEDs with bright red color for dinner alert
|
|
// for (int i = 0; i < NUMPIXELS; i++) {
|
|
// pixels.setPixelColor(i, pixels.Color(255, 0, 0));
|
|
// }
|
|
// } else {
|
|
// // Turn off all LEDs
|
|
// pixels.clear();
|
|
// }
|
|
// pixels.show();
|
|
// }
|
|
// }
|
|
|
|
// void normalAnimation() {
|
|
// pixels.clear();
|
|
// for (int i = 0; i < NUMPIXELS; i++) {
|
|
// pixels.setPixelColor(i, pixels.Color(64, 255, 128));
|
|
// pixels.show();
|
|
// delay(DELAYVAL);
|
|
// }
|
|
// }
|
|
|
|
// void dinnerAnimation() {
|
|
// // Check if 60 seconds have passed
|
|
// if (millis() - dinnerStartTime >= 5 * 1000) {
|
|
// dinnerAlert = false;
|
|
// Serial.println("Dinner alert timeout - returning to normal mode");
|
|
// pixels.clear();
|
|
// pixels.show();
|
|
// } else {
|
|
// blinkAllLEDs();
|
|
// }
|
|
// }
|
|
|
|
void checkButton() {
|
|
int reading = digitalRead(BUTTON_PIN);
|
|
|
|
digitalWrite(LED_BUILTIN, !reading);
|
|
|
|
if (reading != lastButtonState) {
|
|
lastDebounceTime = millis();
|
|
}
|
|
|
|
if ((millis() - lastDebounceTime) > debounceDelay) {
|
|
if (reading != currentButtonState) {
|
|
currentButtonState = reading;
|
|
|
|
// Button pressed (LOW because of INPUT_PULLUP)
|
|
if (currentButtonState == LOW) {
|
|
Serial.println("Button pressed! Sending dinner message...");
|
|
sendDinnerMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
lastButtonState = reading;
|
|
}
|
|
|
|
void loop() {
|
|
checkButton();
|
|
|
|
// Check if dinner alert is active
|
|
// if (dinnerAlert) {
|
|
// dinnerAnimation();
|
|
// } else {
|
|
// normalAnimation();
|
|
// }
|
|
}
|