#include #include "ESP32_NOW.h" #include "WiFi.h" #include // For the MAC2STR and MACSTR macros #define ESPNOW_WIFI_CHANNEL 6 // Define pins for rotary encoder and button #define pinCLK 32 #define pinDT 35 #define pinSW 34 #define pinLED 23 #define pinRocker1 25 // Rocker switch 1 connected to GPIO 25 #define pinRocker2 26 // Rocker switch 2 connected to GPIO 26 #define pinRocker3 27 // Rocker switch 3 connected to GPIO 27 #define pinRocker4 14 // Rocker switch 4 connected to GPIO 14 // Network credentials const char* ssid = "Redmi Note 9 Pro"; const char* password = "3bad6b8ba36e"; // Variables to track the count and last action int rotaryCount = 1; // Variable to keep track of the rotary encoder count int previousStateSW; // Variable to store the previous state of the SW pin int previousStateCLK; // Variable to store the previous state of the CLK pin int previousRockerState1; // Variable to store the previous state of Rocker switch 1 int previousRockerState2; // Variable to store the previous state of Rocker switch 2 int previousRockerState3; // Variable to store the previous state of Rocker switch 3 int previousRockerState4; // Variable to store the previous state of Rocker switch 4 int activatedRockerCount = 1; // Counter for the total number of activated rocker switches /* Classes */ // Creating a new class that inherits from the ESP_NOW_Peer class is required. class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer { public: // Constructor of the class using the broadcast address ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {} // Destructor of the class ~ESP_NOW_Broadcast_Peer() { remove(); } // Function to properly initialize the ESP-NOW and register the broadcast peer bool begin() { if (!ESP_NOW.begin() || !add()) { log_e("Failed to initialize ESP-NOW or register the broadcast peer"); return false; } return true; } // Function to send a message to all devices within the network bool send_message(const uint8_t *data, size_t len) { if (!send(data, len)) { log_e("Failed to broadcast message"); return false; } return true; } }; // Create a broadcast peer object ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL); void setup() { Serial.begin(115200); // Initialize serial communication at 115200 baud rate // Initialize the Wi-Fi module WiFi.mode(WIFI_STA); WiFi.setChannel(ESPNOW_WIFI_CHANNEL); while (!WiFi.STA.started()) delay(100); Serial.println("ESP-NOW Example - Broadcast Master"); Serial.println("Wi-Fi parameters:"); Serial.println(" Mode: STA"); Serial.println(" MAC Address: " + WiFi.macAddress()); Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL); // Register the broadcast peer if (!broadcast_peer.begin()) { Serial.println("Failed to initialize broadcast peer"); Serial.println("Rebooting in 5 seconds..."); delay(5000); ESP.restart(); } Serial.println("Setup complete. Broadcasting messages every 5 seconds."); pinMode(pinSW, INPUT_PULLUP); // Set SW pin as input with internal pull-up resistor pinMode(pinDT, INPUT); // Set DT pin as input pinMode(pinCLK, INPUT); // Set CLK pin as input pinMode(pinLED, OUTPUT); // Set LED pin as output pinMode(pinRocker1, INPUT_PULLUP); // Set Rocker switch 1 as input with internal pull-up resistor pinMode(pinRocker2, INPUT_PULLUP); // Set Rocker switch 2 as input with internal pull-up resistor pinMode(pinRocker3, INPUT_PULLUP); // Set Rocker switch 3 as input with internal pull-up resistor pinMode(pinRocker4, INPUT_PULLUP); // Set Rocker switch 4 as input with internal pull-up resistor previousStateSW = digitalRead(pinSW); // Read initial state of the SW pin previousStateCLK = digitalRead(pinCLK); // Read initial state of the CLK pin previousRockerState1 = digitalRead(pinRocker1); // Read initial state of Rocker switch 1 previousRockerState2 = digitalRead(pinRocker2); // Read initial state of Rocker switch 2 previousRockerState3 = digitalRead(pinRocker3); // Read initial state of Rocker switch 3 previousRockerState4 = digitalRead(pinRocker4); // Read initial state of Rocker switch 4 analogWrite(pinLED, 0); // Initialize the LED with 0 brightness (off) } void loop() { int actualStateCLK = digitalRead(pinCLK); // Read current state of the CLK pin int actualStateDT = digitalRead(pinDT); // Read current state of the DT pin int actualStateSW = digitalRead(pinSW); // Read current state of the SW pin // Read current states of rocker switches again after debounce delay int currentRockerState1 = digitalRead(pinRocker1); int currentRockerState2 = digitalRead(pinRocker2); int currentRockerState3 = digitalRead(pinRocker3); int currentRockerState4 = digitalRead(pinRocker4); // Check if the state has changed and is stable if (currentRockerState1 != previousRockerState1 || currentRockerState2 != previousRockerState2 || currentRockerState3 != previousRockerState3 || currentRockerState4 != previousRockerState4) { // Debounce delay for rocker switches delay(50); // Update activatedRockerCount only if there is a change in any rocker switch state activatedRockerCount = 1; if (currentRockerState1 == LOW) { activatedRockerCount++; } if (currentRockerState2 == LOW) { activatedRockerCount++; } if (currentRockerState3 == LOW) { activatedRockerCount++; } if (currentRockerState4 == LOW) { activatedRockerCount++; } // Print the number of activated rocker switches to the Serial Monitor Serial.print(F("Activated rocker switches: ")); Serial.println(activatedRockerCount); char data[32]; snprintf(data, sizeof(data), "Rockers: %d", activatedRockerCount); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, strlen(data))) { Serial.println("Failed to broadcast message"); } // Update previous rocker states previousRockerState1 = currentRockerState1; previousRockerState2 = currentRockerState2; previousRockerState3 = currentRockerState3; previousRockerState4 = currentRockerState4; } // Push Button verification if (actualStateSW != previousStateSW) { // Check if SW state has changed previousStateSW = actualStateSW; // Update the previous SW state delay(50); // Debounce delay char data[32]; if (actualStateSW == LOW) { Serial.println(F("SW Button pushed")); // Print message when button is pressed snprintf(data, sizeof(data), "SW: pressed"); } else { Serial.println(F("SW Button released")); // Print message when button is released snprintf(data, sizeof(data), "SW: released"); } Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, strlen(data))) { Serial.println("Failed to broadcast message"); } } // Rotary encoder verification if (actualStateCLK != previousStateCLK) { delayMicroseconds(200); // Debounce delay if (digitalRead(pinCLK) == actualStateCLK) { // Check if the CLK signal has settled previousStateCLK = actualStateCLK; char direction[32]; if (actualStateCLK == LOW) { if (actualStateCLK != actualStateDT) { // Comparing CLK and DT, if they're different, direction is counter-clockwise rotaryCount--; // Decrement counter if (rotaryCount < 1) { rotaryCount = 1; } else if (rotaryCount > 5) { rotaryCount = 5; } snprintf(direction, sizeof(direction), "counter-clockwise"); } else { // When CLK and DT are similar, direction is clockwise rotaryCount++; // Increment counter if (rotaryCount < 1) { rotaryCount = 1; } else if (rotaryCount > 5) { rotaryCount = 5; } snprintf(direction, sizeof(direction), "clockwise"); } Serial.print(F("Rotary count: ")); Serial.println(rotaryCount); // Print the updated count char data[32]; snprintf(data, sizeof(data), "%s %d", direction, rotaryCount); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, strlen(data))) { Serial.println("Failed to broadcast message"); } } } } }